@intentius/chant 0.124.76 → 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 +5 -1
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +17 -5
- 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.ts +15 -5
- 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
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The policy engine's binary (#4774): conftest or OPA at a pinned release,
|
|
3
|
+
* fetched once, checked against the sha256 chant records for it, and kept in
|
|
4
|
+
* a cache directory.
|
|
5
|
+
*
|
|
6
|
+
* `ci.policy.install: true` makes the pull-request jobs and Op waves use it
|
|
7
|
+
* (`./components/pr-policy.ts`). A product that runs the engine itself calls
|
|
8
|
+
* {@link policyEngineBinary} with its own `download`, `cache` and `exec`, so
|
|
9
|
+
* it keeps no download code. Tests pass `download`, so nothing reaches the
|
|
10
|
+
* network.
|
|
11
|
+
*
|
|
12
|
+
* The digests are the release's own: conftest's `checksums.txt` and OPA's
|
|
13
|
+
* `.sha256` files, checked against the archives themselves when the pins
|
|
14
|
+
* were set. A download that does not match its pin is never written to the
|
|
15
|
+
* cache. A caller pinning another release passes its `version` and the
|
|
16
|
+
* sha256 for the platform; chant fetches nothing without a pin.
|
|
17
|
+
*
|
|
18
|
+
* Like `./gate-policy.ts` this module imports only node built-ins (and the
|
|
19
|
+
* release-floor constant), so a product can bundle it.
|
|
20
|
+
*/
|
|
21
|
+
/** The first chant with `ci.policy.install` and `@intentius/chant/policy-engine` (#4774). Products feature-detect on it. */
|
|
22
|
+
export declare const POLICY_ENGINE_INSTALL_FLOOR = "0.124.77";
|
|
23
|
+
export type PolicyEngineName = "conftest" | "opa";
|
|
24
|
+
/** The platforms chant has pins for. */
|
|
25
|
+
export type PolicyEnginePlatform = "linux-x64" | "linux-arm64" | "darwin-x64" | "darwin-arm64";
|
|
26
|
+
export declare const POLICY_ENGINE_PLATFORMS: readonly PolicyEnginePlatform[];
|
|
27
|
+
/** The conftest release chant installs. */
|
|
28
|
+
export declare const CONFTEST_VERSION = "0.71.1";
|
|
29
|
+
/** The OPA release chant installs. */
|
|
30
|
+
export declare const OPA_VERSION = "1.21.4";
|
|
31
|
+
/**
|
|
32
|
+
* The sha256 of each pinned release file, as `<engine>-<version>-<platform>`,
|
|
33
|
+
* copied from the release's checksums (conftest's `checksums.txt`, OPA's
|
|
34
|
+
* `<asset>.sha256`) and checked against the files. The files are conftest's
|
|
35
|
+
* `conftest_<v>_<OS>_<arch>.tar.gz` and OPA's static builds, except on
|
|
36
|
+
* darwin-x64, where OPA builds none and the file is `opa_darwin_amd64`. Add
|
|
37
|
+
* a release's lines when moving a default version.
|
|
38
|
+
*/
|
|
39
|
+
export declare const POLICY_ENGINE_SHA256: Readonly<Record<string, string>>;
|
|
40
|
+
/** One release file of one engine for one platform. */
|
|
41
|
+
export interface PolicyEngineRelease {
|
|
42
|
+
engine: PolicyEngineName;
|
|
43
|
+
version: string;
|
|
44
|
+
platform: PolicyEnginePlatform;
|
|
45
|
+
/** The release file's name. OPA ships a bare binary, conftest a `.tar.gz` holding `conftest`. */
|
|
46
|
+
asset: string;
|
|
47
|
+
url: string;
|
|
48
|
+
/** Whether the file is a `.tar.gz` to unpack. */
|
|
49
|
+
archive: boolean;
|
|
50
|
+
}
|
|
51
|
+
/** The release file of `engine` at `version` for `platform`. A version is a plain semver. */
|
|
52
|
+
export declare function policyEngineRelease(engine: PolicyEngineName, platform: PolicyEnginePlatform, version?: string): PolicyEngineRelease;
|
|
53
|
+
/** The sha256 chant records for a release file, or undefined when it records none. */
|
|
54
|
+
export declare function pinnedPolicyEngineSha256(release: PolicyEngineRelease): string | undefined;
|
|
55
|
+
/** This process's platform as chant pins it. Throws on one with no pin. */
|
|
56
|
+
export declare function policyEnginePlatform(platform?: string, arch?: string): PolicyEnginePlatform;
|
|
57
|
+
/** Where installed engines are kept by default: `CHANT_TOOL_CACHE`, else `chant-tools` in the temp directory. */
|
|
58
|
+
export declare function defaultPolicyEngineCache(): string;
|
|
59
|
+
export interface InstallPolicyEngineOptions {
|
|
60
|
+
/** Fetches a release file. Default: Node's fetch. Tests pass their own. */
|
|
61
|
+
download?: (url: string) => Promise<Uint8Array>;
|
|
62
|
+
/** Where installed engines are kept. Default {@link defaultPolicyEngineCache}. */
|
|
63
|
+
cache?: string;
|
|
64
|
+
/** The platform to fetch for. Default this process's ({@link policyEnginePlatform}). */
|
|
65
|
+
platform?: PolicyEnginePlatform;
|
|
66
|
+
/** Another release than chant's default. Needs `sha256` for the platform. */
|
|
67
|
+
version?: string;
|
|
68
|
+
/** The release file's sha256 by platform, in place of {@link POLICY_ENGINE_SHA256}. */
|
|
69
|
+
sha256?: Partial<Record<PolicyEnginePlatform, string>>;
|
|
70
|
+
}
|
|
71
|
+
/** The bytes of the regular file `name` (at the top, or under `./`) in a tar stream. Undefined when it holds none. */
|
|
72
|
+
export declare function tarEntry(tar: Uint8Array, name: string): Uint8Array | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Install `engine` into the cache and return the binary's path. A binary
|
|
75
|
+
* already installed there for that release and platform is returned without
|
|
76
|
+
* a download. Throws when the platform has no pin, the download fails, or
|
|
77
|
+
* the file does not match its pin.
|
|
78
|
+
*/
|
|
79
|
+
export declare function installPolicyEngine(engine: PolicyEngineName, options?: InstallPolicyEngineOptions): Promise<string>;
|
|
80
|
+
/** Whether the engine on PATH answers. Default: runs `conftest --version` or `opa version`. */
|
|
81
|
+
export type PolicyEngineProbe = (engine: PolicyEngineName) => boolean;
|
|
82
|
+
export declare const defaultPolicyEngineProbe: PolicyEngineProbe;
|
|
83
|
+
export interface PolicyEngineBinaryOptions extends InstallPolicyEngineOptions {
|
|
84
|
+
/** Checks the engine on PATH. Default {@link defaultPolicyEngineProbe}. */
|
|
85
|
+
probe?: PolicyEngineProbe;
|
|
86
|
+
/** Skip the PATH and always use the pinned release. Default false. */
|
|
87
|
+
pinnedOnly?: boolean;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The engine's executable: its name when the one on PATH answers (an image
|
|
91
|
+
* that carries the engine pays nothing), else the pinned release installed
|
|
92
|
+
* by {@link installPolicyEngine}.
|
|
93
|
+
*/
|
|
94
|
+
export declare function policyEngineBinary(engine: PolicyEngineName, options?: PolicyEngineBinaryOptions): Promise<string>;
|
|
95
|
+
//# sourceMappingURL=policy-engine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy-engine.d.ts","sourceRoot":"","sources":["../src/policy-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAUH,4HAA4H;AAC5H,eAAO,MAAM,2BAA2B,aAAe,CAAC;AAExD,MAAM,MAAM,gBAAgB,GAAG,UAAU,GAAG,KAAK,CAAC;AAElD,wCAAwC;AACxC,MAAM,MAAM,oBAAoB,GAAG,WAAW,GAAG,aAAa,GAAG,YAAY,GAAG,cAAc,CAAC;AAC/F,eAAO,MAAM,uBAAuB,EAAE,SAAS,oBAAoB,EAA+D,CAAC;AAEnI,2CAA2C;AAC3C,eAAO,MAAM,gBAAgB,WAAW,CAAC;AACzC,sCAAsC;AACtC,eAAO,MAAM,WAAW,WAAW,CAAC;AAEpC;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CASjE,CAAC;AAEF,uDAAuD;AACvD,MAAM,WAAW,mBAAmB;IACl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AAC,CAqB7H;AAED,+FAA+F;AAC/F,MAAM,MAAM,iBAAiB,GAAG,CAAC,MAAM,EAAE,gBAAgB,KAAK,OAAO,CAAC;AAEtE,eAAO,MAAM,wBAAwB,EAAE,iBAGtC,CAAC;AAEF,MAAM,WAAW,yBAA0B,SAAQ,0BAA0B;IAC3E,2EAA2E;IAC3E,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,sEAAsE;IACtE,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,gBAAgB,EAAE,OAAO,GAAE,yBAA8B,GAAG,OAAO,CAAC,MAAM,CAAC,CAG3H"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The policy engine's binary (#4774): conftest or OPA at a pinned release,
|
|
3
|
+
* fetched once, checked against the sha256 chant records for it, and kept in
|
|
4
|
+
* a cache directory.
|
|
5
|
+
*
|
|
6
|
+
* `ci.policy.install: true` makes the pull-request jobs and Op waves use it
|
|
7
|
+
* (`./components/pr-policy.ts`). A product that runs the engine itself calls
|
|
8
|
+
* {@link policyEngineBinary} with its own `download`, `cache` and `exec`, so
|
|
9
|
+
* it keeps no download code. Tests pass `download`, so nothing reaches the
|
|
10
|
+
* network.
|
|
11
|
+
*
|
|
12
|
+
* The digests are the release's own: conftest's `checksums.txt` and OPA's
|
|
13
|
+
* `.sha256` files, checked against the archives themselves when the pins
|
|
14
|
+
* were set. A download that does not match its pin is never written to the
|
|
15
|
+
* cache. A caller pinning another release passes its `version` and the
|
|
16
|
+
* sha256 for the platform; chant fetches nothing without a pin.
|
|
17
|
+
*
|
|
18
|
+
* Like `./gate-policy.ts` this module imports only node built-ins (and the
|
|
19
|
+
* release-floor constant), so a product can bundle it.
|
|
20
|
+
*/
|
|
21
|
+
import { spawnSync } from "node:child_process";
|
|
22
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
23
|
+
import { chmodSync, existsSync, mkdirSync, renameSync, writeFileSync } from "node:fs";
|
|
24
|
+
import { tmpdir } from "node:os";
|
|
25
|
+
import { join } from "node:path";
|
|
26
|
+
import { gunzipSync } from "node:zlib";
|
|
27
|
+
import { NEXT_RELEASE } from "./release-floors.js";
|
|
28
|
+
/** The first chant with `ci.policy.install` and `@intentius/chant/policy-engine` (#4774). Products feature-detect on it. */
|
|
29
|
+
export const POLICY_ENGINE_INSTALL_FLOOR = NEXT_RELEASE;
|
|
30
|
+
export const POLICY_ENGINE_PLATFORMS = ["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64"];
|
|
31
|
+
/** The conftest release chant installs. */
|
|
32
|
+
export const CONFTEST_VERSION = "0.71.1";
|
|
33
|
+
/** The OPA release chant installs. */
|
|
34
|
+
export const OPA_VERSION = "1.21.4";
|
|
35
|
+
/**
|
|
36
|
+
* The sha256 of each pinned release file, as `<engine>-<version>-<platform>`,
|
|
37
|
+
* copied from the release's checksums (conftest's `checksums.txt`, OPA's
|
|
38
|
+
* `<asset>.sha256`) and checked against the files. The files are conftest's
|
|
39
|
+
* `conftest_<v>_<OS>_<arch>.tar.gz` and OPA's static builds, except on
|
|
40
|
+
* darwin-x64, where OPA builds none and the file is `opa_darwin_amd64`. Add
|
|
41
|
+
* a release's lines when moving a default version.
|
|
42
|
+
*/
|
|
43
|
+
export const POLICY_ENGINE_SHA256 = {
|
|
44
|
+
"conftest-0.71.1-linux-x64": "c3f6b2a753bd56e377a1e51a95ae3e057f926f2e9139f7d5b5dfbbaf810f7985",
|
|
45
|
+
"conftest-0.71.1-linux-arm64": "218c682e110ae7e826dc14a746a6f9b43eb782643dbe730b771fd305634e3f47",
|
|
46
|
+
"conftest-0.71.1-darwin-x64": "a309853b5763c18486768157442d440248ee683b20a2aa1f6b28214e818bd780",
|
|
47
|
+
"conftest-0.71.1-darwin-arm64": "e1cc50d2590b6c5e3da6912666a71264bae82d60bdd62d7ae2b99971ccc8eedd",
|
|
48
|
+
"opa-1.21.4-linux-x64": "2ab571f367f293bb19523b76f92f1945bbf245003392c7f392903f95c2b9b630",
|
|
49
|
+
"opa-1.21.4-linux-arm64": "810aa96e44f1f852d044888cf830324e44eafd0f510345339011347ab4deaab1",
|
|
50
|
+
"opa-1.21.4-darwin-x64": "8802bd39bc14b58a705e6b3d4f4dd14f7f24ee657f0362c737338be243190c7e",
|
|
51
|
+
"opa-1.21.4-darwin-arm64": "4c105208a9282f590385bccbe600875c011f0894ccf9431782dfe855e1f99aa6",
|
|
52
|
+
};
|
|
53
|
+
const VERSION = /^\d+\.\d+\.\d+$/;
|
|
54
|
+
const SHA256 = /^[0-9a-f]{64}$/;
|
|
55
|
+
/** The release file of `engine` at `version` for `platform`. A version is a plain semver. */
|
|
56
|
+
export function policyEngineRelease(engine, platform, version = engine === "opa" ? OPA_VERSION : CONFTEST_VERSION) {
|
|
57
|
+
if (!VERSION.test(version))
|
|
58
|
+
throw new Error(`${engine} version ${JSON.stringify(version)} is not a release version such as ${engine === "opa" ? OPA_VERSION : CONFTEST_VERSION}`);
|
|
59
|
+
const [os, cpu] = platform.split("-");
|
|
60
|
+
if (engine === "conftest") {
|
|
61
|
+
const asset = `conftest_${version}_${os === "linux" ? "Linux" : "Darwin"}_${cpu === "x64" ? "x86_64" : "arm64"}.tar.gz`;
|
|
62
|
+
return { engine, version, platform, asset, url: `https://github.com/open-policy-agent/conftest/releases/download/v${version}/${asset}`, archive: true };
|
|
63
|
+
}
|
|
64
|
+
// OPA builds no static binary for darwin on amd64.
|
|
65
|
+
const asset = `opa_${os}_${cpu === "x64" ? "amd64" : "arm64"}${platform === "darwin-x64" ? "" : "_static"}`;
|
|
66
|
+
return { engine, version, platform, asset, url: `https://github.com/open-policy-agent/opa/releases/download/v${version}/${asset}`, archive: false };
|
|
67
|
+
}
|
|
68
|
+
/** The sha256 chant records for a release file, or undefined when it records none. */
|
|
69
|
+
export function pinnedPolicyEngineSha256(release) {
|
|
70
|
+
return POLICY_ENGINE_SHA256[`${release.engine}-${release.version}-${release.platform}`];
|
|
71
|
+
}
|
|
72
|
+
/** This process's platform as chant pins it. Throws on one with no pin. */
|
|
73
|
+
export function policyEnginePlatform(platform = process.platform, arch = process.arch) {
|
|
74
|
+
const os = platform === "linux" || platform === "darwin" ? platform : undefined;
|
|
75
|
+
const cpu = arch === "x64" || arch === "x86_64" || arch === "amd64" ? "x64" : arch === "arm64" || arch === "aarch64" ? "arm64" : undefined;
|
|
76
|
+
if (!os || !cpu)
|
|
77
|
+
throw new Error(`chant pins conftest and OPA for ${POLICY_ENGINE_PLATFORMS.join(", ")}, not ${platform}-${arch}; put the engine on PATH instead`);
|
|
78
|
+
return `${os}-${cpu}`;
|
|
79
|
+
}
|
|
80
|
+
/** Where installed engines are kept by default: `CHANT_TOOL_CACHE`, else `chant-tools` in the temp directory. */
|
|
81
|
+
export function defaultPolicyEngineCache() {
|
|
82
|
+
return process.env.CHANT_TOOL_CACHE || join(tmpdir(), "chant-tools");
|
|
83
|
+
}
|
|
84
|
+
async function fetchBytes(url) {
|
|
85
|
+
const res = await fetch(url);
|
|
86
|
+
if (!res.ok)
|
|
87
|
+
throw new Error(`${url}: HTTP ${res.status}`);
|
|
88
|
+
return new Uint8Array(await res.arrayBuffer());
|
|
89
|
+
}
|
|
90
|
+
/** The bytes of the regular file `name` (at the top, or under `./`) in a tar stream. Undefined when it holds none. */
|
|
91
|
+
export function tarEntry(tar, name) {
|
|
92
|
+
const text = (from, len) => {
|
|
93
|
+
const raw = Buffer.from(tar.subarray(from, from + len)).toString("utf-8");
|
|
94
|
+
const nul = raw.indexOf("\0");
|
|
95
|
+
return nul < 0 ? raw : raw.slice(0, nul);
|
|
96
|
+
};
|
|
97
|
+
let at = 0;
|
|
98
|
+
while (at + 512 <= tar.length) {
|
|
99
|
+
if (tar.subarray(at, at + 512).every((b) => b === 0))
|
|
100
|
+
return undefined;
|
|
101
|
+
const prefix = text(at + 345, 155);
|
|
102
|
+
const entry = (prefix ? `${prefix}/` : "") + text(at, 100);
|
|
103
|
+
const size = parseInt(text(at + 124, 12).trim() || "0", 8);
|
|
104
|
+
const type = text(at + 156, 1);
|
|
105
|
+
const body = at + 512;
|
|
106
|
+
if ((type === "" || type === "0") && entry.replace(/^\.\//, "") === name)
|
|
107
|
+
return tar.subarray(body, body + size);
|
|
108
|
+
at = body + Math.ceil(size / 512) * 512;
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Install `engine` into the cache and return the binary's path. A binary
|
|
114
|
+
* already installed there for that release and platform is returned without
|
|
115
|
+
* a download. Throws when the platform has no pin, the download fails, or
|
|
116
|
+
* the file does not match its pin.
|
|
117
|
+
*/
|
|
118
|
+
export async function installPolicyEngine(engine, options = {}) {
|
|
119
|
+
const platform = options.platform ?? policyEnginePlatform();
|
|
120
|
+
const release = policyEngineRelease(engine, platform, options.version);
|
|
121
|
+
const want = options.sha256 ? options.sha256[platform] : pinnedPolicyEngineSha256(release);
|
|
122
|
+
if (want === undefined)
|
|
123
|
+
throw new Error(`chant records no sha256 for ${engine} ${release.version} on ${platform} (${release.asset}); pass sha256 for it, or use ${engine === "opa" ? OPA_VERSION : CONFTEST_VERSION}`);
|
|
124
|
+
if (!SHA256.test(want))
|
|
125
|
+
throw new Error(`the sha256 for ${release.asset} is not 64 hex digits`);
|
|
126
|
+
const dir = join(options.cache ?? defaultPolicyEngineCache(), `${engine}-${release.version}-${platform}`);
|
|
127
|
+
const bin = join(dir, engine);
|
|
128
|
+
if (existsSync(bin))
|
|
129
|
+
return bin;
|
|
130
|
+
const body = await (options.download ?? fetchBytes)(release.url);
|
|
131
|
+
const sum = createHash("sha256").update(body).digest("hex");
|
|
132
|
+
if (sum !== want)
|
|
133
|
+
throw new Error(`${engine} ${release.version} from ${release.url} does not match its pinned sha256 (want ${want}, got ${sum}); not running it`);
|
|
134
|
+
const binary = release.archive ? tarEntry(gunzipSync(body), engine) : body;
|
|
135
|
+
if (!binary)
|
|
136
|
+
throw new Error(`${release.asset} holds no ${engine} binary`);
|
|
137
|
+
mkdirSync(dir, { recursive: true });
|
|
138
|
+
// Written beside its final name and renamed, so a job that stops halfway leaves no binary behind.
|
|
139
|
+
const part = join(dir, `.${engine}.${randomBytes(6).toString("hex")}`);
|
|
140
|
+
writeFileSync(part, binary);
|
|
141
|
+
chmodSync(part, 0o755);
|
|
142
|
+
renameSync(part, bin);
|
|
143
|
+
return bin;
|
|
144
|
+
}
|
|
145
|
+
export const defaultPolicyEngineProbe = (engine) => {
|
|
146
|
+
const r = spawnSync(engine, engine === "opa" ? ["version"] : ["--version"], { stdio: "ignore" });
|
|
147
|
+
return !r.error && r.status === 0;
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* The engine's executable: its name when the one on PATH answers (an image
|
|
151
|
+
* that carries the engine pays nothing), else the pinned release installed
|
|
152
|
+
* by {@link installPolicyEngine}.
|
|
153
|
+
*/
|
|
154
|
+
export async function policyEngineBinary(engine, options = {}) {
|
|
155
|
+
if (!options.pinnedOnly && (options.probe ?? defaultPolicyEngineProbe)(engine))
|
|
156
|
+
return engine;
|
|
157
|
+
return installPolicyEngine(engine, options);
|
|
158
|
+
}
|
package/dist/release-floors.d.ts
CHANGED
|
@@ -19,5 +19,5 @@
|
|
|
19
19
|
* this constant to the next patch (#4501). The check fails while a floor
|
|
20
20
|
* still names this constant after its version shipped.
|
|
21
21
|
*/
|
|
22
|
-
export declare const NEXT_RELEASE = "0.124.
|
|
22
|
+
export declare const NEXT_RELEASE = "0.124.77";
|
|
23
23
|
//# sourceMappingURL=release-floors.d.ts.map
|
package/dist/release-floors.js
CHANGED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { type GatedRequestLedger } from "./lifecycle/gated-op.js";
|
|
2
|
+
import type { GatedRequestRules } from "./lifecycle/gated-request.js";
|
|
3
|
+
/** The first chant with the engine-neutral state export (#4762). */
|
|
4
|
+
export declare const STATE_EXPORT_FLOOR = "0.124.77";
|
|
5
|
+
/** The version a store holds now. */
|
|
6
|
+
export interface StateVersion {
|
|
7
|
+
/** Whether there is a state at all. */
|
|
8
|
+
exists: boolean;
|
|
9
|
+
/** Its id, when the store keeps versions and can give this one back later. */
|
|
10
|
+
versionId?: string;
|
|
11
|
+
/** Why the store keeps no version of it, when it keeps none. */
|
|
12
|
+
off?: string;
|
|
13
|
+
}
|
|
14
|
+
/** An engine's state, read by version: the adapter a product's engine supplies. */
|
|
15
|
+
export interface StateVersionSource {
|
|
16
|
+
/** Where the state is, as messages and the record name it. Never a credential. */
|
|
17
|
+
readonly location: string;
|
|
18
|
+
/** The version the store holds now. */
|
|
19
|
+
current(): Promise<StateVersion>;
|
|
20
|
+
/** Whether the store holds that version, without reading it. */
|
|
21
|
+
hasVersion(id: string): Promise<boolean>;
|
|
22
|
+
/** That version's bytes, or undefined when the store holds no such version. */
|
|
23
|
+
readVersion(id: string): Promise<string | Uint8Array | undefined>;
|
|
24
|
+
}
|
|
25
|
+
/** The product's words for an export. */
|
|
26
|
+
export interface StateExportWords {
|
|
27
|
+
/** The op the requests wait at, such as `tf-state-export`. */
|
|
28
|
+
op: string;
|
|
29
|
+
/** The request's kind. Default `state-export`. */
|
|
30
|
+
kind?: string;
|
|
31
|
+
/** The command that approves a request, as the product prints it. Default chant's `chant approve <op> <subject> --plan <digest>`. */
|
|
32
|
+
approveCommand?: (subject: string, digest: string) => string;
|
|
33
|
+
/** The file's name in its directory. Default `<subject>.<version>.state`, each with what is not a word character replaced. */
|
|
34
|
+
fileName?: (subject: string, version: string) => string;
|
|
35
|
+
/** The prefix of the private directory a default destination is made in. Default `chant-export-`. */
|
|
36
|
+
tmpPrefix?: string;
|
|
37
|
+
}
|
|
38
|
+
/** The CI variables that mark a job, where an export would become an artifact. */
|
|
39
|
+
export declare const CI_MARKERS: readonly string[];
|
|
40
|
+
/** The CI marker set in `env`, or undefined when none is. */
|
|
41
|
+
export declare function ciMarker(env: NodeJS.ProcessEnv | Record<string, string | undefined>): string | undefined;
|
|
42
|
+
/** The git working tree `path` sits in, or undefined. */
|
|
43
|
+
export declare function enclosingWorkTree(path: string): string | undefined;
|
|
44
|
+
/** Why an export may not go to `out`, or undefined when it may. */
|
|
45
|
+
export declare function exportRefusal(out: string): string | undefined;
|
|
46
|
+
/** A refusal before anything is asked: a version the store does not have, a destination the export may not use. */
|
|
47
|
+
export declare class StateExportError extends Error {
|
|
48
|
+
constructor(message: string);
|
|
49
|
+
}
|
|
50
|
+
export interface StateExportInput {
|
|
51
|
+
/** What is exported, and the gate the request waits at: a root, a stack. */
|
|
52
|
+
subject: string;
|
|
53
|
+
/** The requester. Their own approval never counts. */
|
|
54
|
+
by: string;
|
|
55
|
+
source: StateVersionSource;
|
|
56
|
+
/** The version to export. Default the one the store holds now. */
|
|
57
|
+
version?: string;
|
|
58
|
+
/** Where the file goes. Default a new private directory in the system's temp dir. */
|
|
59
|
+
out?: string;
|
|
60
|
+
words: StateExportWords;
|
|
61
|
+
rules: GatedRequestRules;
|
|
62
|
+
/** Where the ledger is. Default the process's. */
|
|
63
|
+
cwd?: string;
|
|
64
|
+
base?: string;
|
|
65
|
+
/** How long a request waits for its approval. Default 48h. */
|
|
66
|
+
expiry?: string;
|
|
67
|
+
now?: string;
|
|
68
|
+
/** The environment the CI markers are read from. Default the process's. */
|
|
69
|
+
env?: NodeJS.ProcessEnv | Record<string, string | undefined>;
|
|
70
|
+
/** Export in a CI job too. Default false: a CI job refuses. */
|
|
71
|
+
allowCi?: boolean;
|
|
72
|
+
ledger?: GatedRequestLedger;
|
|
73
|
+
}
|
|
74
|
+
/** How an export ended. `exit` is the code its command exits with. */
|
|
75
|
+
export type StateExportOutcome = {
|
|
76
|
+
status: "written";
|
|
77
|
+
exit: 0;
|
|
78
|
+
digest: string;
|
|
79
|
+
version: string;
|
|
80
|
+
location: string;
|
|
81
|
+
file: string;
|
|
82
|
+
approvedBy: string;
|
|
83
|
+
contentDigest: string;
|
|
84
|
+
recorded: boolean;
|
|
85
|
+
} | {
|
|
86
|
+
status: "waiting";
|
|
87
|
+
exit: 3;
|
|
88
|
+
digest: string;
|
|
89
|
+
version: string;
|
|
90
|
+
location: string;
|
|
91
|
+
command: string;
|
|
92
|
+
}
|
|
93
|
+
/** An approval stands and does not count, such as the requester's own. The request still waits. */
|
|
94
|
+
| {
|
|
95
|
+
status: "refused";
|
|
96
|
+
exit: 3;
|
|
97
|
+
digest: string;
|
|
98
|
+
version: string;
|
|
99
|
+
location: string;
|
|
100
|
+
command: string;
|
|
101
|
+
by: string;
|
|
102
|
+
reason: string;
|
|
103
|
+
}
|
|
104
|
+
/** The request exported already; another export asks again. */
|
|
105
|
+
| {
|
|
106
|
+
status: "spent";
|
|
107
|
+
exit: 1;
|
|
108
|
+
digest: string;
|
|
109
|
+
version: string;
|
|
110
|
+
location: string;
|
|
111
|
+
} | {
|
|
112
|
+
status: "failed";
|
|
113
|
+
exit: 1;
|
|
114
|
+
digest: string;
|
|
115
|
+
version: string;
|
|
116
|
+
location: string;
|
|
117
|
+
error: Error;
|
|
118
|
+
recorded: boolean;
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Ask for, or with an approval write, one version of a state. Throws
|
|
122
|
+
* {@link StateExportError} before anything is asked when the version or the
|
|
123
|
+
* destination is refused.
|
|
124
|
+
*/
|
|
125
|
+
export declare function exportStateVersion(input: StateExportInput): Promise<StateExportOutcome>;
|
|
126
|
+
//# sourceMappingURL=state-export.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-export.d.ts","sourceRoot":"","sources":["../src/state-export.ts"],"names":[],"mappings":"AAoCA,OAAO,EAAW,KAAK,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAGnE,oEAAoE;AACpE,eAAO,MAAM,kBAAkB,aAAe,CAAC;AAE/C,qCAAqC;AACrC,MAAM,WAAW,YAAY;IAC3B,uCAAuC;IACvC,MAAM,EAAE,OAAO,CAAC;IAChB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IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE;AACpG,mGAAmG;GACjG;IAAE,MAAM,EAAE,SAAS,CAAC;IAAC,IAAI,EAAE,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE;AAChI,+DAA+D;GAC7D;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAC/E;IAAE,MAAM,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,CAAC,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC;AAMtH;;;;GAIG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA8D7F"}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A state export behind a gated request (#4762): one version of a state,
|
|
3
|
+
* written to the machine of the person who asks once someone else approved
|
|
4
|
+
* it, and recorded with who exported which version.
|
|
5
|
+
*
|
|
6
|
+
* Engine-neutral. The engine's adapter is a {@link StateVersionSource}: where
|
|
7
|
+
* the state is, the version it holds now, and one version's bytes. The
|
|
8
|
+
* terraform lexicon builds one from a root's backend
|
|
9
|
+
* (`@intentius/chant-lexicon-terraform/state-export`); a Pulumi stack's
|
|
10
|
+
* export (`pulumi stack export --version`) fits the same three calls. The
|
|
11
|
+
* product passes its words: the op the requests wait at, the approve
|
|
12
|
+
* command it prints, the name of the file.
|
|
13
|
+
*
|
|
14
|
+
* A state holds every secret its resources hold, so the export goes to the
|
|
15
|
+
* requester only:
|
|
16
|
+
*
|
|
17
|
+
* - never in CI, where a file can become a job artifact readable by everyone
|
|
18
|
+
* who can read the repository ({@link CI_MARKERS}), unless the caller
|
|
19
|
+
* says otherwise;
|
|
20
|
+
* - never inside a git working tree, where it could be committed;
|
|
21
|
+
* - never over a file, and mode 0600. With no `out`, a new private
|
|
22
|
+
* directory in the system's temp dir.
|
|
23
|
+
*
|
|
24
|
+
* The first call reads the version's metadata only, records the pending
|
|
25
|
+
* request (kind `state-export`, subject the root or stack, fields the
|
|
26
|
+
* location and the version id) and answers `waiting` with the approve
|
|
27
|
+
* command. Once someone other than the requester approved its digest, the
|
|
28
|
+
* next call spends the approval, reads the version, writes the file, and the
|
|
29
|
+
* finished line records the version id and the digest of the bytes written,
|
|
30
|
+
* never their contents. A request exports once.
|
|
31
|
+
*/
|
|
32
|
+
import { createHash } from "node:crypto";
|
|
33
|
+
import { existsSync, mkdtempSync, realpathSync, writeFileSync } from "node:fs";
|
|
34
|
+
import { tmpdir } from "node:os";
|
|
35
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
36
|
+
import { CHANT_EXIT } from "./exit-codes.js";
|
|
37
|
+
import { gatedOp } from "./lifecycle/gated-op.js";
|
|
38
|
+
import { NEXT_RELEASE } from "./release-floors.js";
|
|
39
|
+
/** The first chant with the engine-neutral state export (#4762). */
|
|
40
|
+
export const STATE_EXPORT_FLOOR = NEXT_RELEASE;
|
|
41
|
+
/** The CI variables that mark a job, where an export would become an artifact. */
|
|
42
|
+
export const CI_MARKERS = ["CI", "GITHUB_ACTIONS", "GITLAB_CI", "FORGEJO_ACTIONS", "BUILDKITE", "JENKINS_URL"];
|
|
43
|
+
/** The CI marker set in `env`, or undefined when none is. */
|
|
44
|
+
export function ciMarker(env) {
|
|
45
|
+
return CI_MARKERS.find((k) => env[k] !== undefined && env[k] !== "" && env[k] !== "false");
|
|
46
|
+
}
|
|
47
|
+
/** The git working tree `path` sits in, or undefined. */
|
|
48
|
+
export function enclosingWorkTree(path) {
|
|
49
|
+
let dir = resolve(path);
|
|
50
|
+
try {
|
|
51
|
+
dir = realpathSync(dir);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// A path that does not exist is walked as given.
|
|
55
|
+
}
|
|
56
|
+
for (;;) {
|
|
57
|
+
if (existsSync(join(dir, ".git")))
|
|
58
|
+
return dir;
|
|
59
|
+
const up = dirname(dir);
|
|
60
|
+
if (up === dir)
|
|
61
|
+
return undefined;
|
|
62
|
+
dir = up;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** Why an export may not go to `out`, or undefined when it may. */
|
|
66
|
+
export function exportRefusal(out) {
|
|
67
|
+
const abs = resolve(out);
|
|
68
|
+
if (!existsSync(dirname(abs)))
|
|
69
|
+
return `${dirname(abs)} is not a directory`;
|
|
70
|
+
if (existsSync(abs))
|
|
71
|
+
return `${abs} is there already, and an export never writes over a file; name a new one`;
|
|
72
|
+
const tree = enclosingWorkTree(dirname(abs));
|
|
73
|
+
if (tree)
|
|
74
|
+
return `${abs} is inside the git working tree ${tree}, where a state could be committed; name a path outside it`;
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/** A refusal before anything is asked: a version the store does not have, a destination the export may not use. */
|
|
78
|
+
export class StateExportError extends Error {
|
|
79
|
+
constructor(message) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = "StateExportError";
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const sha = (body) => `sha256:${createHash("sha256").update(body).digest("hex")}`;
|
|
85
|
+
const defaultName = (subject, version) => `${subject.replace(/[^\w.-]+/g, "_")}.${version.replace(/[^\w.-]+/g, "_")}.state`;
|
|
86
|
+
/**
|
|
87
|
+
* Ask for, or with an approval write, one version of a state. Throws
|
|
88
|
+
* {@link StateExportError} before anything is asked when the version or the
|
|
89
|
+
* destination is refused.
|
|
90
|
+
*/
|
|
91
|
+
export async function exportStateVersion(input) {
|
|
92
|
+
const { source, words } = input;
|
|
93
|
+
const env = input.env ?? process.env;
|
|
94
|
+
const marker = input.allowCi ? undefined : ciMarker(env);
|
|
95
|
+
if (marker)
|
|
96
|
+
throw new StateExportError(`an export goes to the requester's machine only, and ${marker} is set, so this is a CI job whose files can become artifacts; ask for it from your own machine`);
|
|
97
|
+
if (input.out !== undefined) {
|
|
98
|
+
const why = exportRefusal(input.out);
|
|
99
|
+
if (why)
|
|
100
|
+
throw new StateExportError(why);
|
|
101
|
+
}
|
|
102
|
+
const location = source.location;
|
|
103
|
+
let version = input.version;
|
|
104
|
+
if (!version) {
|
|
105
|
+
const now = await source.current();
|
|
106
|
+
if (!now.exists)
|
|
107
|
+
throw new StateExportError(`${location} holds no state`);
|
|
108
|
+
if (!now.versionId)
|
|
109
|
+
throw new StateExportError(`${location} keeps no versions, so there is no version id to export: ${now.off ?? "the store gave none"}`);
|
|
110
|
+
version = now.versionId;
|
|
111
|
+
}
|
|
112
|
+
else if (!(await source.hasVersion(version))) {
|
|
113
|
+
throw new StateExportError(`${location} has no version ${version}; a lifecycle rule may have expired it`);
|
|
114
|
+
}
|
|
115
|
+
const versionId = version;
|
|
116
|
+
const result = await gatedOp({
|
|
117
|
+
op: words.op,
|
|
118
|
+
request: { kind: words.kind ?? "state-export", subject: input.subject, by: input.by, fields: { location, version_id: versionId } },
|
|
119
|
+
rules: input.rules,
|
|
120
|
+
description: `state export: ${input.subject} version ${versionId}, for ${input.by}`,
|
|
121
|
+
expiry: input.expiry ?? "48h",
|
|
122
|
+
...(input.base ? { base: input.base } : {}),
|
|
123
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
124
|
+
...(input.now ? { now: input.now } : {}),
|
|
125
|
+
...(input.ledger ? { ledger: input.ledger } : {}),
|
|
126
|
+
}, async ({ approvedBy }) => {
|
|
127
|
+
const name = (words.fileName ?? defaultName)(input.subject, versionId);
|
|
128
|
+
const file = input.out !== undefined ? resolve(input.out) : join(mkdtempSync(join(tmpdir(), words.tmpPrefix ?? "chant-export-")), basename(name));
|
|
129
|
+
// Checked again: the destination may have changed while the request waited.
|
|
130
|
+
const why = exportRefusal(file);
|
|
131
|
+
if (why)
|
|
132
|
+
throw new StateExportError(why);
|
|
133
|
+
const body = await source.readVersion(versionId);
|
|
134
|
+
if (body === undefined)
|
|
135
|
+
throw new StateExportError(`${location} has no version ${versionId}; it was deleted since the request`);
|
|
136
|
+
writeFileSync(file, body, { mode: 0o600, flag: "wx" });
|
|
137
|
+
const contentDigest = sha(body);
|
|
138
|
+
return { output: { file, contentDigest }, detail: { by: input.by, location, version_id: versionId, content_digest: contentDigest, approved_by: approvedBy } };
|
|
139
|
+
});
|
|
140
|
+
const command = (digest, fallback) => (words.approveCommand ? words.approveCommand(input.subject, digest) : fallback);
|
|
141
|
+
const at = { digest: result.digest, version: versionId, location };
|
|
142
|
+
switch (result.status) {
|
|
143
|
+
case "done":
|
|
144
|
+
return { status: "written", exit: CHANT_EXIT.done, ...at, file: result.output.file, approvedBy: result.approvedBy, contentDigest: result.output.contentDigest, recorded: result.recorded };
|
|
145
|
+
case "waiting":
|
|
146
|
+
return { status: "waiting", exit: CHANT_EXIT.waiting, ...at, command: command(result.digest, result.command) };
|
|
147
|
+
case "refused":
|
|
148
|
+
return { status: "refused", exit: CHANT_EXIT.waiting, ...at, command: command(result.digest, result.command), by: result.by, reason: result.reason };
|
|
149
|
+
case "spent":
|
|
150
|
+
return { status: "spent", exit: CHANT_EXIT.failed, ...at };
|
|
151
|
+
case "held":
|
|
152
|
+
// An export takes no last check, so nothing holds it; kept for the type.
|
|
153
|
+
return { status: "failed", exit: CHANT_EXIT.failed, ...at, error: new StateExportError(result.reason), recorded: false };
|
|
154
|
+
case "failed":
|
|
155
|
+
return { status: "failed", exit: CHANT_EXIT.failed, ...at, error: result.error, recorded: result.recorded };
|
|
156
|
+
}
|
|
157
|
+
}
|