cloud-foundry 0.5.3__tar.gz → 0.5.4__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.
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/PKG-INFO +1 -1
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/__init__.py +1 -1
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/python_function.py +2 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/rest_api.py +7 -1
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/websocket_api.py +13 -3
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/python_archive_builder.py +11 -3
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/services/authorization_api/security_lambda.py +133 -172
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/services/mail_publisher.py +29 -27
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/idle_reaper_client.py +5 -1
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/.gitignore +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/LICENSE +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/README.md +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/archive_builder.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/mail_publisher.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/migrations/__init__.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/migrations/forget_site_objects.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/__init__.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/api_waf.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/cdn.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/cdn_api_origin.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/cdn_site_origin.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/connection_store.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/custom_domain.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/document_repository.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/domain.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/function.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/idle_reaper.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/postgres_schema.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/publisher.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/queue.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/schedule.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/site_bucket.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/topic.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/topic_subscription.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/ui_publisher.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/pulumi/websocket_function.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/services/postgres_schema_bootstrap.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/aws_openapi_editor.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/hash_comparator.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/logger.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/names.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/openapi_editor.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/utils/websocket_utils.py +0 -0
- {cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: cloud-foundry
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.4
|
|
4
4
|
Summary: cloud-foundry is a curated set of cloud components that can be used to assemble cloud-based applications.
|
|
5
5
|
Project-URL: Documentation, https://github.com/DanRepik/cloud-foundry
|
|
6
6
|
Project-URL: Source, https://github.com/DanRepik/cloud-foundry
|
|
@@ -38,6 +38,8 @@ def python_function(
|
|
|
38
38
|
requirements=requirements,
|
|
39
39
|
working_dir="temp",
|
|
40
40
|
target_architecture=resolved_architecture,
|
|
41
|
+
# "python3.13" -> "3.13"; Function defaults the runtime to python3.12.
|
|
42
|
+
python_version=(runtime or "python3.12").removeprefix("python"),
|
|
41
43
|
)
|
|
42
44
|
return Function(
|
|
43
45
|
name=name,
|
|
@@ -29,6 +29,7 @@ class RestAPI(pulumi.ComponentResource):
|
|
|
29
29
|
rest_api: Optional[aws.apigateway.RestApi] = None
|
|
30
30
|
rest_api_id: pulumi.Output[str] = None # The REST API identifier
|
|
31
31
|
domain: Optional[str] = None # The custom domain name for the API
|
|
32
|
+
_gateway_role: Optional[aws.iam.Role] = None # built once, see _get_gateway_role
|
|
32
33
|
|
|
33
34
|
def __init__(
|
|
34
35
|
self,
|
|
@@ -371,7 +372,6 @@ class RestAPI(pulumi.ComponentResource):
|
|
|
371
372
|
"resourcePath": "$context.resourcePath",
|
|
372
373
|
"status": "$context.status",
|
|
373
374
|
"origin": "$context.request.header.Origin",
|
|
374
|
-
"authorization": "$context.request.header.Authorization",
|
|
375
375
|
"protocol": "$context.protocol",
|
|
376
376
|
"responseLength": "$context.responseLength",
|
|
377
377
|
}
|
|
@@ -548,6 +548,11 @@ class RestAPI(pulumi.ComponentResource):
|
|
|
548
548
|
if not self.content:
|
|
549
549
|
return None
|
|
550
550
|
|
|
551
|
+
# Called from _collect_arns and _create_cognito_permissions; the
|
|
552
|
+
# second call must not declare the same named resources again.
|
|
553
|
+
if self._gateway_role is not None:
|
|
554
|
+
return self._gateway_role
|
|
555
|
+
|
|
551
556
|
content_with_bucket = [item for item in self.content if "bucket_name" in item]
|
|
552
557
|
prefixes = [item.get("prefix") for item in content_with_bucket]
|
|
553
558
|
|
|
@@ -618,6 +623,7 @@ class RestAPI(pulumi.ComponentResource):
|
|
|
618
623
|
)
|
|
619
624
|
log.info("S3 access policy attached successfully: %s", api_gateway_role)
|
|
620
625
|
|
|
626
|
+
self._gateway_role = api_gateway_role
|
|
621
627
|
return api_gateway_role
|
|
622
628
|
|
|
623
629
|
def get_endpoint(self):
|
|
@@ -60,9 +60,8 @@ class WebSocketAPI(pulumi.ComponentResource):
|
|
|
60
60
|
- require_auth (bool, optional): Whether this route
|
|
61
61
|
requires authorization
|
|
62
62
|
authorizer (Optional[dict]): Authorizer configuration with:
|
|
63
|
-
- type (str): "lambda"
|
|
64
|
-
- function: Lambda function for
|
|
65
|
-
- user_pool_arns: List of Cognito user pool ARNs
|
|
63
|
+
- type (str): "lambda" (the only supported type)
|
|
64
|
+
- function: Lambda function for the authorizer
|
|
66
65
|
- identity_source: Identity source expression
|
|
67
66
|
(e.g., "route.request.querystring.token")
|
|
68
67
|
hosted_zone_id (Optional[str]): Route 53 hosted zone ID
|
|
@@ -148,6 +147,17 @@ class WebSocketAPI(pulumi.ComponentResource):
|
|
|
148
147
|
if "function" not in route:
|
|
149
148
|
raise ValueError(f"Route '{route_key}' must have a 'function' defined")
|
|
150
149
|
|
|
150
|
+
# Only a lambda authorizer is built; anything else would leave
|
|
151
|
+
# the route silently open.
|
|
152
|
+
if (
|
|
153
|
+
route.get("require_auth")
|
|
154
|
+
and (self.authorizer or {}).get("type") != "lambda"
|
|
155
|
+
):
|
|
156
|
+
raise ValueError(
|
|
157
|
+
f"Route '{route_key}' has require_auth but no "
|
|
158
|
+
"authorizer of type 'lambda' is configured"
|
|
159
|
+
)
|
|
160
|
+
|
|
151
161
|
# Check if required routes are defined
|
|
152
162
|
missing_routes = required_routes - seen_routes
|
|
153
163
|
if missing_routes:
|
|
@@ -47,6 +47,7 @@ class PythonArchiveBuilder(ArchiveBuilder):
|
|
|
47
47
|
requirements: list[str],
|
|
48
48
|
working_dir: str,
|
|
49
49
|
target_architecture: str = "x86_64",
|
|
50
|
+
python_version: str = "3.12",
|
|
50
51
|
):
|
|
51
52
|
"""
|
|
52
53
|
Initialize the PythonArchiveBuilder with necessary parameters.
|
|
@@ -65,6 +66,8 @@ class PythonArchiveBuilder(ArchiveBuilder):
|
|
|
65
66
|
self._requirements = requirements or []
|
|
66
67
|
self._working_dir = working_dir
|
|
67
68
|
self._target_architecture = self._normalize_architecture(target_architecture)
|
|
69
|
+
# Wheels must match the Lambda runtime's ABI, not the build interpreter.
|
|
70
|
+
self._python_version = python_version
|
|
68
71
|
|
|
69
72
|
# Base directory where Lambda-related files will be stored
|
|
70
73
|
self._base_dir = os.path.join(self._working_dir, f"{self.name}-lambda")
|
|
@@ -187,7 +190,9 @@ class PythonArchiveBuilder(ArchiveBuilder):
|
|
|
187
190
|
info = zipfile.ZipInfo(relative_path, date_time=_ZIP_EPOCH)
|
|
188
191
|
info.compress_type = zipfile.ZIP_DEFLATED
|
|
189
192
|
executable = os.stat(full_path).st_mode & 0o111
|
|
190
|
-
info.external_attr = (
|
|
193
|
+
info.external_attr = (
|
|
194
|
+
0o100755 if executable else 0o100644
|
|
195
|
+
) << 16
|
|
191
196
|
with open(full_path, "rb") as source:
|
|
192
197
|
archive.writestr(info, source.read())
|
|
193
198
|
|
|
@@ -252,9 +257,10 @@ class PythonArchiveBuilder(ArchiveBuilder):
|
|
|
252
257
|
for index, wheel_platform in enumerate(wheel_platforms, start=1):
|
|
253
258
|
strategy_name = f"Strategy {index}"
|
|
254
259
|
log.info(
|
|
255
|
-
"%s: Attempting %s platform wheels for Python
|
|
260
|
+
"%s: Attempting %s platform wheels for Python %s",
|
|
256
261
|
strategy_name,
|
|
257
262
|
wheel_platform,
|
|
263
|
+
self._python_version,
|
|
258
264
|
)
|
|
259
265
|
try:
|
|
260
266
|
subprocess.check_call(
|
|
@@ -271,7 +277,7 @@ class PythonArchiveBuilder(ArchiveBuilder):
|
|
|
271
277
|
"--implementation",
|
|
272
278
|
"cp",
|
|
273
279
|
"--python-version",
|
|
274
|
-
|
|
280
|
+
self._python_version,
|
|
275
281
|
"--upgrade",
|
|
276
282
|
"--no-compile",
|
|
277
283
|
"-r",
|
|
@@ -442,6 +448,8 @@ class PythonArchiveBuilder(ArchiveBuilder):
|
|
|
442
448
|
self._get_file_resource(file_path, destination_path)
|
|
443
449
|
elif source_path.startswith(("http://", "https://")):
|
|
444
450
|
self._get_network_resource(source_path, destination_path)
|
|
451
|
+
elif os.path.exists(source_path):
|
|
452
|
+
self._get_file_resource(source_path, destination_path)
|
|
445
453
|
else: # Inline content
|
|
446
454
|
os.makedirs(os.path.dirname(destination_path), exist_ok=True)
|
|
447
455
|
with open(destination_path, "w") as f:
|
|
@@ -16,6 +16,37 @@ log.setLevel(os.environ.get("LOGGING_LEVEL", "INFO"))
|
|
|
16
16
|
cognito_client = boto3.client("cognito-idp")
|
|
17
17
|
|
|
18
18
|
|
|
19
|
+
# Cognito error code -> HTTP status. Other client errors stay 400, and
|
|
20
|
+
# Cognito's own server errors become 500.
|
|
21
|
+
COGNITO_STATUS = {
|
|
22
|
+
"NotAuthorizedException": 401,
|
|
23
|
+
"UserNotConfirmedException": 403,
|
|
24
|
+
"PasswordResetRequiredException": 403,
|
|
25
|
+
"UserNotFoundException": 404,
|
|
26
|
+
"ResourceNotFoundException": 404,
|
|
27
|
+
"UsernameExistsException": 409,
|
|
28
|
+
"TooManyRequestsException": 429,
|
|
29
|
+
"TooManyFailedAttemptsException": 429,
|
|
30
|
+
"LimitExceededException": 429,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def error_response(e: ClientError, overrides: Optional[dict] = None) -> dict:
|
|
35
|
+
code = e.response["Error"]["Code"]
|
|
36
|
+
status = {**COGNITO_STATUS, **(overrides or {})}.get(code)
|
|
37
|
+
if status is None:
|
|
38
|
+
upstream = e.response.get("ResponseMetadata", {}).get("HTTPStatusCode", 400)
|
|
39
|
+
status = 500 if upstream >= 500 else 400
|
|
40
|
+
return {
|
|
41
|
+
"statusCode": status,
|
|
42
|
+
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def bad_request(message: str) -> dict:
|
|
47
|
+
return {"statusCode": 400, "body": json.dumps({"message": message})}
|
|
48
|
+
|
|
49
|
+
|
|
19
50
|
class AuthorizationServices:
|
|
20
51
|
def __init__(
|
|
21
52
|
self,
|
|
@@ -34,8 +65,6 @@ class AuthorizationServices:
|
|
|
34
65
|
self.cognito_client = boto3.client("cognito-idp", region_name=region)
|
|
35
66
|
|
|
36
67
|
def handler(self, event, context):
|
|
37
|
-
log.info(f"event: {event}")
|
|
38
|
-
|
|
39
68
|
assert (
|
|
40
69
|
self.user_pool_id and self.client_id and self.client_secret
|
|
41
70
|
), "USER_POOL_ID, CLIENT_ID, and CLIENT_SECRET must be set"
|
|
@@ -44,32 +73,38 @@ class AuthorizationServices:
|
|
|
44
73
|
http_method = event.get("httpMethod", "").upper()
|
|
45
74
|
log.info(f"Path: {path}, HTTP Method: {http_method}")
|
|
46
75
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
76
|
+
try:
|
|
77
|
+
if path.endswith("/users") and http_method == "POST":
|
|
78
|
+
return self.create_user(event)
|
|
79
|
+
elif path.endswith("/users/{username}") and http_method == "GET":
|
|
80
|
+
return self.get_user(event)
|
|
81
|
+
elif path.endswith("/users/{username}") and http_method == "DELETE":
|
|
82
|
+
return self.delete_user(event)
|
|
83
|
+
elif path.endswith("/users/me/password") and http_method == "PUT":
|
|
84
|
+
return self.change_user_password(event)
|
|
85
|
+
elif path.endswith("/users/{username}/groups") and http_method == "PUT":
|
|
86
|
+
return self.change_user_groups(event)
|
|
87
|
+
elif path.endswith("/sessions") and http_method == "POST":
|
|
88
|
+
return self.create_session(event)
|
|
89
|
+
elif path.endswith("/sessions/me") and http_method == "DELETE":
|
|
90
|
+
return self.delete_session(event)
|
|
91
|
+
except json.JSONDecodeError:
|
|
92
|
+
return bad_request("Request body must be JSON")
|
|
93
|
+
return {
|
|
94
|
+
"statusCode": 404,
|
|
95
|
+
"body": json.dumps({"message": f"No route for {http_method} {path}"}),
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def body(event) -> dict:
|
|
100
|
+
return json.loads(event.get("body") or "{}")
|
|
68
101
|
|
|
69
102
|
def create_user(self, event):
|
|
70
|
-
body =
|
|
103
|
+
body = self.body(event)
|
|
71
104
|
username = body.get("username") or body.get("email")
|
|
72
105
|
password = body.get("password")
|
|
106
|
+
if not username or not password:
|
|
107
|
+
return bad_request("username and password are required")
|
|
73
108
|
|
|
74
109
|
try:
|
|
75
110
|
|
|
@@ -104,22 +139,12 @@ class AuthorizationServices:
|
|
|
104
139
|
"body": json.dumps({"message": "Signup successful"}),
|
|
105
140
|
}
|
|
106
141
|
except ClientError as e:
|
|
107
|
-
return
|
|
108
|
-
"statusCode": 400,
|
|
109
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
110
|
-
}
|
|
142
|
+
return error_response(e)
|
|
111
143
|
|
|
112
144
|
def get_user(self, event):
|
|
113
|
-
|
|
114
|
-
if
|
|
115
|
-
|
|
116
|
-
if self.user_admin_group not in permissions:
|
|
117
|
-
return {
|
|
118
|
-
"statusCode": 403,
|
|
119
|
-
"body": json.dumps(
|
|
120
|
-
{"message": "You are not authorized to delete users"}
|
|
121
|
-
),
|
|
122
|
-
}
|
|
145
|
+
denied = self.require_admin(event)
|
|
146
|
+
if denied:
|
|
147
|
+
return denied
|
|
123
148
|
|
|
124
149
|
username = unquote(event["pathParameters"]["username"])
|
|
125
150
|
try:
|
|
@@ -129,22 +154,12 @@ class AuthorizationServices:
|
|
|
129
154
|
"body": json.dumps({"user_info": user_info, "groups": sorted(groups)}),
|
|
130
155
|
}
|
|
131
156
|
except ClientError as e:
|
|
132
|
-
return
|
|
133
|
-
"statusCode": 400,
|
|
134
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
135
|
-
}
|
|
157
|
+
return error_response(e)
|
|
136
158
|
|
|
137
159
|
def delete_user(self, event):
|
|
138
|
-
|
|
139
|
-
if
|
|
140
|
-
|
|
141
|
-
if self.user_admin_group not in permissions:
|
|
142
|
-
return {
|
|
143
|
-
"statusCode": 403,
|
|
144
|
-
"body": json.dumps(
|
|
145
|
-
{"message": "You are not authorized to delete users"}
|
|
146
|
-
),
|
|
147
|
-
}
|
|
160
|
+
denied = self.require_admin(event)
|
|
161
|
+
if denied:
|
|
162
|
+
return denied
|
|
148
163
|
|
|
149
164
|
username = event["pathParameters"]["username"]
|
|
150
165
|
try:
|
|
@@ -168,13 +183,10 @@ class AuthorizationServices:
|
|
|
168
183
|
}
|
|
169
184
|
except ClientError as e:
|
|
170
185
|
log.error(f"Error deleting user: {e.response}")
|
|
171
|
-
return
|
|
172
|
-
"statusCode": 400,
|
|
173
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
174
|
-
}
|
|
186
|
+
return error_response(e)
|
|
175
187
|
|
|
176
188
|
def change_user_password(self, event):
|
|
177
|
-
body =
|
|
189
|
+
body = self.body(event)
|
|
178
190
|
try:
|
|
179
191
|
request_context = event.get("requestContext")
|
|
180
192
|
username = request_context.get("authorizer", {}).get("username")
|
|
@@ -212,26 +224,15 @@ class AuthorizationServices:
|
|
|
212
224
|
"body": json.dumps({"message": "Password changed successfully"}),
|
|
213
225
|
}
|
|
214
226
|
except ClientError as e:
|
|
215
|
-
return
|
|
216
|
-
"statusCode": 400,
|
|
217
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
218
|
-
}
|
|
227
|
+
return error_response(e)
|
|
219
228
|
|
|
220
229
|
def change_user_groups(self, event):
|
|
221
|
-
|
|
222
|
-
if
|
|
223
|
-
|
|
224
|
-
log.info(f"permissions: {permissions}")
|
|
225
|
-
if self.user_admin_group not in permissions:
|
|
226
|
-
return {
|
|
227
|
-
"statusCode": 403,
|
|
228
|
-
"body": json.dumps(
|
|
229
|
-
{"message": "You are not authorized to delete users"}
|
|
230
|
-
),
|
|
231
|
-
}
|
|
230
|
+
denied = self.require_admin(event)
|
|
231
|
+
if denied:
|
|
232
|
+
return denied
|
|
232
233
|
|
|
233
234
|
username = unquote(event["pathParameters"]["username"])
|
|
234
|
-
body =
|
|
235
|
+
body = self.body(event)
|
|
235
236
|
groups = body.get("groups", [])
|
|
236
237
|
|
|
237
238
|
try:
|
|
@@ -259,49 +260,53 @@ class AuthorizationServices:
|
|
|
259
260
|
"body": json.dumps({"message": "User groups updated successfully"}),
|
|
260
261
|
}
|
|
261
262
|
except ClientError as e:
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
265
|
-
}
|
|
263
|
+
# AdminAddUserToGroup reports an unknown group as ResourceNotFound.
|
|
264
|
+
return error_response(e, {"ResourceNotFoundException": 400})
|
|
266
265
|
|
|
267
266
|
def create_session(self, event):
|
|
268
|
-
|
|
267
|
+
"""Start a session with a password, or renew one with a refresh token
|
|
268
|
+
(one resource, two grants, like an OAuth token endpoint)."""
|
|
269
|
+
body = self.body(event)
|
|
269
270
|
username = body.get("username") or body.get("email")
|
|
270
|
-
|
|
271
|
-
|
|
271
|
+
if not username:
|
|
272
|
+
return bad_request("username is required")
|
|
273
|
+
if body.get("refresh_token"):
|
|
274
|
+
flow = "REFRESH_TOKEN_AUTH"
|
|
275
|
+
params = {"REFRESH_TOKEN": body["refresh_token"]}
|
|
276
|
+
elif body.get("password"):
|
|
277
|
+
flow = "USER_PASSWORD_AUTH"
|
|
278
|
+
params = {"USERNAME": username, "PASSWORD": body["password"]}
|
|
279
|
+
else:
|
|
280
|
+
return bad_request("password or refresh_token is required")
|
|
281
|
+
params["SECRET_HASH"] = self.calculate_secret_hash(username)
|
|
272
282
|
|
|
273
283
|
try:
|
|
274
284
|
response = cognito_client.initiate_auth(
|
|
275
|
-
AuthFlow=
|
|
276
|
-
AuthParameters={
|
|
277
|
-
"USERNAME": username,
|
|
278
|
-
"PASSWORD": password,
|
|
279
|
-
"SECRET_HASH": secret_hash,
|
|
280
|
-
},
|
|
281
|
-
ClientId=self.client_id,
|
|
285
|
+
AuthFlow=flow, AuthParameters=params, ClientId=self.client_id
|
|
282
286
|
)
|
|
283
|
-
|
|
287
|
+
tokens = response["AuthenticationResult"]
|
|
284
288
|
user_info, groups = self.fetch_user_info(username)
|
|
285
|
-
return {
|
|
286
|
-
"statusCode": 200,
|
|
287
|
-
"body": json.dumps(
|
|
288
|
-
{
|
|
289
|
-
"user_info": user_info,
|
|
290
|
-
"groups": groups,
|
|
291
|
-
"id_token": response["AuthenticationResult"]["IdToken"],
|
|
292
|
-
"access_token": response["AuthenticationResult"]["AccessToken"],
|
|
293
|
-
"refresh_token": response["AuthenticationResult"][
|
|
294
|
-
"RefreshToken"
|
|
295
|
-
],
|
|
296
|
-
}
|
|
297
|
-
),
|
|
298
|
-
}
|
|
299
289
|
except ClientError as e:
|
|
300
|
-
log.error(f"
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
290
|
+
log.error(f"{flow} failed: {e.response['Error']['Code']}")
|
|
291
|
+
# An unknown user is a failed login, not a 404 that tells a
|
|
292
|
+
# caller which usernames exist.
|
|
293
|
+
return error_response(e, {"UserNotFoundException": 401})
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
"statusCode": 200,
|
|
297
|
+
"body": json.dumps(
|
|
298
|
+
{
|
|
299
|
+
"user_info": user_info,
|
|
300
|
+
"groups": groups,
|
|
301
|
+
"id_token": tokens["IdToken"],
|
|
302
|
+
"access_token": tokens["AccessToken"],
|
|
303
|
+
# A refresh returns no new refresh token; keep the old one.
|
|
304
|
+
"refresh_token": tokens.get(
|
|
305
|
+
"RefreshToken", body.get("refresh_token")
|
|
306
|
+
),
|
|
307
|
+
}
|
|
308
|
+
),
|
|
309
|
+
}
|
|
305
310
|
|
|
306
311
|
def delete_session(self, event):
|
|
307
312
|
try:
|
|
@@ -323,10 +328,7 @@ class AuthorizationServices:
|
|
|
323
328
|
"body": json.dumps({"message": "Logout successful"}),
|
|
324
329
|
}
|
|
325
330
|
except ClientError as e:
|
|
326
|
-
return
|
|
327
|
-
"statusCode": 400,
|
|
328
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
329
|
-
}
|
|
331
|
+
return error_response(e)
|
|
330
332
|
|
|
331
333
|
def calculate_secret_hash(self, username):
|
|
332
334
|
message = username + self.client_id
|
|
@@ -335,66 +337,6 @@ class AuthorizationServices:
|
|
|
335
337
|
).digest()
|
|
336
338
|
return base64.b64encode(secret_hash).decode("utf-8")
|
|
337
339
|
|
|
338
|
-
def refresh_session(self, event):
|
|
339
|
-
body = json.loads(event["body"])
|
|
340
|
-
|
|
341
|
-
refresh_token = body.get("refresh_token")
|
|
342
|
-
if not refresh_token:
|
|
343
|
-
return {
|
|
344
|
-
"statusCode": 400,
|
|
345
|
-
"body": json.dumps({"message": "Refresh token is required"}),
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
username = event["requestContext"]["authorizer"].get("username")
|
|
349
|
-
|
|
350
|
-
try:
|
|
351
|
-
# Use the refresh token to get new tokens
|
|
352
|
-
secret_hash = self.calculate_secret_hash(
|
|
353
|
-
username
|
|
354
|
-
) # Calculate the secret hash
|
|
355
|
-
response = cognito_client.initiate_auth(
|
|
356
|
-
AuthFlow="REFRESH_TOKEN_AUTH",
|
|
357
|
-
AuthParameters={
|
|
358
|
-
"REFRESH_TOKEN": refresh_token,
|
|
359
|
-
"SECRET_HASH": secret_hash, # Include the secret hash
|
|
360
|
-
},
|
|
361
|
-
ClientId=self.client_id,
|
|
362
|
-
)
|
|
363
|
-
|
|
364
|
-
# Extract tokens from the response
|
|
365
|
-
access_token = response["AuthenticationResult"]["AccessToken"]
|
|
366
|
-
id_token = response["AuthenticationResult"]["IdToken"]
|
|
367
|
-
new_refresh_token = response["AuthenticationResult"].get(
|
|
368
|
-
"RefreshToken", refresh_token
|
|
369
|
-
) # Use the new refresh token if provided
|
|
370
|
-
|
|
371
|
-
user_info, groups = self.fetch_user_info(username)
|
|
372
|
-
|
|
373
|
-
return {
|
|
374
|
-
"statusCode": 200,
|
|
375
|
-
"body": json.dumps(
|
|
376
|
-
{
|
|
377
|
-
"access_token": access_token,
|
|
378
|
-
"id_token": id_token,
|
|
379
|
-
"refresh_token": new_refresh_token,
|
|
380
|
-
"user_info": user_info,
|
|
381
|
-
"groups": groups,
|
|
382
|
-
}
|
|
383
|
-
),
|
|
384
|
-
}
|
|
385
|
-
except ClientError as e:
|
|
386
|
-
log.error(f"ClientError occurred: {e.response}")
|
|
387
|
-
return {
|
|
388
|
-
"statusCode": 400,
|
|
389
|
-
"body": json.dumps({"message": e.response["Error"]["Message"]}),
|
|
390
|
-
}
|
|
391
|
-
except Exception as e:
|
|
392
|
-
log.error(f"Unexpected error occurred: {str(e)}")
|
|
393
|
-
return {
|
|
394
|
-
"statusCode": 500,
|
|
395
|
-
"body": json.dumps({"message": "An unexpected error occurred"}),
|
|
396
|
-
}
|
|
397
|
-
|
|
398
340
|
def fetch_user_info(self, username):
|
|
399
341
|
# Fetch user attributes
|
|
400
342
|
user_details = cognito_client.admin_get_user(
|
|
@@ -412,6 +354,20 @@ class AuthorizationServices:
|
|
|
412
354
|
|
|
413
355
|
return attributes, groups
|
|
414
356
|
|
|
357
|
+
def require_admin(self, event) -> Optional[dict]:
|
|
358
|
+
"""403 response unless the caller is in the admin group. Fails closed:
|
|
359
|
+
with no USER_ADMIN_GROUP configured nobody gets admin actions, since
|
|
360
|
+
otherwise any signed-in user could delete users or grant themselves
|
|
361
|
+
any group."""
|
|
362
|
+
if self.user_admin_group and self.user_admin_group in (
|
|
363
|
+
self.get_permissions_from_event(event)
|
|
364
|
+
):
|
|
365
|
+
return None
|
|
366
|
+
return {
|
|
367
|
+
"statusCode": 403,
|
|
368
|
+
"body": json.dumps({"message": "Admin permission required"}),
|
|
369
|
+
}
|
|
370
|
+
|
|
415
371
|
def get_permissions_from_event(self, event):
|
|
416
372
|
"""
|
|
417
373
|
Extracts permissions from a decoded JWT token.
|
|
@@ -422,7 +378,12 @@ class AuthorizationServices:
|
|
|
422
378
|
|
|
423
379
|
def to_list(x):
|
|
424
380
|
if isinstance(x, str):
|
|
425
|
-
|
|
381
|
+
x = x.strip()
|
|
382
|
+
# Authorizer context values must be strings, so a token
|
|
383
|
+
# validator passes a list as JSON ('["admin"]').
|
|
384
|
+
if x.startswith("["):
|
|
385
|
+
return to_list(json.loads(x))
|
|
386
|
+
return re.split(r"[,\s]+", x)
|
|
426
387
|
elif isinstance(x, list):
|
|
427
388
|
return x
|
|
428
389
|
else:
|
|
@@ -13,35 +13,37 @@ MAIL_ORIGIN = os.environ["MAIL_ORIGIN"]
|
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
def handler(event, context):
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
subject = sns_message["subject"]
|
|
30
|
-
cc = sns_message.get("cc", []) # Get CC list, default to an empty list
|
|
31
|
-
bcc = sns_message.get("bcc", []) # Get BCC list, default to an empty list
|
|
16
|
+
# Errors must reach Lambda: a normal return tells SNS/SQS the message was
|
|
17
|
+
# delivered, so it is deleted instead of retried or sent to the DLQ.
|
|
18
|
+
failures = []
|
|
19
|
+
for record in event["Records"]:
|
|
20
|
+
try:
|
|
21
|
+
send_record(record)
|
|
22
|
+
except Exception:
|
|
23
|
+
if "messageId" not in record: # SNS or direct invoke: let it retry
|
|
24
|
+
raise
|
|
25
|
+
log.exception("Failed to send SQS message %s", record["messageId"])
|
|
26
|
+
failures.append({"itemIdentifier": record["messageId"]})
|
|
27
|
+
# Read by SQS event source mappings with ReportBatchItemFailures.
|
|
28
|
+
return {"batchItemFailures": failures}
|
|
32
29
|
|
|
33
|
-
email_body = render_template(template_name, context_data)
|
|
34
|
-
response = send_email(recipients, subject, email_body, cc, bcc)
|
|
35
|
-
responses.append(response)
|
|
36
30
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
31
|
+
def send_record(record):
|
|
32
|
+
if "Sns" in record:
|
|
33
|
+
message = json.loads(record["Sns"]["Message"])
|
|
34
|
+
elif "body" in record:
|
|
35
|
+
message = json.loads(record["body"])
|
|
36
|
+
else:
|
|
37
|
+
raise ValueError("Unsupported message format")
|
|
38
|
+
|
|
39
|
+
email_body = render_template(message["template_name"], message["context"])
|
|
40
|
+
return send_email(
|
|
41
|
+
message["recipients"],
|
|
42
|
+
message["subject"],
|
|
43
|
+
email_body,
|
|
44
|
+
message.get("cc", []),
|
|
45
|
+
message.get("bcc", []),
|
|
46
|
+
)
|
|
45
47
|
|
|
46
48
|
|
|
47
49
|
def render_template(template_name, context):
|
|
@@ -51,7 +51,7 @@ def request_mapping_enable(
|
|
|
51
51
|
up from the caller's ambient environment/credentials.
|
|
52
52
|
"""
|
|
53
53
|
events = boto3.client("events", region_name=region_name)
|
|
54
|
-
events.put_events(
|
|
54
|
+
response = events.put_events(
|
|
55
55
|
Entries=[
|
|
56
56
|
{
|
|
57
57
|
"EventBusName": event_bus_name,
|
|
@@ -61,3 +61,7 @@ def request_mapping_enable(
|
|
|
61
61
|
}
|
|
62
62
|
]
|
|
63
63
|
)
|
|
64
|
+
# put_events reports per-entry failures (e.g. throttling) in the response
|
|
65
|
+
# instead of raising; a lost request leaves the mapping disabled.
|
|
66
|
+
if response.get("FailedEntryCount"):
|
|
67
|
+
raise RuntimeError(f"EnableMapping event not accepted: {response['Entries']}")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{cloud_foundry-0.5.3 → cloud_foundry-0.5.4}/cloud_foundry/services/postgres_schema_bootstrap.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|