@@ -379,11 +379,11 @@ def __init__(
379
379
self .api_id = api_id
380
380
self .stage = stage
381
381
self .context = context
382
- self ._allow_methods : List [Dict ] = []
383
- self ._deny_methods : List [Dict ] = []
382
+ self ._allow_routes : List [Dict ] = []
383
+ self ._deny_routes : List [Dict ] = []
384
384
385
- def _add_method (self , effect : str , verb : str , resource : str , conditions : List [Dict ]):
386
- """Adds a method to the internal lists of allowed or denied methods . Each object in
385
+ def _add_route (self , effect : str , verb : str , resource : str , conditions : List [Dict ]):
386
+ """Adds a route to the internal lists of allowed or denied routes . Each object in
387
387
the internal list contains a resource ARN and a condition statement. The condition
388
388
statement can be null."""
389
389
if verb != "*" and not hasattr (HttpVerb , verb ):
@@ -392,16 +392,18 @@ def _add_method(self, effect: str, verb: str, resource: str, conditions: List[Di
392
392
resource_pattern = re .compile (self .path_regex )
393
393
if not resource_pattern .match (resource ):
394
394
raise ValueError (f"Invalid resource path: { resource } . Path should match { self .path_regex } " )
395
+
395
396
if resource [:1 ] == "/" :
396
397
resource = resource [1 :]
397
398
398
399
resource_arn = APIGatewayRouteArn (self .region , self .aws_account_id , self .api_id , self .stage , verb , resource ).arn
399
400
400
- method = {"resourceArn" : resource_arn , "conditions" : conditions }
401
+ route = {"resourceArn" : resource_arn , "conditions" : conditions }
402
+
401
403
if effect .lower () == "allow" :
402
- self ._allow_methods .append (method )
404
+ self ._allow_routes .append (route )
403
405
else : # deny
404
- self ._deny_methods .append (method )
406
+ self ._deny_routes .append (route )
405
407
406
408
@staticmethod
407
409
def _get_empty_statement (effect : str ) -> Dict [str , Any ]:
@@ -431,45 +433,45 @@ def _get_statement_for_effect(self, effect: str, methods: List) -> List:
431
433
432
434
return statements
433
435
434
- def allow_all_methods (self ):
436
+ def allow_all_routes (self ):
435
437
"""Adds a '*' allow to the policy to authorize access to all methods of an API"""
436
- self ._add_method ("Allow" , HttpVerb .ALL , "*" , [])
438
+ self ._add_route ("Allow" , HttpVerb .ALL , "*" , [])
437
439
438
- def deny_all_methods (self ):
440
+ def deny_all_route (self ):
439
441
"""Adds a '*' allow to the policy to deny access to all methods of an API"""
440
- self ._add_method ("Deny" , HttpVerb .ALL , "*" , [])
442
+ self ._add_route ("Deny" , HttpVerb .ALL , "*" , [])
441
443
442
- def allow_method (self , http_method : str , resource : str , conditions : Optional [List [Dict ]] = None ):
444
+ def allow_route (self , http_method : str , resource : str , conditions : Optional [List [Dict ]] = None ):
443
445
"""Adds an API Gateway method (Http verb + Resource path) to the list of allowed
444
446
methods for the policy.
445
447
446
448
Optionally includes a condition for the policy statement. More on AWS policy
447
449
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
448
- self ._add_method ("Allow" , http_method , resource , conditions or [])
450
+ self ._add_route ("Allow" , http_method , resource , conditions or [])
449
451
450
- def deny_method (self , http_method : str , resource : str , conditions : Optional [List [Dict ]] = None ):
452
+ def deny_route (self , http_method : str , resource : str , conditions : Optional [List [Dict ]] = None ):
451
453
"""Adds an API Gateway method (Http verb + Resource path) to the list of denied
452
454
methods for the policy.
453
455
454
456
Optionally includes a condition for the policy statement. More on AWS policy
455
457
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
456
- self ._add_method ("Deny" , http_method , resource , conditions or [])
458
+ self ._add_route ("Deny" , http_method , resource , conditions or [])
457
459
458
460
def asdict (self ) -> Dict [str , Any ]:
459
461
"""Generates the policy document based on the internal lists of allowed and denied
460
462
conditions. This will generate a policy with two main statements for the effect:
461
463
one statement for Allow and one statement for Deny.
462
464
Methods that includes conditions will have their own statement in the policy."""
463
- if len (self ._allow_methods ) == 0 and len (self ._deny_methods ) == 0 :
465
+ if len (self ._allow_routes ) == 0 and len (self ._deny_routes ) == 0 :
464
466
raise ValueError ("No statements defined for the policy" )
465
467
466
468
response : Dict [str , Any ] = {
467
469
"principalId" : self .principal_id ,
468
470
"policyDocument" : {"Version" : self .version , "Statement" : []},
469
471
}
470
472
471
- response ["policyDocument" ]["Statement" ].extend (self ._get_statement_for_effect ("Allow" , self ._allow_methods ))
472
- response ["policyDocument" ]["Statement" ].extend (self ._get_statement_for_effect ("Deny" , self ._deny_methods ))
473
+ response ["policyDocument" ]["Statement" ].extend (self ._get_statement_for_effect ("Allow" , self ._allow_routes ))
474
+ response ["policyDocument" ]["Statement" ].extend (self ._get_statement_for_effect ("Deny" , self ._deny_routes ))
473
475
474
476
if self .context :
475
477
response ["context" ] = self .context
0 commit comments