api-foundry 0.8.5__tar.gz → 0.8.7__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: api-foundry
3
- Version: 0.8.5
3
+ Version: 0.8.7
4
4
  Summary: The `api_foundry` project is a powerful tool designed to automate the deployment of REST APIs on AWS using Lambda services to access and interact with relational databases (RDBMS). This project leverages the OpenAPI specification to define and manage the APIs
5
5
  Project-URL: Documentation, https://github.com/DanRepik/api-foundry
6
6
  Project-URL: Source, https://github.com/DanRepik/api-foundry
@@ -1,4 +1,5 @@
1
1
  # __init__.py
2
+ __version__ = "0.8.7"
2
3
 
3
4
  from api_foundry.iac.pulumi.api_foundry import APIFoundry
4
5
  from cloud_foundry import logger
@@ -17,6 +17,7 @@ class APISpecEditor:
17
17
  function: Optional[Function]
18
18
  integrations: list[dict]
19
19
  batch_path: Optional[str]
20
+ token_validators: list[dict]
20
21
 
21
22
  def __init__(
22
23
  self,
@@ -24,15 +25,99 @@ class APISpecEditor:
24
25
  open_api_spec: Optional[dict],
25
26
  function: Optional[Function],
26
27
  batch_path: Optional[str] = None,
28
+ token_validators: Optional[list[dict]] = None,
27
29
  ):
28
30
  self.function = function
29
31
  self.batch_path = batch_path
32
+ self.token_validators = token_validators or []
30
33
  self.integrations = []
31
34
  self.editor = AWSOpenAPISpecEditor(
32
35
  copy.deepcopy(open_api_spec) if open_api_spec else None
33
36
  )
34
37
 
38
+ def _get_validators_for_schema(self, schema_object: dict) -> list[str]:
39
+ """Extract validator names from schema object.
40
+
41
+ Uses the global security scheme from the OpenAPI spec instead of
42
+ x-af-permissions provider keys.
43
+ """
44
+ validators = []
45
+
46
+ # Defensive: ensure schema_object is actually a dict
47
+ if not isinstance(schema_object, dict):
48
+ return validators
49
+
50
+ # Check if schema has permissions (indicating it needs auth)
51
+ if "x-af-permissions" in schema_object:
52
+ # Use the global security scheme from the spec
53
+ global_security = self.editor.get_spec_part(["security"], create=False)
54
+ if (
55
+ global_security
56
+ and isinstance(global_security, list)
57
+ and len(global_security) > 0
58
+ ):
59
+ # Extract security scheme names from global security
60
+ # Format: [{"auth": []}, {"other": []}]
61
+ for sec_item in global_security:
62
+ if isinstance(sec_item, dict):
63
+ validators.extend(sec_item.keys())
64
+
65
+ # Fallback: if no global security, look for securitySchemes
66
+ if not validators:
67
+ security_schemes = self.editor.get_spec_part(
68
+ ["components", "securitySchemes"], create=False
69
+ )
70
+ if security_schemes and isinstance(security_schemes, dict):
71
+ validators = list(security_schemes.keys())
72
+
73
+ return validators
74
+
75
+ def process_existing_path_operations(self):
76
+ """Process existing path operations with x-af-database.
77
+
78
+ Scans the OpenAPI spec's paths section and adds integrations for
79
+ any custom SQL operations (those with x-af-database attribute).
80
+ This ensures custom path operations get Lambda integrations just
81
+ like auto-generated CRUD operations.
82
+ """
83
+ paths = self.editor.get_spec_part(["paths"], create=False)
84
+ if not paths or not isinstance(paths, dict):
85
+ return
86
+
87
+ for path, path_item in paths.items():
88
+ if not isinstance(path_item, dict):
89
+ continue
90
+
91
+ # Check each HTTP method
92
+ for method in ["get", "post", "put", "delete", "patch"]:
93
+ operation = path_item.get(method)
94
+ if not operation or not isinstance(operation, dict):
95
+ continue
96
+
97
+ # Only process operations with x-af-database
98
+ # (indicates custom SQL managed by API Foundry)
99
+ if "x-af-database" not in operation:
100
+ continue
101
+
102
+ # Add to integrations list so cloud_foundry.rest_api
103
+ # will create the x-amazon-apigateway-integration
104
+ self.integrations.append(
105
+ {
106
+ "path": path,
107
+ "method": method,
108
+ "function": self.function,
109
+ }
110
+ )
111
+
112
+ log.info(
113
+ f"Added integration for custom SQL operation: "
114
+ f"{method.upper()} {path}"
115
+ )
116
+
35
117
  def rest_api_spec(self) -> str:
