@@ -374,22 +374,22 @@ def __init__(
374
374
Optional, context.
375
375
Note: only names of type string and values of type int, string or boolean are supported
376
376
"""
377
- self .principal_id = principal_id
378
377
self .region = region
379
378
self .aws_account_id = aws_account_id
380
379
self .api_id = api_id
381
380
self .stage = stage
381
+ self .principal_id = principal_id
382
382
self .context = context
383
383
self ._allow_routes : List [Dict ] = []
384
384
self ._deny_routes : List [Dict ] = []
385
385
386
- def _add_route (self , effect : str , verb : str , resource : str , conditions : List [Dict ]):
386
+ def _add_route (self , effect : str , http_method : str , resource : str , conditions : Optional [ List [Dict ]] = None ):
387
387
"""Adds a route to the internal lists of allowed or denied routes. Each object in
388
388
the internal list contains a resource ARN and a condition statement. The condition
389
389
statement can be null."""
390
- if verb != "*" and verb not in HttpVerb .__members__ :
390
+ if http_method != "*" and http_method not in HttpVerb .__members__ :
391
391
allowed_values = [verb .value for verb in HttpVerb ]
392
- raise ValueError (f"Invalid HTTP verb: '{ verb } '. Use either '{ allowed_values } '" )
392
+ raise ValueError (f"Invalid HTTP verb: '{ http_method } '. Use either '{ allowed_values } '" )
393
393
394
394
resource_pattern = re .compile (self .path_regex )
395
395
if not resource_pattern .match (resource ):
@@ -398,7 +398,9 @@ def _add_route(self, effect: str, verb: str, resource: str, conditions: List[Dic
398
398
if resource [:1 ] == "/" :
399
399
resource = resource [1 :]
400
400
401
- resource_arn = APIGatewayRouteArn (self .region , self .aws_account_id , self .api_id , self .stage , verb , resource ).arn
401
+ resource_arn = APIGatewayRouteArn (
402
+ self .region , self .aws_account_id , self .api_id , self .stage , http_method , resource
403
+ ).arn
402
404
403
405
route = {"resourceArn" : resource_arn , "conditions" : conditions }
404
406
@@ -412,24 +414,27 @@ def _get_empty_statement(effect: str) -> Dict[str, Any]:
412
414
"""Returns an empty statement object prepopulated with the correct action and the desired effect."""
413
415
return {"Action" : "execute-api:Invoke" , "Effect" : effect .capitalize (), "Resource" : []}
414
416
415
- def _get_statement_for_effect (self , effect : str , methods : List ) -> List :
416
- """This function loops over an array of objects containing a resourceArn and
417
- conditions statement and generates the array of statements for the policy."""
418
- if len (methods ) == 0 :
417
+ def _get_statement_for_effect (self , effect : str , routes : List [ Dict ] ) -> List [ Dict ] :
418
+ """This function loops over an array of objects containing a ` resourceArn` and
419
+ ` conditions` statement and generates the array of statements for the policy."""
420
+ if len (routes ) == 0 :
419
421
return []
420
422
421
- statements = []
422
-
423
+ statements : List [Dict ] = []
423
424
statement = self ._get_empty_statement (effect )
424
- for method in methods :
425
- if method ["conditions" ] is None or len (method ["conditions" ]) == 0 :
426
- statement ["Resource" ].append (method ["resourceArn" ])
427
- else :
425
+
426
+ for route in routes :
427
+ resource_arn = route ["resourceArn" ]
428
+ conditions = route .get ("conditions" )
429
+ if conditions is not None and len (conditions ) > 0 :
428
430
conditional_statement = self ._get_empty_statement (effect )
429
- conditional_statement ["Resource" ].append (method [ "resourceArn" ] )
430
- conditional_statement ["Condition" ] = method [ " conditions" ]
431
+ conditional_statement ["Resource" ].append (resource_arn )
432
+ conditional_statement ["Condition" ] = conditions
431
433
statements .append (conditional_statement )
432
434
435
+ else :
436
+ statement ["Resource" ].append (resource_arn )
437
+
433
438
if len (statement ["Resource" ]) > 0 :
434
439
statements .append (statement )
435
440
@@ -442,7 +447,7 @@ def allow_all_routes(self, http_method: str = HttpVerb.ALL.value):
442
447
----------
443
448
http_method: str
444
449
"""
445
- self ._add_route (effect = "Allow" , verb = http_method , resource = "*" , conditions = [] )
450
+ self ._add_route (effect = "Allow" , http_method = http_method , resource = "*" )
446
451
447
452
def deny_all_routes (self , http_method : str = HttpVerb .ALL .value ):
448
453
"""Adds a '*' allow to the policy to deny access to all methods of an API
@@ -452,25 +457,23 @@ def deny_all_routes(self, http_method: str = HttpVerb.ALL.value):
452
457
http_method: str
453
458
"""
454
459
455
- self ._add_route (effect = "Deny" , verb = http_method , resource = "*" , conditions = [] )
460
+ self ._add_route (effect = "Deny" , http_method = http_method , resource = "*" )
456
461
457
462
def allow_route (self , http_method : str , resource : str , conditions : Optional [List [Dict ]] = None ):
458
463
"""Adds an API Gateway method (Http verb + Resource path) to the list of allowed
459
464
methods for the policy.
460
465
461
466
Optionally includes a condition for the policy statement. More on AWS policy
462
467
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
463
- conditions = conditions or []
464
- self ._add_route (effect = "Allow" , verb = http_method , resource = resource , conditions = conditions )
468
+ self ._add_route (effect = "Allow" , http_method = http_method , resource = resource , conditions = conditions )
465
469
466
470
def deny_route (self , http_method : str , resource : str , conditions : Optional [List [Dict ]] = None ):
467
471
"""Adds an API Gateway method (Http verb + Resource path) to the list of denied
468
472
methods for the policy.
469
473
470
474
Optionally includes a condition for the policy statement. More on AWS policy
471
475
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
472
- conditions = conditions or []
473
- self ._add_route (effect = "Deny" , verb = http_method , resource = resource , conditions = conditions )
476
+ self ._add_route (effect = "Deny" , http_method = http_method , resource = resource , conditions = conditions )
474
477
475
478
def asdict (self ) -> Dict [str , Any ]:
476
479
"""Generates the policy document based on the internal lists of allowed and denied
0 commit comments