Files
AiSOC/scripts/check_cloud_native_collection.py
T
Beenu Arora fb79501a73 fix(connectors): acknowledge a queue only after ingest confirms the batch (#1526)
* test(connectors): pin queue acknowledgement and checkpoint semantics

Four regression tests that fail on the current tree, covering phase 3's
two findings on the queue-backed connectors.

Finding 4 — acknowledgement precedes durable acceptance. Both connectors
answer the source inside `fetch_alerts`: `gcp_pubsub` acks once the batch
is in a local list, `aws_cloudtrail_s3` deletes the SQS receipt once an
object's records are in the same list. The scheduler pushes to ingest
only after `fetch_alerts` has returned, so a crash or a 5xx in between
loses messages that are no longer redeliverable. The tests assert the
property that matters rather than the call: after a fetch, the source
must still be holding the messages.

Finding 5 — `apply_checkpoint` is a monotonic `(event_time, record_id)`
high-water mark, which is correct for an ordered API poll and wrong for
a queue. Neither source is ordered, so an event older than the last
batch's newest is a first arrival, and it is dropped on sight with no
counter and no log. The tests drive an older object and an older entry
through a second poll and require them to survive, alongside the other
half: a genuine redelivery must still be suppressed.

Pre-fix output:

  assert ['h1'] == []
    Left contains one more item: 'h1'
  AssertionError: but nothing downstream has accepted them yet, so the
  receipt must survive the fetch

  assert [] == ['older-1']
  AssertionError: an out-of-order arrival on an unordered queue is a new
  event, not a duplicate

  assert not True ... :acknowledge>.called
  AssertionError: but nothing durable has accepted them, so the
  subscription must still hold them

The remaining tests in this commit describe the contract the fix
introduces and fail on a missing attribute until it lands.

* fix(connectors): acknowledge a queue only after ingest confirms it

Finding 4. Both queue connectors answered the source inside their own
`fetch_alerts`, once the rows were in a local list. The scheduler pushes
to ingest after that function returns, so a crash or a 5xx in between
took messages the source had already been told to forget, and an acked
Pub/Sub message or a deleted SQS receipt cannot be replayed from
anywhere. Withholding the checkpoint could not help: a cursor over data
that is already gone from the queue protects nothing.

Fetching and acknowledging are now separate. The fetch stages receipt
handles and ack ids; the scheduler commits them, because it is the only
layer that sees ingest's answer. Acceptance means broker-confirmed Kafka
delivery -- `services/ingest` awaits PublishBatch with RequiredAcks:
RequireAll and answers 500 on failure -- under exactly the condition the
checkpoint already used: counts present, rejected == 0, accepted ==
submitted. Anything else discards the handles and logs that it did, so
the messages redeliver.

The commit runs after the checkpoint write, deliberately. A crash
between the two leaves messages that will be redelivered and a cursor
that recognises them; the other order leaves a source that has forgotten
them and a cursor that never recorded them.

The bound. A visibility timeout and an ack deadline are both finite, so
a held handle expires. A poll stops taking new work once it has spent
half the deadline, leaving the rest for ingest and the checkpoint write;
overrunning costs a duplicate, because an expired handle is refused and
the message redelivers. Never a gap. Chosen over extending the deadline
because an extension is another call that can fail, on the path whose
failure mode this change exists to remove. The connectors also report
what the source actually confirmed rather than what they asked for, so a
delete nobody is permitted to make is visible instead of presenting as a
queue that never drains.

Finding 5. `apply_checkpoint` is a monotonic (event_time, record_id)
high-water mark. That is right for an ordered, time-windowed API poll --
its design comment says so and the five API connectors keep it -- and
wrong for a queue. Pub/Sub orders nothing without an ordering key and a
Cloud Logging sink sets none; S3 notifications over SQS are unordered by
construction. An event older than the batch maximum was dropped on first
sight, with no counter and no log, after its message had been acked.

Those two connectors now declare `dedup_identity_field` and dedup on a
bounded set of seen identities instead: the Pub/Sub messageId, which is
stable across redelivery where ackId is not, and the S3 object key, which
every record of an object shares and which keeps the set to the few
hundred keys a poll can announce. Identities are stored as truncated
digests scoped to the connector; tenant scope is structural, since the
cursor lives on one instance row. Falling off the end of the bound costs
a duplicate, the opposite of the watermark it replaces. Declaring both
schemes is refused at import.

`azure_event_hubs` keeps its watermark and is not converted. Its
container is never drained and is re-listed from the beginning every
poll, so a bounded identity set would roll over and re-emit the oldest
capture files for ever. It shares the out-of-order weakness across
partitions, but its source is re-readable rather than destroyed, so it
needs a different fix; recorded in the phase document.

Also: `check_cloud_native_collection` accepts either cursor shape as
"it resumes" and its self-test disables both, so it still goes red when
a connector declares neither. Repeated redeliveries of one notification
are called out with the receive count and the redrive policy that
quarantines them, which is the queue's own durable dead-letter rather
than a spool built here. Connector docs regenerated: the timeout fields
now have to cover a poll and the push that follows it, which is a larger
number than before.

* fix(connectors): persist a cursor without destroying the config

A live suite for the phase 3 guarantee, and a defect it found on its
first run that is older than phase 3 and worse than either finding.

`record_checkpoint` bound its patch as `cast(json.dumps(...), JSONB)`.
`cast` declares the bind's type, so SQLAlchemy's JSONB processor
serialised the already-serialised string a second time and Postgres
received a JSON string scalar. `jsonb || <scalar>` coerces both operands
to arrays, so one successful checkpointed poll rewrote connector_config
from

  {"poll_interval_seconds": 120, "filter_rules": [...]}

to

  [{"poll_interval_seconds": 120, ...}, "{\"checkpoint\": {...}}"]

and the next poll raised `AttributeError: 'list' object has no attribute
'items'` reading it. The poll's broad handler filed that as a failure,
so the instance went unhealthy and stayed there, with its poll interval
and filter rules no longer readable and its cursor never once loadable.
Measured against real Postgres: type `array`, next poll crashes, both
config keys lost. It affects every connector that declares a cursor, not
only the two this phase changed, and it means no connector has ever
resumed from a persisted checkpoint on any deployment.

Nothing caught it because every scheduler test patches
`record_checkpoint` with a fake that appends the dict it was handed, so
the statement had never been executed by a test -- the test double was
more capable than the real thing, exactly where the defect lived. The
contract suite covers the in-memory machinery and cannot reach the SQL.

The patch is now bound as a mapping with a JSONB type, serialised once,
and the coalesce default is a literal rather than a second cast carrying
the same bug. Rows already corrupted are recovered on read: the config
is the mapping element and the cursor is inside the encoded string, so
an upgraded deployment resumes without an operator running SQL and
without re-reading its overlap. The repair SQL is in the phase document
for anyone who wants the column itself cleaned.

tests/isolation/test_queue_acknowledgement_live.py drives the real
scheduler, the real ingest client and the real Pub/Sub connector with a
genuinely signed assertion against a real socket and real Postgres. Two
properties live only here: that nothing reaches `:acknowledge` while
ingest has not confirmed the batch -- a guarantee about a request that
must not be made, which a patched method cannot witness -- and that the
cursor survives the JSONB round trip, which is what the fake engine
could never show. It is wired into connector-scheduler-live.yml, which
carries no paths filter, with a negative control that re-injects the
pre-fix acknowledgement placement and requires the suite to fail.

Verified against Postgres 16: 11 passed. Negative controls, each run:
re-injecting the ack placement fails 3 of 11; re-injecting the
double-encode fails 7 of 11.

* docs(remediation): phase 3 queue acknowledgement and checkpoints

Per finding: status, the production path, the regression tests, the fix
and its compatibility implications, the verification actually performed
with its commands and results, and the limitations.

Records the bound chosen for holding a receipt handle and why stopping
the poll early was preferred to extending the deadline; the queue's own
redrive policy as the durable quarantine for a notification naming an
object that can never be read; and the reasoning for leaving
azure_event_hubs on its watermark, so the next reader does not convert it
by symmetry and break a container that is re-listed every poll.

Bounded recovery procedures for source data already missed, split the
way the question actually divides: a CloudTrail notification destroys
only the announcement, so the objects are still in the bucket and can be
re-announced, bounded by the bucket's lifecycle policy; an acknowledged
Pub/Sub message is gone unless retainAckedMessages was enabled, which is
off by default, so the recovery there is to re-read Cloud Logging. The
repair SQL for a connector_config corrupted by finding 6 was executed in
a transaction against Postgres 16 and rolled back, with its measured
before/after recorded -- not written out untested.

Eight limitations stated rather than implied, including the two that
matter most: no vendor was contacted, so expired-handle behaviour comes
from documentation rather than observation, and the Go ingest service
was not run, so no broker acknowledgement was observed end to end.

* fix(connectors): clear three CI findings this branch introduced

All three were real and all three were mine.

`generate_connector_catalog_fallback --check`: the bundled catalogue the
API falls back to is generated from each connector's `schema()`, and this
branch rewrote two help texts -- the Pub/Sub ack deadline and the SQS
visibility timeout now have to cover a poll *and* the push that follows
it. Regenerated; the diff is exactly those two strings.

`Secret scan (gitleaks)`: the live suite's token endpoint answered
`"access_token": "ya29.live-test"`, and `ya29.` is the prefix a real
Google token carries, so `generic-api-key` fired. The scanner is right
to flag that shape in a tracked file whatever the value is, so the stub
now answers `stub-token` -- fixed at the source rather than allowlisted.
Verified with the pinned gitleaks 8.21.2 under `dir .`, which is the
mode CI runs: no leaks found.

`Zero open CodeQL alerts`: alert #1067, `py/unused-local-variable`, on
the class defined inside `pytest.raises` to prove that declaring both
cursor schemes is refused. Nothing used the name, because defining the
class *is* the call under test. Built with `type()` instead, so there is
no name to leave unbound. Ruff does not report this shape, which is why
it reached CI.

The restructured test was re-proven: with the `__init_subclass__` guard
removed it fails, and passes again once restored -- `type()` still
triggers the hook, which is the thing that would otherwise have made
this a test that passes for the wrong reason.

* docs(remediation): keep the recovery query out of the profile gate

`check_connector_profiles` scans documentation for a `connector_type`
field shown next to a value and requires each one to reach a
normalization path, so an example payload in the docs cannot describe a
connector whose events would fall through to the generic fallback. Its
pattern is `connector_type` followed by `:` or `=` and a quoted value,
which the recovery section's `WHERE connector_type = 'aws_cloudtrail_s3'`
matches exactly -- a database query read as a documented ingest payload.

The connector itself resolves correctly: it emits the canonical
`raw_event` envelope, which is how 79 of the 87 reach a profile, and the
gate's only finding was about the document.

Rewritten as an `IN` predicate, which does not match the pattern and is
better documentation anyway -- both queue connectors were affected by
finding 5, so an operator establishing their recovery window wants both
rows rather than one. The gate and its 16-case self-test both pass.

Every gate the lint job runs was then executed locally -- 143
invocations -- rather than discovering them one CI round at a time.
2026-10-27 10:08:02 -07:00

447 lines
19 KiB
Python

#!/usr/bin/env python3
"""Cloud estates do not hand you their logs over a REST list call.
Depth plan 4.1. The three hyperscalers each publish their control-plane and
identity telemetry onto a *queue or object store* once an estate is bigger
than a toy account, and the API-polling shape every connector in this tree
used before now cannot read any of them:
* **AWS** — an organisation trail writes gzipped JSON objects to S3 and
notifies an SQS queue. ``cloudtrail:LookupEvents``, which
``aws_cloudtrail.py`` uses, is capped at two transactions per second per
region per account, returns management events only, and is documented by
AWS as unsuitable for continuous delivery. Data events and VPC flow logs
never appear in it at all.
* **GCP** — a log sink publishes ``LogEntry`` messages to a Pub/Sub topic.
* **Azure** — a diagnostic setting streams Entra sign-in logs, Entra audit
logs and the Activity log to an Event Hub.
Three properties separate a collector that survives a real estate from one
that looks right in a demo, and each is a rule here:
**It resumes.** A queue collector that restarts and re-reads from "now"
loses every object delivered while it was down; one that re-reads from the
beginning replays the whole bucket. Both are silent. The connector has to
declare a cursor the base class's ``apply_checkpoint`` de-duplicates on,
in one of the two shapes that exist:
* ``checkpoint_time_field`` *and* ``checkpoint_id_field`` — a monotonic
``(event_time, id)`` high-water mark, correct where arrival order follows
event order, which is a time-windowed API poll or an object store listed
oldest-first.
* ``dedup_identity_field`` — a bounded set of seen identities, which is
what an *unordered* source needs. Phase 3: on a queue a high-water mark
discards an out-of-order arrival the only time it will ever be offered,
and the message carrying it has already been acknowledged. Pub/Sub orders
nothing without an ordering key, and a Cloud Logging sink sets none; S3
notifications over SQS are unordered by construction.
Declaring both is refused by ``BaseConnector.__init_subclass__`` at import,
because they answer the same question two different ways.
**It stops.** A backlog can be arbitrarily deep — an SQS queue after an
outage, a Pub/Sub subscription with a week of retention — and a poll that
drains it in one pass turns a recovery into an out-of-memory kill or a
flood through ingest. The connector has to declare a bounded per-poll
budget *and* say, in the declaration, that leaving the remainder on the
queue is what applies backpressure.
**It is tested against what the vendor actually sends.** A fixture written
from the connector's own output proves the connector agrees with itself.
These have to be vendor-shaped payloads recorded under
``services/connectors/tests/fixtures/``.
Plus the two things a reader needs to use it at all: a setup guide and a
marketplace manifest.
Parsed with ``ast`` rather than imported, because this runs on a bare
interpreter in CI before any service venv exists — and because importing
``app.connectors`` here would make the gate depend on boto3 resolving.
Usage:
python3 scripts/check_cloud_native_collection.py
python3 scripts/check_cloud_native_collection.py --self-test
"""
from __future__ import annotations
import argparse
import ast
import sys
from dataclasses import dataclass
from pathlib import Path
# ``scripts/`` is on sys.path when this file runs as a program but not when a
# test loads it by path; gate_toolkit sits beside it either way.
sys.path.insert(0, str(Path(__file__).resolve().parent))
from gate_toolkit import TREE_SHAPES, refuses_an_empty_tree, repo_root # noqa: E402
REPO_ROOT = repo_root()
CONNECTORS_PKG = Path("services/connectors/app/connectors")
REGISTRY_REL = CONNECTORS_PKG / "__init__.py"
FIXTURES_REL = Path("services/connectors/tests/fixtures")
DOCS_REL = Path("apps/docs/docs/connectors")
PLUGINS_REL = Path("plugins")
@dataclass(frozen=True)
class CollectionPath:
"""One cloud-native collection path the plan names."""
connector_id: str
module: str
#: What the operator points at. Named so the failure text tells a reader
#: which vendor surface is missing rather than only which file is.
surface: str
#: The three the plan names. Adding a fourth is adding a row here; the gate
#: then fails until the connector, its fixtures, its guide and its manifest
#: all exist, which is the order that keeps a half-built collector from
#: shipping as a catalogue entry.
REQUIRED_PATHS: tuple[CollectionPath, ...] = (
CollectionPath(
connector_id="aws_cloudtrail_s3",
module="aws_cloudtrail_s3",
surface="CloudTrail organisation trail on S3, notified over SQS (management events, data events, VPC flow logs)",
),
CollectionPath(
connector_id="gcp_pubsub",
module="gcp_pubsub",
surface="Pub/Sub subscription fed by a Cloud Logging sink",
),
CollectionPath(
connector_id="azure_event_hubs",
module="azure_event_hubs",
surface="Event Hubs carrying Entra sign-in logs, Entra audit logs and the Activity log",
),
)
#: Class attribute carrying the per-poll budget. One name, so the gate and
#: the base class cannot disagree about the spelling.
BUDGET_ATTR = "collection_budget"
@dataclass(frozen=True)
class Declared:
"""What one connector module says about itself, read from its source."""
module_exists: bool
checkpoint_time: bool
checkpoint_id: bool
budget: bool
budget_leaves_backlog: bool
#: The second shape a resumable cursor comes in: a bounded set of seen
#: identities, for a source whose arrival order is not its event order.
dedup_identity: bool = False
@property
def resumes(self) -> bool:
"""Whether the connector declares a cursor of either shape."""
return (self.checkpoint_time and self.checkpoint_id) or self.dedup_identity
def _class_attribute_values(tree: ast.Module, attr: str) -> list[ast.expr]:
"""Every value assigned to ``attr`` at class scope anywhere in the module."""
out: list[ast.expr] = []
for node in ast.walk(tree):
if not isinstance(node, ast.ClassDef):
continue
for stmt in node.body:
targets: list[ast.expr] = []
if isinstance(stmt, ast.Assign):
targets = list(stmt.targets)
elif isinstance(stmt, ast.AnnAssign):
targets = [stmt.target]
else:
continue
for target in targets:
if isinstance(target, ast.Name) and target.id == attr and stmt.value is not None:
out.append(stmt.value)
return out
def _is_nonempty_tuple(value: ast.expr) -> bool:
return isinstance(value, ast.Tuple) and len(value.elts) > 0
def _budget_keywords(value: ast.expr) -> dict[str, ast.expr]:
"""Keyword arguments of a ``CollectionBudget(...)`` call, if that is what this is."""
if not isinstance(value, ast.Call):
return {}
func = value.func
name = func.attr if isinstance(func, ast.Attribute) else func.id if isinstance(func, ast.Name) else ""
if name != "CollectionBudget":
return {}
return {kw.arg: kw.value for kw in value.keywords if kw.arg}
def _positive_int(value: ast.expr | None) -> bool:
return isinstance(value, ast.Constant) and isinstance(value.value, int) and not isinstance(value.value, bool) and value.value > 0
def read_declaration(root: Path, path: CollectionPath) -> Declared:
source = root / CONNECTORS_PKG / f"{path.module}.py"
if not source.is_file():
return Declared(False, False, False, False, False)
tree = ast.parse(source.read_text(encoding="utf-8"))
checkpoint_time = any(_is_nonempty_tuple(v) for v in _class_attribute_values(tree, "checkpoint_time_field"))
checkpoint_id = any(_is_nonempty_tuple(v) for v in _class_attribute_values(tree, "checkpoint_id_field"))
dedup_identity = any(_is_nonempty_tuple(v) for v in _class_attribute_values(tree, "dedup_identity_field"))
budget = False
leaves_backlog = False
for value in _class_attribute_values(tree, BUDGET_ATTR):
kwargs = _budget_keywords(value)
if not kwargs:
continue
bounded = _positive_int(kwargs.get("max_batches_per_poll")) and _positive_int(kwargs.get("max_events_per_poll"))
if bounded:
budget = True
reason = kwargs.get("backlog_stays_on_the_queue")
if isinstance(reason, ast.Constant) and isinstance(reason.value, str) and reason.value.strip():
leaves_backlog = True
return Declared(True, checkpoint_time, checkpoint_id, budget, leaves_backlog, dedup_identity)
def registered_ids(root: Path) -> set[str]:
"""Connector ids the build knows about, from ``_CONNECTOR_CLASSES``.
Read as "which classes are in the registry tuple", then resolved back to
ids through each module's ``connector_id``. A class present in the file
but absent from that tuple is not reachable from the API, which is the
direction that ships a connector nobody can select.
"""
registry = root / REGISTRY_REL
if not registry.is_file():
return set()
tree = ast.parse(registry.read_text(encoding="utf-8"))
class_names: set[str] = set()
for node in ast.walk(tree):
if isinstance(node, ast.Assign):
targets: list[ast.expr] = list(node.targets)
elif isinstance(node, ast.AnnAssign):
targets = [node.target]
else:
continue
for target in targets:
if isinstance(target, ast.Name) and target.id == "_CONNECTOR_CLASSES" and isinstance(node.value, ast.Tuple):
class_names |= {e.id for e in node.value.elts if isinstance(e, ast.Name)}
ids: set[str] = set()
for module in sorted((root / CONNECTORS_PKG).glob("*.py")):
try:
tree = ast.parse(module.read_text(encoding="utf-8"))
except (OSError, SyntaxError):
continue
for node in ast.walk(tree):
if not isinstance(node, ast.ClassDef) or node.name not in class_names:
continue
for value in _class_attribute_values(ast.Module(body=[node], type_ignores=[]), "connector_id"):
if isinstance(value, ast.Constant) and isinstance(value.value, str):
ids.add(value.value)
return ids
def evaluate(root: Path, paths: tuple[CollectionPath, ...] = REQUIRED_PATHS) -> list[str]:
"""Every rule, over every required path. Returns one line per violation."""
failures: list[str] = []
registry = registered_ids(root)
for path in paths:
where = f"{CONNECTORS_PKG}/{path.module}.py"
declared = read_declaration(root, path)
if not declared.module_exists:
failures.append(
f"{path.connector_id}: no {where}. The plan's collection path — {path.surface} — has no collector, "
f"so an estate of any size cannot be read."
)
continue
if path.connector_id not in registry:
failures.append(
f"{path.connector_id}: the module exists but the class is not in _CONNECTOR_CLASSES "
f"({REGISTRY_REL}), so the catalogue, the setup wizard and the scheduler cannot reach it."
)
if not declared.resumes:
failures.append(
f"{path.connector_id}: declares no resumable cursor in {where} — needs either "
f"checkpoint_time_field and checkpoint_id_field (a high-water mark, for a source whose "
f"arrival order follows its event order) or dedup_identity_field (a bounded set of seen "
f"identities, for an unordered queue). A queue collector without one either loses "
f"everything delivered while it was down or replays the backlog, and both are silent."
)
if not declared.budget:
failures.append(
f"{path.connector_id}: declares no {BUDGET_ATTR} with positive max_batches_per_poll and "
f"max_events_per_poll in {where}. A poll that drains an arbitrarily deep backlog in one pass "
f"turns a recovery into an out-of-memory kill."
)
elif not declared.budget_leaves_backlog:
failures.append(
f"{path.connector_id}: {BUDGET_ATTR} is bounded but does not say what happens to the remainder. "
f"Set backlog_stays_on_the_queue to the sentence describing it — a bound that drops the overflow "
f"is data loss wearing the same shape as backpressure."
)
fixtures = root / FIXTURES_REL / path.connector_id
if not fixtures.is_dir() or not any(fixtures.iterdir()):
failures.append(
f"{path.connector_id}: no vendor-shaped fixtures under {FIXTURES_REL}/{path.connector_id}/. "
f"A test that builds its input from the connector's own normalize() only proves the connector "
f"agrees with itself."
)
guide = root / DOCS_REL / f"{path.connector_id.replace('_', '-')}.md"
legacy_guide = root / DOCS_REL / f"{path.connector_id}.md"
if not guide.is_file() and not legacy_guide.is_file():
failures.append(f"{path.connector_id}: no setup guide at {DOCS_REL}/{guide.name}.")
manifest = root / PLUGINS_REL / path.connector_id.replace("_", "-") / "plugin.yaml"
legacy_manifest = root / PLUGINS_REL / path.connector_id / "plugin.yaml"
if not manifest.is_file() and not legacy_manifest.is_file():
failures.append(f"{path.connector_id}: no marketplace manifest at {PLUGINS_REL}/{manifest.parent.name}/plugin.yaml.")
return failures
def _self_test() -> int:
"""One injected violation per rule, each required to be caught.
Injected into a copy of the real tree rather than asserted in prose: a
rule whose detector silently stopped matching reports OK, and the only
thing that tells them apart is watching a known-bad tree fail.
"""
import shutil
import tempfile
results: list[tuple[str, bool]] = []
baseline = evaluate(REPO_ROOT)
results.append(("the undisturbed tree is clean, so a caught case means something", not baseline))
if baseline:
for line in baseline[:6]:
print(f" {line}")
cases: list[tuple[str, str, str]] = [
# (label, file to rewrite, substitution applied to its text)
("a missing collector module", "DELETE_MODULE", ""),
("a collector that is not in the registry", "REGISTRY", ""),
("a collector with no resumable cursor", "CHECKPOINT", ""),
("a collector with no bounded per-poll budget", "BUDGET", ""),
("a bounded budget that does not say where the remainder goes", "BACKLOG_REASON", ""),
("a collector with no vendor-shaped fixtures", "FIXTURES", ""),
("a collector with no setup guide", "GUIDE", ""),
("a collector with no marketplace manifest", "MANIFEST", ""),
]
victim = REQUIRED_PATHS[0]
for label, kind, _ in cases:
tmp = Path(tempfile.mkdtemp(prefix="aisoc-cnc-selftest-"))
try:
for rel in (CONNECTORS_PKG, FIXTURES_REL, DOCS_REL, PLUGINS_REL):
src = REPO_ROOT / rel
if src.is_dir():
shutil.copytree(src, tmp / rel, dirs_exist_ok=True)
module = tmp / CONNECTORS_PKG / f"{victim.module}.py"
if kind == "DELETE_MODULE":
module.unlink(missing_ok=True)
elif kind == "REGISTRY":
registry = tmp / REGISTRY_REL
registry.write_text(
registry.read_text(encoding="utf-8").replace("AWSCloudTrailS3Connector,\n", "", 1),
encoding="utf-8",
)
elif kind == "CHECKPOINT" and module.is_file():
# Both shapes, because either one satisfies the rule and
# disabling only the one this connector happens to use today
# would stop proving anything the day it switches.
module.write_text(
module.read_text(encoding="utf-8")
.replace("checkpoint_id_field = (", "checkpoint_id_field_disabled = (")
.replace("dedup_identity_field = (", "dedup_identity_field_disabled = ("),
encoding="utf-8",
)
elif kind == "BUDGET" and module.is_file():
module.write_text(
module.read_text(encoding="utf-8").replace("max_events_per_poll=", "max_events_per_poll_disabled="),
encoding="utf-8",
)
elif kind == "BACKLOG_REASON" and module.is_file():
module.write_text(
module.read_text(encoding="utf-8").replace("backlog_stays_on_the_queue=", "backlog_note="),
encoding="utf-8",
)
elif kind == "FIXTURES":
shutil.rmtree(tmp / FIXTURES_REL / victim.connector_id, ignore_errors=True)
elif kind == "GUIDE":
(tmp / DOCS_REL / f"{victim.connector_id.replace('_', '-')}.md").unlink(missing_ok=True)
elif kind == "MANIFEST":
(tmp / PLUGINS_REL / victim.connector_id.replace("_", "-") / "plugin.yaml").unlink(missing_ok=True)
caught = bool(evaluate(tmp, (victim,)))
results.append((f"catches {label}", caught))
finally:
shutil.rmtree(tmp, ignore_errors=True)
# Both shapes: the directories absent, and the directories present and
# empty. They are different questions — a gate whose first act is "is the
# directory there" refuses the first for a reason that says nothing about
# its corpus, and reports the second clean.
details: list[str] = []
for shape in TREE_SHAPES:
refused, detail = refuses_an_empty_tree(Path(__file__).name, shape=shape)
results.append((f"refuses a {shape} tree rather than reporting it clean", refused))
details.append(f"{shape}: {detail}")
ok = True
for description, passed in results:
ok &= passed
print(f" {'PASS' if passed else 'FAIL'} {description}")
for line in "\n".join(details).splitlines():
print(f" {line}")
print()
if not ok:
print(f"{Path(__file__).name}: self-test FAILED")
return 1
print(f"{Path(__file__).name}: self-test OK")
return 0
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("--self-test", action="store_true", help="inject one violation per rule and require each to be caught")
args = parser.parse_args()
if args.self_test:
return _self_test()
if not (REPO_ROOT / CONNECTORS_PKG).is_dir():
print(
f"check_cloud_native_collection: {CONNECTORS_PKG} is not there — nothing to render a verdict about.",
file=sys.stderr,
)
return 2
failures = evaluate(REPO_ROOT)
if failures:
print("Cloud-native collection (depth plan 4.1) is incomplete:\n", file=sys.stderr)
for line in failures:
print(f" - {line}", file=sys.stderr)
print(f"\n{len(failures)} problem(s) across {len(REQUIRED_PATHS)} required collection path(s).", file=sys.stderr)
return 1
print(
f"OK — {len(REQUIRED_PATHS)} cloud-native collection path(s): "
f"each registered, resumable, bounded, fixtured, documented and published."
)
return 0
if __name__ == "__main__":
raise SystemExit(main())