mirror of
https://github.com/Sea-Haven-Industries/procurement-ingest.git
synced 2026-09-30 07:13:13 +00:00
Some checks are pending
Deploy / deploy (push) Waiting to run
* feat(api): add procurement-api stack - read API + OpenAPI docs page Third CDK stack: API Gateway REST API (IAM SigV4) over both pipelines' tables, replacing SHOC's retired SyncController cross-account DynamoDB scan as the reconciliation/backfill path. - lambdas/api/: handler (healthcheck + docs-token gate + router dispatch), router (single route table), pagination (opaque cursor, hostile -> 400), Decimal-safe serialization, wo_repo/po_repo reads. No VendorReplies. - OpenAPI 3.1 spec as source of truth incl. top-level webhooks section documenting the outbound SHOC feed; phase-2 write endpoints x-planned (router answers 501). Self-contained /docs page, no CDN. - Auth: AWS_IAM on data routes + resource policy scoped to exactly arn:aws:iam::396287094661:role/shoc-backend-dev on GET/*; /docs and /openapi.json carve-out is token-gated in the Lambda via shared web_ui_auth (fail-closed, INFRA-74 posture). - KMS: explicit Decrypt/DescribeKey on the DynamoDB CMK from SSM (name-imported table drops the key association - INFRA-104 class). - Alarms: errors/throttles/duration(p99>=22.5s) + gateway 5xx, ALARM-only to site-alerts. No access logging in v1 (docs ?token= shim stays out of logs); cloud_watch_role=False. - Tests: handler auth-seam + routing + Decimal round-trip; moto cursor pagination incl. hostile cursors; spec<->router drift gate; bundle AST pins for the api command; pytest.ini --cov + loader siblings. - Deploy role: third stack DescribeStacks ARN + procurement-api smoke invoke ARN (re-run create-deploy-role.sh before merge). * harden(api): apply sh-security-review findings to procurement-api Fan-out (6 detectors) + review findings resolved: Correctness / DoS: - pagination: require EXACT key-set match (was subset) so a partial/foreign composite cursor can't reach DynamoDB as an inconsistent ExclusiveStartKey -> ValidationException -> 500; comments Query now pins the cursor's work_order_id to the path entity. - handler: map botocore ValidationException to 400 (defense in depth) so a crafted cursor can't drive the zero-threshold 5xx alarm. - web_ui_auth: compare tokens as bytes; a non-ASCII presented token now fails closed (401) instead of crashing hmac.compare_digest into a 500. Resolves the pre-existing xfail(strict) follow-up test; hardens the web UIs too. Docs page: - typeStr() now escapes the one spec-derived string that reached innerHTML. - spec inlined into the docs <script> block escapes "<" -> < (</script> breakout guard); /openapi.json still served byte-faithful. - Cache-Control: no-store + Referrer-Policy: no-referrer on docs responses so the ?token= URL stays out of caches/Referer. - spec-drift test asserts the committed spec carries no "</" / "<!--". IAM / IaC: - resource policy enumerates the 7 data GET resources instead of GET/* so a future GET route can't silently inherit SHOC cross-account reach. - kms:Decrypt grant gains a kms:ViaService=dynamodb condition. - stage throttling (50 rps / 100 burst) bounds the unauthenticated /docs blast radius below the 10k account default. - corrected the PATCH/POST comment (same-account callers aren't blocked by the resource policy; 501 handler + absent write grant are the gate). - documented the RETAIN log-group first-deploy rollback trap and the resource-policy-needs-redeploy gotcha in-stack. Mandatory GPT-4.1 cross-family review of the full policy surface: no BLOCK/FIX. 675 tests pass, ruff clean, cdk synth green.
271 lines
8.4 KiB
Python
271 lines
8.4 KiB
Python
"""procurement-api handler tests (offline, fake Dynamo).
|
|
|
|
Pins the auth seam both ways: docs routes fail closed on the shared token
|
|
(including the browser ?token= shim), while data routes must NOT consult the
|
|
token at all -- SigV4 authorization is API Gateway's job upstream, and a
|
|
handler-side token check there would break every legitimate SigV4 caller.
|
|
Also pins routing, 404/501/400 mapping, Decimal-safe serialization, and limit
|
|
clamping.
|
|
"""
|
|
|
|
import json
|
|
from decimal import Decimal
|
|
|
|
import pytest
|
|
|
|
from tests.support import load_lambda_module
|
|
|
|
|
|
class _FakeTable:
|
|
def __init__(self, items):
|
|
self._items = items
|
|
self.last_kwargs = None
|
|
|
|
def scan(self, **kwargs):
|
|
self.last_kwargs = kwargs
|
|
return {"Items": list(self._items)}
|
|
|
|
def query(self, **kwargs):
|
|
self.last_kwargs = kwargs
|
|
return {"Items": list(self._items)}
|
|
|
|
def get_item(self, Key): # noqa: N803 (boto3 kwarg name)
|
|
pk_attr, pk_val = next(iter(Key.items()))
|
|
for item in self._items:
|
|
if item.get(pk_attr) == pk_val:
|
|
return {"Item": item}
|
|
return {}
|
|
|
|
|
|
class _FakeDynamo:
|
|
def __init__(self, tables):
|
|
self.tables = tables
|
|
|
|
def Table(self, name): # noqa: N802 (boto3 method name)
|
|
return self.tables[name]
|
|
|
|
|
|
def _event(method, resource, path_params=None, qs=None, headers=None):
|
|
return {
|
|
"httpMethod": method,
|
|
"resource": resource,
|
|
"pathParameters": path_params or {},
|
|
"queryStringParameters": qs or {},
|
|
"headers": headers or {},
|
|
}
|
|
|
|
|
|
@pytest.fixture()
|
|
def api(monkeypatch):
|
|
mod = load_lambda_module("api", "handler")
|
|
wo_items = [
|
|
{
|
|
"work_order_id": "11144580730",
|
|
"wo_status": "assigned",
|
|
"description": "Dock door 14",
|
|
}
|
|
]
|
|
comment_items = [
|
|
{
|
|
"work_order_id": "11144580730",
|
|
"comment_id": "11144580730#2026-01-01T00:00:00#abc123def456",
|
|
"text": "Vendor dispatched",
|
|
}
|
|
]
|
|
po_items = [
|
|
{
|
|
"po_number": "2D-22030794",
|
|
"total_amount": Decimal("123.45"),
|
|
"line_items": [{"quantity": Decimal("5"), "price": Decimal("24.69")}],
|
|
}
|
|
]
|
|
site_items = [{"siteCode": "JFK8", "state": "NY", "poCount": Decimal("7")}]
|
|
|
|
tables = {
|
|
mod.wo_repo.WORK_ORDERS_TABLE: _FakeTable(wo_items),
|
|
mod.wo_repo.COMMENTS_TABLE: _FakeTable(comment_items),
|
|
mod.po_repo.PO_TABLE: _FakeTable(po_items),
|
|
mod.po_repo.VERIFIED_SITES_TABLE: _FakeTable(site_items),
|
|
}
|
|
fake = _FakeDynamo(tables)
|
|
monkeypatch.setattr(mod.wo_repo, "dynamodb", fake)
|
|
monkeypatch.setattr(mod.po_repo, "dynamodb", fake)
|
|
return mod, fake
|
|
|
|
|
|
def test_healthcheck_short_circuits(api):
|
|
mod, _ = api
|
|
assert mod.handler({"healthcheck": True}, None) == {"healthcheck": "ok"}
|
|
|
|
|
|
def test_docs_fail_closed_without_token(api):
|
|
# No WEB_UI_AUTH_TOKEN_SECRET_ARN configured -> web_ui_auth returns None
|
|
# -> the docs routes must 401, not serve the spec.
|
|
mod, _ = api
|
|
for resource in ("/docs", "/openapi.json"):
|
|
response = mod.handler(_event("GET", resource), None)
|
|
assert response["statusCode"] == 401
|
|
|
|
|
|
def test_docs_served_when_authenticated(api, monkeypatch):
|
|
mod, _ = api
|
|
monkeypatch.setattr(mod, "is_authenticated", lambda event: True)
|
|
docs = mod.handler(_event("GET", "/docs"), None)
|
|
assert docs["statusCode"] == 200
|
|
assert docs["headers"]["Content-Type"].startswith("text/html")
|
|
assert "Procurement Ingest API" in docs["body"]
|
|
assert mod._SPEC_PLACEHOLDER not in docs["body"]
|
|
|
|
spec = mod.handler(_event("GET", "/openapi.json"), None)
|
|
assert spec["statusCode"] == 200
|
|
assert json.loads(spec["body"])["openapi"] == "3.1.0"
|
|
|
|
|
|
def test_token_query_shim_synthesizes_header(api, monkeypatch):
|
|
mod, _ = api
|
|
seen = {}
|
|
|
|
def capture(event):
|
|
seen.update(event.get("headers") or {})
|
|
return False
|
|
|
|
monkeypatch.setattr(mod, "is_authenticated", capture)
|
|
original = _event("GET", "/docs", qs={"token": "sekrit"})
|
|
mod.handler(original, None)
|
|
assert seen.get("x-auth-token") == "sekrit"
|
|
# The shim must not mutate the caller's event in place.
|
|
assert "x-auth-token" not in original["headers"]
|
|
|
|
|
|
def test_non_ascii_token_fails_closed_not_500(api):
|
|
# A non-ASCII presented token must 401 (fail closed), not crash
|
|
# hmac.compare_digest into a 500 that pages the 5xx alarm. Reach the
|
|
# web_ui_auth module namespace through the imported is_authenticated's
|
|
# globals and seed a valid cached token.
|
|
mod, _ = api
|
|
auth_globals = mod.is_authenticated.__globals__
|
|
auth_globals["_auth_token_cache"] = "correct-token"
|
|
auth_globals["_auth_token_cached_at"] = float("inf")
|
|
try:
|
|
response = mod.handler(
|
|
_event("GET", "/docs", headers={"x-auth-token": "é"}), None
|
|
)
|
|
assert response["statusCode"] == 401
|
|
finally:
|
|
auth_globals["_auth_token_cache"] = None
|
|
auth_globals["_auth_token_cached_at"] = 0.0
|
|
|
|
|
|
def test_data_routes_never_consult_docs_token(api, monkeypatch):
|
|
mod, _ = api
|
|
|
|
def explode(event):
|
|
raise AssertionError("data routes must not consult the docs token")
|
|
|
|
monkeypatch.setattr(mod, "is_authenticated", explode)
|
|
response = mod.handler(_event("GET", "/work-orders"), None)
|
|
assert response["statusCode"] == 200
|
|
|
|
|
|
def test_list_and_get_routing(api):
|
|
mod, _ = api
|
|
listing = mod.handler(_event("GET", "/work-orders"), None)
|
|
body = json.loads(listing["body"])
|
|
assert body["items"][0]["work_order_id"] == "11144580730"
|
|
assert body["next_cursor"] is None
|
|
|
|
hit = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/work-orders/{workOrderId}",
|
|
path_params={"workOrderId": "11144580730"},
|
|
),
|
|
None,
|
|
)
|
|
assert json.loads(hit["body"])["wo_status"] == "assigned"
|
|
|
|
miss = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/work-orders/{workOrderId}",
|
|
path_params={"workOrderId": "999"},
|
|
),
|
|
None,
|
|
)
|
|
assert miss["statusCode"] == 404
|
|
|
|
comments = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/work-orders/{workOrderId}/comments",
|
|
path_params={"workOrderId": "11144580730"},
|
|
),
|
|
None,
|
|
)
|
|
assert json.loads(comments["body"])["items"][0]["text"] == "Vendor dispatched"
|
|
|
|
sites = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/verified-sites/{siteCode}",
|
|
path_params={"siteCode": "JFK8"},
|
|
),
|
|
None,
|
|
)
|
|
assert json.loads(sites["body"])["state"] == "NY"
|
|
|
|
|
|
def test_decimal_serialization_round_trips(api):
|
|
mod, _ = api
|
|
response = mod.handler(
|
|
_event(
|
|
"GET",
|
|
"/purchase-orders/{poNumber}",
|
|
path_params={"poNumber": "2D-22030794"},
|
|
),
|
|
None,
|
|
)
|
|
body = json.loads(response["body"])
|
|
assert body["total_amount"] == 123.45
|
|
assert body["line_items"][0]["quantity"] == 5
|
|
assert isinstance(body["line_items"][0]["quantity"], int)
|
|
assert "Decimal" not in response["body"]
|
|
|
|
|
|
def test_limit_clamped_to_bounds(api):
|
|
mod, fake = api
|
|
mod.handler(_event("GET", "/work-orders", qs={"limit": "9999"}), None)
|
|
assert fake.tables[mod.wo_repo.WORK_ORDERS_TABLE].last_kwargs["Limit"] == 500
|
|
mod.handler(_event("GET", "/work-orders", qs={"limit": "0"}), None)
|
|
assert fake.tables[mod.wo_repo.WORK_ORDERS_TABLE].last_kwargs["Limit"] == 1
|
|
|
|
|
|
def test_non_integer_limit_is_400(api):
|
|
mod, _ = api
|
|
response = mod.handler(_event("GET", "/work-orders", qs={"limit": "abc"}), None)
|
|
assert response["statusCode"] == 400
|
|
|
|
|
|
def test_planned_routes_return_501(api):
|
|
mod, _ = api
|
|
for method, resource in mod.PLANNED_ROUTES:
|
|
response = mod.handler(_event(method, resource), None)
|
|
assert response["statusCode"] == 501, (method, resource)
|
|
|
|
|
|
def test_unknown_route_is_404(api):
|
|
mod, _ = api
|
|
response = mod.handler(_event("GET", "/nope"), None)
|
|
assert response["statusCode"] == 404
|
|
|
|
|
|
def test_unexpected_error_maps_to_clean_500(api, monkeypatch):
|
|
mod, fake = api
|
|
|
|
def boom(**kwargs):
|
|
raise RuntimeError("dynamo fell over")
|
|
|
|
monkeypatch.setattr(fake.tables[mod.wo_repo.WORK_ORDERS_TABLE], "scan", boom)
|
|
response = mod.handler(_event("GET", "/work-orders"), None)
|
|
assert response["statusCode"] == 500
|
|
assert json.loads(response["body"]) == {"error": "internal error"}
|