@intentius/chant 0.124.75 → 0.124.77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/plywood.js +19 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
- package/dist/__fixtures__/gated-ledger.d.ts +10 -0
- package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
- package/dist/__fixtures__/gated-ledger.js +51 -0
- package/dist/agent-guard.d.ts +143 -0
- package/dist/agent-guard.d.ts.map +1 -0
- package/dist/agent-guard.js +456 -0
- package/dist/ci-commit.d.ts +54 -3
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +69 -7
- package/dist/ci-own-jobs.d.ts +81 -0
- package/dist/ci-own-jobs.d.ts.map +1 -0
- package/dist/ci-own-jobs.js +234 -0
- package/dist/ci-product.d.ts +39 -0
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +40 -0
- package/dist/cli/handlers/approve.d.ts +5 -1
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +17 -11
- package/dist/cli/handlers/build.d.ts +1 -1
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +88 -2
- package/dist/cli/handlers/commit.d.ts.map +1 -1
- package/dist/cli/handlers/commit.js +3 -1
- package/dist/cli/handlers/notify-relay.d.ts +18 -1
- package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
- package/dist/cli/handlers/notify-relay.js +90 -14
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +3 -1
- package/dist/cli/handlers/release.d.ts +9 -1
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +34 -6
- package/dist/cli/handlers/run-generate.d.ts.map +1 -1
- package/dist/cli/handlers/run-generate.js +2 -1
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +21 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +60 -4
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/pr-env-components.d.ts +65 -0
- package/dist/components/pr-env-components.d.ts.map +1 -0
- package/dist/components/pr-env-components.js +163 -0
- package/dist/components/pr-policy.d.ts +15 -0
- package/dist/components/pr-policy.d.ts.map +1 -1
- package/dist/components/pr-policy.js +24 -1
- package/dist/components/release-compile.d.ts +15 -3
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +66 -15
- package/dist/gate-policy.d.ts +14 -1
- package/dist/gate-policy.d.ts.map +1 -1
- package/dist/gate-policy.js +16 -6
- package/dist/lexicon.d.ts +17 -1
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +1 -0
- package/dist/lifecycle/gated-op.d.ts +95 -0
- package/dist/lifecycle/gated-op.d.ts.map +1 -0
- package/dist/lifecycle/gated-op.js +96 -0
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +83 -2
- package/dist/notify-relay-repo.d.ts +131 -0
- package/dist/notify-relay-repo.d.ts.map +1 -0
- package/dist/notify-relay-repo.js +202 -0
- package/dist/notify-relay.d.ts +25 -1
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +28 -2
- package/dist/op/op-waves-capability.js +2 -2
- package/dist/policy-engine.d.ts +95 -0
- package/dist/policy-engine.d.ts.map +1 -0
- package/dist/policy-engine.js +158 -0
- package/dist/release-floors.d.ts +1 -1
- package/dist/release-floors.js +1 -1
- package/dist/state-export.d.ts +126 -0
- package/dist/state-export.d.ts.map +1 -0
- package/dist/state-export.js +157 -0
- package/dist/state-migration.d.ts +148 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +147 -0
- package/dist/state-unlock.d.ts +167 -0
- package/dist/state-unlock.d.ts.map +1 -0
- package/dist/state-unlock.js +197 -0
- package/dist/wave-runtime.mjs +329 -329
- package/dist/workspace/declaration.schema.json +1 -1
- package/dist/workspace/release-workflow.d.ts +10 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +8 -3
- package/package.json +1 -1
- package/src/__fixtures__/agent-guard/plywood.ts +27 -0
- package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
- package/src/__fixtures__/gated-ledger.ts +58 -0
- package/src/agent-guard.test.ts +320 -0
- package/src/agent-guard.ts +548 -0
- package/src/ci-commit.test.ts +50 -2
- package/src/ci-commit.ts +93 -9
- package/src/ci-own-jobs.test.ts +109 -0
- package/src/ci-own-jobs.ts +253 -0
- package/src/ci-product.ts +81 -0
- package/src/cli/handlers/approve-cwd.test.ts +112 -0
- package/src/cli/handlers/approve.ts +22 -11
- package/src/cli/handlers/build-own-jobs.test.ts +80 -0
- package/src/cli/handlers/build-pr-loop-control.test.ts +88 -0
- package/src/cli/handlers/build-run-group.test.ts +60 -0
- package/src/cli/handlers/build.ts +83 -3
- package/src/cli/handlers/commit.ts +3 -0
- package/src/cli/handlers/notify-relay.ts +99 -17
- package/src/cli/handlers/pr.ts +3 -1
- package/src/cli/handlers/release-cut-archives.test.ts +21 -0
- package/src/cli/handlers/release.ts +31 -6
- package/src/cli/handlers/run-generate.ts +2 -1
- package/src/cli/main.ts +21 -1
- package/src/cli/parse-args.ts +45 -3
- package/src/cli/registry.ts +16 -0
- package/src/components/pr-env-components.test.ts +138 -0
- package/src/components/pr-env-components.ts +214 -0
- package/src/components/pr-policy.test.ts +41 -1
- package/src/components/pr-policy.ts +25 -1
- package/src/components/release-compile.test.ts +47 -0
- package/src/components/release-compile.ts +70 -13
- package/src/gate-policy-bundle.test.ts +14 -0
- package/src/gate-policy.test.ts +7 -0
- package/src/gate-policy.ts +22 -7
- package/src/lexicon.ts +18 -1
- package/src/lifecycle/gated-op.ts +158 -0
- package/src/lifecycle/git.test.ts +92 -0
- package/src/lifecycle/git.ts +71 -2
- package/src/notify-relay-repo.test.ts +284 -0
- package/src/notify-relay-repo.ts +234 -0
- package/src/notify-relay.test.ts +12 -0
- package/src/notify-relay.ts +30 -3
- package/src/op/op-waves-capability.ts +2 -2
- package/src/policy-engine.test.ts +163 -0
- package/src/policy-engine.ts +201 -0
- package/src/release-floors.ts +1 -1
- package/src/state-export.test.ts +125 -0
- package/src/state-export.ts +228 -0
- package/src/state-migration.test.ts +134 -0
- package/src/state-migration.ts +237 -0
- package/src/state-unlock.test.ts +166 -0
- package/src/state-unlock.ts +294 -0
- package/src/workspace/declaration.schema.json +1 -1
- package/src/workspace/release-workflow.test.ts +34 -0
- package/src/workspace/release-workflow.ts +17 -3
|
@@ -2035,7 +2035,7 @@
|
|
|
2035
2035
|
},
|
|
2036
2036
|
"ciPolicy": {
|
|
2037
2037
|
"type": "object",
|
|
2038
|
-
"description": "Gate-time policy on the pull-request jobs (#4294): a policy engine run over the change set, with the policy read at base. engine names the result shape the command prints, conftest (the default) or opa. path is the policy directory, relative to the declaration (default policy). namespace is the package to read. command replaces the engine's own argv, with {policy} for the policy directory exported from base and {input} for the input file. deny rules refuse the apply unless an override counts; hold rules make the plan wait for a person's approval; warn rules are advice. There is no unattended outcome. source (#4613) is { repo, ref, path? }: a shared repository whose ref the job fetches, read in place of path, with the commit read recorded on the report and the wave's decision; path inside it defaults to policy, and path beside source is refused. packs (#4613) is a list of { dir, config? }: CrossGuard packs every pulumi-up preview runs, copied from the declaration's directory at base, or from the top of source when it is set. Added in schema 1 by chant 0.124.41, so a declaration that uses it sets minReader to 0.124.41 or newer; source and packs were added in schema 1 by chant 0.124.55, so a declaration that sets either sets minReader to 0.124.55 or newer."
|
|
2038
|
+
"description": "Gate-time policy on the pull-request jobs (#4294): a policy engine run over the change set, with the policy read at base. engine names the result shape the command prints, conftest (the default) or opa. path is the policy directory, relative to the declaration (default policy). namespace is the package to read. command replaces the engine's own argv, with {policy} for the policy directory exported from base and {input} for the input file. deny rules refuse the apply unless an override counts; hold rules make the plan wait for a person's approval; warn rules are advice. There is no unattended outcome. source (#4613) is { repo, ref, path? }: a shared repository whose ref the job fetches, read in place of path, with the commit read recorded on the report and the wave's decision; path inside it defaults to policy, and path beside source is refused. packs (#4613) is a list of { dir, config? }: CrossGuard packs every pulumi-up preview runs, copied from the declaration's directory at base, or from the top of source when it is set. Added in schema 1 by chant 0.124.41, so a declaration that uses it sets minReader to 0.124.41 or newer; source and packs were added in schema 1 by chant 0.124.55, so a declaration that sets either sets minReader to 0.124.55 or newer. install (#4774) is true to fetch conftest or opa at the release chant pins, checked against its recorded sha256, when the engine is not on PATH; it is refused beside command. Read by chant 0.124.77 and newer, so a declaration that sets install sets minReader to 0.124.77 or newer."
|
|
2039
2039
|
},
|
|
2040
2040
|
"ciPhaseName": {
|
|
2041
2041
|
"type": "string",
|
|
@@ -68,6 +68,16 @@ export interface ReleaseWorkflowInput {
|
|
|
68
68
|
imageArchives?: string[];
|
|
69
69
|
/** `release.lane` (#4692): the gate's green lane. */
|
|
70
70
|
lane?: string;
|
|
71
|
+
/**
|
|
72
|
+
* `release.publish` (#4767). On GitHub, a `forge: "github"` target with no
|
|
73
|
+
* `tokenEnv` reads `GITHUB_TOKEN`, so the publish job sets it to the job's
|
|
74
|
+
* token unless `release.workflow.env` sets it.
|
|
75
|
+
*/
|
|
76
|
+
publish?: ReadonlyArray<{
|
|
77
|
+
type: string;
|
|
78
|
+
forge?: string;
|
|
79
|
+
tokenEnv?: string;
|
|
80
|
+
}>;
|
|
71
81
|
}
|
|
72
82
|
/** The flags `chant release publish` gets under the groups. */
|
|
73
83
|
export declare function publishFlags(groups: ReadonlySet<SecurityGroup>): string[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"release-workflow.d.ts","sourceRoot":"","sources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gB,qBAAqB,CAAC,KAAK,EAAE,oBAAoB,GAAG,MAAM,CAoGzE;AAkFD,wEAAwE;AACxE,eAAO,MAAM,uBAAuB,yBAAyB,CAAC;AA6L9D,wFAAwF;AACxF,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,OAAO,iBAAiB,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,CA6DvG"}
|
|
@@ -59,9 +59,13 @@ export function renderReleaseWorkflow(input) {
|
|
|
59
59
|
const after = p.after ?? [];
|
|
60
60
|
const images = input.images === true;
|
|
61
61
|
const extra = jobExtras(p, input.forge);
|
|
62
|
+
// #4767: Forgejo's job token is not a GitHub token, and GitLab has no such expression.
|
|
63
|
+
const forgeToken = input.forge === "github" && p.env?.GITHUB_TOKEN === undefined && (input.publish ?? []).some((t) => t.type === "forge" && t.forge === "github" && !t.tokenEnv)
|
|
64
|
+
? [" GITHUB_TOKEN: ${{ github.token }}"]
|
|
65
|
+
: [];
|
|
62
66
|
if (p.trigger === "dispatch") {
|
|
63
67
|
const archives = images ? (input.imageArchives ?? []).map((a) => (root ? `${root}/${a.replace(/^\.\//, "")}` : a.replace(/^\.\//, ""))) : [];
|
|
64
|
-
const o = { chant, install, build, root, verify, flags, after, images, archives, forge: input.forge, extra, gate: p.gate ?? "last-green", ...(input.lane ? { lane: input.lane } : {}) };
|
|
68
|
+
const o = { chant, install, build, root, verify, flags, after, images, archives, forge: input.forge, extra, forgeToken, gate: p.gate ?? "last-green", ...(input.lane ? { lane: input.lane } : {}) };
|
|
65
69
|
return input.forge === "gitlab" ? renderGitlabDispatch(o) : renderGithubDispatch(o);
|
|
66
70
|
}
|
|
67
71
|
// #4657: the images are built from the release, so chant/lifecycle is fetched before them and the build after.
|
|
@@ -107,7 +111,7 @@ export function renderReleaseWorkflow(input) {
|
|
|
107
111
|
" publish:",
|
|
108
112
|
" runs-on: ubuntu-latest",
|
|
109
113
|
...(root ? [" defaults:", " run:", ` working-directory: ${root}`] : []),
|
|
110
|
-
...extra.githubEnv(
|
|
114
|
+
...extra.githubEnv(forgeToken),
|
|
111
115
|
" steps:",
|
|
112
116
|
...githubSetup(),
|
|
113
117
|
` - run: ${install}`,
|
|
@@ -284,7 +288,7 @@ function renderGithubDispatch(o) {
|
|
|
284
288
|
" needs: [gate, cut]",
|
|
285
289
|
" runs-on: ubuntu-latest",
|
|
286
290
|
...defaults,
|
|
287
|
-
...o.extra.githubEnv(
|
|
291
|
+
...o.extra.githubEnv(o.forgeToken),
|
|
288
292
|
" steps:",
|
|
289
293
|
...githubSetup(commit),
|
|
290
294
|
` - run: ${o.install}`,
|
|
@@ -404,6 +408,7 @@ export async function runReleaseWorkflow(ctx) {
|
|
|
404
408
|
...(release?.workflow ? { pipeline: release.workflow } : {}),
|
|
405
409
|
...((release?.images?.length ?? 0) > 0 ? { images: true, imageArchives: release.images.map((i) => i.archive) } : {}),
|
|
406
410
|
...(release?.lane ? { lane: release.lane } : {}),
|
|
411
|
+
...(release?.publish ? { publish: release.publish } : {}),
|
|
407
412
|
...(args.ciInstall ? { install: args.ciInstall } : {}),
|
|
408
413
|
...(args.buildCommand ? { build: args.buildCommand } : {}),
|
|
409
414
|
...(args.ciChant ? { chant: args.ciChant } : {}),
|
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* plywood's agent guard words and rules, as packages/plywood/src/guard.ts
|
|
3
|
+
* passes them to chant's `installAgentGuard` (#4770). Its tests, ported from
|
|
4
|
+
* plywood's test/agents.test.ts ("the agent guard"), run against these.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentGuardEngine, AgentGuardSpec } from "../../agent-guard";
|
|
7
|
+
|
|
8
|
+
const engine: AgentGuardEngine = {
|
|
9
|
+
refuse: ["up", "update", "destroy"],
|
|
10
|
+
valuedFlags: ["-C", "--cwd", "-s", "--stack", "--color", "--config-file", "--logflow", "-v", "--verbose", "--tracing", "--profiling", "--memprofilerate"],
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const PLYWOOD_GUARD: AgentGuardSpec = {
|
|
14
|
+
words: {
|
|
15
|
+
product: "plywood",
|
|
16
|
+
refusal: "an agent session does not run '{what}'. Open a pull request; plywood previews it, and the update runs after a person approves the digest.",
|
|
17
|
+
},
|
|
18
|
+
rules: {
|
|
19
|
+
engines: { pulumi: engine, plunge: engine },
|
|
20
|
+
// plywood's own verbs that update, approve, override or move a stack, refused as terragucci refuses its own.
|
|
21
|
+
product: {
|
|
22
|
+
commands: [["up"], ["resume"], ["approve"], ["override"], ["cancel"], ["move"], ["rollback"], ["action"]],
|
|
23
|
+
flagValues: [["--respond", "up"]],
|
|
24
|
+
},
|
|
25
|
+
wrapperExit: 1,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* terragucci's agent guard words and rules, as packages/terragucci/src/guard.ts
|
|
3
|
+
* passes them to chant's `installAgentGuard` (#4770). Its tests, ported from
|
|
4
|
+
* terragucci's test/guard.test.ts, run against these.
|
|
5
|
+
*/
|
|
6
|
+
import type { AgentGuardEngine, AgentGuardSpec } from "../../agent-guard";
|
|
7
|
+
|
|
8
|
+
const tf: AgentGuardEngine = {
|
|
9
|
+
refuse: ["apply", "destroy", "import", "taint", "untaint", "force-unlock"],
|
|
10
|
+
refuseSub: { state: ["rm", "mv", "push", "replace-provider"] },
|
|
11
|
+
refuseFlags: ["-auto-approve", "-terragrunt-auto-approve"],
|
|
12
|
+
dirFlags: ["-chdir", "-working-dir", "-terragrunt-working-dir"],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const TERRAGUCCI_GUARD: AgentGuardSpec = {
|
|
16
|
+
words: {
|
|
17
|
+
product: "terragucci",
|
|
18
|
+
refusal:
|
|
19
|
+
"{what} is refused{where}. Propose the change instead: commit it to a branch and call the propose tool of terragucci mcp --propose, which pushes the branch and opens a pull request. The pipeline plans it, and a person approves its apply. init, validate, fmt and plan still run here.",
|
|
20
|
+
where: " in {root}, a root of this repo's pipeline",
|
|
21
|
+
},
|
|
22
|
+
rules: {
|
|
23
|
+
engines: {
|
|
24
|
+
tofu: tf,
|
|
25
|
+
terraform: tf,
|
|
26
|
+
terragrunt: {
|
|
27
|
+
...tf,
|
|
28
|
+
allFlags: ["--all", "-a"],
|
|
29
|
+
prefixes: [{ words: ["run-all"], all: true }, { words: ["stack", "run"], all: true }, { words: ["run"] }],
|
|
30
|
+
},
|
|
31
|
+
choudoufu: tf,
|
|
32
|
+
},
|
|
33
|
+
product: {
|
|
34
|
+
commands: [["stage", "tf-apply"], ["approve"], ["override"], ["unlock-state"], ["pr-merge"]],
|
|
35
|
+
flagValues: [["--mode", "apply"]],
|
|
36
|
+
},
|
|
37
|
+
wrapperExit: 3,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A gated-request ledger in memory, for the state operations' tests
|
|
3
|
+
* (#4762): requests wait until `approve` is called, an approval by the
|
|
4
|
+
* requester is refused as chant's ledger refuses it, and a spent request
|
|
5
|
+
* answers `spent`. Nothing is written to git.
|
|
6
|
+
*/
|
|
7
|
+
import { gatedRequestDigest, type GatedRequest } from "../lifecycle/gated-request";
|
|
8
|
+
import type { GatedRequestLedger } from "../lifecycle/gated-op";
|
|
9
|
+
|
|
10
|
+
export interface FakeLedger extends GatedRequestLedger {
|
|
11
|
+
approve(by: string): void;
|
|
12
|
+
/** What was written, in order: `pending`, `applied`, `finished:<result>`. */
|
|
13
|
+
writes: string[];
|
|
14
|
+
/** The finished lines' detail, in order. */
|
|
15
|
+
details: Array<Record<string, string | number | boolean>>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function fakeLedger(): FakeLedger {
|
|
19
|
+
let approvedBy: string | undefined;
|
|
20
|
+
const writes: string[] = [];
|
|
21
|
+
const details: Array<Record<string, string | number | boolean>> = [];
|
|
22
|
+
const asked = new Map<string, { op: string; request: GatedRequest; expiresAt: string }>();
|
|
23
|
+
const spent = new Set<string>();
|
|
24
|
+
return {
|
|
25
|
+
writes,
|
|
26
|
+
details,
|
|
27
|
+
approve(by) {
|
|
28
|
+
approvedBy = by;
|
|
29
|
+
},
|
|
30
|
+
async requestGate(input) {
|
|
31
|
+
const digest = gatedRequestDigest(input.op, input.request);
|
|
32
|
+
if (spent.has(digest)) return { status: "spent", digest, request: input.request };
|
|
33
|
+
if (!asked.has(digest)) {
|
|
34
|
+
asked.set(digest, { op: input.op, request: input.request, expiresAt: "2099-01-01T00:00:00Z" });
|
|
35
|
+
writes.push("pending");
|
|
36
|
+
}
|
|
37
|
+
const command = `chant approve ${input.op} ${input.request.subject} --plan ${digest}`;
|
|
38
|
+
if (!approvedBy) return { status: "waiting", digest, request: input.request, command };
|
|
39
|
+
if (approvedBy === input.request.by) return { status: "refused", digest, request: input.request, by: approvedBy, reason: "the requester's own approval does not count", command };
|
|
40
|
+
return { status: "approved", digest, request: input.request, approval: { version: 1, kind: "resolution", op: input.op, gate: input.request.subject, planDigest: digest, resolvedBy: approvedBy, timestamp: "2026-11-08T10:00:00Z" } as never };
|
|
41
|
+
},
|
|
42
|
+
async spendGatedRequest(input) {
|
|
43
|
+
spent.add(input.digest);
|
|
44
|
+
writes.push("applied");
|
|
45
|
+
return { version: 1, kind: "applied", op: input.op, gate: input.gate, planDigest: input.digest, approvedAt: "", approvedBy: "", timestamp: "2026-11-08T11:00:00Z" } as never;
|
|
46
|
+
},
|
|
47
|
+
async finishGatedRequest(input) {
|
|
48
|
+
writes.push(`finished:${input.result}`);
|
|
49
|
+
details.push(input.detail ?? {});
|
|
50
|
+
return { record: {} as never, pushed: true };
|
|
51
|
+
},
|
|
52
|
+
async readGatedRequests(op) {
|
|
53
|
+
return [...asked.entries()]
|
|
54
|
+
.filter(([, a]) => a.op === op)
|
|
55
|
+
.map(([digest, a]) => ({ op, gate: a.request.subject, digest, request: a.request, pending: { expiresAt: a.expiresAt } as never, approvals: [], ...(spent.has(digest) ? { applied: {} as never } : {}) }));
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent guard (#4770), proven against both products' words and rules:
|
|
3
|
+
* terragucci's tests (its test/guard.test.ts) and plywood's ("the agent
|
|
4
|
+
* guard" in its test/agents.test.ts), ported to the shared code, and the
|
|
5
|
+
* cases where the two disagreed, settled on the stricter answer.
|
|
6
|
+
*/
|
|
7
|
+
import { spawnSync } from "node:child_process";
|
|
8
|
+
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { describe, expect, it, onTestFinished } from "vitest";
|
|
12
|
+
import { AGENT_GUARD_FLOOR, agentGuard, CLAUDE_SETTINGS_LOCAL, CLAUDE_SETTINGS_SHARED, installAgentGuard, withAgentGuardHook } from "./agent-guard";
|
|
13
|
+
import { NEXT_RELEASE } from "./release-floors";
|
|
14
|
+
import { PLYWOOD_GUARD } from "./__fixtures__/agent-guard/plywood";
|
|
15
|
+
import { TERRAGUCCI_GUARD } from "./__fixtures__/agent-guard/terragucci";
|
|
16
|
+
|
|
17
|
+
const tmp = (): string => mkdtempSync(join(tmpdir(), "chant-guard-"));
|
|
18
|
+
const git = (repo: string, ...args: string[]): string => {
|
|
19
|
+
const r = spawnSync("git", ["-C", repo, ...args], { encoding: "utf-8" });
|
|
20
|
+
if (r.status !== 0) throw new Error(r.stderr);
|
|
21
|
+
return r.stdout;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** Stand-ins for the real binaries, logging their calls. */
|
|
25
|
+
function fakeBinaries(names: string[]): { dir: string; log: string } {
|
|
26
|
+
const dir = tmp();
|
|
27
|
+
const log = join(dir, "calls.log");
|
|
28
|
+
for (const b of names) {
|
|
29
|
+
writeFileSync(join(dir, b), `#!/bin/sh\necho "${b} $*" >> ${JSON.stringify(log)}\nexit 0\n`);
|
|
30
|
+
chmodSync(join(dir, b), 0o755);
|
|
31
|
+
}
|
|
32
|
+
return { dir, log };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function runner(bin: string, real: { dir: string; log: string }, env: NodeJS.ProcessEnv = process.env) {
|
|
36
|
+
return (name: string, args: string[], cwd: string) => {
|
|
37
|
+
const r = spawnSync(join(bin, name), args, { cwd, encoding: "utf-8", env: { ...env, PATH: `${bin}:${real.dir}:${process.env.PATH ?? ""}` } });
|
|
38
|
+
return { status: r.status, stderr: r.stderr, ran: existsSync(real.log) ? readFileSync(real.log, "utf-8") : "" };
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function hookOf(script: string) {
|
|
43
|
+
return (command: string, cwd: string, env: NodeJS.ProcessEnv = process.env) => {
|
|
44
|
+
const r = spawnSync(process.execPath, [script, "hook"], { input: JSON.stringify({ tool_name: "Bash", tool_input: { command }, cwd }), encoding: "utf-8", env });
|
|
45
|
+
return { status: r.status, stderr: r.stderr };
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const ROOTS = ["envs/dev/app", "envs/prod/app"];
|
|
50
|
+
|
|
51
|
+
/** terragucci's fixture: a repo with two pipeline roots and a directory that is not one, the guard written locally. */
|
|
52
|
+
function terragucci() {
|
|
53
|
+
const repo = tmp();
|
|
54
|
+
for (const d of [...ROOTS, "modules/net"]) mkdirSync(join(repo, d), { recursive: true });
|
|
55
|
+
writeFileSync(join(repo, "README.md"), "x\n");
|
|
56
|
+
git(repo, "init", "-q", "-b", "main");
|
|
57
|
+
const real = fakeBinaries(Object.keys(TERRAGUCCI_GUARD.rules.engines));
|
|
58
|
+
const g = installAgentGuard({ ...TERRAGUCCI_GUARD, repo, install: "local", roots: ROOTS });
|
|
59
|
+
return { repo, g, run: runner(g.bin, real), hook: hookOf(g.script) };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
describe("AGENT_GUARD_FLOOR", () => {
|
|
63
|
+
it("names the next release", () => expect(AGENT_GUARD_FLOOR).toBe(NEXT_RELEASE));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe("terragucci's guard: the wrappers", () => {
|
|
67
|
+
it("writes the wrappers and the script under the git directory, out of commits", () => {
|
|
68
|
+
const { repo, g } = terragucci();
|
|
69
|
+
const gitDir = git(repo, "rev-parse", "--absolute-git-dir").trim();
|
|
70
|
+
expect(g.script).toBe(join(gitDir, "terragucci", "guard", "guard.mjs"));
|
|
71
|
+
for (const b of ["tofu", "terraform", "terragrunt", "choudoufu"]) expect(existsSync(join(g.bin, b))).toBe(true);
|
|
72
|
+
expect(g.path).toBe(`export PATH="${g.bin}:$PATH"`);
|
|
73
|
+
expect(git(repo, "status", "--porcelain", "--untracked-files=all")).not.toMatch(/terragucci\/guard|settings\.local\.json/);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("refuses apply, destroy and -auto-approve in a pipeline root, naming propose, and runs nothing", () => {
|
|
77
|
+
const { repo, run } = terragucci();
|
|
78
|
+
const root = join(repo, "envs/prod/app");
|
|
79
|
+
for (const args of [["apply"], ["destroy"], ["apply", "-auto-approve"], ["plan", "-auto-approve"], ["import", "aws_s3_bucket.b", "b"], ["state", "rm", "aws_s3_bucket.b"], ["state", "mv", "a", "b"], ["state", "push", "x.tfstate"]]) {
|
|
80
|
+
const r = run("tofu", args, root);
|
|
81
|
+
expect(r.status, args.join(" ")).toBe(3);
|
|
82
|
+
expect(r.stderr).toContain("terragucci guard: tofu");
|
|
83
|
+
expect(r.stderr).toContain("in envs/prod/app, a root of this repo's pipeline");
|
|
84
|
+
expect(r.stderr).toContain("the propose tool of terragucci mcp --propose");
|
|
85
|
+
}
|
|
86
|
+
expect(run("terraform", ["-chdir=envs/dev/app", "apply"], repo).status).not.toBe(0);
|
|
87
|
+
expect(run("tofu", ["-chdir", "envs/dev/app", "destroy"], repo).status).not.toBe(0);
|
|
88
|
+
expect(run("tofu", ["apply"], root).ran).toBe("");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("passes init, validate, fmt, plan and state reads through to the real binary in a root", () => {
|
|
92
|
+
const { repo, run } = terragucci();
|
|
93
|
+
const root = join(repo, "envs/prod/app");
|
|
94
|
+
for (const args of [["init"], ["validate"], ["fmt", "-check"], ["plan", "-out=tfplan"], ["state", "list"], ["show", "-json"]]) expect(run("tofu", args, root).status, args.join(" ")).toBe(0);
|
|
95
|
+
expect(run("tofu", ["version"], root).ran).toBe(["tofu init", "tofu validate", "tofu fmt -check", "tofu plan -out=tfplan", "tofu state list", "tofu show -json", "tofu version", ""].join("\n"));
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("passes every command through outside the pipeline's roots", () => {
|
|
99
|
+
const { repo, run } = terragucci();
|
|
100
|
+
for (const cwd of [repo, join(repo, "modules/net"), tmp()]) {
|
|
101
|
+
expect(run("tofu", ["apply", "-auto-approve"], cwd).status, cwd).toBe(0);
|
|
102
|
+
expect(run("terraform", ["destroy"], cwd).status, cwd).toBe(0);
|
|
103
|
+
}
|
|
104
|
+
expect(run("tofu", ["version"], repo).ran.split("\n").filter(Boolean)).toHaveLength(7);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("refuses terragrunt's --all and run-all apply from a directory that holds a root, and its plan", () => {
|
|
108
|
+
const { repo, run } = terragucci();
|
|
109
|
+
expect(run("terragrunt", ["run", "--all", "apply"], join(repo, "envs")).status).not.toBe(0);
|
|
110
|
+
expect(run("terragrunt", ["run-all", "destroy"], repo).status).not.toBe(0);
|
|
111
|
+
expect(run("terragrunt", ["apply"], join(repo, "envs/dev/app")).status).not.toBe(0);
|
|
112
|
+
expect(run("terragrunt", ["run", "--all", "plan"], join(repo, "envs")).status).toBe(0);
|
|
113
|
+
// Without --all, terragrunt applies the directory it runs in, which is no root here.
|
|
114
|
+
expect(run("terragrunt", ["apply"], join(repo, "envs")).status).toBe(0);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("terragucci's guard: the hook", () => {
|
|
119
|
+
it("writes a PreToolUse hook into Claude Code's local settings, kept out of commits, and keeps what else the file holds", () => {
|
|
120
|
+
const saved = { GIT_CONFIG_GLOBAL: process.env.GIT_CONFIG_GLOBAL, XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME };
|
|
121
|
+
process.env.GIT_CONFIG_GLOBAL = "/dev/null";
|
|
122
|
+
process.env.XDG_CONFIG_HOME = tmp();
|
|
123
|
+
onTestFinished(() => {
|
|
124
|
+
for (const [k, v] of Object.entries(saved)) if (v === undefined) delete process.env[k];
|
|
125
|
+
else process.env[k] = v;
|
|
126
|
+
});
|
|
127
|
+
const { repo, g } = terragucci();
|
|
128
|
+
const settings = JSON.parse(readFileSync(join(repo, CLAUDE_SETTINGS_LOCAL), "utf-8"));
|
|
129
|
+
expect(settings.hooks.PreToolUse).toEqual([{ matcher: "Bash", hooks: [{ type: "command", command: `"${process.execPath}" "${g.script}" hook` }] }]);
|
|
130
|
+
expect(readFileSync(join(repo, ".git/info/exclude"), "utf-8")).toContain(`/${CLAUDE_SETTINGS_LOCAL}`);
|
|
131
|
+
writeFileSync(join(repo, CLAUDE_SETTINGS_LOCAL), JSON.stringify({ ...settings, permissions: { allow: ["Bash(ls)"] } }));
|
|
132
|
+
installAgentGuard({ ...TERRAGUCCI_GUARD, repo, install: "local", roots: ROOTS });
|
|
133
|
+
const again = JSON.parse(readFileSync(join(repo, CLAUDE_SETTINGS_LOCAL), "utf-8"));
|
|
134
|
+
expect(again.permissions).toEqual({ allow: ["Bash(ls)"] });
|
|
135
|
+
expect(again.hooks.PreToolUse).toHaveLength(1);
|
|
136
|
+
expect(readFileSync(join(repo, ".git/info/exclude"), "utf-8").split(`/${CLAUDE_SETTINGS_LOCAL}`)).toHaveLength(2);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("blocks the same commands in the agent's Bash tool with exit 2, following cd and sh -c", () => {
|
|
140
|
+
const { repo, hook } = terragucci();
|
|
141
|
+
for (const [command, cwd] of [
|
|
142
|
+
["cd envs/prod/app && tofu apply", repo],
|
|
143
|
+
["tofu -chdir=envs/dev/app destroy", repo],
|
|
144
|
+
["AWS_PROFILE=prod terraform apply -auto-approve", join(repo, "envs/prod/app")],
|
|
145
|
+
["bash -c 'cd envs/dev/app; tofu apply'", repo],
|
|
146
|
+
["/usr/local/bin/tofu state rm aws_s3_bucket.b", join(repo, "envs/dev/app")],
|
|
147
|
+
["npx terragucci approve wave-1 --plan sha256:abc", repo],
|
|
148
|
+
["npx -y @intentius/terragucci override envs/prod/app --rule r --reason x", repo],
|
|
149
|
+
["npx @intentius/terragucci@0.4.35 pr-merge --pr 7 --sha abc", repo],
|
|
150
|
+
["terragucci stage tf-apply --wave 1 --layers envs/prod/app", repo],
|
|
151
|
+
["npx terragucci rollout net --mode apply", repo],
|
|
152
|
+
] as const) {
|
|
153
|
+
const r = hook(command, cwd);
|
|
154
|
+
expect(r.status, command).toBe(2);
|
|
155
|
+
expect(r.stderr).toContain("propose");
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("lets plans, reads and everything outside the repo's roots through", () => {
|
|
160
|
+
const { repo, g, hook } = terragucci();
|
|
161
|
+
for (const [command, cwd] of [
|
|
162
|
+
["cd envs/prod/app && tofu init && tofu plan -out=p", repo],
|
|
163
|
+
["tofu apply", repo],
|
|
164
|
+
["tofu apply -auto-approve", tmp()],
|
|
165
|
+
["git commit -m 'apply the fix'", repo],
|
|
166
|
+
["echo apply", join(repo, "envs/prod/app")],
|
|
167
|
+
["npx terragucci plan --json", repo],
|
|
168
|
+
["npx terragucci approve wave-1", tmp()],
|
|
169
|
+
] as const) expect(hook(command, cwd).status, command).toBe(0);
|
|
170
|
+
expect(spawnSync(process.execPath, [g.script, "hook"], { input: "not json", encoding: "utf-8" }).status).toBe(0);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("refuses to rewrite a settings file that is not JSON, or not an object", () => {
|
|
174
|
+
expect(() => withAgentGuardHook("{ nope", "x", CLAUDE_SETTINGS_LOCAL)).toThrow(/is not JSON/);
|
|
175
|
+
expect(() => withAgentGuardHook("[]", "x", CLAUDE_SETTINGS_LOCAL)).toThrow(/not a JSON object/);
|
|
176
|
+
class ConfigError extends Error {}
|
|
177
|
+
expect(() => withAgentGuardHook("{ nope", "x", CLAUDE_SETTINGS_LOCAL, [], (m) => new ConfigError(m))).toThrow(ConfigError);
|
|
178
|
+
expect(JSON.parse(withAgentGuardHook(undefined, "x", CLAUDE_SETTINGS_LOCAL)).hooks.PreToolUse[0].hooks[0].command).toBe("x");
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("says it needs a git checkout for a local install", () => {
|
|
182
|
+
expect(() => installAgentGuard({ ...TERRAGUCCI_GUARD, repo: tmp(), install: "local", roots: [] })).toThrow(/terragucci guard runs in a git checkout/);
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("plywood's guard", () => {
|
|
187
|
+
const refused = ["pulumi up", "pulumi up --yes --stack dev", "cd shop && pulumi -C infra up", "npx plunge up", "FOO=1 pulumi destroy -y", "/usr/local/bin/pulumi update", "env -i pulumi up"];
|
|
188
|
+
const allowed = ["pulumi preview", "pulumi stack ls", "plunge drift --resource urn:x", "git commit -m 'pulumi up later'", "plywood preview", "echo pulumi"];
|
|
189
|
+
|
|
190
|
+
/** plywood's fixture: the guard written into a project's `.claude/`, refused anywhere. */
|
|
191
|
+
function plywood() {
|
|
192
|
+
const repo = tmp();
|
|
193
|
+
mkdirSync(join(repo, ".claude"));
|
|
194
|
+
writeFileSync(join(repo, CLAUDE_SETTINGS_SHARED), JSON.stringify({ permissions: { allow: ["Bash(ls)"] } }));
|
|
195
|
+
const g = installAgentGuard({ ...PLYWOOD_GUARD, repo, install: "project" });
|
|
196
|
+
return { repo, g, hook: hookOf(g.script) };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
it("refuses an update or destroy, and lets every read through", () => {
|
|
200
|
+
const guard = agentGuard(PLYWOOD_GUARD, { repo: tmp() });
|
|
201
|
+
for (const line of refused) expect(guard.checkCommand(line, guard.repo), line).toBeDefined();
|
|
202
|
+
for (const line of allowed) expect(guard.checkCommand(line, guard.repo), line).toBeUndefined();
|
|
203
|
+
expect(guard.checkCommand("pulumi up", guard.repo)).toMatchObject({ bin: "pulumi", command: "up", message: "plywood guard: an agent session does not run 'pulumi up'. Open a pull request; plywood previews it, and the update runs after a person approves the digest." });
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("gives the same answers in the hook it writes, from any directory", () => {
|
|
207
|
+
const { repo, hook } = plywood();
|
|
208
|
+
for (const line of [...refused, ...allowed]) for (const cwd of [repo, tmp()]) expect(hook(line, cwd).status === 2, `${line} in ${cwd}`).toBe(refused.includes(line));
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it("blocks pulumi up in a fixture Claude Code session (exit 2), with the hook as plywood's settings name it", () => {
|
|
212
|
+
const { repo, g } = plywood();
|
|
213
|
+
const settings = JSON.parse(readFileSync(join(repo, CLAUDE_SETTINGS_SHARED), "utf-8"));
|
|
214
|
+
const command = settings.hooks.PreToolUse[0].hooks[0].command as string;
|
|
215
|
+
expect(command).toBe('node "$CLAUDE_PROJECT_DIR/.claude/hooks/plywood-guard.mjs" hook');
|
|
216
|
+
expect(g.written).toEqual([".claude/hooks/plywood-guard.mjs", ".plywood/agent-bin/pulumi", ".plywood/agent-bin/plunge", ".claude/settings.json"]);
|
|
217
|
+
const call = (c: string) => spawnSync("sh", ["-c", command], { input: JSON.stringify({ tool_name: "Bash", tool_input: { command: c } }), encoding: "utf-8", env: { ...process.env, CLAUDE_PROJECT_DIR: repo } });
|
|
218
|
+
const blocked = call("pulumi up --yes");
|
|
219
|
+
expect(blocked.status).toBe(2);
|
|
220
|
+
expect(blocked.stderr).toContain("pull request");
|
|
221
|
+
expect(call("pulumi preview").status).toBe(0);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it("installs the hook once into .claude/settings.json and keeps the rest", () => {
|
|
225
|
+
const { repo } = plywood();
|
|
226
|
+
installAgentGuard({ ...PLYWOOD_GUARD, repo, install: "project" });
|
|
227
|
+
const settings = JSON.parse(readFileSync(join(repo, CLAUDE_SETTINGS_SHARED), "utf-8"));
|
|
228
|
+
expect(settings.permissions).toEqual({ allow: ["Bash(ls)"] });
|
|
229
|
+
expect(settings.hooks.PreToolUse).toHaveLength(1);
|
|
230
|
+
expect(JSON.parse(withAgentGuardHook(JSON.stringify(settings), settings.hooks.PreToolUse[0].hooks[0].command, CLAUDE_SETTINGS_SHARED))).toEqual(settings);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("replaces the hook an earlier plywood guard install wrote", () => {
|
|
234
|
+
const old = { hooks: { PreToolUse: [{ matcher: "Bash", hooks: [{ type: "command", command: 'node "$CLAUDE_PROJECT_DIR/.claude/hooks/plywood-guard.mjs"' }] }] } };
|
|
235
|
+
const next = JSON.parse(withAgentGuardHook(JSON.stringify(old), 'node "$CLAUDE_PROJECT_DIR/.claude/hooks/plywood-guard.mjs" hook', CLAUDE_SETTINGS_SHARED, ["plywood-guard.mjs"]));
|
|
236
|
+
expect(next.hooks.PreToolUse).toEqual([{ matcher: "Bash", hooks: [{ type: "command", command: 'node "$CLAUDE_PROJECT_DIR/.claude/hooks/plywood-guard.mjs" hook' }] }]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("writes wrappers that refuse with plywood's exit code, without PLYWOOD_AGENT_GUARD set, and run the real pulumi for the rest", () => {
|
|
240
|
+
const { repo, g } = plywood();
|
|
241
|
+
const real = fakeBinaries(["pulumi", "plunge"]);
|
|
242
|
+
const env = { ...process.env };
|
|
243
|
+
delete env.PLYWOOD_AGENT_GUARD;
|
|
244
|
+
delete env.CLAUDECODE;
|
|
245
|
+
const run = runner(g.bin, real, env);
|
|
246
|
+
const up = run("pulumi", ["--stack", "dev", "up", "--yes"], repo);
|
|
247
|
+
expect(up.status).toBe(1);
|
|
248
|
+
expect(up.stderr).toContain("plywood guard: an agent session does not run 'pulumi up'");
|
|
249
|
+
expect(run("plunge", ["destroy"], tmp()).status).toBe(1);
|
|
250
|
+
expect(run("pulumi", ["preview"], repo).status).toBe(0);
|
|
251
|
+
expect(run("pulumi", ["stack", "ls"], repo).ran).toBe("pulumi preview\npulumi stack ls\n");
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe("the script", () => {
|
|
256
|
+
it("runs when chant runs from source under tsx, whose keepNames wraps the rule's functions in __name", () => {
|
|
257
|
+
const repo = tmp();
|
|
258
|
+
const code = `import { installAgentGuard } from ${JSON.stringify(join(__dirname, "agent-guard.ts"))}; import { PLYWOOD_GUARD } from ${JSON.stringify(join(__dirname, "__fixtures__/agent-guard/plywood.ts"))}; installAgentGuard({ ...PLYWOOD_GUARD, repo: ${JSON.stringify(repo)}, install: "project" });`;
|
|
259
|
+
const w = spawnSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", code], { encoding: "utf-8" });
|
|
260
|
+
expect(w.status, w.stderr).toBe(0);
|
|
261
|
+
const script = join(repo, ".claude/hooks/plywood-guard.mjs");
|
|
262
|
+
expect(readFileSync(script, "utf-8")).toContain("__name(");
|
|
263
|
+
expect(hookOf(script)("pulumi up", repo).status).toBe(2);
|
|
264
|
+
expect(hookOf(script)("pulumi preview", repo).status).toBe(0);
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe("where the two guards disagreed: the stricter answer", () => {
|
|
269
|
+
const ply = () => agentGuard(PLYWOOD_GUARD, { repo: tmp() });
|
|
270
|
+
const tg = () => {
|
|
271
|
+
const repo = tmp();
|
|
272
|
+
for (const d of ROOTS) mkdirSync(join(repo, d), { recursive: true });
|
|
273
|
+
return { repo, guard: agentGuard(TERRAGUCCI_GUARD, { repo, roots: ROOTS }) };
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
it("follows sh -c, bash -lc and eval, which plywood's guard let through", () => {
|
|
277
|
+
const g = ply();
|
|
278
|
+
for (const line of ["bash -c 'pulumi up'", "sh -c \"cd x && pulumi destroy\"", "bash -lc 'pulumi up'", "eval pulumi up", "eval 'pulumi up --yes'"]) expect(g.checkCommand(line, g.repo), line).toBeDefined();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("skips a launcher's flags, which terragucci's guard read as the command", () => {
|
|
282
|
+
const { repo, guard } = tg();
|
|
283
|
+
for (const line of ["env -i tofu apply", "env -u AWS_PROFILE tofu apply", "sudo -u ops tofu destroy", "nice -n 5 terraform apply", "xargs tofu apply", "command -p tofu apply"]) expect(guard.checkCommand(line, join(repo, "envs/dev/app")), line).toBeDefined();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("finds the command after npx's flags, which plywood's guard did not", () => {
|
|
287
|
+
const g = ply();
|
|
288
|
+
for (const line of ["npx -y plunge up", "npx --yes pulumi destroy", "npx -p @pulumi/cli pulumi up", "bunx plunge up"]) expect(g.checkCommand(line, g.repo), line).toBeDefined();
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it("skips a flag's value before the subcommand, which terragucci's guard read as the subcommand", () => {
|
|
292
|
+
const g = ply();
|
|
293
|
+
expect(g.checkCommand("pulumi --stack dev up", g.repo)).toBeDefined();
|
|
294
|
+
expect(g.checkCommand("pulumi -C infra destroy", g.repo)).toBeDefined();
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("refuses the product's own applying commands, which plywood's guard let through", () => {
|
|
298
|
+
const g = ply();
|
|
299
|
+
for (const line of ["plywood up", "npx @intentius/plywood approve up --plan sha256:x", "plywood override dev --rule r --reason x", "plywood resume --wave 1", "plywood drift --respond up", "plywood cancel dev"]) expect(g.checkCommand(line, g.repo), line).toBeDefined();
|
|
300
|
+
for (const line of ["plywood preview", "plywood drift --respond issue", "plywood guard check pulumi up", "plywood audit"]) expect(g.checkCommand(line, g.repo), line).toBeUndefined();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("reads a quoted separator as text, not a second command", () => {
|
|
304
|
+
const g = ply();
|
|
305
|
+
expect(g.checkCommand('echo "done; pulumi up later"', g.repo)).toBeUndefined();
|
|
306
|
+
expect(g.checkCommand("echo ok; pulumi up", g.repo)).toBeDefined();
|
|
307
|
+
expect(g.checkCommand("echo $(pulumi up)", g.repo)).toBeDefined();
|
|
308
|
+
expect(g.checkCommand("echo `pulumi destroy`", g.repo)).toBeDefined();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("keeps roots a product rule: refused anywhere without them, only in a root with them, nowhere with none", () => {
|
|
312
|
+
const { repo, guard } = tg();
|
|
313
|
+
expect(guard.checkCommand("tofu apply", repo)).toBeUndefined();
|
|
314
|
+
expect(guard.checkCommand("tofu apply", join(repo, "envs/dev/app"))).toMatchObject({ where: "envs/dev/app" });
|
|
315
|
+
const anywhere = agentGuard(TERRAGUCCI_GUARD, { repo });
|
|
316
|
+
expect(anywhere.checkCommand("tofu apply", tmp())).toMatchObject({ what: "tofu apply" });
|
|
317
|
+
expect(anywhere.checkCommand("tofu apply", tmp())!.message).not.toContain(" in ");
|
|
318
|
+
expect(agentGuard(TERRAGUCCI_GUARD, { repo, roots: [] }).checkCommand("tofu apply", join(repo, "envs/dev/app"))).toBeUndefined();
|
|
319
|
+
});
|
|
320
|
+
});
|