api-foundry-query-engine 0.8.83__tar.gz → 0.8.85__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.
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/PKG-INFO +1 -1
- api_foundry_query_engine-0.8.85/api_foundry_query_engine/__init__.py +1 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/connectors/postgres_connection.py +13 -1
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/lambda_handler.py +11 -3
- api_foundry_query_engine-0.8.85/api_foundry_query_engine/services/transactional_service.py +81 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/logger.py +29 -12
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/token_decoder.py +23 -1
- api_foundry_query_engine-0.8.83/api_foundry_query_engine/__init__.py +0 -1
- api_foundry_query_engine-0.8.83/api_foundry_query_engine/services/transactional_service.py +0 -52
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/.gitignore +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/LICENSE +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/.pre-commit-config.yaml +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/adapters/adapter.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/adapters/gateway_adapter.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/connectors/connection.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/connectors/connection_factory.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/connectors/data_api_connection.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/batch_operation_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/dao.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/operation_dao.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_custom_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_delete_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_insert_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_restore_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_select_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_subselect_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/dao/sql_update_query_handler.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/operation.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/services/service.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/api_model.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/app_exception.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/claims_check.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/dependency_resolver.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/api_foundry_query_engine/utils/reference_resolver.py +0 -0
- {api_foundry_query_engine-0.8.83 → api_foundry_query_engine-0.8.85}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: api-foundry-query-engine
|
|
3
|
-
Version: 0.8.
|
|
3
|
+
Version: 0.8.85
|
|
4
4
|
Summary: The AWS lambda service handler use by 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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.8.85"
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
1
3
|
from api_foundry_query_engine.connectors.connection import (
|
|
2
4
|
Connection,
|
|
3
5
|
Cursor,
|
|
@@ -8,6 +10,16 @@ from api_foundry_query_engine.utils.logger import logger
|
|
|
8
10
|
# Initialize the logger
|
|
9
11
|
log = logger(__name__)
|
|
10
12
|
|
|
13
|
+
# postgresql://user:secret@host/db and "... password=secret ..."
|
|
14
|
+
_URI_PASSWORD = re.compile(r"(://[^:/@\s]*:)[^@\s]*(@)")
|
|
15
|
+
_KEYWORD_PASSWORD = re.compile(r"(\bpassword\s*=\s*)('(?:[^'\\]|\\.)*'|\S+)", re.IGNORECASE)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def redact_dsn(dsn: str) -> str:
|
|
19
|
+
"""The DSN with its password replaced, so it can be logged."""
|
|
20
|
+
dsn = _URI_PASSWORD.sub(r"\1***\2", dsn)
|
|
21
|
+
return _KEYWORD_PASSWORD.sub(r"\1***", dsn)
|
|
22
|
+
|
|
11
23
|
|
|
12
24
|
class PostgresCursor(Cursor):
|
|
13
25
|
def __init__(self, cursor):
|
|
@@ -121,7 +133,7 @@ class PostgresConnection(Connection):
|
|
|
121
133
|
|
|
122
134
|
# If DSN is provided, use it directly (simplifies fixture_foundry integration)
|
|
123
135
|
if "dsn" in self.db_config:
|
|
124
|
-
log.info("Connecting using DSN: %s", self.db_config["dsn"])
|
|
136
|
+
log.info("Connecting using DSN: %s", redact_dsn(self.db_config["dsn"]))
|
|
125
137
|
return connect(self.db_config["dsn"])
|
|
126
138
|
|
|
127
139
|
# Otherwise, build connection from individual parameters
|
|
@@ -80,13 +80,21 @@ class QueryEngine:
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
|
|
83
|
+
def load_engine_config():
|
|
84
|
+
log.info("Loading engine config from environment variables")
|
|
85
|
+
config = os.environ
|
|
86
|
+
# Names only: the Lambda environment holds the execution role's
|
|
87
|
+
# credentials (AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN) and often
|
|
88
|
+
# database DSNs with passwords.
|
|
89
|
+
log.debug("engine_config keys: %s", sorted(config))
|
|
90
|
+
return config
|
|
91
|
+
|
|
92
|
+
|
|
83
93
|
@token_decoder()
|
|
84
94
|
@claims_check(validate_path_scope=False, validate_scope_format=False)
|
|
85
95
|
def handler(event, _):
|
|
86
96
|
if not hasattr(handler, "engine_config"):
|
|
87
|
-
|
|
88
|
-
handler.engine_config = os.environ
|
|
89
|
-
log.info("engine_config: %s", handler.engine_config)
|
|
97
|
+
handler.engine_config = load_engine_config()
|
|
90
98
|
|
|
91
99
|
if not hasattr(handler, "query_engine"):
|
|
92
100
|
set_api_model(handler.engine_config)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import traceback
|
|
2
|
+
from typing import Mapping, Optional
|
|
3
|
+
|
|
4
|
+
from api_foundry_query_engine.utils.logger import logger
|
|
5
|
+
from api_foundry_query_engine.utils.app_exception import ApplicationException
|
|
6
|
+
from api_foundry_query_engine.operation import Operation
|
|
7
|
+
from api_foundry_query_engine.services.service import ServiceAdapter
|
|
8
|
+
from api_foundry_query_engine.connectors.connection_factory import ConnectionFactory
|
|
9
|
+
from api_foundry_query_engine.dao.operation_dao import OperationDAO
|
|
10
|
+
from api_foundry_query_engine.utils.api_model import (
|
|
11
|
+
get_path_operation,
|
|
12
|
+
get_schema_object,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
log = logger(__name__)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class TransactionalService(ServiceAdapter):
|
|
19
|
+
def __init__(self, config: Mapping[str, str]):
|
|
20
|
+
super().__init__()
|
|
21
|
+
self.config = config
|
|
22
|
+
self.connection_factory = ConnectionFactory(config)
|
|
23
|
+
|
|
24
|
+
def execute(self, operation: Operation) -> list[dict]:
|
|
25
|
+
if operation.entity == "batch" and operation.action == "create":
|
|
26
|
+
database = self._batch_database(operation)
|
|
27
|
+
else:
|
|
28
|
+
database = self._database(operation.entity, operation.action)
|
|
29
|
+
if database is None:
|
|
30
|
+
raise ApplicationException(500, f"Unknown operation: {operation.entity}")
|
|
31
|
+
|
|
32
|
+
# Pass config to connection_factory if needed (future extension)
|
|
33
|
+
connection = self.connection_factory.get_connection(database)
|
|
34
|
+
|
|
35
|
+
try:
|
|
36
|
+
result = OperationDAO(operation, connection.engine()).execute(connection)
|
|
37
|
+
if operation.action != "read":
|
|
38
|
+
connection.commit()
|
|
39
|
+
if isinstance(result, dict):
|
|
40
|
+
return [result]
|
|
41
|
+
return result
|
|
42
|
+
except Exception as error:
|
|
43
|
+
log.error("transaction exception: %s", error)
|
|
44
|
+
log.error("traceback: %s", traceback.format_exc())
|
|
45
|
+
raise error
|
|
46
|
+
finally:
|
|
47
|
+
connection.close()
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def _database(entity, action) -> Optional[str]:
|
|
51
|
+
path_operation = get_path_operation(entity, action)
|
|
52
|
+
if path_operation:
|
|
53
|
+
return path_operation.database
|
|
54
|
+
schema_object = get_schema_object(entity)
|
|
55
|
+
if schema_object:
|
|
56
|
+
return schema_object.database
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
def _batch_database(self, operation: Operation) -> str:
|
|
60
|
+
"""The database a batch runs against.
|
|
61
|
+
|
|
62
|
+
"batch" is not an entity in the API model, so the database comes
|
|
63
|
+
from the sub-operations. They share one connection, so they must all
|
|
64
|
+
resolve to the same database. A sub-operation naming an unknown
|
|
65
|
+
entity doesn't pick the database; it fails on its own inside the
|
|
66
|
+
batch, where continueOnError decides what happens next.
|
|
67
|
+
"""
|
|
68
|
+
operations = (operation.store_params or {}).get("operations")
|
|
69
|
+
if not isinstance(operations, list) or not operations:
|
|
70
|
+
raise ApplicationException(400, "Batch request must contain at least one operation")
|
|
71
|
+
|
|
72
|
+
databases = {self._database(op.get("entity"), op.get("action")) for op in operations if isinstance(op, dict)}
|
|
73
|
+
databases.discard(None)
|
|
74
|
+
if not databases:
|
|
75
|
+
raise ApplicationException(500, "Unknown operation: no batch operation names a known entity")
|
|
76
|
+
if len(databases) > 1:
|
|
77
|
+
raise ApplicationException(
|
|
78
|
+
400,
|
|
79
|
+
"Batch operations must all use the same database; got " + ", ".join(sorted(databases)),
|
|
80
|
+
)
|
|
81
|
+
return databases.pop()
|
|
@@ -1,14 +1,36 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
import os
|
|
3
3
|
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
4
|
+
# The root log level comes from LOGGING_LEVEL, defaulting to INFO. force=True
|
|
5
|
+
# so the configuration applies even when the Lambda runtime has already
|
|
6
|
+
# installed a handler.
|
|
7
|
+
DEFAULT_LOGGING_LEVEL = "INFO"
|
|
8
|
+
|
|
9
|
+
# At DEBUG, botocore logs every request it signs: the headers, including the
|
|
10
|
+
# caller's x-amz-security-token, and the body -- which for the RDS Data API
|
|
11
|
+
# carries every SQL parameter. These loggers are therefore held at WARNING
|
|
12
|
+
# whatever LOGGING_LEVEL is, unless AWS_SDK_LOGGING_LEVEL explicitly says
|
|
13
|
+
# otherwise.
|
|
14
|
+
AWS_SDK_LOGGERS = ("boto3", "botocore", "s3transfer", "urllib3")
|
|
15
|
+
DEFAULT_AWS_SDK_LOGGING_LEVEL = "WARNING"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _logging_level() -> str:
|
|
19
|
+
return os.getenv("LOGGING_LEVEL", DEFAULT_LOGGING_LEVEL).upper()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _quiet_aws_sdk_loggers() -> None:
|
|
23
|
+
level = os.getenv("AWS_SDK_LOGGING_LEVEL", DEFAULT_AWS_SDK_LOGGING_LEVEL).upper()
|
|
24
|
+
for name in AWS_SDK_LOGGERS:
|
|
25
|
+
logging.getLogger(name).setLevel(level)
|
|
26
|
+
|
|
27
|
+
|
|
7
28
|
logging.basicConfig(
|
|
8
29
|
format="%(name)s:%(lineno)s - %(levelname)s - %(message)s",
|
|
9
|
-
level=
|
|
30
|
+
level=_logging_level(),
|
|
10
31
|
force=True,
|
|
11
32
|
)
|
|
33
|
+
_quiet_aws_sdk_loggers()
|
|
12
34
|
|
|
13
35
|
WARN = logging.WARN
|
|
14
36
|
INFO = logging.INFO
|
|
@@ -26,14 +48,9 @@ def logger(name=None):
|
|
|
26
48
|
logging.Logger: Logger object with the specified name or the root logger.
|
|
27
49
|
|
|
28
50
|
"""
|
|
29
|
-
#
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
# Setting the logging level for the root logger to the obtained logging level
|
|
34
|
-
logging.getLogger().setLevel(loggingLevel)
|
|
35
|
-
|
|
36
|
-
# Returning a logger object with the specified name or the root logger
|
|
51
|
+
# Re-applied on every call: LOGGING_LEVEL may have changed since import.
|
|
52
|
+
logging.getLogger().setLevel(_logging_level())
|
|
53
|
+
_quiet_aws_sdk_loggers()
|
|
37
54
|
return logging.getLogger(name)
|
|
38
55
|
|
|
39
56
|
|
|
@@ -43,6 +43,27 @@ def _log_jwt_configuration():
|
|
|
43
43
|
log.debug("===============================")
|
|
44
44
|
|
|
45
45
|
|
|
46
|
+
# Headers never written to the log, even at DEBUG: bearer tokens, session
|
|
47
|
+
# cookies and API keys are credentials, not diagnostics.
|
|
48
|
+
_REDACTED_HEADERS = frozenset({"authorization", "cookie", "x-api-key"})
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _redacted_event(event: Dict[str, Any]) -> Dict[str, Any]:
|
|
52
|
+
"""A copy of `event` safe to log: credential headers masked in both
|
|
53
|
+
`headers` and `multiValueHeaders`, matched case-insensitively."""
|
|
54
|
+
if not isinstance(event, dict):
|
|
55
|
+
return event
|
|
56
|
+
redacted = dict(event)
|
|
57
|
+
for key in ("headers", "multiValueHeaders"):
|
|
58
|
+
headers = event.get(key)
|
|
59
|
+
if isinstance(headers, dict):
|
|
60
|
+
redacted[key] = {
|
|
61
|
+
name: ("[REDACTED]" if str(name).lower() in _REDACTED_HEADERS else value)
|
|
62
|
+
for name, value in headers.items()
|
|
63
|
+
}
|
|
64
|
+
return redacted
|
|
65
|
+
|
|
66
|
+
|
|
46
67
|
class LambdaTokenValidator:
|
|
47
68
|
"""Validates tokens by invoking an AWS Lambda TOKEN authorizer."""
|
|
48
69
|
|
|
@@ -265,7 +286,8 @@ def token_decoder(
|
|
|
265
286
|
if log.isEnabledFor(logging.DEBUG):
|
|
266
287
|
_log_jwt_configuration()
|
|
267
288
|
|
|
268
|
-
log.
|
|
289
|
+
if log.isEnabledFor(logging.DEBUG):
|
|
290
|
+
log.debug("Event structure: %s", json.dumps(_redacted_event(event), default=str))
|
|
269
291
|
|
|
270
292
|
# Check if authorizer already exists (gateway validated)
|
|
271
293
|
if event.get("requestContext", {}).get("authorizer"):
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.8.83"
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import traceback
|
|
2
|
-
from typing import Mapping
|
|
3
|
-
|
|
4
|
-
from api_foundry_query_engine.utils.logger import logger
|
|
5
|
-
from api_foundry_query_engine.utils.app_exception import ApplicationException
|
|
6
|
-
from api_foundry_query_engine.operation import Operation
|
|
7
|
-
from api_foundry_query_engine.services.service import ServiceAdapter
|
|
8
|
-
from api_foundry_query_engine.connectors.connection_factory import ConnectionFactory
|
|
9
|
-
from api_foundry_query_engine.dao.operation_dao import OperationDAO
|
|
10
|
-
from api_foundry_query_engine.utils.api_model import (
|
|
11
|
-
get_path_operation,
|
|
12
|
-
get_schema_object,
|
|
13
|
-
)
|
|
14
|
-
|
|
15
|
-
log = logger(__name__)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
class TransactionalService(ServiceAdapter):
|
|
19
|
-
def __init__(self, config: Mapping[str, str]):
|
|
20
|
-
super().__init__()
|
|
21
|
-
self.config = config
|
|
22
|
-
self.connection_factory = ConnectionFactory(config)
|
|
23
|
-
|
|
24
|
-
def execute(self, operation: Operation) -> list[dict]:
|
|
25
|
-
path_operation = get_path_operation(operation.entity, operation.action)
|
|
26
|
-
if path_operation:
|
|
27
|
-
database = path_operation.database
|
|
28
|
-
else:
|
|
29
|
-
schema_object = get_schema_object(operation.entity)
|
|
30
|
-
if schema_object:
|
|
31
|
-
database = schema_object.database
|
|
32
|
-
else:
|
|
33
|
-
raise ApplicationException(
|
|
34
|
-
500, f"Unknown operation: {operation.entity}"
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
# Pass config to connection_factory if needed (future extension)
|
|
38
|
-
connection = self.connection_factory.get_connection(database)
|
|
39
|
-
|
|
40
|
-
try:
|
|
41
|
-
result = OperationDAO(operation, connection.engine()).execute(connection)
|
|
42
|
-
if operation.action != "read":
|
|
43
|
-
connection.commit()
|
|
44
|
-
if isinstance(result, dict):
|
|
45
|
-
return [result]
|
|
46
|
-
return result
|
|
47
|
-
except Exception as error:
|
|
48
|
-
log.error("transaction exception: %s", error)
|
|
49
|
-
log.error("traceback: %s", traceback.format_exc())
|
|
50
|
-
raise error
|
|
51
|
-
finally:
|
|
52
|
-
connection.close()
|
|
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
|