118
+ # Process existing path operations with x-af-database (custom SQL operations)
119
+ self.process_existing_path_operations()
120
+
36
121
  schemas = self.editor.get_spec_part(["components", "schemas"], create=False)
37
122
  if schemas:
38
123
  if isinstance(schemas, dict):
@@ -62,13 +147,25 @@ class APISpecEditor:
62
147
  schema_object: dict,
63
148
  function: Optional[Function] = None,
64
149
  ):
65
- # Ensure any schema-level permissions are carried onto the operation if not set.
150
+ # Ensure schema-level permissions carry onto operation if not set.
66
151
  if "x-af-permissions" not in operation and isinstance(schema_object, dict):
67
152
  permissions = schema_object.get("x-af-permissions")
68
153
  if permissions:
69
154
  operation["x-af-permissions"] = permissions
155
+
156
+ # Add security requirements based on validators
157
+ if "security" not in operation and isinstance(schema_object, dict):
158
+ validators = self._get_validators_for_schema(schema_object)
159
+ if validators:
160
+ # Build security array: [{validator_name: []}]
161
+ operation["security"] = [{v: []} for v in validators]
162
+
70
163
  self.integrations.append(
71
- {"path": path, "method": method, "function": function or self.function}
164
+ {
165
+ "path": path,
166
+ "method": method,
167
+ "function": function or self.function,
168
+ }
72
169
  )
73
170
  self.editor.add_operation(
74
171
  path=path,
@@ -96,16 +193,19 @@ class APISpecEditor:
96
193
 
97
194
  elif property["type"] == "string" and property.get("format", None) == "date":
98
195
  # Assuming ISO 8601 date format (YYYY-MM-DD)
99
- regex_pattern = r"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])" # Date part: YYYY-MM-DD
196
+ regex_pattern = r"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])"
100
197
 
101
198
  elif (
102
199
  property["type"] == "string" and property.get("format", None) == "date-time"
103
200
  ):
104
201
  regex_pattern = (
105
- r"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])" # Date part: YYYY-MM-DD
202
+ r"\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])"
203
+ # Date part: YYYY-MM-DD
106
204
  r"T" # Separator: T
107
- r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])" # Time part: HH:MM:SS
108
- r"(?:\.\d+)?(?:Z|[+-](?:0[0-9]|1[0-4]):[0-5][0-9])?" # Optional: fractional seconds and timezone
205
+ r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"
206
+ # Time part: HH:MM:SS
207
+ r"(?:\.\d+)?(?:Z|[+-](?:0[0-9]|1[0-4]):[0-5][0-9])?"
208
+ # Optional: fractional seconds and timezone
109
209
  )
110
210
 
111
211
  elif property["type"] == "string":
@@ -114,7 +214,8 @@ class APISpecEditor:
114
214
  else:
115
215
  max_length = property.get("max_length", 200)
116
216
  min_length = property.get("min_length", 0)
117
- regex_pattern = rf"[\w\s]{min_length},{max_length}" # Allows letters, numbers, and underscores
217
+ regex_pattern = rf"[\w\s]{min_length},{max_length}"
218
+ # Allows letters, numbers, and underscores
118
219
 
119
220
  elif property["type"] == "integer":
120
221
  signed = property.get("signed", True)
@@ -123,11 +123,15 @@ class APIFoundry(ComponentResource):
123
123
  api_spec: Union[str, list[str]],
