Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: appengine/machine_provider/handlers_endpoints.py

Issue 2716783002: Enforce two-day limit on lease requests (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2015 The LUCI Authors. All rights reserved. 1 # Copyright 2015 The LUCI Authors. All rights reserved.
2 # Use of this source code is governed under the Apache License, Version 2.0 2 # Use of this source code is governed under the Apache License, Version 2.0
3 # that can be found in the LICENSE file. 3 # that can be found in the LICENSE file.
4 4
5 """Cloud endpoints for the Machine Provider API.""" 5 """Cloud endpoints for the Machine Provider API."""
6 6
7 import hashlib 7 import hashlib
8 import json 8 import json
9 import logging 9 import logging
10 10
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 logging.info( 344 logging.info(
345 'Received LeaseRequest:\nUser: %s\nRequest hash: %s\n%s', 345 'Received LeaseRequest:\nUser: %s\nRequest hash: %s\n%s',
346 user, 346 user,
347 request_hash, 347 request_hash,
348 request, 348 request,
349 ) 349 )
350 return self._lease(request, user, request_hash) 350 return self._lease(request, user, request_hash)
351 351
352 def _lease(self, request, user, request_hash): 352 def _lease(self, request, user, request_hash):
353 """Handles an incoming LeaseRequest.""" 353 """Handles an incoming LeaseRequest."""
354 # Arbitrary limit. Increase if necessary.
355 MAX_LEASE_DURATION = 60 * 60 * 24 * 2
356 now = utils.time_time()
357 max_lease_expiration_ts = now + MAX_LEASE_DURATION
358
354 metrics.lease_requests_received.increment() 359 metrics.lease_requests_received.increment()
355 if request.duration: 360 if request.duration:
356 if request.lease_expiration_ts: 361 if request.lease_expiration_ts:
357 return rpc_messages.LeaseResponse( 362 return rpc_messages.LeaseResponse(
358 client_request_id=request.request_id, 363 client_request_id=request.request_id,
359 error=rpc_messages.LeaseRequestError.MUTUAL_EXCLUSION_ERROR, 364 error=rpc_messages.LeaseRequestError.MUTUAL_EXCLUSION_ERROR,
360 ) 365 )
361 if request.duration < 1: 366 if request.duration < 1:
362 return rpc_messages.LeaseResponse( 367 return rpc_messages.LeaseResponse(
363 client_request_id=request.request_id, 368 client_request_id=request.request_id,
364 error=rpc_messages.LeaseRequestError.NONPOSITIVE_DEADLINE, 369 error=rpc_messages.LeaseRequestError.NONPOSITIVE_DEADLINE,
365 ) 370 )
371 if request.duration > MAX_LEASE_DURATION:
372 return rpc_messages.LeaseResponse(
373 client_request_id=request.request_id,
374 error=rpc_messages.LeaseRequestError.LEASE_TOO_LONG,
375 )
366 elif request.lease_expiration_ts: 376 elif request.lease_expiration_ts:
367 if request.lease_expiration_ts <= utils.time_time(): 377 if request.lease_expiration_ts <= now:
368 return rpc_messages.LeaseResponse( 378 return rpc_messages.LeaseResponse(
369 client_request_id=request.request_id, 379 client_request_id=request.request_id,
370 error=rpc_messages.LeaseRequestError.LEASE_EXPIRATION_TS_ERROR, 380 error=rpc_messages.LeaseRequestError.LEASE_EXPIRATION_TS_ERROR,
371 ) 381 )
382 if request.lease_expiration_ts > max_lease_expiration_ts:
383 return rpc_messages.LeaseResponse(
384 client_request_id=request.request_id,
385 error=rpc_messages.LeaseRequestError.LEASE_TOO_LONG,
386 )
372 else: 387 else:
373 return rpc_messages.LeaseResponse( 388 return rpc_messages.LeaseResponse(
374 client_request_id=request.request_id, 389 client_request_id=request.request_id,
375 error=rpc_messages.LeaseRequestError.LEASE_LENGTH_UNSPECIFIED, 390 error=rpc_messages.LeaseRequestError.LEASE_LENGTH_UNSPECIFIED,
376 ) 391 )
377 if request.pubsub_topic: 392 if request.pubsub_topic:
378 if not pubsub.validate_topic(request.pubsub_topic): 393 if not pubsub.validate_topic(request.pubsub_topic):
379 logging.warning( 394 logging.warning(
380 'Invalid topic for Cloud Pub/Sub: %s', 395 'Invalid topic for Cloud Pub/Sub: %s',
381 request.pubsub_topic, 396 request.pubsub_topic,
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 def get_routes(): 582 def get_routes():
568 return endpoints_webapp2.api_routes(config.ConfigApi) 583 return endpoints_webapp2.api_routes(config.ConfigApi)
569 584
570 585
571 def create_endpoints_app(): 586 def create_endpoints_app():
572 return endpoints.api_server([ 587 return endpoints.api_server([
573 CatalogEndpoints, 588 CatalogEndpoints,
574 MachineProviderEndpoints, 589 MachineProviderEndpoints,
575 config.ConfigApi, 590 config.ConfigApi,
576 ]) 591 ])
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698