-
Notifications
You must be signed in to change notification settings - Fork 0
EventGate Lambda entrypoint OOP #108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
AquaSec has completed a full security repository scan ✅ You can find the analysis results for this PR branch on this overview.
|
WalkthroughReworks the Lambda entry to a ROUTE_MAP-driven dispatcher, adds HandlerApi to serve api.yaml, refactors HandlerTopic to load access config (S3 or local) and expose handle_request, renames token/public-key loader, and updates tests to match the new wiring and file paths. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Lambda
participant ROUTE_MAP
participant HandlerToken
participant HandlerTopic
participant S3
Client->>Lambda: invoke (HTTP-like event)
Lambda->>ROUTE_MAP: lookup handler by resource/method
ROUTE_MAP-->>Lambda: handler reference
Lambda->>HandlerToken: ensure keys loaded (with_public_keys_queried)
Lambda->>HandlerTopic: call handle_request(event)
HandlerTopic->>S3: with_load_access_config / with_load_topic_schemas (if needed)
S3-->>HandlerTopic: return access config / schemas
HandlerTopic->>HandlerToken: validate/extract token (via HandlerToken)
HandlerTopic-->>Lambda: return response dict (statusCode, headers, body)
Lambda-->>Client: return response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🤖 Fix all issues with AI agents
In `@src/event_gate_lambda.py`:
- Around line 79-87: The /terminate route in ROUTE_MAP currently maps to an
unprotected lambda that calls sys.exit("TERMINATING"); remove or guard this
endpoint: either delete the "/terminate" entry from ROUTE_MAP, or replace it
with a dedicated terminate handler that first checks an environment flag (e.g.,
ENABLE_TERMINATE or only allow in non-production) and validates a
secret/authorization token from the request before calling sys.exit; ensure the
new handler is the symbol referenced in ROUTE_MAP instead of the direct lambda
so authorization and env checks run prior to invoking sys.exit("TERMINATING").
In `@src/handlers/handler_topic.py`:
- Around line 114-125: The handler reads event["pathParameters"]["topic_name"],
event["body"], and calls json.loads without validating, which raises
KeyError/TypeError/JSONDecodeError; update the function handling this request to
validate inputs: check event is a dict, ensure pathParameters exists and
contains a non-empty topic_name (normalize with .lower()), verify httpMethod is
present and supported, and for POST ensure event["body"] is not None and is
valid JSON before calling json.loads; on any client-side issue return
build_error_response(400, "request", "<clear message>") and for unsupported
methods return 404 as before; keep references to topic_name, _get_topic_schema,
_post_topic_message, handler_token.extract_token, and build_error_response when
implementing these checks.
In `@tests/handlers/test_handler_api.py`:
- Around line 1-5: Add the standard 2026 copyright header to the top of the new
test file tests/handlers/test_handler_api.py so it includes the required project
header for new Python files created in 2026; modify the top of the file (above
the imports) to insert the exact project copyright block used across the repo
(year 2026) to satisfy header checks.
🧹 Nitpick comments (2)
tests/test_conf_validation.py (1)
62-64: Assertion is ineffective with@pytest.mark.parametrize.When the glob returns an empty list,
pytest.mark.parametrizewill simply skip the test (0 test cases generated) rather than fail it. Theassert topic_fileon line 64 will never execute if there are no files, so it cannot catch missing schema files.Consider using a separate test or fixture to validate that schema files exist:
💡 Suggested approach
def test_topic_schemas_directory_has_files(): """Ensure at least one topic schema exists.""" files = glob(os.path.join(CONF_DIR, "topic_schemas", "*.json")) assert files, "No *.json files found in topic_schemas/" `@pytest.mark.parametrize`("topic_file", glob(os.path.join(CONF_DIR, "topic_schemas", "*.json"))) def test_topic_json_schemas_basic(topic_file): schema = load_json(topic_file) # ... rest of validationsrc/handlers/handler_api.py (1)
51-63: Consider handling uninitialized state.
get_api()returns an empty body ifload_api_definition()was never called. While the current initialization flow inevent_gate_lambda.pychains these calls correctly, a defensive check could improve robustness.💡 Optional defensive check
def get_api(self) -> Dict[str, Any]: """ Return the OpenAPI specification. Returns: Dict[str, Any]: API Gateway response with OpenAPI spec. """ logger.debug("Handling GET API") + if not self.api_spec: + logger.warning("API spec not loaded; returning empty response") return { "statusCode": 200, "headers": {"Content-Type": "application/yaml"}, "body": self.api_spec, }
oto-macenauer-absa
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add some checks for the initialization and slightly change the names of the initializer methods
# Conflicts: # src/event_gate_lambda.py # src/handlers/handler_topic.py # tests/handlers/test_handler_topic.py
Overview
This pull request refactors the EventGate Lambda entrypoint and related handler classes to improve modularity, testability, and maintainability. The main changes include extracting the OpenAPI handler into its own class, refactoring the topic handler.
Release Notes
Related
Closes #105
Summary by CodeRabbit
New Features
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.