124
124
  batch_path: Optional[str] = None,
125
125
  secrets: Optional[str] = None,
126
+ path_prefix: Optional[str] = None,
126
127
  environment: Optional[dict[str, Union[str, pulumi.Output[str]]]] = None,
127
128
  integrations: Optional[list[dict]] = None,
128
129
  token_validators: Optional[list[dict]] = None,
130
+ timeout_seconds: Optional[int] = None,
129
131
  policy_statements: Optional[list] = None,
130
132
  vpc_config: Optional[dict] = None,
133
+ hosted_zone_id: Optional[str] = None,
134
+ subdomain: Optional[str] = None,
131
135
  export_api: Optional[str] = None,
132
136
  opts=None,
133
137
  ):
@@ -152,25 +156,66 @@ class APIFoundry(ComponentResource):
152
156
  env_vars["SECRETS"] = secrets
153
157
  requirements = []
154
158
 
155
- # Grant read access to referenced secrets (single broad stmt)
156
- if json.loads(secrets):
157
- policy_statements.append(
158
- {
159
- "Effect": "Allow",
160
- "Actions": ["secretsmanager:GetSecretValue"],
161
- "Resources": ["*"],
162
- }
163
- )
159
+ # Grant read access to referenced secrets
160
+ if secrets:
161
+ try:
162
+ secret_arns = list(json.loads(secrets).values())
163
+ policy_statements.append(
164
+ {
165
+ "Effect": "Allow",
166
+ "Actions": ["secretsmanager:GetSecretValue"],
167
+ "Resources": secret_arns,
168
+ }
169
+ )
170
+ except (json.JSONDecodeError, AttributeError):
171
+ # If secrets is an Output or invalid JSON, we can't parse it yet
172
+ # Add a broad policy (will be restricted during deployment)
173
+ policy_statements.append(
174
+ {
175
+ "Effect": "Allow",
176
+ "Actions": ["secretsmanager:GetSecretValue"],
177
+ "Resources": "*",
178
+ }
179
+ )
164
180
 
165
181
  requirements.extend(
166
- ["psycopg2-binary", "pyyaml", "api_foundry_query_engine"]
182
+ [
183
+ "psycopg2-binary",
184
+ "pyyaml",
185
+ "api_foundry_query_engine",
186
+ ]
167
187
  )
188
+
189
+ # Configure Lambda-based token validation if validators are provided
190
+ if token_validators:
191
+ # Get the first validator function (primary validator)
192
+ primary_validator = token_validators[0]
193
+ validator_function = primary_validator.get("function")
194
+
195
+ if validator_function:
196
+ # Check if validator_function has 'arn' attribute
197
+ # (Pulumi Output)
198
+ if hasattr(validator_function, "arn"):
199
+ # Store the ARN for the query engine to invoke
200
+ env_vars["TOKEN_VALIDATOR_LAMBDA_ARN"] = validator_function.arn
201
+
202
+ # Grant invoke permission for the query lambda
203
+ policy_statements.append(
204
+ {
205
+ "Effect": "Allow",
206
+ "Actions": ["lambda:InvokeFunction"],
207
+ "Resources": [validator_function.arn],
208
+ }
209
+ )
210
+ log.info("Configured Lambda token validator for query engine")
211
+
168
212
  if env_vars.get("JWKS_HOST"):
169
213
  requirements.extend(["PyJWT", "cryptography", "requests"])
170
214
 
171
215
  self.api_function = cloud_foundry.python_function(
172
216
  name=name,
173
217
  environment=env_vars,
218
+ runtime="python3.12",
174
219
  handler="api_foundry_query_engine.lambda_handler.handler",
175
220
  sources={
176
221
  "api_spec.yaml": yaml.safe_dump(
@@ -178,6 +223,7 @@ class APIFoundry(ComponentResource):
178
223
  ),
179
224
  },
180
225
  requirements=requirements,
226
+ timeout=timeout_seconds or 30,
181
227
  policy_statements=policy_statements,
182
228
  vpc_config=vpc_config,
183
229
  )
@@ -186,6 +232,7 @@ class APIFoundry(ComponentResource):
186
232
  open_api_spec=api_spec_dict,
187
233
  function=self.api_function,
188
234
  batch_path=batch_path,
235
+ token_validators=token_validators,
189
236
  )
190
237
 
191
238
  # Merge gateway_spec.integrations with user-provided integrations
@@ -198,6 +245,9 @@ class APIFoundry(ComponentResource):
198
245
  integrations=merged_integrations,
199
246
  token_validators=token_validators or [],
200
247
  export_api=export_api,
248
+ path_prefix=path_prefix,
249
+ hosted_zone_id=hosted_zone_id,
250
+ subdomain=subdomain,
201
251
  opts=pulumi.ResourceOptions(parent=self),
202
252
  )
203
253
 
@@ -303,12 +303,66 @@ class SchemaObjectAssociation(OpenAPIElement):
303
303
  def __init__(self, name: str, prop: Dict[str, Any], parent_key):
304
304
  super().__init__()
305
305
  self.api_name = name
306
+
307
+ # Validate type attribute exists
308
+ if "type" not in prop:
309
+ raise ApplicationException(
310
+ 500,
311
+ (
312
+ f"Association property '{name}' is missing 'type' "
313
+ f"attribute. Schema associations must specify "
314
+ f"'type: object' or 'type: array'."
315
+ ),
316
+ )
306
317
  self.api_type = prop["type"]
307
318
 
308
- self.schema_name = (
309
- prop["items"]["$ref"] if self.api_type == "array" else prop["$ref"]
310
- ).split("/")[-1]
319
+ # Extract and validate schema reference
320
+ if self.api_type == "array":
321
+ if "items" not in prop:
322
+ raise ApplicationException(
323
+ 500,
324
+ (
325
+ f"Association property '{name}' has type 'array' but "
326
+ f"is missing 'items' attribute. Array associations "
327
+ f"must specify items with a $ref to the target schema."
328
+ ),
329
+ )
330
+ items = prop["items"]
331
+ if "$ref" not in items:
332
+ raise ApplicationException(
333
+ 500,
334
+ (
335
+ f"Association property '{name}' array items are "
336
+ f"missing '$ref' attribute. Use '$ref: "
337
+ f"#/components/schemas/YourSchema' to reference the "
338
+ f"target schema."
339
+ ),
340
+ )
341
+ ref = items["$ref"]
342
+ else:
343
+ if "$ref" not in prop:
344
+ raise ApplicationException(
345
+ 500,
346
+ (
347
+ f"Association property '{name}' is missing '$ref' "
348
+ f"attribute. Use '$ref: #/components/schemas/YourSchema' "
349
+ f"to reference the target schema."
350
+ ),
351
+ )
352
+ ref = prop["$ref"]
353
+
354
+ # Validate $ref format
355
+ if not ref.startswith("#/components/schemas/"):
356
+ raise ApplicationException(
357
+ 500,
358
+ (
359
+ f"Association property '{name}' has invalid $ref format: "
360
+ f"'{ref}'. References must use the format "
361
+ f"'#/components/schemas/SchemaName'."
362
+ ),
363
+ )
311
364
 
365
+ self.schema_name = ref.split("/")[-1]
312
366
  self.child_property = prop.get("x-af-child-property", None)
313
367
  self.parent_property = prop.get("x-af-parent-property", parent_key)
314
368
 
@@ -319,7 +373,20 @@ class SchemaObject(OpenAPIElement):
319
373
  def __init__(self, api_name: str, schema_object: Dict[str, Any]):
320
374
  super().__init__()
321
375
  self.api_name = api_name
376
+
377
+ # Get database name (should always exist as caller filters)
322
378
  self.database = schema_object.get("x-af-database", "").lower()
379
+
380
+ # Validate database name is not empty
381
+ if not self.database:
382
+ raise ApplicationException(
383
+ 500,
384
+ (
385
+ f"Schema object '{api_name}' has empty 'x-af-database' "
386
+ f"value. Provide a valid database connection name."
387
+ ),
388
+ )
389
+
323
390
  self.table_name = self._get_table_name(schema_object)
