Skip to content

fix: dry-run k8s manifests before approval instead of applying them (#102) - #106

Merged
OkiriGabriel merged 1 commit into
mainfrom
fix/k8s-apply-approval-order
Oct 20, 2026
Merged

OkiriGabriel merged 1 commit into
mainfrom
fix/k8s-apply-approval-order

Conversation

@OkiriGabriel

Copy link
Copy Markdown
Collaborator

Description

Fixes #102.

With AUTO_APPLY=false and dry_run=false, K8sTools.apply_manifest ran the live kubectl apply and only afterwards returned requires_approval with "Manifest validated (dry-run OK)". The cluster was changed before anyone approved anything, and no dry run had actually happened.

There was a second bug on the same path: the approval request contained kubectl apply -f /tmp/xxx.yaml, but that temp file was deleted in finally before the method returned. Even after a human approved, the command would have failed with "file not found".

Now, when approval is needed:

  1. Only kubectl apply -f <tmp> -n <ns> --dry-run=client runs.
  2. If validation fails, the method returns the error and no approval is requested.
  3. Otherwise the live command carries the manifest itself: printf %s <base64> | base64 --decode | kubectl apply -f - -n <ns>. It is stored with CommandApprovalStore.retain (from fix: require approval for host-mutating tools when AUTO_APPLY is disabled (#89, #70) #103), so Slack gets a short single-use reference instead of a manifest-sized button value. Approving it runs the apply exactly once.
  4. The namespace and temp path are now shlex.quoted.

dry_run=true and AUTO_APPLY=true behave as before.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Platform/Component

  • Kubernetes
  • Tools/Executor
  • Tests

Testing

  • New tests/test_k8s_apply_approval.py (7 tests). Against the old k8s_tools.py, 4 of them fail: live apply before approval, unusable approval, approval sent after failed validation, and manifest round-trip.
    • only a dry run executes before approval; the response and Slack request carry the approval reference
    • the approved reference runs the live apply once, without --dry-run and without the temp-file path; a replay is blocked
    • the approved pipeline, run in a real sh with kubectl swapped for cat, reproduces a manifest containing $( ), backticks, quotes and | & > byte-for-byte
    • failed validation: no approval requested
    • dry_run=true only validates; AUTO_APPLY=true applies directly; a disallowed namespace runs nothing
  • Full suite: 655 passed, 2 skipped (Python 3.11)
  • flake8 --select=E9,F63,F7,F82: 0; bandit -ll on tracked files: no issues

Breaking Changes

None. The approval message changes from "Manifest validated (dry-run OK)…" to "Manifest passed kubectl --dry-run=client. Approval request sent to Slack; nothing was applied."

Thanks to @Nitjsefnie for finding and reporting this while working on #103.

🤖 Generated with Claude Code

With AUTO_APPLY=false and dry_run=false, apply_manifest ran the live
`kubectl apply` first and only then returned requires_approval (#102).
The approval it sent was also unusable: it pointed at the temp manifest
file, which was deleted before anyone could approve it.

Now, without AUTO_APPLY, only `kubectl apply --dry-run=client` runs. If
validation fails, no approval is requested. Otherwise the live command
carries the manifest as base64 piped into `kubectl apply -f -` and is
retained as a single-use approval reference (CommandApprovalStore, from
#103), so it fits in a Slack button and can be executed once approved.
Namespace and paths are shell-quoted.

Fixes #102

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@OkiriGabriel
OkiriGabriel merged commit 342ef64 into main Oct 20, 2026
10 checks passed
@OkiriGabriel
OkiriGabriel deleted the fix/k8s-apply-approval-order branch November 26, 2026 10:13
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.

[SECURITY] apply_k8s_manifest executes the live kubectl apply before its AUTO_APPLY approval check

1 participant