api-foundry 0.1.21__tar.gz → 0.8.1__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.1.21
3
+ Version: 0.8.1
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
@@ -83,9 +83,7 @@ def load_api_spec(api_spec: Union[str, list[str]]) -> dict:
83
83
  try:
84
84
  as_yaml = yaml.safe_load(spec)
85
85
  except yaml.YAMLError as exc: # not YAML
86
- raise ValueError(
87
- f"Invalid OpenAPI spec source: {spec}"
88
- ) from exc
86
+ raise ValueError(f"Invalid OpenAPI spec source: {spec}") from exc
89
87
 
90
88
  if is_valid_openapi_spec(as_yaml):
91
89
  inputs.append(as_yaml)
@@ -124,9 +122,7 @@ class APIFoundry(ComponentResource):
124
122
  *,
125
123
  api_spec: Union[str, list[str]],
126
124
  secrets: Optional[str] = None,
127
- environment: Optional[
128
- dict[str, Union[str, pulumi.Output[str]]]
129
- ] = None,
125
+ environment: Optional[dict[str, Union[str, pulumi.Output[str]]]] = None,
130
126
  integrations: Optional[list[dict]] = None,
131
127
  token_validators: Optional[list[dict]] = None,
132
128
  policy_statements: Optional[list] = None,
@@ -153,8 +149,9 @@ class APIFoundry(ComponentResource):
153
149
  vpc_config = vpc_config or config_defaults.get("vpc_config", {})
154
150
 
155
151
  env_vars["SECRETS"] = secrets
152
+ requirements = []
156
153
 
157
- # Grant read access to referenced secrets (single broad stmt)
154
+ # Grant read access to referenced secrets (single broad stmt)
158
155
  if json.loads(secrets):
159
156
  policy_statements.append(
160
157
  {
@@ -164,6 +161,12 @@ class APIFoundry(ComponentResource):
164
161
  }
165
162
  )
166
163
 
164
+ requirements.extend(
165
+ ["psycopg2-binary", "pyyaml", "../api_foundry_query_engine"]
166
+ )
167
+ if env_vars.get("JWKS_HOST"):
168
+ requirements.extend(["PyJWT", "cryptography", "requests"])
169
+
167
170
  self.api_function = cloud_foundry.python_function(
168
171
  name=name,
169
172
  environment=env_vars,
@@ -173,11 +176,7 @@ class APIFoundry(ComponentResource):
173
176
  ModelFactory(api_spec_dict).get_config_output()
174
177
  ),
175
178
  },
176
- requirements=[
177
- "psycopg2-binary",
178
- "pyyaml",
179
- "api_foundry_query_engine",
180
- ],
179
+ requirements=requirements,
181
180
  policy_statements=policy_statements,
182
181
  vpc_config=vpc_config,
183
182
  )
@@ -112,9 +112,7 @@ class SchemaObjectProperty(OpenAPIElement):
112
112
  "double",
113
113
  }:
114
114
  self.column_type = (
115
- "real"
116
- if self.numeric_format == "float"
117
- else "double precision"
115
+ "real" if self.numeric_format == "float" else "double precision"
118
116
  )
119
117
  else:
120
118
  self.column_type = prop.get("type") or "string"
@@ -128,13 +126,9 @@ class SchemaObjectProperty(OpenAPIElement):
128
126
  self.concurrency_control = self._concurrency_control(schema_name, prop)
129
127
  # For embedded objects/arrays
130
128
  self.sub_properties: Optional[Dict[str, SchemaObjectProperty]] = None
131
- self.items_sub_properties: Optional[
132
- Dict[str, SchemaObjectProperty]
133
- ] = None
129
+ self.items_sub_properties: Optional[Dict[str, SchemaObjectProperty]] = None
134
130
 
135
- def _concurrency_control(
136
- self, schema_name: str, prop_dict: dict
137
- ) -> Optional[str]:
131
+ def _concurrency_control(self, schema_name: str, prop_dict: dict) -> Optional[str]:
138
132
  concurrency_control = prop_dict.get("x-af-concurrency-control", None)
139
133
  if concurrency_control:
140
134
  concurrency_control = concurrency_control.lower()
@@ -153,9 +147,7 @@ class SchemaObjectProperty(OpenAPIElement):
153
147
  class SchemaObjectKey(SchemaObjectProperty):
154
148
  """Represents a primary key in a schema object."""
155
149
 
156
- def __init__(
157
- self, schema_name: str, name: str, properties: Dict[str, Any]
158
- ):
150
+ def __init__(self, schema_name: str, name: str, properties: Dict[str, Any]):
159
151
  super().__init__(schema_name, name, properties)
