feat(api): procurement-api read stack + OpenAPI docs (SHOC reconciliation path) (#127)
* 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.
2026-07-23 19:32:20 -04:00
|
|
|
"""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")
|
feat(api): swap /docs from Swagger UI to Redoc (vendored offline) (#129)
Redoc 2.5.3 standalone bundle (MIT) replaces the three swagger-ui-dist
assets: one ~1.05MB JS file instead of ~1.8MB of JS+CSS+preset, and the
layout traps (StandaloneLayout/BaseLayout) go away. Redoc is read-only by
design, which matches the existing posture: try-it-out was already
disabled since data routes need SigV4 (Postman for live calls).
Unchanged: single token-gated response, offline vendoring (no CDN),
per-request server-URL injection, script-breakout guards, cached shell
with per-request spec splice. Bundle self-containment verified: the
search worker is an inlined Blob, and the only new Worker(filename) path
is Prism's async mode, which Redoc never invokes.
Verified via headless-Chrome render of the real handler output: all
routes, the OpenAPI 3.1 webhooks section, and planned-route markers
render; no placeholder leakage.
2026-07-24 12:00:22 -04:00
|
|
|
# The vendored Redoc bundle is inlined and the spec (carrying the title)
|
|
|
|
|
# with it.
|
|
|
|
|
assert "Redoc.init" in docs["body"]
|
feat(api): procurement-api read stack + OpenAPI docs (SHOC reconciliation path) (#127)
* 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.
2026-07-23 19:32:20 -04:00
|
|
|
assert "Procurement Ingest API" in docs["body"]
|
2026-07-23 20:19:47 -04:00
|
|
|
# Every placeholder must be substituted, or the page renders broken.
|
feat(api): Redocly lint gate + SHOC-themed /docs (Redoc theming, topbar, collapsible samples) (#130)
* feat(api): Add @redocly/cli as a dev dependency
Signed-off-by: Adam Moussa <adam@seahavenind.com>
* feat(api): Add Redocly configuration file with custom rules
Signed-off-by: Adam Moussa <adam@seahavenind.com>
* chore(api): Redocly lint config + bring openapi.json into compliance
redocly.yaml from the Redocly guidelines builder, with three generated
rules corrected: response-contains-property had the status codes as the
required body fields (intent was the Error schema's top-level 'error';
403 exempt since API Gateway emits AWS's {message} shape, 501 not 503);
operation-4xx-problem-details-rfc7807 off (adopting RFC 7807 would be a
runtime + SHOC-contract change, decided against); the two inert casing
rules (parameter names, schema properties) removed because both name
sets are contract-pinned (gateway resource paths, DynamoDB items).
Spec changes, no runtime impact: operationIds renamed to method-prefixed
kebab-case (get-work-orders, post-work-order-comment, ...); tags added to
all 15 operations + root tags object (groups the Redoc sidebar); examples
on all six parameters; license field; server description punctuation; two
descriptions reworded to start capitalized. Real linter catches fixed:
the two x-planned ops were missing their {workOrderId} path parameter
and any 4xx response (403 added - true today, gateway rejects unsigned).
.redocly.lint-ignore.yaml pins the six deliberate exceptions: webhook
keys are the shipped SHOC contract event names (not renameable), and the
x-planned ops answer only 501 (no 2xx to document).
package.json: npm run lint:api. Verified: lint 0 errors, 675 pytest,
headless-Chrome render of the tagged docs page.
* feat(api): SHOC design-system theme for /docs (vendored fonts)
Themes the Redoc page with the canonical SHOC token set: Montserrat 600
headings / DM Sans body / JetBrains Mono code, primary #1c75bc, navy
#262262 sidebar text + right panel, #f9fafb background, 244px sidebar.
sortRequiredPropsFirst on; 200 responses pre-expanded.
Fonts ship as lambdas/api/fonts.css (latin woff2 subsets from
@fontsource 5.3.0, embedded as data URIs, ~90KB) and inline via a new
__FONTS_CSS__ placeholder with the same </style breakout guard --
the offline single-response invariant holds, nothing fetches Google
Fonts (test-pinned). Bundling cp + bundle-consistency pin + spec-drift
asset checks extended.
Verified: headless-Chrome render (theme + fonts applied), ruff, 675
pytest, cdk synth + staged-asset check.
* feat(api): SHOC gradient topbar on /docs
64px fixed header with the SHOC shell gradient token (#1b1f52 ->
#1c4f8f -> #1c75bc), Sea Haven wordmark in Montserrat 600, page name
right-aligned in DM Sans. Redoc's scrollYOffset: 64 keeps the sticky
sidebar and anchor scrolling clear of the fixed bar. Verified via
headless-Chrome render.
* style(api): normalize /docs header and right-panel blues
The right panel's #262262 is a purple-leaning navy that clashed with
the cyan-leaning gradient, and the bar's brightest point sat directly
over the dark panel. Right panel now uses #1b1f52 (the gradient's own
dark endpoint) and the gradient runs bright-to-dark so its dark end
lands flush on the panel -- no seam, one blue family. Verified via
headless-Chrome render.
* style(api): right-panel gradient on /docs via bundle-pinned override
Redoc's theme only takes solid colors (it derives shades from
rightPanel.backgroundColor), so the gradient (#1b3d79 -> #1b3068 ->
#1b1f52, continuing the topbar blend) rides as a CSS override on the
styled-components classes of the per-section right-panel divs
(.sc-iGgWBj.sc-gsFSXq + the .sc-dExYaf stub). Those names are
deterministic for the vendored 2.5.3 bundle (verified across loads) but
change on any Redoc bump: re-derive via headless probe (find elements
whose computed background equals the rightPanel color). If they stop
matching, the panel falls back to the solid #1b1f52 theme color --
cosmetic only. Verified via headless-Chrome render.
* feat(api): collapsible samples column on /docs
Redoc CE has no built-in panel toggle, so the topbar gains a Hide/Show
samples button that flips .samples-collapsed on <html>: the right-panel
divs hide (same bundle-pinned styled-components classes as the gradient
override) and each section's content half takes the full width. Choice
persists in localStorage; aria-pressed tracks state. If the pinned
classes stop matching after a Redoc bump the toggle goes inert --
cosmetic only. Both states verified via headless-Chrome render.
* ci(api): spec-lint CI gate + npm Dependabot coverage
New spec-lint job mirrors the local npm run lint:api so openapi.json
cannot drift from redocly.yaml with green CI. Dependabot gains the npm
ecosystem (package.json is new; nothing watched @redocly/cli).
* feat(api): docs finishing touches - x-tagGroups, favicon, docs:preview
x-tagGroups sections the Redoc sidebar (Read API / Meta / SHOC Feed);
inline data-URI SVG favicon (SHOC blue) stops the browser's follow-up
/favicon.ico request 403ing at the gateway; npm run docs:preview wraps
the real-handler local render (scripts/preview_docs.py); README gains a
docs-page architecture section covering the inline pattern, theme,
pinned-selector caveat, and tooling. Lint 0 errors, 675 pytest,
headless render verified.
---------
Signed-off-by: Adam Moussa <adam@seahavenind.com>
2026-07-24 14:04:30 -04:00
|
|
|
for placeholder in (
|
|
|
|
|
mod._SPEC_PLACEHOLDER,
|
|
|
|
|
mod._JS_PLACEHOLDER,
|
|
|
|
|
mod._FONTS_PLACEHOLDER,
|
|
|
|
|
):
|
2026-07-23 20:19:47 -04:00
|
|
|
assert placeholder not in docs["body"]
|
feat(api): procurement-api read stack + OpenAPI docs (SHOC reconciliation path) (#127)
* 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.
2026-07-23 19:32:20 -04:00
|
|
|
|
|
|
|
|
spec = mod.handler(_event("GET", "/openapi.json"), None)
|
|
|
|
|
assert spec["statusCode"] == 200
|
|
|
|
|
assert json.loads(spec["body"])["openapi"] == "3.1.0"
|
|
|
|
|
|
|
|
|
|
|
2026-07-24 18:41:10 -04:00
|
|
|
def test_base_url_custom_domain_omits_stage(api):
|
|
|
|
|
mod, _ = api
|
|
|
|
|
# execute-api host keeps the /{stage} segment...
|
|
|
|
|
execapi = {
|
|
|
|
|
"requestContext": {
|
|
|
|
|
"domainName": "mvul1efda2.execute-api.us-east-1.amazonaws.com",
|
|
|
|
|
"stage": "prod",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert mod._base_url(execapi) == (
|
|
|
|
|
"https://mvul1efda2.execute-api.us-east-1.amazonaws.com/prod"
|
|
|
|
|
)
|
|
|
|
|
# ...but a custom domain (base-path mapping) serves the stage at the root,
|
|
|
|
|
# so no /{stage} is appended (it would advertise a broken server URL).
|
|
|
|
|
custom = {
|
|
|
|
|
"requestContext": {
|
|
|
|
|
"domainName": "procurement-api.seahaven.com",
|
|
|
|
|
"stage": "prod",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert mod._base_url(custom) == "https://procurement-api.seahaven.com"
|
|
|
|
|
assert mod._base_url({}) is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_spec_injects_custom_domain_server(api):
|
|
|
|
|
mod, _ = api
|
|
|
|
|
event = {
|
|
|
|
|
"requestContext": {
|
|
|
|
|
"domainName": "procurement-api.seahaven.com",
|
|
|
|
|
"stage": "prod",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
spec = json.loads(mod._spec_for_request(event))
|
|
|
|
|
urls = [s["url"] for s in spec["servers"]]
|
|
|
|
|
# Accessed via the custom domain, every server URL resolves to the custom
|
|
|
|
|
# domain (the static entry plus the injected one), and none carry a /prod
|
|
|
|
|
# suffix. Exact-equality checks (no substring-in-URL) so this reads
|
|
|
|
|
# cleanly and doesn't trip CodeQL's URL-sanitization heuristic.
|
|
|
|
|
assert set(urls) == {"https://procurement-api.seahaven.com"}
|
|
|
|
|
|
|
|
|
|
|
feat(api): procurement-api read stack + OpenAPI docs (SHOC reconciliation path) (#127)
* 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.
2026-07-23 19:32:20 -04:00
|
|
|
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"}
|