324
391
  self.properties = self._resolve_properties(schema_object)
325
392
  self.primary_key = self._get_primary_key(schema_object)
@@ -438,40 +505,59 @@ class SchemaObject(OpenAPIElement):
438
505
  return concurrency_property_name
439
506
 
440
507
  def _get_primary_key(self, schema_object: dict) -> Optional[str]:
508
+ primary_keys = []
441
509
  for property_name, properties in schema_object.get("properties", {}).items():
442
510
  if "x-af-primary-key" in properties:
443
- prop_obj = self.properties[property_name]
444
- prop_obj.key_type = properties.get("x-af-primary-key", "auto")
445
-
446
- if prop_obj.key_type not in [
447
- "manual",
448
- "uuid",
449
- "auto",
450
- "sequence",
451
- ]:
452
- raise ApplicationException(
453
- 500,
454
- (
455
- f"Invalid primary key type '{prop_obj.key_type}' "
456
- + f"in schema object '{self.api_name}'"
457
- + f", property '{property_name}'"
458
- ),
459
- )
511
+ primary_keys.append(property_name)
460
512
 
461
- if prop_obj.key_type == "sequence":
462
- prop_obj.sequence_name = properties.get("x-af-sequence-name", None)
463
- if not prop_obj.sequence_name:
464
- raise ApplicationException(
465
- 500,
466
- (
467
- "Sequence-based primary keys must have a "
468
- "sequence name "
469
- + f"in schema object '{self.api_name}', "
470
- + f"property '{property_name}'"
471
- ),
472
- )
473
- return property_name
474
- return None
513
+ # Validate only one primary key is defined
514
+ if len(primary_keys) > 1:
515
+ raise ApplicationException(
516
+ 500,
517
+ (
518
+ f"Schema object '{self.api_name}' has multiple primary "
519
+ f"keys defined: {', '.join(primary_keys)}. Only one "
520
+ f"property can be marked with 'x-af-primary-key'."
521
+ ),
522
+ )
523
+
524
+ if not primary_keys:
525
+ return None
526
+
527
+ property_name = primary_keys[0]
528
+ properties = schema_object["properties"][property_name]
529
+ prop_obj = self.properties[property_name]
530
+ prop_obj.key_type = properties.get("x-af-primary-key", "auto")
531
+
532
+ if prop_obj.key_type not in [
533
+ "manual",
534
+ "uuid",
535
+ "auto",
536
+ "sequence",
537
+ ]:
538
+ raise ApplicationException(
539
+ 500,
540
+ (
541
+ f"Invalid primary key type '{prop_obj.key_type}' "
542
+ f"in schema object '{self.api_name}', "
543
+ f"property '{property_name}'. Valid types are: "
544
+ f"'manual', 'uuid', 'auto', 'sequence'."
545
+ ),
546
+ )
547
+
548
+ if prop_obj.key_type == "sequence":
549
+ prop_obj.sequence_name = properties.get("x-af-sequence-name", None)
550
+ if not prop_obj.sequence_name:
551
+ raise ApplicationException(
552
+ 500,
553
+ (
554
+ f"Sequence-based primary key '{property_name}' in "
555
+ f"schema object '{self.api_name}' is missing "
556
+ f"'x-af-sequence-name' attribute. Specify the "
557
+ f"database sequence name to use."
558
+ ),
559
+ )
560
+ return property_name
475
561
 
476
562
  def _get_permissions(self, schema_object: dict) -> dict:
