@intentius/chant-lexicon-terraform 0.124.51 → 0.124.54
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/README.md +7 -6
- package/dist/__fixtures__/pr-loop/estate.component.d.ts +7 -0
- package/dist/__fixtures__/pr-loop/estate.component.d.ts.map +1 -0
- package/dist/__fixtures__/pr-loop/estate.component.js +7 -0
- package/dist/__fixtures__/report/generate.d.ts +26 -0
- package/dist/__fixtures__/report/generate.d.ts.map +1 -0
- package/dist/__fixtures__/report/generate.js +58 -0
- package/dist/applied-state.d.ts +27 -0
- package/dist/applied-state.d.ts.map +1 -0
- package/dist/applied-state.js +44 -0
- package/dist/backend-config.d.ts +53 -0
- package/dist/backend-config.d.ts.map +1 -0
- package/dist/backend-config.js +136 -0
- package/dist/behaviour/kinds.d.ts.map +1 -1
- package/dist/behaviour/kinds.js +4 -0
- package/dist/behaviour/predict.d.ts.map +1 -1
- package/dist/behaviour/predict.js +6 -2
- package/dist/behaviour/reference-catalog.d.ts +16 -5
- package/dist/behaviour/reference-catalog.d.ts.map +1 -1
- package/dist/behaviour/reference-catalog.js +16 -5
- package/dist/behaviour/request.d.ts.map +1 -1
- package/dist/behaviour/request.js +3 -2
- package/dist/changed-roots.d.ts +6 -4
- package/dist/changed-roots.d.ts.map +1 -1
- package/dist/changed-roots.js +14 -18
- package/dist/commands.d.ts +5 -0
- package/dist/commands.d.ts.map +1 -1
- package/dist/commands.js +87 -4
- package/dist/components/capability-plugin.d.ts +65 -3
- package/dist/components/capability-plugin.d.ts.map +1 -1
- package/dist/components/capability-plugin.js +197 -13
- package/dist/components/compose-group.d.ts +145 -0
- package/dist/components/compose-group.d.ts.map +1 -0
- package/dist/components/compose-group.js +407 -0
- package/dist/components/placeholder-preview.d.ts +78 -0
- package/dist/components/placeholder-preview.d.ts.map +1 -0
- package/dist/components/placeholder-preview.js +254 -0
- package/dist/compose/apply-events.d.ts +46 -0
- package/dist/compose/apply-events.d.ts.map +1 -0
- package/dist/compose/apply-events.js +77 -0
- package/dist/compose/estate.d.ts +194 -0
- package/dist/compose/estate.d.ts.map +1 -0
- package/dist/compose/estate.js +363 -0
- package/dist/compose/hcl.d.ts +99 -0
- package/dist/compose/hcl.d.ts.map +1 -0
- package/dist/compose/hcl.js +302 -0
- package/dist/compose/module-address.d.ts +24 -0
- package/dist/compose/module-address.d.ts.map +1 -0
- package/dist/compose/module-address.js +43 -0
- package/dist/compose/root.d.ts +127 -0
- package/dist/compose/root.d.ts.map +1 -0
- package/dist/compose/root.js +586 -0
- package/dist/compose/run.d.ts +183 -0
- package/dist/compose/run.d.ts.map +1 -0
- package/dist/compose/run.js +447 -0
- package/dist/compose/slice.d.ts +102 -0
- package/dist/compose/slice.d.ts.map +1 -0
- package/dist/compose/slice.js +249 -0
- package/dist/compose/state-address.d.ts +28 -0
- package/dist/compose/state-address.d.ts.map +1 -0
- package/dist/compose/state-address.js +154 -0
- package/dist/compose/state-merge.d.ts +52 -0
- package/dist/compose/state-merge.d.ts.map +1 -0
- package/dist/compose/state-merge.js +135 -0
- package/dist/compose.d.ts +179 -0
- package/dist/compose.d.ts.map +1 -0
- package/dist/compose.js +482 -0
- package/dist/composites/terraform-apply-op.d.ts.map +1 -1
- package/dist/composites/terraform-apply-op.js +7 -3
- package/dist/config.d.ts +59 -13
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +43 -9
- package/dist/describe-resources.d.ts +74 -12
- package/dist/describe-resources.d.ts.map +1 -1
- package/dist/describe-resources.js +374 -66
- package/dist/drift-read.d.ts +33 -0
- package/dist/drift-read.d.ts.map +1 -0
- package/dist/drift-read.js +243 -0
- package/dist/drift-record.d.ts +147 -0
- package/dist/drift-record.d.ts.map +1 -0
- package/dist/drift-record.js +383 -0
- package/dist/hcl/edges.d.ts.map +1 -1
- package/dist/hcl/edges.js +24 -1
- package/dist/hcl/files.d.ts +61 -0
- package/dist/hcl/files.d.ts.map +1 -0
- package/dist/hcl/files.js +221 -0
- package/dist/hcl/instances.d.ts +82 -0
- package/dist/hcl/instances.d.ts.map +1 -0
- package/dist/hcl/instances.js +390 -0
- package/dist/hcl/parse.d.ts +81 -9
- package/dist/hcl/parse.d.ts.map +1 -1
- package/dist/hcl/parse.js +114 -16
- package/dist/hcl/roots.d.ts.map +1 -1
- package/dist/hcl/roots.js +5 -0
- package/dist/hcl/suppressions.d.ts.map +1 -1
- package/dist/hcl/suppressions.js +8 -2
- package/dist/index.d.ts +11 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +21 -2
- package/dist/integrity.json +3 -3
- package/dist/live-plan-reasons.d.ts +21 -0
- package/dist/live-plan-reasons.d.ts.map +1 -0
- package/dist/live-plan-reasons.js +47 -0
- package/dist/lsp/option-keys.d.ts.map +1 -1
- package/dist/lsp/option-keys.js +2 -0
- package/dist/manifest.json +1 -1
- package/dist/op/activities/index.d.ts +6 -2
- package/dist/op/activities/index.d.ts.map +1 -1
- package/dist/op/activities/index.js +5 -1
- package/dist/op/activities/live-detect.d.ts +4 -3
- package/dist/op/activities/live-detect.d.ts.map +1 -1
- package/dist/op/activities/live-detect.js +57 -32
- package/dist/op/activities/pin-rollout.d.ts +2 -0
- package/dist/op/activities/pin-rollout.d.ts.map +1 -1
- package/dist/op/activities/pin-rollout.js +1 -0
- package/dist/op/activities/terraform.d.ts +153 -5
- package/dist/op/activities/terraform.d.ts.map +1 -1
- package/dist/op/activities/terraform.js +205 -20
- package/dist/op/activity-contracts.d.ts +27 -0
- package/dist/op/activity-contracts.d.ts.map +1 -1
- package/dist/op/activity-contracts.js +19 -1
- package/dist/op/builders.d.ts +7 -1
- package/dist/op/builders.d.ts.map +1 -1
- package/dist/op/builders.js +9 -0
- package/dist/pin/edit.d.ts.map +1 -1
- package/dist/pin/edit.js +35 -0
- package/dist/pin/rollout.d.ts +2 -0
- package/dist/pin/rollout.d.ts.map +1 -1
- package/dist/pin/rollout.js +14 -11
- package/dist/plan-shape.d.ts +30 -0
- package/dist/plan-shape.d.ts.map +1 -0
- package/dist/plan-shape.js +125 -0
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +32 -18
- package/dist/report.d.ts +216 -0
- package/dist/report.d.ts.map +1 -0
- package/dist/report.js +666 -0
- package/dist/skills/chant-terraform.md +1 -1
- package/dist/state/azure-blob.d.ts +68 -0
- package/dist/state/azure-blob.d.ts.map +1 -0
- package/dist/state/azure-blob.js +201 -0
- package/dist/state/backend.d.ts +158 -0
- package/dist/state/backend.d.ts.map +1 -0
- package/dist/state/backend.js +406 -0
- package/dist/state/exec.d.ts +39 -0
- package/dist/state/exec.d.ts.map +1 -0
- package/dist/state/exec.js +80 -0
- package/dist/state/gcs.d.ts +62 -0
- package/dist/state/gcs.d.ts.map +1 -0
- package/dist/state/gcs.js +224 -0
- package/dist/state/index.d.ts +15 -0
- package/dist/state/index.d.ts.map +1 -0
- package/dist/state/index.js +14 -0
- package/dist/state/locks.d.ts +105 -0
- package/dist/state/locks.d.ts.map +1 -0
- package/dist/state/locks.js +130 -0
- package/dist/state/object-store.d.ts +37 -0
- package/dist/state/object-store.d.ts.map +1 -0
- package/dist/state/object-store.js +20 -0
- package/dist/state/root-states.d.ts +131 -0
- package/dist/state/root-states.d.ts.map +1 -0
- package/dist/state/root-states.js +241 -0
- package/dist/state/s3.d.ts +68 -0
- package/dist/state/s3.d.ts.map +1 -0
- package/dist/state/s3.js +170 -0
- package/dist/state/store.d.ts +51 -0
- package/dist/state/store.d.ts.map +1 -0
- package/dist/state/store.js +185 -0
- package/dist/terragrunt/affected.d.ts.map +1 -1
- package/dist/terragrunt/affected.js +2 -1
- package/package.json +7 -3
- package/src/__fixtures__/ACCEPTANCE.md +69 -7
- package/src/__fixtures__/compose/apply-failed-in-app.jsonl +12 -0
- package/src/__fixtures__/compose/state-app.json +66 -0
- package/src/__fixtures__/compose/state-edge.json +40 -0
- package/src/__fixtures__/compose/state-net.json +58 -0
- package/src/__fixtures__/compose/three-app.plan.json +184 -0
- package/src/__fixtures__/compose/three-composed.plan.json +509 -0
- package/src/__fixtures__/compose/three-edge.plan.json +144 -0
- package/src/__fixtures__/compose/three-net.plan.json +174 -0
- package/src/__fixtures__/compose/unknown-composed.plan.json +228 -0
- package/src/__fixtures__/compose/working-after-apply.json +145 -0
- package/src/__fixtures__/config-blocks/app/main.tf +61 -0
- package/src/__fixtures__/config-blocks/app/modules/network/main.tf +4 -0
- package/src/__fixtures__/config-blocks/app-json/main.tf.json +39 -0
- package/src/__fixtures__/config-files/shadow/main.tf +3 -0
- package/src/__fixtures__/config-files/shadow/main.tofu +3 -0
- package/src/__fixtures__/config-files/shadow/other.tf +3 -0
- package/src/__fixtures__/config-files/tf/main.tf +11 -0
- package/src/__fixtures__/config-files/tf-json/main.tf.json +18 -0
- package/src/__fixtures__/config-files/tofu/main.tofu +11 -0
- package/src/__fixtures__/config-files/tofu-json/main.tofu.json +18 -0
- package/src/__fixtures__/config-files/tofu-only/main.tofu +3 -0
- package/src/__fixtures__/drift-record/README.md +11 -0
- package/src/__fixtures__/drift-record/live-plan-sections.json +27 -0
- package/src/__fixtures__/instance-forms/app/main.tf +110 -0
- package/src/__fixtures__/instance-forms/app/modules/svc/main.tf +3 -0
- package/src/__fixtures__/instance-vars/app/main.tf +52 -0
- package/src/__fixtures__/instance-vars/app/modules/app/main.tf +12 -0
- package/src/__fixtures__/instance-vars/app/modules/edge/main.tf +3 -0
- package/src/__fixtures__/instance-vars/app/terraform.tfvars +1 -0
- package/src/__fixtures__/live-module-keyed/live-graph.json +259 -0
- package/src/__fixtures__/live-module-keyed/live-plan.json +87 -0
- package/src/__fixtures__/live-module-keyed/root/estate.chdf.hcl +1 -0
- package/src/__fixtures__/live-module-keyed/root/main.tf +21 -0
- package/src/__fixtures__/live-module-keyed/root/modules/app/main.tf +17 -0
- package/src/__fixtures__/live-module-keyed/root/modules/edge/main.tf +7 -0
- package/src/__fixtures__/live-module-keyed/root/versions.tf +30 -0
- package/src/__fixtures__/live-module-keyed/show-state.json +146 -0
- package/src/__fixtures__/pr-loop/README.md +50 -0
- package/src/__fixtures__/pr-loop/chant.config.json +13 -0
- package/src/__fixtures__/pr-loop/estate.component.ts +10 -0
- package/src/__fixtures__/pr-loop/roots/a/main.tf +11 -0
- package/src/__fixtures__/pr-loop/roots/app/main.tf +19 -0
- package/src/__fixtures__/pr-loop/roots/b/main.tf +7 -0
- package/src/__fixtures__/pr-loop/roots/dns/main.tf +3 -0
- package/src/__fixtures__/pr-loop/roots/net/main.tf +7 -0
- package/src/__fixtures__/report/applied.show.json +51 -0
- package/src/__fixtures__/report/generate.ts +63 -0
- package/src/__fixtures__/report/plan.show.json +200 -0
- package/src/__fixtures__/report/tf-apply/change-set.json +170 -0
- package/src/__fixtures__/report/tf-apply/report.json +338 -0
- package/src/__fixtures__/report/tf-plan/change-set.json +170 -0
- package/src/__fixtures__/report/tf-plan/report.json +320 -0
- package/src/__fixtures__/three-cloud-azure.graph.json +290 -0
- package/src/applied-state.ts +52 -0
- package/src/backend-config.test.ts +105 -0
- package/src/backend-config.ts +133 -0
- package/src/behaviour/kinds.ts +4 -0
- package/src/behaviour/predict.ts +5 -2
- package/src/behaviour/reference-catalog.plugin.test.ts +65 -0
- package/src/behaviour/reference-catalog.ts +16 -5
- package/src/behaviour/request.ts +3 -2
- package/src/change-set-instances.test.ts +98 -0
- package/src/changed-roots.ts +14 -16
- package/src/commands.ts +84 -4
- package/src/components/capability-plugin.test.ts +172 -5
- package/src/components/capability-plugin.ts +212 -11
- package/src/components/change-set-graph.e2e.test.ts +11 -5
- package/src/components/compose-group.test.ts +486 -0
- package/src/components/compose-group.ts +489 -0
- package/src/components/demo-composed-roots.e2e.test.ts +440 -0
- package/src/components/member-outputs.test.ts +76 -0
- package/src/components/placeholder-preview.test.ts +147 -0
- package/src/components/placeholder-preview.ts +257 -0
- package/src/components/pr-loop-compose.tofu.e2e.test.ts +363 -0
- package/src/components/pr-loop.tofu.test.ts +112 -56
- package/src/compose/apply-events.test.ts +62 -0
- package/src/compose/apply-events.ts +100 -0
- package/src/compose/estate.choudoufu.e2e.test.ts +112 -0
- package/src/compose/estate.test.ts +421 -0
- package/src/compose/estate.ts +443 -0
- package/src/compose/hcl.ts +311 -0
- package/src/compose/module-address.ts +49 -0
- package/src/compose/root.test.ts +416 -0
- package/src/compose/root.ts +592 -0
- package/src/compose/run.test.ts +440 -0
- package/src/compose/run.tofu.test.ts +176 -0
- package/src/compose/run.ts +527 -0
- package/src/compose/slice.test.ts +184 -0
- package/src/compose/slice.ts +290 -0
- package/src/compose/state-address.ts +157 -0
- package/src/compose/state-merge.test.ts +131 -0
- package/src/compose/state-merge.ts +148 -0
- package/src/compose.test.ts +248 -0
- package/src/compose.ts +627 -0
- package/src/composites/terraform-apply-op.test.ts +9 -0
- package/src/composites/terraform-apply-op.ts +7 -3
- package/src/composites/terraform-pin-rollout-op.test.ts +2 -2
- package/src/config-file-listers.test.ts +265 -0
- package/src/config.ts +44 -9
- package/src/describe-resources.engine-facts.test.ts +412 -0
- package/src/describe-resources.live-module-keyed.test.ts +290 -0
- package/src/describe-resources.ts +416 -70
- package/src/drift-read.ts +284 -0
- package/src/drift-record.test.ts +223 -0
- package/src/drift-record.ts +455 -0
- package/src/hcl/config-blocks.test.ts +174 -0
- package/src/hcl/edges.ts +21 -1
- package/src/hcl/files.ts +234 -0
- package/src/hcl/instance-forms.test.ts +94 -0
- package/src/hcl/instances.test.ts +137 -0
- package/src/hcl/instances.ts +422 -0
- package/src/hcl/parse.test.ts +139 -1
- package/src/hcl/parse.ts +149 -16
- package/src/hcl/roots.ts +6 -0
- package/src/hcl/suppressions.ts +8 -2
- package/src/index.ts +76 -0
- package/src/live-plan-reasons.ts +54 -0
- package/src/lsp/completions.test.ts +1 -1
- package/src/lsp/option-keys.ts +2 -0
- package/src/op/activities/choudoufu-plan-set-op.test.ts +111 -0
- package/src/op/activities/index.ts +9 -0
- package/src/op/activities/live-detect.ts +55 -26
- package/src/op/activities/pin-rollout.ts +3 -0
- package/src/op/activities/registry.test.ts +2 -1
- package/src/op/activities/terraform.test.ts +55 -0
- package/src/op/activities/terraform.ts +266 -22
- package/src/op/activity-contracts.test.ts +1 -0
- package/src/op/activity-contracts.ts +24 -1
- package/src/op/builders.ts +14 -0
- package/src/pin/bundle.test.ts +2 -0
- package/src/pin/edit.ts +30 -0
- package/src/pin/rollout.ts +16 -11
- package/src/plan-shape.test.ts +148 -0
- package/src/plan-shape.ts +128 -0
- package/src/plugin.ts +33 -19
- package/src/report.test.ts +423 -0
- package/src/report.ts +752 -0
- package/src/signal-only-root.test.ts +72 -0
- package/src/skills/chant-terraform.md +1 -1
- package/src/state/azure-blob.ts +223 -0
- package/src/state/backend-verdict.test.ts +187 -0
- package/src/state/backend.test.ts +127 -0
- package/src/state/backend.ts +488 -0
- package/src/state/exec.ts +111 -0
- package/src/state/gcs.ts +254 -0
- package/src/state/index.ts +15 -0
- package/src/state/locks.test.ts +84 -0
- package/src/state/locks.ts +201 -0
- package/src/state/object-store.ts +35 -0
- package/src/state/root-states-floci.e2e.test.ts +66 -0
- package/src/state/root-states.test.ts +462 -0
- package/src/state/root-states.ts +309 -0
- package/src/state/s3.ts +193 -0
- package/src/state/store.ts +225 -0
- package/src/terragrunt/affected.ts +2 -1
- package/src/workspace-kinds.test.ts +4 -17
- package/workspace-kinds.json +1 -24
- package/dist/workspace-member.d.ts +0 -30
- package/dist/workspace-member.d.ts.map +0 -1
- package/dist/workspace-member.js +0 -91
- package/schemas/pulumi.schema.json +0 -34
- package/src/testdata/pulumi-member/Pulumi.yaml +0 -2
- package/src/testdata/pulumi-member/stack-export.json +0 -53
- package/src/testdata/pulumi-unexported/Pulumi.yaml +0 -2
- package/src/workspace-member.test.ts +0 -89
- package/src/workspace-member.ts +0 -126
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A `signalOnly` root (#4484, C5 of #4477). The dns member declares the azure
|
|
3
|
+
* estate's root only so an azure change brings dns into the change. Change
|
|
4
|
+
* detection counts it; the build and the live reads leave it out, so dns's
|
|
5
|
+
* graph draws only dns's own root and dns's live reads never list azure.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { afterAll, describe, expect, test } from "vitest";
|
|
12
|
+
import { terraformConfigSchema, ownRoots } from "./config";
|
|
13
|
+
import { terraformPlugin } from "./plugin";
|
|
14
|
+
import { liveRootNames } from "./describe-resources";
|
|
15
|
+
|
|
16
|
+
const scratch: string[] = [];
|
|
17
|
+
afterAll(() => {
|
|
18
|
+
for (const d of scratch) rmSync(d, { recursive: true, force: true });
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const TERRAFORM: { binary: "choudoufu"; roots: Record<string, { dir: string; signalOnly?: boolean }> } = {
|
|
22
|
+
binary: "choudoufu",
|
|
23
|
+
roots: {
|
|
24
|
+
dns: { dir: "estate" },
|
|
25
|
+
azure: { dir: "../azure", signalOnly: true },
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** A workspace slice: `dns/` (the chant member, with its own root under `estate/`) beside `azure/`. */
|
|
30
|
+
function estate(): string {
|
|
31
|
+
const root = realpathSync(mkdtempSync(join(tmpdir(), "chant-signal-only-")));
|
|
32
|
+
scratch.push(root);
|
|
33
|
+
const files: Record<string, string> = {
|
|
34
|
+
"dns/chant.config.json": JSON.stringify({ lexicons: ["terraform"], terraform: TERRAFORM }),
|
|
35
|
+
"dns/estate/main.tf": 'resource "null_resource" "zone" {}\n',
|
|
36
|
+
"dns/estate/estate.chdf.hcl": 'estate = "dns"\n',
|
|
37
|
+
"azure/main.tf": 'resource "null_resource" "vnet" {}\n',
|
|
38
|
+
"azure/estate.chdf.hcl": 'estate = "azure"\n',
|
|
39
|
+
};
|
|
40
|
+
for (const [path, text] of Object.entries(files)) {
|
|
41
|
+
mkdirSync(dirname(join(root, path)), { recursive: true });
|
|
42
|
+
writeFileSync(join(root, path), text);
|
|
43
|
+
}
|
|
44
|
+
return join(root, "dns");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe("a signalOnly root (#4484)", () => {
|
|
48
|
+
test("the config schema takes the marker", () => {
|
|
49
|
+
expect(terraformConfigSchema.safeParse(TERRAFORM).success).toBe(true);
|
|
50
|
+
expect(Object.keys(ownRoots(TERRAFORM.roots))).toEqual(["dns"]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test("the build draws only dns's own root, so the azure estate is not drawn under dns", async () => {
|
|
54
|
+
const dns = estate();
|
|
55
|
+
const { entities } = await terraformPlugin.buildRoots!({ projectRoot: dns, config: { terraform: TERRAFORM }, entities: new Map() });
|
|
56
|
+
const keys = [...entities.keys()];
|
|
57
|
+
expect(keys.length).toBeGreaterThan(0);
|
|
58
|
+
expect(keys.every((k) => k.startsWith("dns/"))).toBe(true);
|
|
59
|
+
expect(keys.some((k) => k.startsWith("azure/"))).toBe(false);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("the live reads list dns's root and not the azure estate", async () => {
|
|
63
|
+
const dns = estate();
|
|
64
|
+
expect(await liveRootNames(dns)).toEqual([{ root: "dns", estate: "dns" }]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("an azure change still brings dns into the change", async () => {
|
|
68
|
+
const dns = estate();
|
|
69
|
+
const touched = await terraformPlugin.changedUnits!({ projectRoot: dns, config: { terraform: TERRAFORM }, changedFiles: ["../azure/main.tf"] });
|
|
70
|
+
expect(touched).toEqual(["azure"]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -33,7 +33,7 @@ export default {
|
|
|
33
33
|
|
|
34
34
|
## What a build produces
|
|
35
35
|
|
|
36
|
-
Every block of every configured root becomes one entity, keyed `<root>/<address>`: `terraform { }` -> `Terraform::Terraform`, `provider "x" {}` -> `Terraform::Provider`, `resource "x" "y" {}` -> `Terraform::Resource`, `data "x" "y" {}` -> `Terraform::Data`, `module "x" {}` -> `Terraform::Module`, `variable "x" {}` -> `Terraform::Variable`, `output "x" {}` -> `Terraform::Output`, `locals {}` -> `Terraform::Locals`. Each carries `props.address`, `props.body` (the block, verbatim), `props.file` and `props.root`. A root whose `dir` doesn't exist, or whose HCL the parser refuses, warns and contributes no entities — the rest of the build is unaffected.
|
|
36
|
+
Every block of every configured root becomes one entity, keyed `<root>/<address>`: `terraform { }` -> `Terraform::Terraform`, `provider "x" {}` -> `Terraform::Provider`, `resource "x" "y" {}` -> `Terraform::Resource`, `data "x" "y" {}` -> `Terraform::Data`, `module "x" {}` -> `Terraform::Module`, `variable "x" {}` -> `Terraform::Variable`, `output "x" {}` -> `Terraform::Output`, `locals {}` -> `Terraform::Locals`, `moved {}` -> `Terraform::Moved`, `import {}` -> `Terraform::Import`, `removed {}` -> `Terraform::Removed`, `check "x" {}` -> `Terraform::Check`. Each carries `props.address`, `props.body` (the block, verbatim), `props.file` and `props.root`. A root whose `dir` doesn't exist, or whose HCL the parser refuses, warns and contributes no entities — the rest of the build is unaffected.
|
|
37
37
|
|
|
38
38
|
## The one thing to check before anything else
|
|
39
39
|
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Azure Blob Storage for an azurerm backend's state, over its REST API with
|
|
3
|
+
* `fetch` and `node:crypto`: a blob's properties and metadata, its text, a
|
|
4
|
+
* write (under a lease when one holds it), a snapshot, a delete, and the
|
|
5
|
+
* blob lease the backend locks a state with.
|
|
6
|
+
*
|
|
7
|
+
* Requests are signed with Shared Key for an account key, carry a SAS as it
|
|
8
|
+
* is, or carry a storage token the job's OIDC identity is traded for at Entra
|
|
9
|
+
* ID (a client assertion, the way the azurerm backend does it).
|
|
10
|
+
*
|
|
11
|
+
* Lifted from terragucci's `report/azure-blob.ts` (c493e0e), without SAS
|
|
12
|
+
* links, version reads and version listing.
|
|
13
|
+
*/
|
|
14
|
+
import { createHmac } from "node:crypto";
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { defaultFetch, encodePath, StoreError, xmlText, type StoreFetch } from "./object-store";
|
|
17
|
+
|
|
18
|
+
/** The REST version every request names. */
|
|
19
|
+
export const AZURE_VERSION = "2021-08-06";
|
|
20
|
+
|
|
21
|
+
/** Entra ID's public cloud. */
|
|
22
|
+
export const AZURE_AUTHORITY = "https://login.microsoftonline.com";
|
|
23
|
+
|
|
24
|
+
const STORAGE_SCOPE = "https://storage.azure.com/.default";
|
|
25
|
+
|
|
26
|
+
export interface AzureLocation {
|
|
27
|
+
account: string;
|
|
28
|
+
container: string;
|
|
29
|
+
/** The account's blob endpoint, no trailing slash: https://<account>.blob.core.windows.net, or an emulator's http://host:10000/<account>. */
|
|
30
|
+
endpoint: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The job's federated identity: an Entra app or managed identity that trusts the forge's token. */
|
|
34
|
+
export interface AzureOidc {
|
|
35
|
+
tenantId: string;
|
|
36
|
+
clientId: string;
|
|
37
|
+
tokenFile: string;
|
|
38
|
+
/** Entra ID's address, https://host, no path. */
|
|
39
|
+
authority: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type AzureTarget = AzureLocation & ({ accountKey: string } | { oidc: AzureOidc } | { sas: string });
|
|
43
|
+
|
|
44
|
+
/** A blob's properties, as a HEAD answers them: never its body. */
|
|
45
|
+
export interface BlobHead {
|
|
46
|
+
exists: boolean;
|
|
47
|
+
etag?: string;
|
|
48
|
+
/** x-ms-lease-state: `leased` while a lease holds it. */
|
|
49
|
+
leaseState?: string;
|
|
50
|
+
/** Its metadata, by name without the x-ms-meta- prefix. */
|
|
51
|
+
metadata: Record<string, string>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const hmac64 = (key: string, s: string): string => createHmac("sha256", Buffer.from(key, "base64")).update(s, "utf8").digest("base64");
|
|
55
|
+
|
|
56
|
+
/** The headers whose values Shared Key signs by position, in order. */
|
|
57
|
+
const SIGNED = ["content-encoding", "content-language", "content-length", "content-md5", "content-type", "date", "if-modified-since", "if-match", "if-none-match", "if-unmodified-since", "range"];
|
|
58
|
+
|
|
59
|
+
/** The Shared Key Authorization header for a request. */
|
|
60
|
+
export function sharedKey(account: string, accountKey: string, method: string, rawUrl: string, headers: Record<string, string>): string {
|
|
61
|
+
const url = new URL(rawUrl);
|
|
62
|
+
const h = Object.fromEntries(Object.entries(headers).map(([k, v]) => [k.toLowerCase(), v]));
|
|
63
|
+
const fixed = SIGNED.map((n) => (n === "content-length" && h[n] === "0" ? "" : (h[n] ?? "")));
|
|
64
|
+
const ms = Object.keys(h).filter((n) => n.startsWith("x-ms-")).sort().map((n) => `${n}:${h[n]!.trim()}\n`).join("");
|
|
65
|
+
const params = new Map<string, string[]>();
|
|
66
|
+
for (const [k, v] of url.searchParams) params.set(k.toLowerCase(), [...(params.get(k.toLowerCase()) ?? []), v]);
|
|
67
|
+
const query = [...params.keys()].sort().map((k) => `\n${k}:${params.get(k)!.sort().join(",")}`).join("");
|
|
68
|
+
const toSign = [method, ...fixed].join("\n") + "\n" + ms + `/${account}${url.pathname}` + query;
|
|
69
|
+
return `SharedKey ${account}:${hmac64(accountKey, toSign)}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Entra ID's error, or the start of the body. */
|
|
73
|
+
const why = (text: string): string => {
|
|
74
|
+
try {
|
|
75
|
+
const j = JSON.parse(text) as { error?: string; error_description?: string };
|
|
76
|
+
if (j.error) return [j.error, j.error_description?.split("\r\n")[0]].filter(Boolean).join(": ");
|
|
77
|
+
} catch {
|
|
78
|
+
// Not JSON: Blob Storage answers in XML.
|
|
79
|
+
}
|
|
80
|
+
return [xmlText(text, "Code"), xmlText(text, "Message")?.split("\n")[0]].filter(Boolean).join(": ") || text.slice(0, 300);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export class AzureBlobClient {
|
|
84
|
+
private token?: Promise<{ token: string; expires: number }>;
|
|
85
|
+
|
|
86
|
+
constructor(readonly target: AzureTarget, private readonly fetchFn: StoreFetch = defaultFetch, private readonly readToken: (file: string) => string = (f) => readFileSync(f, "utf-8")) {}
|
|
87
|
+
|
|
88
|
+
private where(key: string): string {
|
|
89
|
+
return `az://${this.target.account}/${this.target.container}/${key}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private blobUrl(key: string, query = ""): string {
|
|
93
|
+
const t = this.target;
|
|
94
|
+
const q = [query, "sas" in t ? t.sas.replace(/^\?/, "") : ""].filter(Boolean).join("&");
|
|
95
|
+
return `${t.endpoint}/${t.container}/${encodePath(key)}${q ? `?${q}` : ""}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The storage token for the OIDC identity: once per client, again a minute before it expires. */
|
|
99
|
+
private async bearer(o: AzureOidc): Promise<string> {
|
|
100
|
+
const fetchToken = async (): Promise<{ token: string; expires: number }> => {
|
|
101
|
+
let assertion: string;
|
|
102
|
+
try {
|
|
103
|
+
assertion = this.readToken(o.tokenFile).trim();
|
|
104
|
+
} catch (e) {
|
|
105
|
+
throw new StoreError(`cannot read the OIDC token in ${o.tokenFile}: ${(e as Error).message}`);
|
|
106
|
+
}
|
|
107
|
+
if (!assertion) throw new StoreError(`the OIDC token file ${o.tokenFile} is empty`);
|
|
108
|
+
const body = new URLSearchParams({
|
|
109
|
+
client_id: o.clientId,
|
|
110
|
+
scope: STORAGE_SCOPE,
|
|
111
|
+
grant_type: "client_credentials",
|
|
112
|
+
client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
|
113
|
+
client_assertion: assertion,
|
|
114
|
+
}).toString();
|
|
115
|
+
const res = await this.fetchFn(`${o.authority}/${o.tenantId}/oauth2/v2.0/token`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded" }, body });
|
|
116
|
+
const text = await res.text();
|
|
117
|
+
if (!res.ok) throw new StoreError(`Entra ID refused the OIDC token for client ${o.clientId}: ${res.status} ${why(text)}`);
|
|
118
|
+
let j: { access_token?: string; expires_in?: number | string } = {};
|
|
119
|
+
try {
|
|
120
|
+
j = JSON.parse(text) as typeof j;
|
|
121
|
+
} catch {
|
|
122
|
+
// Reported below as no token.
|
|
123
|
+
}
|
|
124
|
+
if (!j.access_token) throw new StoreError(`Entra ID answered client ${o.clientId} without a token`);
|
|
125
|
+
return { token: j.access_token, expires: Date.now() + Number(j.expires_in ?? 3600) * 1000 };
|
|
126
|
+
};
|
|
127
|
+
const again = (): Promise<{ token: string; expires: number }> =>
|
|
128
|
+
(this.token = fetchToken().catch((e: unknown) => {
|
|
129
|
+
this.token = undefined;
|
|
130
|
+
throw e;
|
|
131
|
+
}));
|
|
132
|
+
let held = await (this.token ?? again());
|
|
133
|
+
if (held.expires - Date.now() < 60_000) held = await again();
|
|
134
|
+
return held.token;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The headers for one request, signed or with the bearer token. */
|
|
138
|
+
private async headers(method: string, url: string, extra: Record<string, string>): Promise<Record<string, string>> {
|
|
139
|
+
const h: Record<string, string> = { "x-ms-date": new Date().toUTCString(), "x-ms-version": AZURE_VERSION, ...extra };
|
|
140
|
+
const t = this.target;
|
|
141
|
+
if ("accountKey" in t) return { ...h, authorization: sharedKey(t.account, t.accountKey, method, url, h) };
|
|
142
|
+
if ("sas" in t) return h;
|
|
143
|
+
return { ...h, authorization: `Bearer ${await this.bearer(t.oidc)}` };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async get(key: string): Promise<string | undefined> {
|
|
147
|
+
const url = this.blobUrl(key);
|
|
148
|
+
const res = await this.fetchFn(url, { method: "GET", headers: await this.headers("GET", url, {}) });
|
|
149
|
+
if (res.status === 404) return undefined;
|
|
150
|
+
if (!res.ok) throw new StoreError(`GET ${this.where(key)}: ${res.status} ${why(await res.text())}`);
|
|
151
|
+
return res.text();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A blob's properties and metadata, from a HEAD. */
|
|
155
|
+
async head(key: string): Promise<BlobHead> {
|
|
156
|
+
const url = this.blobUrl(key);
|
|
157
|
+
const res = await this.fetchFn(url, { method: "HEAD", headers: await this.headers("HEAD", url, {}) });
|
|
158
|
+
if (res.status === 404) return { exists: false, metadata: {} };
|
|
159
|
+
if (!res.ok) throw new StoreError(`HEAD ${this.where(key)}: ${res.status}`);
|
|
160
|
+
const get = (n: string): string | undefined => res.headers?.get(n) ?? undefined;
|
|
161
|
+
const metadata: Record<string, string> = {};
|
|
162
|
+
const all = res.headers as unknown as { forEach?: (fn: (v: string, k: string) => void) => void } | undefined;
|
|
163
|
+
all?.forEach?.((v, k) => {
|
|
164
|
+
if (k.toLowerCase().startsWith("x-ms-meta-")) metadata[k.toLowerCase().slice("x-ms-meta-".length)] = v;
|
|
165
|
+
});
|
|
166
|
+
const etag = get("etag");
|
|
167
|
+
const leaseState = get("x-ms-lease-state");
|
|
168
|
+
return { exists: true, ...(etag ? { etag } : {}), ...(leaseState ? { leaseState } : {}), metadata };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Take a snapshot of a blob: its id, the time Azure gives it. */
|
|
172
|
+
async snapshot(key: string): Promise<string> {
|
|
173
|
+
const url = this.blobUrl(key, "comp=snapshot");
|
|
174
|
+
const res = await this.fetchFn(url, { method: "PUT", headers: await this.headers("PUT", url, { "content-length": "0" }) });
|
|
175
|
+
if (!res.ok) throw new StoreError(`snapshot of ${this.where(key)}: ${res.status} ${why(await res.text())}`);
|
|
176
|
+
const id = res.headers?.get("x-ms-snapshot");
|
|
177
|
+
if (!id) throw new StoreError(`snapshot of ${this.where(key)}: Azure answered without x-ms-snapshot`);
|
|
178
|
+
return id;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Write a block blob with metadata, under a lease when one holds it; with `ifNoneMatch`, only when there is none (false when one was there). */
|
|
182
|
+
async putBlob(key: string, body: string, contentType: string, options: { leaseId?: string; metadata?: Record<string, string>; ifNoneMatch?: boolean } = {}): Promise<boolean> {
|
|
183
|
+
const url = this.blobUrl(key);
|
|
184
|
+
const meta = Object.fromEntries(Object.entries(options.metadata ?? {}).map(([k, v]) => [`x-ms-meta-${k}`, v]));
|
|
185
|
+
const extra: Record<string, string> = {
|
|
186
|
+
"x-ms-blob-type": "BlockBlob",
|
|
187
|
+
"content-type": contentType,
|
|
188
|
+
"content-length": String(Buffer.byteLength(body)),
|
|
189
|
+
...meta,
|
|
190
|
+
...(options.leaseId ? { "x-ms-lease-id": options.leaseId } : {}),
|
|
191
|
+
...(options.ifNoneMatch ? { "if-none-match": "*" } : {}),
|
|
192
|
+
};
|
|
193
|
+
const res = await this.fetchFn(url, { method: "PUT", headers: await this.headers("PUT", url, extra), body });
|
|
194
|
+
if (options.ifNoneMatch && (res.status === 409 || res.status === 412)) return false;
|
|
195
|
+
if (!res.ok) throw new StoreError(`PUT ${this.where(key)}: ${res.status} ${why(await res.text())}`);
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Delete a blob. One that is not there is deleted already. */
|
|
200
|
+
async remove(key: string): Promise<void> {
|
|
201
|
+
const url = this.blobUrl(key);
|
|
202
|
+
const res = await this.fetchFn(url, { method: "DELETE", headers: await this.headers("DELETE", url, {}) });
|
|
203
|
+
if (!res.ok && res.status !== 404) throw new StoreError(`DELETE ${this.where(key)}: ${res.status} ${why(await res.text())}`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** A blob lease: acquire one that never expires with the id given (false when one holds it), or release it. */
|
|
207
|
+
async lease(key: string, action: "acquire" | "release", id: string): Promise<boolean> {
|
|
208
|
+
const url = this.blobUrl(key, "comp=lease");
|
|
209
|
+
const extra: Record<string, string> = { "x-ms-lease-action": action, "content-length": "0", ...(action === "acquire" ? { "x-ms-lease-duration": "-1", "x-ms-proposed-lease-id": id } : { "x-ms-lease-id": id }) };
|
|
210
|
+
const res = await this.fetchFn(url, { method: "PUT", headers: await this.headers("PUT", url, extra) });
|
|
211
|
+
if (action === "acquire" && res.status === 409) return false;
|
|
212
|
+
if (!res.ok) throw new StoreError(`${action} the lease of ${this.where(key)}: ${res.status} ${why(await res.text())}`);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Set a blob's metadata, all of it, under its lease when one holds it. */
|
|
217
|
+
async setMetadata(key: string, metadata: Record<string, string>, leaseId?: string): Promise<void> {
|
|
218
|
+
const url = this.blobUrl(key, "comp=metadata");
|
|
219
|
+
const extra = { "content-length": "0", ...Object.fromEntries(Object.entries(metadata).map(([k, v]) => [`x-ms-meta-${k}`, v])), ...(leaseId ? { "x-ms-lease-id": leaseId } : {}) };
|
|
220
|
+
const res = await this.fetchFn(url, { method: "PUT", headers: await this.headers("PUT", url, extra) });
|
|
221
|
+
if (!res.ok) throw new StoreError(`set the metadata of ${this.where(key)}: ${res.status} ${why(await res.text())}`);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One backend verdict for compose and state (chant#4478). The composition's
|
|
3
|
+
* `backend` fallback (#4438) and the state refusals (#4437) both come from
|
|
4
|
+
* `lockRefusal(readBackend(...))`, so the same root gets the same answer from
|
|
5
|
+
* each, before init (the code and flags) and after (what init recorded,
|
|
6
|
+
* compared with the code).
|
|
7
|
+
*/
|
|
8
|
+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { afterAll, describe, expect, test } from "vitest";
|
|
12
|
+
import type { DriverComponent } from "@intentius/chant/components/driver";
|
|
13
|
+
import { compositionFor, type CompositionInput } from "../compose";
|
|
14
|
+
import { rootState } from "../compose/hcl";
|
|
15
|
+
import type { TerraformConfig } from "../config";
|
|
16
|
+
import { LOCKABLE_BACKENDS, LOCKS, cliBackendConfig, declaredBackend, isRefused, lockRefusal, readBackend, stateObject } from "./backend";
|
|
17
|
+
|
|
18
|
+
const made: string[] = [];
|
|
19
|
+
afterAll(() => {
|
|
20
|
+
for (const d of made) rmSync(d, { recursive: true, force: true });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
function project(files: Record<string, string>): string {
|
|
24
|
+
const dir = mkdtempSync(join(tmpdir(), "chant-4478-"));
|
|
25
|
+
made.push(dir);
|
|
26
|
+
for (const [path, text] of Object.entries(files)) {
|
|
27
|
+
mkdirSync(dirname(join(dir, path)), { recursive: true });
|
|
28
|
+
writeFileSync(join(dir, path), text);
|
|
29
|
+
}
|
|
30
|
+
return dir;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const EMPTY = 'resource "terraform_data" "x" {}\n';
|
|
34
|
+
const tf = (name: string): DriverComponent => ({ name, dependsOn: [], deploy: [{ phase: "Apply", steps: [{ kind: "terraform-apply", root: name }] }] });
|
|
35
|
+
const record = (type: string, config: Record<string, unknown>): string => JSON.stringify({ version: 3, backend: { type, config, hash: 1 } });
|
|
36
|
+
|
|
37
|
+
/** Credentials for every lockable backend, so a lockable root resolves to a state rather than a credentials refusal. */
|
|
38
|
+
const CREDS: NodeJS.ProcessEnv = { AWS_ACCESS_KEY_ID: "AK", AWS_SECRET_ACCESS_KEY: "SK", GOOGLE_OAUTH_ACCESS_TOKEN: "t", ARM_ACCESS_KEY: Buffer.from("k").toString("base64") };
|
|
39
|
+
|
|
40
|
+
interface Case {
|
|
41
|
+
name: string;
|
|
42
|
+
/** Root `c`'s files, beside the plain roots `a` and `b`. */
|
|
43
|
+
files: Record<string, string>;
|
|
44
|
+
env?: NodeJS.ProcessEnv;
|
|
45
|
+
terraform?: Partial<TerraformConfig>;
|
|
46
|
+
/** The refusal kind both sides give, or undefined when chant locks it. */
|
|
47
|
+
refused?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const s3 = (body: string): string => `terraform {\n backend "s3" {\n bucket = "b"\n key = "c.tfstate"\n${body} }\n}\n`;
|
|
51
|
+
|
|
52
|
+
const CASES: Case[] = [
|
|
53
|
+
{ name: "s3 with use_lockfile = true in the code", files: { "main.tf": s3(" use_lockfile = true\n") } },
|
|
54
|
+
{ name: "s3 with no lock file", files: { "main.tf": s3("") }, refused: "no-lockfile" },
|
|
55
|
+
{ name: "s3 whose use_lockfile = true reaches init only through TF_CLI_ARGS_init", files: { "main.tf": s3("") }, env: { TF_CLI_ARGS_init: "-input=false -backend-config=use_lockfile=true" } },
|
|
56
|
+
{ name: "s3 whose use_lockfile = true is in backendConfig", files: { "main.tf": s3("") }, terraform: { roots: { a: { dir: "roots/a" }, b: { dir: "roots/b" }, c: { dir: "roots/c", backendConfig: { use_lockfile: "true" } } } } },
|
|
57
|
+
{ name: "gcs", files: { "main.tf": 'terraform {\n backend "gcs" {\n bucket = "b"\n prefix = "c"\n }\n}\n' } },
|
|
58
|
+
{ name: "azurerm", files: { "main.tf": 'terraform {\n backend "azurerm" {\n storage_account_name = "acct"\n container_name = "tfstate"\n key = "c.tfstate"\n }\n}\n' } },
|
|
59
|
+
{ name: "local, declared", files: { "main.tf": 'terraform {\n backend "local" {}\n}\n' } },
|
|
60
|
+
{ name: "local, by declaring nothing", files: { "main.tf": EMPTY } },
|
|
61
|
+
{ name: "a cloud block", files: { "main.tf": 'terraform {\n cloud {\n organization = "o"\n }\n}\n' }, refused: "cloud" },
|
|
62
|
+
{ name: "a cloud block in a .tofu file of a tofu root", files: { "main.tofu": 'terraform {\n cloud {\n organization = "o"\n }\n}\n' }, terraform: { binary: "tofu" }, refused: "cloud" },
|
|
63
|
+
{ name: "another backend", files: { "main.tf": 'terraform {\n backend "consul" {\n path = "c"\n }\n}\n' }, refused: "backend" },
|
|
64
|
+
{
|
|
65
|
+
name: "s3 whose use_lockfile = true is only in a backend-config file (chant#4459)",
|
|
66
|
+
files: { "main.tf": s3(""), "backend.hcl": "# the lock\nuse_lockfile = true\n" },
|
|
67
|
+
terraform: { roots: { a: { dir: "roots/a" }, b: { dir: "roots/b" }, c: { dir: "roots/c", backendConfigFiles: ["backend.hcl"] } } },
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "init recorded another bucket than the code names",
|
|
71
|
+
files: { "main.tf": s3(" use_lockfile = true\n"), ".terraform/terraform.tfstate": record("s3", { bucket: "elsewhere", key: "c.tfstate", use_lockfile: true }) },
|
|
72
|
+
refused: "disagree",
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "init recorded s3 and the code now names gcs",
|
|
76
|
+
files: { "main.tf": 'terraform {\n backend "gcs" {\n bucket = "b"\n }\n}\n', ".terraform/terraform.tfstate": record("s3", { bucket: "b", key: "k", use_lockfile: true }) },
|
|
77
|
+
refused: "disagree",
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
name: "init recorded what the code names, plus defaults the code leaves out",
|
|
81
|
+
files: { "main.tf": s3(" use_lockfile = true\n"), ".terraform/terraform.tfstate": record("s3", { bucket: "b", key: "c.tfstate", use_lockfile: true, region: "us-east-1", workspace_key_prefix: null }) },
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
|
|
85
|
+
function both(c: Case): { fallback?: string; state: ReturnType<typeof stateObject> } {
|
|
86
|
+
const dir = project({ "roots/a/main.tf": EMPTY, "roots/b/main.tf": EMPTY, ...Object.fromEntries(Object.entries(c.files).map(([f, t]) => [`roots/c/${f}`, t])) });
|
|
87
|
+
const env = { ...CREDS, ...c.env };
|
|
88
|
+
const terraform: TerraformConfig = { roots: { a: { dir: "roots/a" }, b: { dir: "roots/b" }, c: { dir: "roots/c" } }, compose: { enabled: true }, ...c.terraform } as TerraformConfig;
|
|
89
|
+
const input: CompositionInput = { projectRoot: dir, components: [tf("a"), tf("b"), tf("c")], env, terraform };
|
|
90
|
+
const fallback = compositionFor(input).fallbacks.find((f) => f.member === "c" && f.kind === "backend")?.reason;
|
|
91
|
+
const state = stateObject(join(dir, "roots/c"), env, undefined, terraform.binary, {
|
|
92
|
+
...(terraform.roots.c?.backendConfig ? { backendConfig: terraform.roots.c.backendConfig } : {}),
|
|
93
|
+
...(terraform.roots.c?.backendConfigFiles ? { backendConfigFiles: terraform.roots.c.backendConfigFiles } : {}),
|
|
94
|
+
});
|
|
95
|
+
return { ...(fallback ? { fallback } : {}), state };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
describe("compose and state give one verdict per root (chant#4478)", () => {
|
|
99
|
+
for (const c of CASES) {
|
|
100
|
+
test(c.name, () => {
|
|
101
|
+
const { fallback, state } = both(c);
|
|
102
|
+
if (c.refused === undefined) {
|
|
103
|
+
expect(fallback).toBeUndefined();
|
|
104
|
+
expect(isRefused(state)).toBe(false);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
expect(state).toMatchObject({ refused: c.refused });
|
|
108
|
+
expect(fallback).toBe(`\`c\`: ${isRefused(state) ? state.reason : ""}`);
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
test("a disagreement names both sides", () => {
|
|
113
|
+
const { state } = both(CASES.find((c) => c.name.startsWith("init recorded another bucket"))!);
|
|
114
|
+
expect(isRefused(state) && state.reason).toContain('bucket = "b" in the code and flags, bucket = "elsewhere" in what init recorded');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("a disagreement over a secret names the key, not its values", () => {
|
|
118
|
+
const dir = project({ "main.tf": 'terraform {\n backend "s3" {\n bucket = "b"\n key = "k"\n use_lockfile = true\n secret_key = "one"\n }\n}\n' });
|
|
119
|
+
const reading = readBackend(dir, { recorded: { type: "s3", config: { bucket: "b", key: "k", use_lockfile: true, secret_key: "two" } } });
|
|
120
|
+
expect(reading.disagreement).toContain("secret_key (value hidden)");
|
|
121
|
+
expect(reading.disagreement).not.toContain("one");
|
|
122
|
+
expect(reading.disagreement).not.toContain("two");
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("the parts of the verdict", () => {
|
|
127
|
+
test("the lockable list is the one LOCKS spells", () => {
|
|
128
|
+
expect(LOCKABLE_BACKENDS).toEqual(["s3", "gcs", "azurerm", "local"]);
|
|
129
|
+
expect(LOCKS).toBe("chant locks and writes s3 (with use_lockfile = true), gcs, azurerm and local states");
|
|
130
|
+
for (const type of LOCKABLE_BACKENDS) expect(lockRefusal({ backend: { type, config: { use_lockfile: true } } })).toBeUndefined();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("TF_CLI_ARGS and TF_CLI_ARGS_init -backend-config flags, pairs and files, later over earlier", () => {
|
|
134
|
+
expect(cliBackendConfig({ TF_CLI_ARGS: "-backend-config=region=eu-west-1 -backend-config backend.hcl", TF_CLI_ARGS_init: '-backend-config="region=us-east-2" --backend-config=use_lockfile=true' })).toMatchObject({
|
|
135
|
+
pairs: { region: "us-east-2", use_lockfile: "true" },
|
|
136
|
+
files: ["backend.hcl"],
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("the declared backend: code, then TF_CLI_ARGS*, then backendConfig", () => {
|
|
141
|
+
const dir = project({ "main.tf": 'terraform {\n backend "s3" {\n bucket = "b"\n key = "k"\n assume_role {\n role_arn = "nested"\n }\n }\n}\n' });
|
|
142
|
+
expect(declaredBackend(dir, { env: { TF_CLI_ARGS_init: "-backend-config=key=from-env" }, backendConfig: { use_lockfile: "true" } })).toEqual({
|
|
143
|
+
type: "s3",
|
|
144
|
+
config: { bucket: "b", key: "from-env", use_lockfile: true },
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("the declared backend in Terraform's JSON syntax", () => {
|
|
149
|
+
const dir = project({ "main.tf.json": JSON.stringify({ terraform: { backend: { s3: { bucket: "b", key: "k", use_lockfile: true } } } }) });
|
|
150
|
+
expect(declaredBackend(dir)).toEqual({ type: "s3", config: { bucket: "b", key: "k", use_lockfile: true } });
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe("compose's state address comes from the same verdict (chant#4478 round 2)", () => {
|
|
155
|
+
const S3_NO_KEY = 'terraform {\n backend "s3" {\n bucket = "b"\n use_lockfile = true\n }\n}\n';
|
|
156
|
+
const reader = 'data "terraform_remote_state" "a" {\n backend = "s3"\n config = {\n bucket = "b"\n key = "a.tfstate"\n }\n}\n\nresource "terraform_data" "x" {\n input = data.terraform_remote_state.a.outputs.id\n}\n';
|
|
157
|
+
|
|
158
|
+
test("a key only TF_CLI_ARGS_init gives is the root's state address, as stateObject reads it", () => {
|
|
159
|
+
const dir = project({ "main.tf": S3_NO_KEY });
|
|
160
|
+
const env = { ...CREDS, TF_CLI_ARGS_init: "-backend-config=key=from-init-args.tfstate" };
|
|
161
|
+
expect(rootState(dir, "app", { env, recorded: null }).own).toEqual({ bucket: "b", key: "from-init-args.tfstate" });
|
|
162
|
+
expect(stateObject(dir, env, undefined)).toMatchObject({ backend: "s3", bucket: "b", key: "from-init-args.tfstate" });
|
|
163
|
+
expect(rootState(dir, "app", { env: {}, recorded: null }).own).toBeUndefined();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a key TF_CLI_ARGS gives is overridden by TF_CLI_ARGS_init, as init applies them", () => {
|
|
167
|
+
const dir = project({ "main.tf": S3_NO_KEY });
|
|
168
|
+
const env = { TF_CLI_ARGS: "-backend-config=key=first.tfstate", TF_CLI_ARGS_init: "-backend-config=key=second.tfstate" };
|
|
169
|
+
expect(rootState(dir, "app", { env, recorded: null }).own).toEqual({ bucket: "b", key: "second.tfstate" });
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("after init, the recorded configuration names the state", () => {
|
|
173
|
+
const dir = project({ "main.tf": S3_NO_KEY, ".terraform/terraform.tfstate": record("s3", { bucket: "b", key: "recorded.tfstate", use_lockfile: true, region: null }) });
|
|
174
|
+
expect(rootState(dir, "app", { env: {} }).own).toEqual({ bucket: "b", key: "recorded.tfstate" });
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("a composition lines a read up with a member whose key comes only from TF_CLI_ARGS_init", () => {
|
|
178
|
+
const dir = project({ "roots/a/main.tf": S3_NO_KEY, "roots/b/main.tf": S3_NO_KEY + reader });
|
|
179
|
+
const c = compositionFor({
|
|
180
|
+
projectRoot: dir,
|
|
181
|
+
env: { TF_CLI_ARGS_init: "-backend-config=key=a.tfstate" },
|
|
182
|
+
components: [tf("a"), tf("b")],
|
|
183
|
+
terraform: { roots: { a: { dir: "roots/a" }, b: { dir: "roots/b" } }, compose: { enabled: true } },
|
|
184
|
+
});
|
|
185
|
+
expect(c.reads.get("b")).toEqual([{ via: "remote-state", upstream: "a", name: "a" }]);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a root's state is (#4437). Carried over from terragucci at c493e0e:
|
|
3
|
+
* test/state-versions.test.ts "where a root's state is" and
|
|
4
|
+
* test/state-stores.test.ts "where a gcs or azurerm root's state is", with
|
|
5
|
+
* the refusals chant adds (no lock file, a cloud block, any other backend).
|
|
6
|
+
*/
|
|
7
|
+
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { dirname, join } from "node:path";
|
|
11
|
+
import { describe, expect, it } from "vitest";
|
|
12
|
+
import { initialisedBackend, isRefused, s3StateKey, stateLocation, stateObject, workspaceOf } from "./backend";
|
|
13
|
+
|
|
14
|
+
const tmp = (): string => mkdtempSync(join(tmpdir(), "chant-state-"));
|
|
15
|
+
const write = (dir: string, files: Record<string, string>): string => {
|
|
16
|
+
for (const [f, text] of Object.entries(files)) {
|
|
17
|
+
mkdirSync(dirname(join(dir, f)), { recursive: true });
|
|
18
|
+
writeFileSync(join(dir, f), text);
|
|
19
|
+
}
|
|
20
|
+
return dir;
|
|
21
|
+
};
|
|
22
|
+
/** A root whose `.terraform` records `type` and `config`, as init leaves it. */
|
|
23
|
+
const initialised = (type: string, config: Record<string, unknown>, files: Record<string, string> = {}): string =>
|
|
24
|
+
write(tmp(), { ".terraform/terraform.tfstate": JSON.stringify({ version: 3, backend: { type, config, hash: 1 } }), ...files });
|
|
25
|
+
|
|
26
|
+
const ENV = { AWS_ACCESS_KEY_ID: "AK", AWS_SECRET_ACCESS_KEY: "SK", AWS_REGION: "us-east-1" };
|
|
27
|
+
const S3_CONFIG = { bucket: "acme-state", key: "envs/dev/platform.tfstate", region: "eu-west-1", use_lockfile: true, endpoints: { s3: "http://floci:4566", dynamodb: null }, access_key: null, workspace_key_prefix: null };
|
|
28
|
+
const GCS = { bucket: "acme-state", prefix: "envs/dev/app", storage_custom_endpoint: "http://gcs.test/storage/v1/", access_token: "backend-token" };
|
|
29
|
+
const AZ = { storage_account_name: "acme", container_name: "tfstate", key: "app.tfstate", access_key: Buffer.from("key").toString("base64") };
|
|
30
|
+
|
|
31
|
+
describe("where an s3 or local root's state is", () => {
|
|
32
|
+
it("reads the backend init recorded, s3 with its bucket, key, region and endpoint", () => {
|
|
33
|
+
const dir = initialised("s3", S3_CONFIG);
|
|
34
|
+
expect(initialisedBackend(dir)?.type).toBe("s3");
|
|
35
|
+
const o = stateObject(dir, ENV);
|
|
36
|
+
expect(o).toMatchObject({ backend: "s3", bucket: "acme-state", key: "envs/dev/platform.tfstate", region: "eu-west-1", endpoint: "http://floci:4566", lockfile: true });
|
|
37
|
+
expect(stateLocation(o)).toBe("s3://acme-state/envs/dev/platform.tfstate");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("takes the endpoint from AWS_ENDPOINT_URL when the backend names none, and the key of another workspace", () => {
|
|
41
|
+
const dir = initialised("s3", { ...S3_CONFIG, endpoints: null, workspace_key_prefix: "ws" });
|
|
42
|
+
expect(stateObject(dir, { ...ENV, AWS_ENDPOINT_URL: "http://floci:4566/", TF_WORKSPACE: "blue" })).toMatchObject({ endpoint: "http://floci:4566", key: "ws/blue/envs/dev/platform.tfstate" });
|
|
43
|
+
expect(s3StateKey("k", "default")).toBe("k");
|
|
44
|
+
expect(s3StateKey("k", "green")).toBe("env:/green/k");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("reads the workspace select recorded, and TF_DATA_DIR in place of .terraform", () => {
|
|
48
|
+
const dir = write(tmp(), { "data/terraform.tfstate": JSON.stringify({ backend: { type: "s3", config: S3_CONFIG } }), "data/environment": "green\n" });
|
|
49
|
+
expect(workspaceOf(dir, { TF_DATA_DIR: "data" })).toBe("green");
|
|
50
|
+
expect(stateObject(dir, { ...ENV, TF_DATA_DIR: "data" })).toMatchObject({ key: "env:/green/envs/dev/platform.tfstate" });
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("signs with the backend's own static keys when its configuration names them", () => {
|
|
54
|
+
const o = stateObject(initialised("s3", { ...S3_CONFIG, access_key: "BACKENDKEY", secret_key: "s" }), ENV);
|
|
55
|
+
expect(o).toMatchObject({ target: { accessKeyId: "BACKENDKEY", secretAccessKey: "s" } });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("is a local file when init recorded no backend or a local one", () => {
|
|
59
|
+
expect(stateObject(tmp(), ENV)).toEqual({ backend: "local", path: "terraform.tfstate" });
|
|
60
|
+
expect(stateObject(initialised("local", { path: "state/x.tfstate" }), ENV)).toEqual({ backend: "local", path: "state/x.tfstate" });
|
|
61
|
+
expect(stateObject(initialised("local", {}), { TF_WORKSPACE: "blue" })).toEqual({ backend: "local", path: join("terraform.tfstate.d", "blue", "terraform.tfstate") });
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("where a gcs or azurerm root's state is", () => {
|
|
66
|
+
it("names a gcs state <prefix>/<workspace>.tfstate, its lock beside it, with the backend's own credentials first", () => {
|
|
67
|
+
expect(stateObject(initialised("gcs", GCS), { GOOGLE_APPLICATION_CREDENTIALS: "/job/creds.json" })).toMatchObject({ backend: "gcs", bucket: "acme-state", key: "envs/dev/app/default.tfstate", lock: "envs/dev/app/default.tflock", target: { endpoint: "http://gcs.test", accessToken: "backend-token" } });
|
|
68
|
+
expect(stateObject(initialised("gcs", { bucket: "b", prefix: "p/" }), { TF_WORKSPACE: "blue", GOOGLE_APPLICATION_CREDENTIALS: "/job/creds.json" })).toMatchObject({ key: "p/blue.tfstate", lock: "p/blue.tflock", target: { endpoint: "https://storage.googleapis.com", credentialsFile: "/job/creds.json" } });
|
|
69
|
+
expect(stateObject(initialised("gcs", { bucket: "b" }), { GOOGLE_CREDENTIALS: '{"type":"service_account"}' })).toMatchObject({ key: "default.tfstate", target: { credentialsJson: '{"type":"service_account"}' } });
|
|
70
|
+
expect(stateObject(initialised("gcs", { bucket: "b", impersonate_service_account: "sa@p.iam.gserviceaccount.com" }), { GOOGLE_OAUTH_ACCESS_TOKEN: "t" })).toMatchObject({ target: { accessToken: "t", impersonate: "sa@p.iam.gserviceaccount.com" } });
|
|
71
|
+
expect(stateObject(initialised("gcs", { bucket: "b" }), {})).toMatchObject({ backend: "gcs", refused: "credentials" });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("names an azurerm blob by its key, <key>env:<workspace> in another workspace, at the cloud's endpoint, with the key, a SAS or the job's OIDC identity", () => {
|
|
75
|
+
expect(stateObject(initialised("azurerm", AZ), {})).toMatchObject({ backend: "azurerm", account: "acme", container: "tfstate", key: "app.tfstate", endpoint: "https://acme.blob.core.windows.net", auth: { accountKey: AZ.access_key } });
|
|
76
|
+
expect(stateObject(initialised("azurerm", { ...AZ, access_key: null, environment: "usgovernment" }), { TF_WORKSPACE: "blue", ARM_SAS_TOKEN: "sv=x&sig=y" })).toMatchObject({ key: "app.tfstateenv:blue", endpoint: "https://acme.blob.core.usgovcloudapi.net", auth: { sas: "sv=x&sig=y" } });
|
|
77
|
+
const oidc = { ARM_TENANT_ID: "tenant", ARM_CLIENT_ID: "client", ARM_OIDC_TOKEN_FILE_PATH: "/t" };
|
|
78
|
+
expect(stateObject(initialised("azurerm", { ...AZ, access_key: null, snapshot: true }), oidc)).toMatchObject({ snapshot: true, auth: { oidc: { tenantId: "tenant", clientId: "client", tokenFile: "/t", authority: "https://login.microsoftonline.com" } } });
|
|
79
|
+
expect(stateObject(initialised("azurerm", { ...AZ, access_key: null }), {})).toMatchObject({ refused: "credentials" });
|
|
80
|
+
expect(stateObject(initialised("azurerm", { key: "k" }), {})).toMatchObject({ refused: "config", reason: expect.stringContaining("storage_account_name, container_name or key") });
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("a state chant does not lock", () => {
|
|
85
|
+
it("is an s3 backend without use_lockfile, named as such", () => {
|
|
86
|
+
for (const use_lockfile of [undefined, false]) {
|
|
87
|
+
const o = stateObject(initialised("s3", { ...S3_CONFIG, use_lockfile }), ENV);
|
|
88
|
+
expect(o).toMatchObject({ backend: "s3", refused: "no-lockfile", reason: expect.stringContaining("use_lockfile = true is unset") });
|
|
89
|
+
expect(stateLocation(o)).toBeUndefined();
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("is a root with a cloud block, in .tf or .tf.json code, or one init recorded as cloud", () => {
|
|
94
|
+
const hcl = write(tmp(), { "main.tf": 'terraform {\n cloud {\n organization = "acme"\n }\n}\n' });
|
|
95
|
+
expect(stateObject(hcl, ENV)).toMatchObject({ refused: "cloud", reason: expect.stringContaining("cloud block") });
|
|
96
|
+
const json = write(tmp(), { "cdk.tf.json": JSON.stringify({ terraform: [{ required_version: ">= 1.10" }, { cloud: { organization: "acme" } }] }) });
|
|
97
|
+
expect(stateObject(json, ENV)).toMatchObject({ refused: "cloud" });
|
|
98
|
+
expect(stateObject(initialised("cloud", { organization: "acme" }), ENV)).toMatchObject({ backend: "cloud", refused: "cloud" });
|
|
99
|
+
// A cloud block added after init recorded s3: the code and the record disagree, and the refusal names both (chant#4478).
|
|
100
|
+
expect(stateObject(initialised("s3", S3_CONFIG, { "main.tf": "terraform {\n cloud {}\n}\n" }), ENV)).toMatchObject({
|
|
101
|
+
refused: "disagree",
|
|
102
|
+
reason: expect.stringContaining("names the cloud backend and init recorded s3"),
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("is any other backend, named by its type", () => {
|
|
107
|
+
for (const type of ["pg", "kubernetes", "consul", "http", "remote", "cos"]) {
|
|
108
|
+
const o = stateObject(initialised(type, {}), ENV);
|
|
109
|
+
expect(isRefused(o)).toBe(true);
|
|
110
|
+
expect(o).toMatchObject({ backend: type, refused: "backend", reason: expect.stringContaining(`backend is ${type}`) });
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("is an s3 backend without its bucket or key, or without credentials", () => {
|
|
115
|
+
expect(stateObject(initialised("s3", { ...S3_CONFIG, key: null }), ENV)).toMatchObject({ refused: "config" });
|
|
116
|
+
expect(stateObject(initialised("s3", S3_CONFIG), {})).toMatchObject({ refused: "credentials", reason: expect.stringContaining("s3://acme-state") });
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("the state module", () => {
|
|
121
|
+
it("is plain lexicon code: nothing in it imports from @intentius/chant/components", () => {
|
|
122
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
123
|
+
const sources = readdirSync(here).filter((f) => f.endsWith(".ts") && !f.endsWith(".test.ts"));
|
|
124
|
+
expect(sources.length).toBeGreaterThan(5);
|
|
125
|
+
for (const f of sources) expect(readFileSync(join(here, f), "utf-8"), f).not.toMatch(/@intentius\/chant\/components/);
|
|
126
|
+
});
|
|
127
|
+
});
|