lambda-api-decorators-cdk 0.2.3__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lambda_api_decorators_cdk-0.4.0/.github/workflows/tests.yml +36 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/PKG-INFO +4 -3
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/pyproject.toml +4 -3
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/ast_helper.py +21 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/lambda_api_config.py +38 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/resource_builder.py +129 -6
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk.egg-info/PKG-INFO +4 -3
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk.egg-info/SOURCES.txt +9 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk.egg-info/scm_file_list.json +9 -0
- lambda_api_decorators_cdk-0.4.0/src/lambda_api_decorators_cdk.egg-info/scm_version.json +8 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_auth_ast.py +122 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_authorization_diagnostics.py +101 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_authorization_http.py +82 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_authorization_lambda_api.py +72 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_authorization_resolution.py +122 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_authorization_rest.py +94 -0
- lambda_api_decorators_cdk-0.4.0/tests/test_authorizer_config.py +113 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_lambda_api_config.py +5 -3
- lambda_api_decorators_cdk-0.4.0/tests/test_python_compatibility.py +65 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_resource_builder_state.py +3 -3
- lambda_api_decorators_cdk-0.2.3/src/lambda_api_decorators_cdk.egg-info/scm_version.json +0 -8
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/.github/workflows/release.yml +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/.gitignore +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/AGENTS.md +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/LICENSE +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/README.md +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/setup.cfg +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/__init__.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/api_type.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/lambda_api.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk/source_layout.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk.egg-info/dependency_links.txt +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk.egg-info/requires.txt +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/src/lambda_api_decorators_cdk.egg-info/top_level.txt +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/conftest.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_ast_decorator_invocations.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_ast_helper.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_http_builder.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_lambda_api.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_lambda_function.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_layer_autodiscovery.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_packaging_current.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_permission_resolution.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_public_api.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_resource_builder_options.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_resource_graph.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_resource_registries.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_rest_builder.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.0}/tests/test_source_layout.py +0 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
fail-fast: false
|
|
15
|
+
matrix:
|
|
16
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Check out repository
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
23
|
+
uses: actions/setup-python@v5
|
|
24
|
+
with:
|
|
25
|
+
python-version: ${{ matrix.python-version }}
|
|
26
|
+
|
|
27
|
+
- name: Install package and test dependencies
|
|
28
|
+
run: |
|
|
29
|
+
python -m pip install --upgrade pip
|
|
30
|
+
python -m pip install --editable . pytest tomli
|
|
31
|
+
|
|
32
|
+
- name: Verify package import
|
|
33
|
+
run: python -c "import lambda_api_decorators_cdk"
|
|
34
|
+
|
|
35
|
+
- name: Run tests
|
|
36
|
+
run: pytest -q
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lambda-api-decorators-cdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: AWS CDK integration for Lambda API Decorators. Generate AWS Lambda and API Gateway infrastructure from decorated Python handlers.
|
|
5
5
|
Author: Mateo Marcos, Emanuel Arguinarena, Lucas Picchi
|
|
6
6
|
License: MIT License
|
|
@@ -34,11 +34,12 @@ Classifier: Framework :: AWS CDK :: 2
|
|
|
34
34
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
35
35
|
Classifier: Topic :: Software Development :: Build Tools
|
|
36
36
|
Classifier: License :: OSI Approved :: MIT License
|
|
37
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
38
37
|
Classifier: Programming Language :: Python :: 3.10
|
|
39
38
|
Classifier: Programming Language :: Python :: 3.11
|
|
40
39
|
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
-
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
42
|
+
Requires-Python: >=3.10
|
|
42
43
|
Description-Content-Type: text/markdown
|
|
43
44
|
License-File: LICENSE
|
|
44
45
|
Requires-Dist: aws-cdk.aws-lambda-python-alpha
|
|
@@ -27,7 +27,7 @@ description = "AWS CDK integration for Lambda API Decorators. Generate AWS Lambd
|
|
|
27
27
|
readme = "README.md"
|
|
28
28
|
|
|
29
29
|
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires
|
|
30
|
-
requires-python = ">=3.
|
|
30
|
+
requires-python = ">=3.10"
|
|
31
31
|
|
|
32
32
|
|
|
33
33
|
# https://packaging.python.org/en/latest/specifications/core-metadata/#license
|
|
@@ -54,10 +54,11 @@ classifiers = [
|
|
|
54
54
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
|
55
55
|
'Topic :: Software Development :: Build Tools',
|
|
56
56
|
'License :: OSI Approved :: MIT License',
|
|
57
|
-
'Programming Language :: Python :: 3.9',
|
|
58
57
|
'Programming Language :: Python :: 3.10',
|
|
59
58
|
'Programming Language :: Python :: 3.11',
|
|
60
|
-
'Programming Language :: Python :: 3.12'
|
|
59
|
+
'Programming Language :: Python :: 3.12',
|
|
60
|
+
'Programming Language :: Python :: 3.13',
|
|
61
|
+
'Programming Language :: Python :: 3.14'
|
|
61
62
|
]
|
|
62
63
|
|
|
63
64
|
# For an analysis of this field vs pip's requirements files see:
|
|
@@ -256,6 +256,8 @@ def _validate_permission(invocation):
|
|
|
256
256
|
|
|
257
257
|
|
|
258
258
|
def _parse_invocation(decorator):
|
|
259
|
+
if isinstance(decorator, ast.Name) and decorator.id == "public":
|
|
260
|
+
return DecoratorInvocation(name="public", args=(), kwargs=())
|
|
259
261
|
if not isinstance(decorator, ast.Call) or not isinstance(decorator.func, ast.Name):
|
|
260
262
|
return None
|
|
261
263
|
name = decorator.func.id
|
|
@@ -275,6 +277,12 @@ def _parse_invocation(decorator):
|
|
|
275
277
|
_validate_grant(invocation, "bucket_name")
|
|
276
278
|
elif name == "permission":
|
|
277
279
|
invocation = _validate_permission(invocation)
|
|
280
|
+
elif name == "authorizer":
|
|
281
|
+
if len(invocation.args) != 1 or invocation.kwargs:
|
|
282
|
+
raise ValueError("authorizer requires exactly one positional key")
|
|
283
|
+
_require_non_empty_string(invocation.args[0], name, "key")
|
|
284
|
+
elif name == "public":
|
|
285
|
+
raise ValueError("public must be used as a bare decorator")
|
|
278
286
|
return invocation
|
|
279
287
|
|
|
280
288
|
|
|
@@ -301,6 +309,19 @@ def get_file_nodes(parsed_tree, id, directory):
|
|
|
301
309
|
is_lambda_http = True
|
|
302
310
|
paths.append((path, invocation.name))
|
|
303
311
|
|
|
312
|
+
auth_invocations = [
|
|
313
|
+
invocation for invocation in decorator_invocations
|
|
314
|
+
if invocation.name in ("authorizer", "public")
|
|
315
|
+
]
|
|
316
|
+
if len(auth_invocations) > 1:
|
|
317
|
+
raise ValueError(
|
|
318
|
+
"Conflicting or repeated authentication decorators are not allowed"
|
|
319
|
+
)
|
|
320
|
+
if len(paths) > 1 and auth_invocations:
|
|
321
|
+
raise ValueError(
|
|
322
|
+
"Authentication on a handler with multiple routes is ambiguous"
|
|
323
|
+
)
|
|
324
|
+
|
|
304
325
|
#ast.FunctionDef ends. If the Function had an HTTP Decorator it means it's a lambda function
|
|
305
326
|
if is_lambda_http:
|
|
306
327
|
for key,value in paths:
|
|
@@ -27,6 +27,8 @@ class LambdaApiConfig:
|
|
|
27
27
|
environment: Optional[Mapping[str, str]] = None,
|
|
28
28
|
dynamodb_tables: Optional[Mapping[str, dynamodb.ITable]] = None,
|
|
29
29
|
s3_buckets: Optional[Mapping[str, s3.IBucket]] = None,
|
|
30
|
+
authorizers: Optional[Mapping[str, object]] = None,
|
|
31
|
+
default_authorizer: Optional[str] = None,
|
|
30
32
|
) -> None:
|
|
31
33
|
self._default_runtime = runtime
|
|
32
34
|
self._default_timeout = timeout
|
|
@@ -49,10 +51,15 @@ class LambdaApiConfig:
|
|
|
49
51
|
|
|
50
52
|
self._dynamodb_tables = {}
|
|
51
53
|
self._s3_buckets = {}
|
|
54
|
+
self._authorizers = {}
|
|
52
55
|
for key, table in (dynamodb_tables or {}).items():
|
|
53
56
|
self.add_dynamodb_table(key, table)
|
|
54
57
|
for key, bucket in (s3_buckets or {}).items():
|
|
55
58
|
self.add_s3_bucket(key, bucket)
|
|
59
|
+
for key, authorizer in (authorizers or {}).items():
|
|
60
|
+
self.add_authorizer(key, authorizer)
|
|
61
|
+
self._default_authorizer = None
|
|
62
|
+
self.set_default_authorizer(default_authorizer)
|
|
56
63
|
|
|
57
64
|
def set_default_runtime(self, runtime: Optional[lambda_.Runtime]) -> None:
|
|
58
65
|
self._default_runtime = runtime
|
|
@@ -117,6 +124,35 @@ class LambdaApiConfig:
|
|
|
117
124
|
def add_s3_bucket(self, key: str, bucket: s3.IBucket) -> None:
|
|
118
125
|
self._add_resource(key, bucket, self._s3_buckets, "S3 bucket")
|
|
119
126
|
|
|
127
|
+
def add_authorizer(self, key: str, authorizer: object) -> None:
|
|
128
|
+
if not isinstance(key, str):
|
|
129
|
+
raise TypeError("Authorizer registry keys must be strings")
|
|
130
|
+
if not key.strip():
|
|
131
|
+
raise ValueError("Authorizer registry keys must not be empty or whitespace")
|
|
132
|
+
interfaces = getattr(authorizer, "__jsii_ifaces__", ()) if authorizer is not None else ()
|
|
133
|
+
supported = {
|
|
134
|
+
"aws_cdk.aws_apigateway.IAuthorizer",
|
|
135
|
+
"aws_cdk.aws_apigatewayv2.IHttpRouteAuthorizer",
|
|
136
|
+
}
|
|
137
|
+
if not any(f"{interface.__module__}.{interface.__name__}" in supported
|
|
138
|
+
for interface in interfaces):
|
|
139
|
+
raise TypeError("Authorizer must be a CDK authorizer object")
|
|
140
|
+
if key in self._authorizers:
|
|
141
|
+
raise ValueError(f"Authorizer key {key!r} is already registered")
|
|
142
|
+
self._authorizers[key] = authorizer
|
|
143
|
+
|
|
144
|
+
def set_default_authorizer(self, key: Optional[str]) -> None:
|
|
145
|
+
if key is None:
|
|
146
|
+
self._default_authorizer = None
|
|
147
|
+
return
|
|
148
|
+
if not isinstance(key, str):
|
|
149
|
+
raise TypeError("Default authorizer key must be a string or None")
|
|
150
|
+
if not key.strip():
|
|
151
|
+
raise ValueError("Default authorizer key must not be empty or whitespace")
|
|
152
|
+
if key not in self._authorizers:
|
|
153
|
+
raise KeyError(f"Authorizer key {key!r} is not registered")
|
|
154
|
+
self._default_authorizer = key
|
|
155
|
+
|
|
120
156
|
@staticmethod
|
|
121
157
|
def _add_resource(key: str, resource, registry: dict, resource_type: str) -> None:
|
|
122
158
|
if not isinstance(key, str):
|
|
@@ -148,4 +184,6 @@ class LambdaApiConfig:
|
|
|
148
184
|
custom_vpcs=dict(self._custom_vpcs),
|
|
149
185
|
dynamodb_tables=dict(self._dynamodb_tables),
|
|
150
186
|
s3_buckets=dict(self._s3_buckets),
|
|
187
|
+
authorizers=dict(self._authorizers),
|
|
188
|
+
default_authorizer=self._default_authorizer,
|
|
151
189
|
)
|
|
@@ -8,13 +8,16 @@ from aws_cdk import (
|
|
|
8
8
|
aws_apigatewayv2_integrations as integrations,
|
|
9
9
|
aws_lambda as lambda_,
|
|
10
10
|
aws_s3 as s3,
|
|
11
|
-
aws_lambda_python_alpha as _lambda_python
|
|
11
|
+
aws_lambda_python_alpha as _lambda_python,
|
|
12
|
+
Annotations)
|
|
12
13
|
import hashlib
|
|
14
|
+
import inspect
|
|
13
15
|
import os
|
|
14
16
|
from pathlib import Path
|
|
15
17
|
from lambda_api_decorators_cdk import ast_helper
|
|
16
18
|
from lambda_api_decorators_cdk.source_layout import SourceLayout
|
|
17
19
|
from typing import Optional, List, Dict
|
|
20
|
+
from constructs import Construct
|
|
18
21
|
|
|
19
22
|
class ResourceBuilder():
|
|
20
23
|
'''
|
|
@@ -42,10 +45,12 @@ class ResourceBuilder():
|
|
|
42
45
|
custom_vpcs = None, ####TODO
|
|
43
46
|
dynamodb_tables: Optional[Dict[str, dynamodb.ITable]] = None,
|
|
44
47
|
s3_buckets: Optional[Dict[str, s3.IBucket]] = None,
|
|
48
|
+
authorizers: Optional[Dict[str, object]] = None,
|
|
49
|
+
default_authorizer: Optional[str] = None,
|
|
45
50
|
) -> 'ResourceBuilder':
|
|
46
51
|
|
|
47
52
|
'''
|
|
48
|
-
Creates base instance of a Resource Builder. By default, there are no predetermined custom, common nor default settings with the exception of the following custom runtimes: python3.8, python3.9,
|
|
53
|
+
Creates base instance of a Resource Builder. By default, there are no predetermined custom, common nor default settings with the exception of the following custom runtimes: python3.8, python3.9, python3.10, python3.11, python3.12, python3.13, python3.14.
|
|
49
54
|
You can optionally specify arguments (Keep in mind some of them are constructs of the aws_cdk toolkit) such as:
|
|
50
55
|
@param default_runtime
|
|
51
56
|
@param default_timeout
|
|
@@ -83,6 +88,8 @@ class ResourceBuilder():
|
|
|
83
88
|
self.custom_vpcs = custom_vpcs if custom_vpcs is not None else {}
|
|
84
89
|
self.dynamodb_tables = dynamodb_tables if dynamodb_tables is not None else {}
|
|
85
90
|
self.s3_buckets = s3_buckets if s3_buckets is not None else {}
|
|
91
|
+
self.authorizers = authorizers if authorizers is not None else {}
|
|
92
|
+
self.default_authorizer = default_authorizer
|
|
86
93
|
self._physical_dynamodb_tables = {}
|
|
87
94
|
self._physical_s3_buckets = {}
|
|
88
95
|
|
|
@@ -91,6 +98,8 @@ class ResourceBuilder():
|
|
|
91
98
|
self.custom_runtimes.update({'python3.10':lambda_.Runtime.PYTHON_3_10})
|
|
92
99
|
self.custom_runtimes.update({'python3.11':lambda_.Runtime.PYTHON_3_11})
|
|
93
100
|
self.custom_runtimes.update({'python3.12':lambda_.Runtime.PYTHON_3_12})
|
|
101
|
+
self.custom_runtimes.update({'python3.13':lambda_.Runtime.PYTHON_3_13})
|
|
102
|
+
self.custom_runtimes.update({'python3.14':lambda_.Runtime.PYTHON_3_14})
|
|
94
103
|
|
|
95
104
|
|
|
96
105
|
#Setters
|
|
@@ -236,6 +245,7 @@ class ResourceBuilder():
|
|
|
236
245
|
if print_tree:
|
|
237
246
|
ast_helper.dump_tree(graph)
|
|
238
247
|
self._prepare_layers(construct, graph, layer_sources)
|
|
248
|
+
self._emit_authorization_diagnostics(construct, graph)
|
|
239
249
|
self.build_from_graph(
|
|
240
250
|
construct, graph, api_resource, lambda_root, source_layout)
|
|
241
251
|
|
|
@@ -257,6 +267,7 @@ class ResourceBuilder():
|
|
|
257
267
|
if print_tree:
|
|
258
268
|
ast_helper.dump_tree(graph)
|
|
259
269
|
self._prepare_layers(construct, graph, layer_sources)
|
|
270
|
+
self._emit_authorization_diagnostics(construct, graph)
|
|
260
271
|
self.build_http_from_graph(
|
|
261
272
|
construct, graph, http_api, lambda_root, source_layout)
|
|
262
273
|
|
|
@@ -493,6 +504,114 @@ class ResourceBuilder():
|
|
|
493
504
|
decorators[invocation.name] = value
|
|
494
505
|
return decorators
|
|
495
506
|
|
|
507
|
+
@staticmethod
|
|
508
|
+
def _auth_override(method: ast_helper.Method):
|
|
509
|
+
"""Return inherit, public, or an explicit logical authorizer key."""
|
|
510
|
+
for invocation in method.get_decorator_invocations():
|
|
511
|
+
if invocation.name == "public":
|
|
512
|
+
return ("public", None)
|
|
513
|
+
if invocation.name == "authorizer":
|
|
514
|
+
return ("authorizer", invocation.args[0])
|
|
515
|
+
return ("inherit", None)
|
|
516
|
+
|
|
517
|
+
def _effective_authorizer_key(self, method: ast_helper.Method):
|
|
518
|
+
state, key = self._auth_override(method)
|
|
519
|
+
if state == "public":
|
|
520
|
+
return None
|
|
521
|
+
return key if state == "authorizer" else self.default_authorizer
|
|
522
|
+
|
|
523
|
+
@staticmethod
|
|
524
|
+
def _implements_interface(authorizer, interface) -> bool:
|
|
525
|
+
return interface in getattr(authorizer, "__jsii_ifaces__", ())
|
|
526
|
+
|
|
527
|
+
def _resolve_authorizer(self, method: ast_helper.Method, family: str):
|
|
528
|
+
key = self._effective_authorizer_key(method)
|
|
529
|
+
if key is None:
|
|
530
|
+
return None
|
|
531
|
+
try:
|
|
532
|
+
authorizer = self.authorizers[key]
|
|
533
|
+
except KeyError:
|
|
534
|
+
raise KeyError(f"Authorizer key {key!r} is not registered") from None
|
|
535
|
+
interface = (
|
|
536
|
+
apigateway.IAuthorizer
|
|
537
|
+
if family == "REST"
|
|
538
|
+
else apigateway2.IHttpRouteAuthorizer
|
|
539
|
+
)
|
|
540
|
+
if not self._implements_interface(authorizer, interface):
|
|
541
|
+
raise TypeError(
|
|
542
|
+
f"Authorizer key {key!r} is not compatible with {family} APIs"
|
|
543
|
+
)
|
|
544
|
+
return authorizer
|
|
545
|
+
|
|
546
|
+
def _rest_method_options(self, method: ast_helper.Method):
|
|
547
|
+
authorizer = self._resolve_authorizer(method, "REST")
|
|
548
|
+
if authorizer is None:
|
|
549
|
+
return {
|
|
550
|
+
"authorizer": None,
|
|
551
|
+
"authorization_type": apigateway.AuthorizationType.NONE,
|
|
552
|
+
}
|
|
553
|
+
return {
|
|
554
|
+
"authorizer": authorizer,
|
|
555
|
+
"authorization_type": authorizer.authorization_type,
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
def _add_rest_method(self, resource, method, integration):
|
|
559
|
+
options = self._rest_method_options(method)
|
|
560
|
+
parameters = inspect.signature(resource.add_method).parameters.values()
|
|
561
|
+
accepts_options = any(
|
|
562
|
+
parameter.kind is inspect.Parameter.VAR_KEYWORD
|
|
563
|
+
or parameter.name == "authorization_type"
|
|
564
|
+
for parameter in parameters
|
|
565
|
+
)
|
|
566
|
+
if accepts_options:
|
|
567
|
+
return resource.add_method(method.get_method(), integration, **options)
|
|
568
|
+
# Preserve compatibility with narrow direct-builder test doubles and
|
|
569
|
+
# existing callers that expose only the historical two-argument shape.
|
|
570
|
+
return resource.add_method(method.get_method(), integration)
|
|
571
|
+
|
|
572
|
+
def _http_route_authorizer(self, method: ast_helper.Method):
|
|
573
|
+
authorizer = self._resolve_authorizer(method, "HTTP")
|
|
574
|
+
return authorizer if authorizer is not None else apigateway2.HttpNoneAuthorizer()
|
|
575
|
+
|
|
576
|
+
def _emit_authorization_diagnostics(self, construct, graph):
|
|
577
|
+
if not isinstance(construct, Construct):
|
|
578
|
+
return
|
|
579
|
+
if self.default_authorizer is None:
|
|
580
|
+
Annotations.of(construct).add_warning_v2(
|
|
581
|
+
"LAD_AUTH_PUBLIC_DEFAULT",
|
|
582
|
+
"LAD_AUTH_PUBLIC_DEFAULT: Routes without explicit authorization are public because no "
|
|
583
|
+
"default authorizer is configured.",
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
deviations = []
|
|
587
|
+
|
|
588
|
+
def visit(resource):
|
|
589
|
+
for method in resource.get_methods():
|
|
590
|
+
state, key = self._auth_override(method)
|
|
591
|
+
if self.default_authorizer is None:
|
|
592
|
+
if state == "authorizer":
|
|
593
|
+
deviations.append((resource.get_path(), method.get_method(), key))
|
|
594
|
+
elif state == "public":
|
|
595
|
+
deviations.append((resource.get_path(), method.get_method(), "PUBLIC"))
|
|
596
|
+
elif state == "authorizer" and key != self.default_authorizer:
|
|
597
|
+
deviations.append((resource.get_path(), method.get_method(), key))
|
|
598
|
+
for child in resource.get_connections():
|
|
599
|
+
visit(child)
|
|
600
|
+
|
|
601
|
+
visit(graph)
|
|
602
|
+
if not deviations:
|
|
603
|
+
return
|
|
604
|
+
deviations.sort(key=lambda item: (item[0], item[1]))
|
|
605
|
+
if self.default_authorizer is None:
|
|
606
|
+
heading = "Default authorizer: PUBLIC\n\nProtected routes:"
|
|
607
|
+
else:
|
|
608
|
+
heading = f"Default authorizer: {self.default_authorizer}\n\nOverrides:"
|
|
609
|
+
details = "\n".join(
|
|
610
|
+
f" {method} {path} {value}"
|
|
611
|
+
for path, method, value in deviations
|
|
612
|
+
)
|
|
613
|
+
Annotations.of(construct).add_info(f"{heading}\n{details}")
|
|
614
|
+
|
|
496
615
|
@staticmethod
|
|
497
616
|
def _physical_resource_id(resource_type: str, physical_name: str) -> str:
|
|
498
617
|
"""Create a stable, collision-safe construct ID for an imported resource."""
|
|
@@ -734,7 +853,8 @@ class ResourceBuilder():
|
|
|
734
853
|
for method in graph.get_methods():
|
|
735
854
|
lbda = self._build_discovered_lambda(
|
|
736
855
|
construct, method, lambda_root, source_layout)
|
|
737
|
-
|
|
856
|
+
self._add_rest_method(
|
|
857
|
+
new_resource, method, apigateway.LambdaIntegration(lbda))
|
|
738
858
|
else:
|
|
739
859
|
#We can get a skip from /something to /something/one/two/method, so resources with no methods "one" and "two" should be created
|
|
740
860
|
new_api_resources = path[len(api_resource.path):].lstrip('/').split('/')
|
|
@@ -746,7 +866,8 @@ class ResourceBuilder():
|
|
|
746
866
|
for method in graph.get_methods():
|
|
747
867
|
lbda = self._build_discovered_lambda(
|
|
748
868
|
construct, method, lambda_root, source_layout)
|
|
749
|
-
|
|
869
|
+
self._add_rest_method(
|
|
870
|
+
new_resource, method, apigateway.LambdaIntegration(lbda))
|
|
750
871
|
|
|
751
872
|
for node in graph.get_connections():
|
|
752
873
|
self.build_from_graph(
|
|
@@ -776,7 +897,8 @@ class ResourceBuilder():
|
|
|
776
897
|
http_api.add_routes(
|
|
777
898
|
path='/',
|
|
778
899
|
methods=[method_mapping[method.get_method()]],
|
|
779
|
-
integration=
|
|
900
|
+
integration=api_lbda_integration,
|
|
901
|
+
authorizer=self._http_route_authorizer(method),
|
|
780
902
|
)
|
|
781
903
|
else:
|
|
782
904
|
for method in graph.get_methods():
|
|
@@ -786,7 +908,8 @@ class ResourceBuilder():
|
|
|
786
908
|
http_api.add_routes(
|
|
787
909
|
path=path,
|
|
788
910
|
methods=[method_mapping[method.get_method()]],
|
|
789
|
-
integration=
|
|
911
|
+
integration=api_lbda_integration,
|
|
912
|
+
authorizer=self._http_route_authorizer(method),
|
|
790
913
|
)
|
|
791
914
|
|
|
792
915
|
for node in graph.get_connections():
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lambda-api-decorators-cdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: AWS CDK integration for Lambda API Decorators. Generate AWS Lambda and API Gateway infrastructure from decorated Python handlers.
|
|
5
5
|
Author: Mateo Marcos, Emanuel Arguinarena, Lucas Picchi
|
|
6
6
|
License: MIT License
|
|
@@ -34,11 +34,12 @@ Classifier: Framework :: AWS CDK :: 2
|
|
|
34
34
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
35
35
|
Classifier: Topic :: Software Development :: Build Tools
|
|
36
36
|
Classifier: License :: OSI Approved :: MIT License
|
|
37
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
38
37
|
Classifier: Programming Language :: Python :: 3.10
|
|
39
38
|
Classifier: Programming Language :: Python :: 3.11
|
|
40
39
|
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
-
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
42
|
+
Requires-Python: >=3.10
|
|
42
43
|
Description-Content-Type: text/markdown
|
|
43
44
|
License-File: LICENSE
|
|
44
45
|
Requires-Dist: aws-cdk.aws-lambda-python-alpha
|
|
@@ -4,6 +4,7 @@ LICENSE
|
|
|
4
4
|
README.md
|
|
5
5
|
pyproject.toml
|
|
6
6
|
.github/workflows/release.yml
|
|
7
|
+
.github/workflows/tests.yml
|
|
7
8
|
src/lambda_api_decorators_cdk/__init__.py
|
|
8
9
|
src/lambda_api_decorators_cdk/api_type.py
|
|
9
10
|
src/lambda_api_decorators_cdk/ast_helper.py
|
|
@@ -21,6 +22,13 @@ src/lambda_api_decorators_cdk.egg-info/top_level.txt
|
|
|
21
22
|
tests/conftest.py
|
|
22
23
|
tests/test_ast_decorator_invocations.py
|
|
23
24
|
tests/test_ast_helper.py
|
|
25
|
+
tests/test_auth_ast.py
|
|
26
|
+
tests/test_authorization_diagnostics.py
|
|
27
|
+
tests/test_authorization_http.py
|
|
28
|
+
tests/test_authorization_lambda_api.py
|
|
29
|
+
tests/test_authorization_resolution.py
|
|
30
|
+
tests/test_authorization_rest.py
|
|
31
|
+
tests/test_authorizer_config.py
|
|
24
32
|
tests/test_http_builder.py
|
|
25
33
|
tests/test_lambda_api.py
|
|
26
34
|
tests/test_lambda_api_config.py
|
|
@@ -29,6 +37,7 @@ tests/test_layer_autodiscovery.py
|
|
|
29
37
|
tests/test_packaging_current.py
|
|
30
38
|
tests/test_permission_resolution.py
|
|
31
39
|
tests/test_public_api.py
|
|
40
|
+
tests/test_python_compatibility.py
|
|
32
41
|
tests/test_resource_builder_options.py
|
|
33
42
|
tests/test_resource_builder_state.py
|
|
34
43
|
tests/test_resource_graph.py
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"files": [
|
|
3
3
|
".github/workflows/release.yml",
|
|
4
|
+
".github/workflows/tests.yml",
|
|
4
5
|
".gitignore",
|
|
5
6
|
"AGENTS.md",
|
|
6
7
|
"LICENSE",
|
|
@@ -16,6 +17,13 @@
|
|
|
16
17
|
"tests/conftest.py",
|
|
17
18
|
"tests/test_ast_decorator_invocations.py",
|
|
18
19
|
"tests/test_ast_helper.py",
|
|
20
|
+
"tests/test_auth_ast.py",
|
|
21
|
+
"tests/test_authorization_diagnostics.py",
|
|
22
|
+
"tests/test_authorization_http.py",
|
|
23
|
+
"tests/test_authorization_lambda_api.py",
|
|
24
|
+
"tests/test_authorization_resolution.py",
|
|
25
|
+
"tests/test_authorization_rest.py",
|
|
26
|
+
"tests/test_authorizer_config.py",
|
|
19
27
|
"tests/test_http_builder.py",
|
|
20
28
|
"tests/test_lambda_api.py",
|
|
21
29
|
"tests/test_lambda_api_config.py",
|
|
@@ -24,6 +32,7 @@
|
|
|
24
32
|
"tests/test_packaging_current.py",
|
|
25
33
|
"tests/test_permission_resolution.py",
|
|
26
34
|
"tests/test_public_api.py",
|
|
35
|
+
"tests/test_python_compatibility.py",
|
|
27
36
|
"tests/test_resource_builder_options.py",
|
|
28
37
|
"tests/test_resource_builder_state.py",
|
|
29
38
|
"tests/test_resource_graph.py",
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import ast
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from conftest import decorator_invocations
|
|
6
|
+
from lambda_api_decorators_cdk.ast_helper import get_file_nodes
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def parse_methods(source):
|
|
10
|
+
resources = get_file_nodes(ast.parse(source), "handler.py", "lambdas")
|
|
11
|
+
return [method for resource in resources for method in resource.get_methods()]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def parse_method(source):
|
|
15
|
+
return parse_methods(source)[0]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_authorizer_invocation_is_captured_losslessly():
|
|
19
|
+
method = parse_method(
|
|
20
|
+
'@GET("/admin")\n@authorizer(" users ")\ndef handler(event, context): pass\n'
|
|
21
|
+
)
|
|
22
|
+
assert decorator_invocations(method) == [
|
|
23
|
+
("GET", ("/admin",), ()),
|
|
24
|
+
("authorizer", (" users ",), ()),
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@pytest.mark.parametrize(
|
|
29
|
+
"decorator",
|
|
30
|
+
[
|
|
31
|
+
"authorizer()",
|
|
32
|
+
'authorizer("users", "extra")',
|
|
33
|
+
'authorizer(key="users")',
|
|
34
|
+
"authorizer(123)",
|
|
35
|
+
'authorizer("")',
|
|
36
|
+
'authorizer(" ")',
|
|
37
|
+
"authorizer(AUTHORIZER_KEY)",
|
|
38
|
+
"authorizer(get_key())",
|
|
39
|
+
'authorizer(f"{tenant}-users")',
|
|
40
|
+
],
|
|
41
|
+
)
|
|
42
|
+
def test_invalid_authorizer_syntax_fails_during_source_interpretation(decorator):
|
|
43
|
+
with pytest.raises((TypeError, ValueError), match="(?i)authorizer"):
|
|
44
|
+
parse_method(
|
|
45
|
+
f'@GET("/admin")\n@{decorator}\ndef handler(event, context): pass\n'
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_bare_public_is_captured_and_called_public_is_rejected():
|
|
50
|
+
method = parse_method(
|
|
51
|
+
'@GET("/health")\n@public\ndef handler(event, context): pass\n'
|
|
52
|
+
)
|
|
53
|
+
assert decorator_invocations(method) == [
|
|
54
|
+
("GET", ("/health",), ()),
|
|
55
|
+
("public", (), ()),
|
|
56
|
+
]
|
|
57
|
+
with pytest.raises((TypeError, ValueError), match="(?i)public"):
|
|
58
|
+
parse_method(
|
|
59
|
+
'@GET("/health")\n@public()\ndef handler(event, context): pass\n'
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@pytest.mark.parametrize(
|
|
64
|
+
"decorators,expected",
|
|
65
|
+
[
|
|
66
|
+
(
|
|
67
|
+
'@GET("/admin")\n@authorizer("admins")',
|
|
68
|
+
[("GET", ("/admin",), ()), ("authorizer", ("admins",), ())],
|
|
69
|
+
),
|
|
70
|
+
(
|
|
71
|
+
'@authorizer("admins")\n@GET("/admin")',
|
|
72
|
+
[("authorizer", ("admins",), ()), ("GET", ("/admin",), ())],
|
|
73
|
+
),
|
|
74
|
+
(
|
|
75
|
+
'@GET("/health")\n@public',
|
|
76
|
+
[("GET", ("/health",), ()), ("public", (), ())],
|
|
77
|
+
),
|
|
78
|
+
(
|
|
79
|
+
'@public\n@GET("/health")',
|
|
80
|
+
[("public", (), ()), ("GET", ("/health",), ())],
|
|
81
|
+
),
|
|
82
|
+
],
|
|
83
|
+
)
|
|
84
|
+
def test_auth_metadata_preserves_top_to_bottom_lexical_order(decorators, expected):
|
|
85
|
+
method = parse_method(f"{decorators}\ndef handler(event, context): pass\n")
|
|
86
|
+
assert decorator_invocations(method) == expected
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@pytest.mark.parametrize(
|
|
90
|
+
"decorators",
|
|
91
|
+
[
|
|
92
|
+
'@public\n@authorizer("users")',
|
|
93
|
+
'@authorizer("users")\n@public',
|
|
94
|
+
"@public\n@public",
|
|
95
|
+
'@authorizer("users")\n@authorizer("users")',
|
|
96
|
+
'@authorizer("users")\n@authorizer("admins")',
|
|
97
|
+
],
|
|
98
|
+
)
|
|
99
|
+
def test_aggregate_auth_conflicts_are_rejected(decorators):
|
|
100
|
+
with pytest.raises(ValueError, match="(?i)(auth|public|conflict|repeat)"):
|
|
101
|
+
parse_method(
|
|
102
|
+
f'@GET("/")\n{decorators}\ndef handler(event, context): pass\n'
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
@pytest.mark.parametrize("auth", ['@authorizer("users")', "@public"])
|
|
107
|
+
def test_auth_on_multi_route_handler_is_rejected_as_ambiguous(auth):
|
|
108
|
+
with pytest.raises(ValueError, match="(?i)(ambiguous|multiple|route|auth)"):
|
|
109
|
+
parse_methods(
|
|
110
|
+
f'@GET("/a")\n@POST("/b")\n{auth}\n'
|
|
111
|
+
"def handler(event, context): pass\n"
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_multi_route_handler_without_auth_remains_supported():
|
|
116
|
+
methods = parse_methods(
|
|
117
|
+
'@GET("/a")\n@POST("/b")\ndef handler(event, context): pass\n'
|
|
118
|
+
)
|
|
119
|
+
assert [(method.get_method(), method.get_path_to_file()) for method in methods] == [
|
|
120
|
+
("GET", "lambdas"),
|
|
121
|
+
("POST", "lambdas"),
|
|
122
|
+
]
|