477
563
  """Extract permissions from schema using x-af-permissions only.
@@ -536,8 +622,37 @@ class PathOperation(OpenAPIElement):
536
622
  super().__init__()
537
623
  self.entity = path.lower().rsplit("/", 1)[-1]
538
624
  self.action = METHODS_TO_ACTIONS[method]
625
+
626
+ # Validate required x-af-database attribute
627
+ if "x-af-database" not in path_operation:
628
+ raise ApplicationException(
629
+ 500,
630
+ (
631
+ f"Path operation '{method.upper()} {path}' is missing "
632
+ f"required 'x-af-database' attribute. Custom path operations "
633
+ f"must specify which database connection to use."
634
+ ),
635
+ )
539
636
  self.database = path_operation["x-af-database"]
637
+
638
+ # Validate required x-af-sql attribute for custom operations
639
+ if "x-af-sql" not in path_operation:
640
+ raise ApplicationException(
641
+ 500,
642
+ (
643
+ f"Path operation '{method.upper()} {path}' is missing "
644
+ f"required 'x-af-sql' attribute. Custom path operations must "
645
+ f"provide the SQL query/statement to execute.\n\n"
646
+ f"Note: If you intended to create CRUD endpoints, define a "
647
+ f"schema object in components/schemas with 'x-af-database' "
648
+ f"instead of creating custom path operations. API Foundry "
649
+ f"auto-generates CRUD endpoints from schema objects.\n\n"
650
+ f"For custom SQL operations, add:\n"
651
+ f" x-af-sql: 'SELECT ...' or 'INSERT ...' etc."
652
+ ),
653
+ )
540
654
  self.sql = path_operation["x-af-sql"]
655
+
541
656
  self.inputs = self.get_inputs(path_operation)
542
657
  self.outputs = self._extract_properties(path_operation, "responses")
543
658
  self.permissions = self._get_permissions(path_operation)
@@ -670,25 +785,32 @@ class ModelFactory:
670
785
  return resolve(spec)
671
786
 
672
787
  def _load_schema_objects(self) -> Dict[str, SchemaObject]:
673
- """Loads all schema objects from the OpenAPI specification."""
788
+ """Loads all schema objects from the OpenAPI specification.
789
+
790
+ Only schemas with x-af-database attribute are loaded. Other schemas
791
+ (like DTOs, enums, or reference-only types) are silently skipped.
792
+ """
674
793
  schema_objects = {}
675
794
  schemas = self.spec.get("components", {}).get("schemas", {})
676
795
  for name, schema in schemas.items():
677
796
  if "x-af-database" in schema:
678
797
  schema_objects[name] = SchemaObject(name, schema)
798
+ else:
799
+ log.debug(f"Skipping schema '{name}' - no x-af-database attribute")
679
800
  return schema_objects
680
801
 
681
802
  def _load_path_operations(self) -> Dict[str, PathOperation]:
682
803
  """Loads all path operations from the OpenAPI specification."""
683
804
  path_operations = {}
684
805
  paths = self.spec.get("paths", {})
685
- for path, methods in paths.items():
686
- for method, operation in methods.items():
687
- if "x-af-database" in operation:
688
- path_operation = PathOperation(path, method, operation)
689
- path_operations[
690
- f"{path_operation.entity}_{path_operation.action}"
691
- ] = path_operation
806
+ if paths:
807
+ for path, methods in paths.items():
808
+ for method, operation in methods.items():
809
+ if "x-af-database" in operation:
810
+ path_operation = PathOperation(path, method, operation)
811
+ path_operations[
812
+ f"{path_operation.entity}_{path_operation.action}"
813
+ ] = path_operation
692
814
  return path_operations
693
815
 
694
816
  def get_config_output(self) -> Dict[str, Any]:
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "api-foundry"
7
- version = "0.8.5"
7
+ dynamic = ["version"]
8
8
  authors = [
9
9
  { name="Daniel Repik", email="danrepik@icloud.com" },
10
10
  ]
@@ -54,6 +54,10 @@ install_secret = "api_foundry.scripts.install_secret:main"
54
54
  [tool.hatch.metadata]
55
55
  allow-direct-references = true
56
56
 
57
+ [tool.hatch.version]
58
+ path = "api_foundry/__init__.py"
59
+
60
+
57
61
  [tool.pytest.ini_options]
58
62
  markers = [
59
63
  "unit: marks tests as unit tests that do not require a database",
File without changes
File without changes