mirror of
https://github.com/Sea-Haven-Industries/procurement-ingest.git
synced 2026-09-30 18:53:14 +00:00
* docs(webhook): revise SHOC webhook contract and plan for post-migration reality Branch re-cut on main 2026-07-23 (old base carried stale PR #99 commits). Contract Rev 2026-07-23: - Producer account corrected: seahaven-prod (011934824531); mgmt frozen - Reconciliation backstop is the new procurement read API, not SyncController - wo_status "unknown" is real; SHOC must map it (checklist item added) - write_origin forward-compat note for phase-2 write-back echo suppression - SyncVendorReplies retirement flagged (dead table, no vendor_reply event) Plan updates: - Account gate: seahaven-prod only; never enable streams on mgmt tables - Emitter ships DARK (ESMs enabled=False); activation is a deliberate flip after the SHOC receiver passes shared HMAC vectors - Post-refactor conventions: common.py helpers, bundle-consistency AST pins, pytest.ini --cov additions, consolidated test roots - Dedicated-CMK rationale, secret-ARN handooff step, consumer audit refreshed (slack-bot decommissioned), enum golden test, write_origin skip-branch test * feat(webhook): SHOC WO webhook emitter — dark-ship streams, HMAC secret + rotation Implements docs/shoc-webhook-plan.md Phases 1-5 (PR-2 of the SHOC call-and-be-called effort). Everything ships DARK: both DynamoDB event source mappings deploy enabled=False; activation is a deliberate one-line follow-up PR gated on the SHOC receiver passing the shared HMAC test vectors. - Streams: NEW_AND_OLD_IMAGES on WorkOrders + WorkOrderComments (in-place update, RETAIN + logical IDs untouched; no existing consumers — verified live, neither table had a stream). - workorder-shoc-emitter (Py3.12/ARM64): stream -> envelope -> HMAC-signed POST per docs/shoc-webhook-contract.md; strict per-shard ordering (parallelization 1, bisect off, retry until 24h age, ReportBatchItemFailures); 429/5xx/timeout block the shard in order, other 4xx park to workorder-shoc-emitter-rejected; ESM failures -> workorder-shoc-emitter-failures (metadata; replay rebuilds from DynamoDB). Echo guard skips write_origin=shoc-write-api. - Secret workorder-ingest/shoc-webhook-hmac on a dedicated CMK (alias workorder-ingest-shoc-webhook-kms); cross-account GetSecretValue/DescribeSecret + kms:Decrypt granted to exactly arn:aws:iam::396287094661:role/shoc-backend-dev. RemovalPolicy DESTROY deliberately (machine-generated material; avoids the fixed-name RETAIN-orphan deadlock). - workorder-shoc-hmac-rotator: 30-day rotation, dual-key overlap, 64-hex keys, kid = UTC %Y-%m-%dT%H. - Alarms (ALARM-only -> site-alerts): emitter errors/throttles/ duration + iterator-age (>=10 min) + failures/rejected queue depth; rotator standard trio. - scripts/replay_shoc_webhooks.py: dry-run-default operator replay (rebuilds from tables, replay:true envelopes). - Tests: 742 passing, 85.56% aggregate; golden HMAC vectors shared with SHOC in docs/shoc-webhook-test-vectors.json (emitter + replay signing pinned to identical vectors); bundle-consistency AST pins for both new bundles. - README: WO stack + webhook feed section, alarm table, runbooks; removed stale seahaven-slack-bot consumer references. * fix(webhook): kms:ViaService pins, https-only delivery, cross-account principal CI pin GPT-4.1 cross-family review of the policy surface (no BLOCK): FIX applied to the cross-account shoc-backend-dev Decrypt statement and both Lambda role KMS grants (the key is only ever used via Secrets Manager); its invariant-enforcement QUESTION answered durably with tests/test_cross_account_principal_pin.py (any new foreign IAM principal in cdk/ fails CI). Scanner mediums fixed: delivery.py and the replay script now refuse non-https URLs (urllib follows file:// and http://). SQS metadata-action and dynamodb:ListStreams NITs skipped: standard CDK grant shapes; ListStreams has no resource-level scoping. The 4 gitleaks HIGHs on docs/shoc-webhook-test-vectors.json are deliberate non-secrets (shared receiver-verification vectors) suppressed machine-level with justification. * harden(webhook): resolve /sh-security-review findings (1 confirmed medium + cheap fixes) High-recall detector fan-out (injection/authz/secrets-crypto/iac-iam/logic) + proof-or-kill verifier. Gate PASSES: 1 confirmed medium, 0 confirmed critical/high. Confirmed finding fixed; several unverified-but-cheap hardenings applied since the emitter ships dark and activation is weeks out. - CONFIRMED medium (confused deputy): the rotation Lambda's generated invoke permission for secretsmanager.amazonaws.com carried no SourceAccount/SourceArn, so any account's Secrets Manager could invoke the rotator. Patched the generated CfnPermission in place (a second permission would be additive, not restrictive) to pin account + this secret ARN. - delivery + replay: refuse to follow receiver 3xx redirects (no-redirect opener) so live X-SH-* auth headers can't be forwarded to a receiver-chosen Location and an http:// Location can't slip past the https guard. Fixed the "unfollowed 3xx" comment that was factually wrong. - delivery: classify 401/403 as retryable (invalidate key cache + retry in order) instead of parking -- transient auth failures (rotation outran the TTL cache, clock skew) are availability events, not contract bugs. - envelope: build_event now genuinely total (guarded eventID / ApproximateCreationDateTime subscripts) per its own never-raise contract. - handler: catch-all so an unexpected per-record error (e.g. SQS park failure) reports only that record instead of failing the whole batch (which would re-deliver every earlier success for 24h); per-invocation emit/skip batch summary so a systemic silent drop is queryable/alarmable. - rotator: narrow the AWSCURRENT-read except to ResourceNotFound/JSONDecode (transient SM/KMS errors re-raise so the overlap key isn't silently dropped); kid uniqueness checked against ALL retained kids with a random suffix on collision (never reissue a kid for a different secret). - contract: skeleton-upsert required on ANY unknown work_order_id (not just comment-before-create) + monotonicity guard (ignore older updated_at), so a parked created or an out-of-order replay can't corrupt receiver state. Unverified/refuted findings left as-is with rationale: the two "high" logic claims (whole-batch crash triggers, ordering violation) were refuted on reachability (real stream records carry required fields; persistence writes strings only; full-state idempotent upsert absorbs the ordering gap). Signed kid/version binding (AUTHZ-002) declined: coordinated contract change, not cheap, no exploit with one algorithm/key. * fix(webhook): drop kid from rotator test_ok log (CodeQL clear-text-logging FP) GHAS CodeQL flagged py/clear-text-logging-sensitive-data (high) at _test_secret's success log because head["kid"] is subscripted from the same parsed-secret dict that holds head["secret"] — the taint tracker can't tell the non-secret key id from the secret. The secret value is never logged. Rather than dismiss the alert (fragile; re-alerts on line moves), remove the flow: kid is already logged at stage time in _create_secret and version_id correlates the steps, so the test_ok log keeps only event + version_id. Also hardens against a future edit that swaps the logged field.
230 lines
7.8 KiB
Python
230 lines
7.8 KiB
Python
"""Batch-loop tests for the SHOC webhook emitter handler (plan Phase 5).
|
|
|
|
Pins the partial-batch ordering contract: on a retryable failure at record i
|
|
the loop STOPS -- record i's DynamoDB SequenceNumber is reported via
|
|
report_batch_item_failures (so the ESM retries from it, in order) and later
|
|
records are never attempted; earlier in-batch successes are not re-delivered.
|
|
Non-retryable rejections park the full envelope on the rejected queue and the
|
|
loop CONTINUES (a contract bug must never block the shard). Skip records
|
|
(REMOVE / echo guard) produce no delivery attempts at all.
|
|
|
|
delivery.deliver is monkeypatched on the handler's own sibling instance (the
|
|
handler imports it by bare name); envelope.build_event runs for real, so
|
|
these batches exercise the true record -> envelope -> deliver path. SQS is a
|
|
plain fake on the handler's public ``sqs`` module global.
|
|
"""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from tests.support import load_lambda_module
|
|
|
|
WO_STREAM_ARN = (
|
|
"arn:aws:dynamodb:us-east-1:011934824531:table/WorkOrders"
|
|
"/stream/2026-07-23T00:00:00.000"
|
|
)
|
|
|
|
REJECTED_QUEUE_URL = (
|
|
"https://sqs.us-east-1.amazonaws.com/011934824531/workorder-shoc-emitter-rejected"
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def handler_mod():
|
|
return load_lambda_module("wo", "shoc_emitter/handler")
|
|
|
|
|
|
class _FakeSQS:
|
|
def __init__(self):
|
|
self.sent = []
|
|
|
|
def send_message(self, QueueUrl, MessageBody): # noqa: N803 (boto3 kwargs)
|
|
self.sent.append({"QueueUrl": QueueUrl, "MessageBody": MessageBody})
|
|
|
|
|
|
def _wo_record(event_id, sequence_number, event_name="INSERT", **image_overrides):
|
|
image = {
|
|
"work_order_id": {"S": f"wo-{event_id}"},
|
|
"wo_status": {"S": "new"},
|
|
"record_type": {"S": "new_work_order"},
|
|
}
|
|
image.update(image_overrides)
|
|
stream = {
|
|
"ApproximateCreationDateTime": 1784642602.0,
|
|
"SequenceNumber": sequence_number,
|
|
"StreamViewType": "NEW_AND_OLD_IMAGES",
|
|
}
|
|
if event_name != "REMOVE":
|
|
stream["NewImage"] = image
|
|
return {
|
|
"eventID": event_id,
|
|
"eventName": event_name,
|
|
"eventSource": "aws:dynamodb",
|
|
"eventSourceARN": WO_STREAM_ARN,
|
|
"dynamodb": stream,
|
|
}
|
|
|
|
|
|
def _wire(monkeypatch, handler_mod, outcomes):
|
|
"""Patch delivery.deliver with a per-delivery_id outcome table.
|
|
|
|
``outcomes`` maps delivery_id (the record eventID) to either a
|
|
("delivered"|"rejected", status) tuple or the string "retryable" (raise).
|
|
Returns the ordered list of attempted delivery_ids and the fake SQS.
|
|
"""
|
|
attempted = []
|
|
|
|
def _fake_deliver(webhook_event):
|
|
attempted.append(webhook_event["delivery_id"])
|
|
outcome = outcomes[webhook_event["delivery_id"]]
|
|
if outcome == "retryable":
|
|
raise handler_mod.delivery.RetryableDeliveryError(
|
|
"receiver returned 503", status_code=503
|
|
)
|
|
return outcome
|
|
|
|
monkeypatch.setattr(handler_mod.delivery, "deliver", _fake_deliver)
|
|
fake_sqs = _FakeSQS()
|
|
monkeypatch.setattr(handler_mod, "sqs", fake_sqs)
|
|
monkeypatch.setattr(handler_mod, "REJECTED_QUEUE_URL", REJECTED_QUEUE_URL)
|
|
return attempted, fake_sqs
|
|
|
|
|
|
def test_retryable_middle_record_stops_batch_and_reports_its_sequence(
|
|
monkeypatch, handler_mod
|
|
):
|
|
event = {
|
|
"Records": [
|
|
_wo_record("evt-1", "101"),
|
|
_wo_record("evt-2", "102"),
|
|
_wo_record("evt-3", "103"),
|
|
]
|
|
}
|
|
attempted, fake_sqs = _wire(
|
|
monkeypatch,
|
|
handler_mod,
|
|
{"evt-1": ("delivered", 200), "evt-2": "retryable"},
|
|
)
|
|
|
|
result = handler_mod.handler(event, None)
|
|
|
|
# EXACTLY the failed record's SequenceNumber: the ESM retries from it in
|
|
# order, and evt-1 (already delivered) is not re-delivered.
|
|
assert result == {"batchItemFailures": [{"itemIdentifier": "102"}]}
|
|
# The loop stopped at the failure: record 3 was never attempted.
|
|
assert attempted == ["evt-1", "evt-2"]
|
|
assert fake_sqs.sent == []
|
|
|
|
|
|
def test_rejected_record_parks_to_sqs_and_processing_continues(
|
|
monkeypatch, handler_mod
|
|
):
|
|
event = {
|
|
"Records": [
|
|
_wo_record("evt-1", "201"),
|
|
_wo_record("evt-2", "202"),
|
|
_wo_record("evt-3", "203"),
|
|
]
|
|
}
|
|
attempted, fake_sqs = _wire(
|
|
monkeypatch,
|
|
handler_mod,
|
|
{
|
|
"evt-1": ("delivered", 200),
|
|
"evt-2": ("rejected", 422),
|
|
"evt-3": ("delivered", 204),
|
|
},
|
|
)
|
|
|
|
result = handler_mod.handler(event, None)
|
|
|
|
# A 4xx rejection must NOT block the shard: no batch item failures, and
|
|
# the records after the rejection were still attempted.
|
|
assert result == {"batchItemFailures": []}
|
|
assert attempted == ["evt-1", "evt-2", "evt-3"]
|
|
|
|
# The full envelope was parked for operator replay.
|
|
assert len(fake_sqs.sent) == 1
|
|
assert fake_sqs.sent[0]["QueueUrl"] == REJECTED_QUEUE_URL
|
|
parked = json.loads(fake_sqs.sent[0]["MessageBody"])
|
|
assert set(parked) == {"envelope", "response_status"}
|
|
assert parked["response_status"] == 422
|
|
assert parked["envelope"]["delivery_id"] == "evt-2"
|
|
assert parked["envelope"]["event_type"] == "work_order.created"
|
|
assert parked["envelope"]["data"]["work_order_id"] == "wo-evt-2"
|
|
|
|
|
|
def test_skip_records_produce_no_delivery_calls(monkeypatch, handler_mod):
|
|
event = {
|
|
"Records": [
|
|
_wo_record("evt-1", "301", event_name="REMOVE"),
|
|
_wo_record("evt-2", "302", write_origin={"S": "shoc-write-api"}),
|
|
]
|
|
}
|
|
attempted, fake_sqs = _wire(monkeypatch, handler_mod, {})
|
|
|
|
result = handler_mod.handler(event, None)
|
|
|
|
assert result == {"batchItemFailures": []}
|
|
assert attempted == []
|
|
assert fake_sqs.sent == []
|
|
|
|
|
|
def test_unexpected_error_reports_record_not_whole_batch(monkeypatch, handler_mod):
|
|
# An unexpected exception (here: SQS park failure on a 4xx rejection) must
|
|
# NOT escape the loop -- that would fail the invocation and make the ESM
|
|
# re-deliver every earlier success for 24h. The offending record is
|
|
# reported so the ESM retries from it in order; evt-1 is not re-delivered.
|
|
event = {
|
|
"Records": [
|
|
_wo_record("evt-1", "401"),
|
|
_wo_record("evt-2", "402"),
|
|
_wo_record("evt-3", "403"),
|
|
]
|
|
}
|
|
attempted, fake_sqs = _wire(
|
|
monkeypatch,
|
|
handler_mod,
|
|
{
|
|
"evt-1": ("delivered", 200),
|
|
"evt-2": ("rejected", 400),
|
|
"evt-3": ("delivered", 200),
|
|
},
|
|
)
|
|
|
|
def _boom(QueueUrl, MessageBody): # noqa: N803 (boto3 kwargs)
|
|
raise RuntimeError("sqs unavailable")
|
|
|
|
monkeypatch.setattr(fake_sqs, "send_message", _boom)
|
|
|
|
result = handler_mod.handler(event, None)
|
|
|
|
assert result == {"batchItemFailures": [{"itemIdentifier": "402"}]}
|
|
# Stopped at the failing record; evt-3 not attempted.
|
|
assert attempted == ["evt-1", "evt-2"]
|
|
|
|
|
|
def test_record_missing_sequence_number_is_skipped_not_crashed(
|
|
monkeypatch, handler_mod
|
|
):
|
|
# A record that maps to an envelope but lacks SequenceNumber (unreachable
|
|
# for real streams) must be skipped, not crash the failure paths (Open SWE
|
|
# #9/#26). Craft one that build_event accepts but strip SequenceNumber.
|
|
rec = _wo_record("evt-1", "999")
|
|
del rec["dynamodb"]["SequenceNumber"]
|
|
attempted, _ = _wire(
|
|
monkeypatch, handler_mod, {"evt-1": "retryable", "evt-2": ("delivered", 200)}
|
|
)
|
|
good = _wo_record("evt-2", "1000")
|
|
result = handler_mod.handler({"Records": [rec, good]}, None)
|
|
# The seq-less record is skipped (never delivered); the good one delivers.
|
|
assert result == {"batchItemFailures": []}
|
|
assert "evt-1" not in attempted
|
|
assert "evt-2" in attempted
|
|
|
|
|
|
def test_empty_batch_returns_no_failures(monkeypatch, handler_mod):
|
|
attempted, _ = _wire(monkeypatch, handler_mod, {})
|
|
assert handler_mod.handler({"Records": []}, None) == {"batchItemFailures": []}
|
|
assert attempted == []
|