Skip to content

audit: fail closed when required policy cannot be resolved - #4920

Merged
Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
audit-fail-closed-policy-resolution
Nov 23, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
audit-fail-closed-policy-resolution

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Bare apm audit resolved security.audit.fail_on_drift by discovering org policy and returning "off" on any discovery error or unresolved policy, ignoring policy.fetch_failure_default. A project that requires an org policy could therefore get exit 0 on a failed drift check. apm audit --ci let a raised discovery escape as an unnamed traceback.

Both audit paths now route discovery through the policy-outcome owner (policy/outcome_routing.py):

  • discovery_failure_result (new, shared with discover_trust_policy) turns a raised discovery into a routable result. A rejected inheritance chain (PolicyInheritanceError: cycle, cross-host extends, excess depth) becomes the always-block incomplete_chain outcome, with the already-redacted message. Any other error becomes cache_miss_fetch_fail, reporting the exception type only. Audit converts only the owner's POLICY_DISCOVERY_ERRORS; any other exception propagates.
  • Bare apm audit: _resolve_fail_on_drift asks classify_project_policy. When the owner says the policy is required (fetch_failure_default: block, or an always-block outcome) and drift fails, the audit exits 1 with [x] Org policy could not be resolved; fail_on_drift could not be applied: <cause> (policy.fetch_failure_default=block) and drift renders as [x]. An optional policy keeps drift advisory and prints the same text as a [!] notice. APM_POLICY_DISABLE=1 still skips discovery.
  • apm audit --ci: the error goes through the existing routed render, so it prints [x] Policy fetch failed: policy discovery failed (<Type>) (policy.fetch_failure_default=block) and exits 1, or [!] ...; enforcement skipped under warn. That wording was already pinned by lifecycle journeys, so it is unchanged.
  • The install-deployment-policy-outcome-routing guard now also requires the bare-audit reader to call classify_project_policy. The owner registry lists _resolve_fail_on_drift as a consumer.

Behaviour change to note: under the warn posture, a transport-type discovery error (OSError, ValueError, RuntimeError) in --ci used to crash with exit 1 and a traceback. It now behaves like any other fetch failure: a [!] line, enforcement skipped, and exit 0 when nothing else fails. That matches the existing cache_miss_fetch_fail warn behaviour. A rejected chain always fails, under any posture, and so does any unexpected exception.

Issue and approved scope

Refs: maintainer-initiated hardening of apm audit policy resolution.

Scope: maintainer-initiated; reviewed by the maintainers.

Does this PR complete the issue, or what remains? Completes the scoped item.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally
  • All existing tests pass
  • Added tests for new functionality (if applicable)

New module tests/unit/test_audit_unresolved_policy.py (component, P-OWN-policy-outcome-routing). It drives the real audit command and routing owner, and replaces only discovery and the drift replay.

Cell main this PR
bare, required, discovery raises -> exit 1 + [x] cause FAIL (exit 0) pass
bare, required, discovery resolves nothing -> exit 1 + [x] cause FAIL (exit 0) pass
bare, optional, discovery raises -> exit 0 + [!] notice FAIL (silent) pass
bare, found policy, fail_on_drift: false -> exit 0 (control) pass pass
bare, APM_POLICY_DISABLE=1 -> no discovery, exit 0 (control) pass pass
--ci, required, discovery raises -> exit 1 + [x] cause FAIL (traceback) pass
--ci, optional, discovery raises -> exit 0 + [!] FAIL (traceback) pass
bare, optional, rejected chain -> exit 1 + [x] FAIL (exit 0) pass
--ci, optional, rejected chain -> exit 1 + [x] FAIL (traceback) pass

Updated expectations:

  • tests/lifecycle/security/test_policy_outcomes.py audit-exec-replay block cells now expect exit 1 plus the [x] cause. They had pinned the old exit 0.
  • tests/integration/test_architecture_semantic_rule_mutations.py: the install-deployment-audit-policy-discovery fragment now matches the new indentation of the lambda.
  • Two req-pl-014 spec cells now stub discovery with a real found PolicyFetchResult. They had used a bare namespace without outcome, which the owner routes as an unknown outcome.

Validation (all passed):

  • uv run --extra dev pytest -n 0 tests/unit/test_audit_unresolved_policy.py: 9 passed. Against main's src/: 7 failed, 2 passed.
  • uv run --extra dev pytest -n 4 tests/unit/test_audit*.py tests/unit/commands/test_audit_error_handling.py tests/unit/policy/test_outcome_routing.py tests/spec_conformance/test_audit_recorded_target_reqs.py tests/spec_conformance/test_policy_reqs.py tests/lifecycle/security/test_policy_outcomes.py tests/integration/test_audit_drift_exit_contract_lifecycle.py tests/integration/test_audit_scan_hermetic.py tests/integration/test_audit_coverage.py tests/unit/scripts/test_architecture_runner.py tests/unit/policy/ tests/integration/test_architecture_semantic_rule_mutations.py tests/integration/test_architecture_owner_rule_mutations.py: 3713 passed, 1 skipped.
  • uvx [email protected] check and format --check on changed files: passed.
  • python scripts/check_test_assertions.py: clean. python scripts/lint_architecture_boundaries.py --root .: clean.
  • python -m tests.quality.taxonomy --changed: clean. python -m tests.lifecycle.generator --check: up to date.
  • scripts/check_complexity_ratchet.py --base-ref origin/main: clean. pylint R0801: 10.00/10.
  • tests/spec_conformance/mode_b_detector.sh: OK (below threshold).