160
152
  self.key_type = properties.get("x-af-primary-key", "auto")
161
153
 
@@ -212,9 +204,7 @@ class SchemaObject(OpenAPIElement):
212
204
  self.properties = self._resolve_properties(schema_object)
213
205
  self.primary_key = self._get_primary_key(schema_object)
214
206
  self.relations = self._resolve_relations(schema_object)
215
- self.concurrency_property = self._get_concurrency_property(
216
- schema_object
217
- )
207
+ self.concurrency_property = self._get_concurrency_property(schema_object)
218
208
  self.permissions = self._get_permissions(schema_object)
219
209
 
220
210
  def _get_table_name(self, schema_object: dict) -> str:
@@ -305,19 +295,14 @@ class SchemaObject(OpenAPIElement):
305
295
  property_name, prop, self.primary_key
306
296
  )
307
297
  # Array of $ref (array relation)
308
- elif (
309
- prop.get("type") == "array"
310
- and "$ref" in prop.get("items", {})
311
- ):
298
+ elif prop.get("type") == "array" and "$ref" in prop.get("items", {}):
312
299
  relations[property_name.lower()] = SchemaObjectAssociation(
313
300
  property_name, prop, self.primary_key
314
301
  )
315
302
  return relations
316
303
 
317
304
  def _get_concurrency_property(self, schema_object: dict) -> Optional[str]:
318
- concurrency_property_name = schema_object.get(
319
- "x-af-concurrency-control"
320
- )
305
+ concurrency_property_name = schema_object.get("x-af-concurrency-control")
321
306
  if (
322
307
  concurrency_property_name
323
308
  and concurrency_property_name not in self.properties
@@ -332,14 +317,10 @@ class SchemaObject(OpenAPIElement):
332
317
  return concurrency_property_name
333
318
 
334
319
  def _get_primary_key(self, schema_object: dict) -> Optional[str]:
335
- for property_name, properties in schema_object.get(
336
- "properties", {}
337
- ).items():
320
+ for property_name, properties in schema_object.get("properties", {}).items():
338
321
  if "x-af-primary-key" in properties:
339
322
  prop_obj = self.properties[property_name]
340
- prop_obj.key_type = properties.get(
341
- "x-af-primary-key", "auto"
342
- )
323
+ prop_obj.key_type = properties.get("x-af-primary-key", "auto")
343
324
 
344
325
  if prop_obj.key_type not in [
345
326
  "manual",
@@ -357,9 +338,7 @@ class SchemaObject(OpenAPIElement):
357
338
  )
358
339
 
359
340
  if prop_obj.key_type == "sequence":
360
- prop_obj.sequence_name = properties.get(
361
- "x-af-sequence-name", None
362
- )
341
+ prop_obj.sequence_name = properties.get("x-af-sequence-name", None)
363
342
  if not prop_obj.sequence_name:
364
343
  raise ApplicationException(
365
344
  500,
@@ -376,6 +355,7 @@ class SchemaObject(OpenAPIElement):
376
355
  def _get_permissions(self, schema_object: dict) -> dict:
377
356
  """Extract permissions from schema using x-af-permissions only.
378
357
 
358
+ Expects provider -> action -> role -> rule format.
379
359
  Also normalizes action names so 'create'/'update' map to 'write'.
380
360
  """
381
361
  raw_permissions = schema_object.get("x-af-permissions") or {}
@@ -387,40 +367,14 @@ class SchemaObject(OpenAPIElement):
387
367
  result[key] = value
388
368
  return result
389
369
 
390
- def is_legacy_role_form(obj: Dict[str, Any]) -> bool:
391
- # Legacy: role -> { action: rule }
392
- if not isinstance(obj, dict):
393
- return False
394
- for v in obj.values():
395
- if isinstance(v, dict):
396
- # Any dict that looks like action->rule (value not dict)
397
- for k2, v2 in v.items():
398
- if (
399
- k2
400
- in {"read", "write", "delete", "create", "update"}
401
- and not isinstance(v2, dict)
402
- ):
403
- return True
404
- return False
405
-
406
370
  normalized: Dict[str, Any] = {}
407
371
  if isinstance(raw_permissions, dict):
408
- if is_legacy_role_form(raw_permissions):
409
- # Keep legacy shape, just normalize action names
410
- for role, actions in raw_permissions.items():
411
- if isinstance(actions, dict):
412
- normalized[role] = normalize_actions(actions)
413
- else:
414
- normalized[role] = actions
415
- else:
416
- # New form: provider -> action -> role
417
- for provider, actions in raw_permissions.items():
418
- if isinstance(actions, dict):
419
- normalized[provider] = normalize_actions(actions)
420
- else:
421
- normalized[provider] = actions
422
- else:
423
- normalized = {}
372
+ # Expected format: provider -> action -> role -> rule
373
+ for provider, actions in raw_permissions.items():
374
+ if isinstance(actions, dict):
375
+ normalized[provider] = normalize_actions(actions)
376
+ else:
377
+ normalized[provider] = actions
424
378
 
425
379
  if normalized:
426
380
  validate_permissions(normalized)
@@ -429,9 +383,7 @@ class SchemaObject(OpenAPIElement):
429
383
  def to_dict(self) -> Dict[str, Any]:
430
384
  """Recursively convert this schema object and its properties."""
431
385
  data = super().to_dict()
432
- data["properties"] = {
433
- k: v.to_dict() for k, v in self.properties.items()
434
- }
386
+ data["properties"] = {k: v.to_dict() for k, v in self.properties.items()}
435
387
  data["relations"] = {k: v.to_dict() for k, v in self.relations.items()}
436
388
  if self.concurrency_property:
437
389
  data["concurrency_property"] = self.concurrency_property
@@ -477,9 +429,7 @@ class PathOperation(OpenAPIElement):
477
429
  for name, prop_schema in (
478
430
  path_operation.get("requestBody", {}).get("content", {}) or {}
479
431
  ).items():
480
- properties[name] = SchemaObjectProperty(
481
- self.entity, name, prop_schema
482
- )
432
+ properties[name] = SchemaObjectProperty(self.entity, name, prop_schema)
483
433
  elif section == "parameters":
484
434
  for param in path_operation.get("parameters", {}) or []:
485
435
  properties[param["name"]] = SchemaObjectProperty(
@@ -506,8 +456,8 @@ class PathOperation(OpenAPIElement):
506
456
  def _get_permissions(self, path_operation: dict) -> dict:
507
457
  """
508
458
  Extract permissions from a path operation using x-af-permissions
509
- only. Also normalizes action names so 'create'/'update' map to
510
- 'write'.
459
+ only. Expects provider -> action -> role -> rule format.
460
+ Also normalizes action names so 'create'/'update' map to 'write'.
511
461
  """
512
462
  raw_permissions = path_operation.get("x-af-permissions") or {}
513
463
 
@@ -518,36 +468,14 @@ class PathOperation(OpenAPIElement):
518
468
  result[key] = value
519
469
  return result
520
470
 
521
- def is_legacy_role_form(obj: Dict[str, Any]) -> bool:
522
- if not isinstance(obj, dict):
523
- return False
524
- for v in obj.values():
525
- if isinstance(v, dict):
526
- for k2, v2 in v.items():
527
- if (
528
- k2
529
- in {"read", "write", "delete", "create", "update"}
530
- and not isinstance(v2, dict)
531
- ):
532
- return True
533
- return False
534
-
535
471
  normalized: Dict[str, Any] = {}
536
472
  if isinstance(raw_permissions, dict):
537
- if is_legacy_role_form(raw_permissions):
538
- for role, actions in raw_permissions.items():
539
- if isinstance(actions, dict):
540
- normalized[role] = normalize_actions(actions)
541
- else:
542
- normalized[role] = actions
543
- else:
544
- for provider, actions in raw_permissions.items():
545
- if isinstance(actions, dict):
546
- normalized[provider] = normalize_actions(actions)
547
- else:
548
- normalized[provider] = actions
549
- else:
550
- normalized = {}
473
+ # Expected format: provider -> action -> role -> rule
474
+ for provider, actions in raw_permissions.items():
475
+ if isinstance(actions, dict):
476
+ normalized[provider] = normalize_actions(actions)
477
+ else:
478
+ normalized[provider] = actions
551
479
 
552
480
  if normalized:
553
481
  validate_permissions(normalized)
@@ -632,11 +560,9 @@ class ModelFactory:
632
560
  log.info("path_operations: %s", self.path_operations)
633
561
  return {
634
562
  "schema_objects": {
635
- name: obj.to_dict()
636
- for name, obj in self.schema_objects.items()
563
+ name: obj.to_dict() for name, obj in self.schema_objects.items()
637
564
  },
638
565
  "path_operations": {
639
- name: obj.to_dict()
640
- for name, obj in self.path_operations.items()
566
+ name: obj.to_dict() for name, obj in self.path_operations.items()
641
567
  },
642
568
  }
@@ -36,7 +36,7 @@ def validate_permissions(permissions):
36
36
  def _validate_rule(action: str, role_name: str, rule):
37
37
  if action in {"read", "write"}:
38
38
  # Accept either a string regex or an object
39
- # {fields: regex, where?: string}
39
+ # {properties: regex, where?: string}
40
40
  if isinstance(rule, str):
41
41
  try:
42
42
  re.compile(rule)
@@ -52,7 +52,8 @@ def validate_permissions(permissions):
52
52
  + str(e)
53
53
  ) from e
54
54
  elif isinstance(rule, dict):
55
- allowed_keys = {"fields", "where"}
55
+ # Support both for backward compatibility
56
+ allowed_keys = {"properties", "fields", "where"}
56
57
  unknown = set(rule.keys()) - allowed_keys
57
58
  if unknown:
58
59
  raise ValueError(
@@ -60,18 +61,19 @@ def validate_permissions(permissions):
60
61
  f"'{role_name}' action '{action}'. "
61
62
  f"Allowed keys: {allowed_keys}."
62
63
  )
63
- fields = rule.get("fields")
64
- if not isinstance(fields, str):
64
+ # Accept both 'properties' (preferred) and 'fields' (legacy)
65
+ properties = rule.get("properties") or rule.get("fields")
66
+ if not isinstance(properties, str):
65
67
  raise ValueError(
66
68
  f"Rule for role '{role_name}' action "
67
- f"'{action}' must include 'fields' as a regex "
69
+ f"'{action}' must include 'properties' as a regex "
68
70
  f"string."
69
71
  )
70
72
  try:
71
- re.compile(fields)
73
+ re.compile(properties)
72
74
  except re.error as e:
73
75
  raise ValueError(
74
- "Invalid regex pattern in 'fields' for role '"
76
+ "Invalid regex pattern in 'properties' for role '"
75
77
  + role_name
76
78
  + "' action '"
77
79
  + action
@@ -87,7 +89,7 @@ def validate_permissions(permissions):
87
89
  else:
88
90
  raise ValueError(
89
91
  f"Rule for role '{role_name}' action '{action}' "
90
- f"must be a string or object with 'fields' "
92
+ f"must be a string or object with 'properties' "
91
93
  f"(and optional 'where')."
92
94
  )
93
95
 
@@ -130,22 +132,25 @@ def validate_permissions(permissions):
130
132
  f"'read', 'write', and 'delete'."
131
133
  )
132
134
 
133
- # Detect legacy form: role -> {action: rule}
135
+ # Detect legacy form: role -> {action: rule} or role with 'where'
134
136
  def _is_legacy_form(obj: dict) -> bool:
135
137
  if not obj:
136
138
  return False
137
139
  # If any top-level value is a dict where an action maps to a non-dict
138
140
  # rule (e.g., string/bool/object rule), it's the legacy form.
141
+ # Also detect if there's a 'where' key at the role level.
139
142
  for v in obj.values():
140
143
  if isinstance(v, dict):
144
+ # Check for role-level 'where' clause (hybrid approach)
145
+ if "where" in v:
146
+ return True
141
147
  for k2, v2 in v.items():
142
- if k2 in {"read", "write", "delete"} and not isinstance(
143
- v2, dict
144
- ):
148
+ actions = {"read", "write", "delete"}
149
+ if k2 in actions and not isinstance(v2, dict):
145
150
  return True
146
151
  return False
147
152
 
148
- # Legacy path: {role: {read|write|delete: rule}}
153
+ # Legacy path: {role: {read|write|delete: rule, where?: string}}
149
154
  if _is_legacy_form(permissions):
150
155
  for role_name, actions in permissions.items():
151
156
  if not isinstance(role_name, str):
@@ -156,7 +161,15 @@ def validate_permissions(permissions):
156
161
  f"of actions."
157
162
  )
158
163
  for action, rule in actions.items():
159
- _validate_rule(action, role_name, rule)
164
+ if action == "where":
165
+ # Validate role-level WHERE clause
166
+ if not isinstance(rule, str):
167
+ raise ValueError(
168
+ f"Role-level 'where' clause for role "
169
+ f"'{role_name}' must be a string."
170
+ )
171
+ else:
172
+ _validate_rule(action, role_name, rule)
160
173
  return True
161
174
 
162
175
  # New form: {provider: {action: {role: rule}}}
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "api-foundry"
7
- version = "0.1.21"
7
+ version = "0.8.1"
8
8
  authors = [
9
9
  { name="Daniel Repik", email="danrepik@icloud.com" },
10
10
  ]
File without changes
File without changes