|
| 1 | +import enum |
1 | 2 | import re
|
2 | 3 | from typing import Any, Dict, List, Optional
|
3 | 4 |
|
@@ -312,7 +313,7 @@ def asdict(self) -> dict:
|
312 | 313 | return response
|
313 | 314 |
|
314 | 315 |
|
315 |
| -class HttpVerb: |
| 316 | +class HttpVerb(enum.Enum): |
316 | 317 | GET = "GET"
|
317 | 318 | POST = "POST"
|
318 | 319 | PUT = "PUT"
|
@@ -386,8 +387,9 @@ def _add_route(self, effect: str, verb: str, resource: str, conditions: List[Dic
|
386 | 387 | """Adds a route to the internal lists of allowed or denied routes. Each object in
|
387 | 388 | the internal list contains a resource ARN and a condition statement. The condition
|
388 | 389 | statement can be null."""
|
389 |
| - if verb != "*" and not hasattr(HttpVerb, verb): |
390 |
| - raise ValueError(f"Invalid HTTP verb {verb}. Allowed verbs in HttpVerb class") |
| 390 | + if verb != "*" and verb not in HttpVerb.__members__: |
| 391 | + allowed_values = [verb.value for verb in HttpVerb] |
| 392 | + raise ValueError(f"Invalid HTTP verb: '{verb}'. Use either '{allowed_values}'") |
391 | 393 |
|
392 | 394 | resource_pattern = re.compile(self.path_regex)
|
393 | 395 | if not resource_pattern.match(resource):
|
@@ -433,29 +435,42 @@ def _get_statement_for_effect(self, effect: str, methods: List) -> List:
|
433 | 435 |
|
434 | 436 | return statements
|
435 | 437 |
|
436 |
| - def allow_all_routes(self): |
437 |
| - """Adds a '*' allow to the policy to authorize access to all methods of an API""" |
438 |
| - self._add_route("Allow", HttpVerb.ALL, "*", []) |
| 438 | + def allow_all_routes(self, http_method: str = HttpVerb.ALL.value): |
| 439 | + """Adds a '*' allow to the policy to authorize access to all methods of an API |
439 | 440 |
|
440 |
| - def deny_all_routes(self): |
441 |
| - """Adds a '*' allow to the policy to deny access to all methods of an API""" |
442 |
| - self._add_route("Deny", HttpVerb.ALL, "*", []) |
| 441 | + Parameters |
| 442 | + ---------- |
| 443 | + http_method: str |
| 444 | + """ |
| 445 | + self._add_route(effect="Allow", verb=http_method, resource="*", conditions=[]) |
| 446 | + |
| 447 | + def deny_all_routes(self, http_method: str = HttpVerb.ALL.value): |
| 448 | + """Adds a '*' allow to the policy to deny access to all methods of an API |
| 449 | +
|
| 450 | + Parameters |
| 451 | + ---------- |
| 452 | + http_method: str |
| 453 | + """ |
| 454 | + |
| 455 | + self._add_route(effect="Deny", verb=http_method, resource="*", conditions=[]) |
443 | 456 |
|
444 | 457 | def allow_route(self, http_method: str, resource: str, conditions: Optional[List[Dict]] = None):
|
445 | 458 | """Adds an API Gateway method (Http verb + Resource path) to the list of allowed
|
446 | 459 | methods for the policy.
|
447 | 460 |
|
448 | 461 | Optionally includes a condition for the policy statement. More on AWS policy
|
449 | 462 | conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
|
450 |
| - self._add_route("Allow", http_method, resource, conditions or []) |
| 463 | + conditions = conditions or [] |
| 464 | + self._add_route(effect="Allow", verb=http_method, resource=resource, conditions=conditions) |
451 | 465 |
|
452 | 466 | def deny_route(self, http_method: str, resource: str, conditions: Optional[List[Dict]] = None):
|
453 | 467 | """Adds an API Gateway method (Http verb + Resource path) to the list of denied
|
454 | 468 | methods for the policy.
|
455 | 469 |
|
456 | 470 | Optionally includes a condition for the policy statement. More on AWS policy
|
457 | 471 | conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
|
458 |
| - self._add_route("Deny", http_method, resource, conditions or []) |
| 472 | + conditions = conditions or [] |
| 473 | + self._add_route(effect="Deny", verb=http_method, resource=resource, conditions=conditions) |
459 | 474 |
|
460 | 475 | def asdict(self) -> Dict[str, Any]:
|
461 | 476 | """Generates the policy document based on the internal lists of allowed and denied
|
|
0 commit comments