Changelog

  • One fragment per section, changelog.d/<pr>.<section>.md, holding
    one line: user-visible effect first, 25 words or fewer, ending with
    (#PR); breaking changes only under upgrade notes; no Highlights; no
    new lines under ## [Unreleased]. See changelog.d/README.md and
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • Spec edit
  • Manifest edit
  • Test edit
  • CONFORMANCE.{md,json} regenerated
  • N/A -- this PR does not change OpenAPM-observable behaviour. req-pl-014 still holds: drift alone does not change the exit status when fail_on_drift is off. The new exit 1 comes from a required policy that could not be resolved.

Bare `apm audit` treated any org-policy discovery error, and any
unresolved policy, as "fail_on_drift off". When the project requires a
policy (policy.fetch_failure_default: block, or an always-block outcome)
and drift fails, the audit now exits 1 with a named [x] cause; an
optional policy keeps drift advisory and prints a [!] notice.

`apm audit --ci` routed a discovery exception as an unnamed crash; it now
routes it as a fetch failure through the policy-outcome owner, so the
existing [x]/[!] lines name the cause.

Both paths reuse policy/outcome_routing.py: classify_project_policy
decides "required", and the new discovery_failure_result converts a
raised discovery (shared with discover_trust_policy). The outcome-routing
guard now also requires the fail_on_drift reader to ask the owner.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Copilot AI balanced review requested due to automatic review settings November 23, 2026 05:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical routing issue converts policy inheritance validation failures into cache misses, allowing CI audits to skip enforcement and exit successfully under the default warn posture.

1 open issue
What changed in this PR

This PR routes audit policy-discovery failures through the shared policy-outcome owner so required policies no longer silently leave failed drift checks advisory.

Changes:

  • Adds shared discovery-error conversion and routed diagnostics for bare audit and --ci.
  • Extends architecture guards and regression coverage for unresolved policies.
  • Updates audit documentation and adds a changelog fragment.
File Description
tests/​unit/​test_audit_unresolved_policy.py Adds seven policy-resolution regression cases.
tests/​spec_conformance/​test_policy_reqs.py Uses typed discovery results in policy stubs.
tests/​lifecycle/​security/​test_policy_outcomes.py Updates blocked replay exit and diagnostic expectations.
src/​apm_cli/​policy/​outcome_routing.py Adds shared discovery-error conversion.
src/​apm_cli/​commands/​audit.py Routes discovery failures and applies required-policy drift gating.
scripts/​architecture_linter/​checks/​install_policy_outcome_routing.py Requires bare audit to call the routing owner.
packages/​apm-guide/​.apm/​skills/​apm-usage/​commands.md Documents required-policy audit failures.
docs/​src/​content/​docs/​reference/​cli/​audit.md Updates policy diagnostics and exit-code guidance.
changelog.d/​4920.fixed.md Records the user-visible audit fix.
.apm/​architecture/​owners/​install-deployment.json Registers the bare-audit policy reader as a consumer.

Review effort: Balanced

Comment thread src/apm_cli/policy/outcome_routing.py Resolved
A rejected inheritance chain (cycle, cross-host extends, excess depth)
now maps to the always-block incomplete_chain outcome in the owner, so a
warn posture no longer skips enforcement for it. Audit converts only the
owner's known discovery errors; any other exception propagates. Update
the audit-policy-discovery mutation fragment for the new indentation.

Co-authored-by: copilot-swe-agent[bot] <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical uncaught PolicyInheritanceError can escape bare-audit drift checking before the named policy failure is rendered, requiring broader exception handling and a regression test.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread src/apm_cli/policy/outcome_routing.py Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The documentation incorrectly promises advisory drift under warn, although rejected inheritance chains, policy hash mismatches, and ambiguous git remotes still cause exit 1.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the changes centralizing audit policy-discovery failure handling.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the audit policy-discovery failure-routing change.

No open issues 🎉

Review effort: Balanced

@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit cc8fc7d into main Nov 23, 2026
98 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the audit-fail-closed-policy-resolution branch November 23, 2026 08:15
Daniel Meppiel (danielmeppiel) added a commit that referenced this pull request Nov 23, 2026
Move the #4678, #4890, #4892, #4912 and #4934 lines into [0.38.1]: they merged before the v0.38.1 tag and shipped in it. [0.38.2] keeps #4920.

Co-authored-by: Copilot App <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants