@intentius/chant-lexicon-terraform 0.124.96 → 0.124.98

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.
Files changed (104) hide show
  1. package/dist/behaviour/reference-catalog.d.ts +42 -6
  2. package/dist/behaviour/reference-catalog.d.ts.map +1 -1
  3. package/dist/behaviour/reference-catalog.js +169 -8
  4. package/dist/describe-resources.js +1 -1
  5. package/dist/hcl/evaluate.d.ts +151 -0
  6. package/dist/hcl/evaluate.d.ts.map +1 -0
  7. package/dist/hcl/evaluate.js +940 -0
  8. package/dist/hcl/instances.d.ts +11 -1
  9. package/dist/hcl/instances.d.ts.map +1 -1
  10. package/dist/hcl/instances.js +89 -3
  11. package/dist/hcl/parse.d.ts +23 -1
  12. package/dist/hcl/parse.d.ts.map +1 -1
  13. package/dist/hcl/parse.js +9 -3
  14. package/dist/hcl/redact.d.ts +5 -1
  15. package/dist/hcl/redact.d.ts.map +1 -1
  16. package/dist/hcl/redact.js +7 -3
  17. package/dist/hcl/references.d.ts +12 -1
  18. package/dist/hcl/references.d.ts.map +1 -1
  19. package/dist/hcl/references.js +33 -16
  20. package/dist/integrity.json +6 -6
  21. package/dist/lint/post-synth/tf001.d.ts +4 -1
  22. package/dist/lint/post-synth/tf001.d.ts.map +1 -1
  23. package/dist/lint/post-synth/tf001.js +20 -8
  24. package/dist/lint/post-synth/tf002.d.ts +11 -5
  25. package/dist/lint/post-synth/tf002.d.ts.map +1 -1
  26. package/dist/lint/post-synth/tf002.js +42 -8
  27. package/dist/lint/post-synth/tf006.d.ts +7 -3
  28. package/dist/lint/post-synth/tf006.d.ts.map +1 -1
  29. package/dist/lint/post-synth/tf006.js +9 -3
  30. package/dist/lint/post-synth/tf022.d.ts +6 -0
  31. package/dist/lint/post-synth/tf022.d.ts.map +1 -1
  32. package/dist/lint/post-synth/tf022.js +18 -1
  33. package/dist/lint/secret-shape.d.ts +8 -0
  34. package/dist/lint/secret-shape.d.ts.map +1 -1
  35. package/dist/lint/secret-shape.js +76 -0
  36. package/dist/manifest.json +1 -1
  37. package/dist/plugin.d.ts.map +1 -1
  38. package/dist/plugin.js +7 -2
  39. package/dist/rules/tf001.ts +21 -7
  40. package/dist/rules/tf002.ts +44 -7
  41. package/dist/rules/tf006.ts +8 -3
  42. package/dist/rules/tf022.ts +18 -1
  43. package/package.json +2 -2
  44. package/src/__fixtures__/resolved-arguments/build-trusted/extras.tf +28 -0
  45. package/src/__fixtures__/resolved-arguments/build-trusted/main.tf +22 -0
  46. package/src/__fixtures__/resolved-arguments/build-trusted/serviceaccounts.tf +26 -0
  47. package/src/__fixtures__/resolved-arguments/modules/gke-cluster/main.tf +15 -0
  48. package/src/__fixtures__/resolved-arguments/modules/gke-cluster/variables.tf +12 -0
  49. package/src/__fixtures__/resolved-arguments/modules/gke-project/main.tf +9 -0
  50. package/src/__fixtures__/resolved-arguments/modules/gke-project/outputs.tf +9 -0
  51. package/src/__fixtures__/resolved-arguments/modules/gke-project/variables.tf +7 -0
  52. package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/main.tf +16 -0
  53. package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/outputs.tf +4 -0
  54. package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/variables.tf +21 -0
  55. package/src/behaviour/reference-catalog.plugin.test.ts +101 -2
  56. package/src/behaviour/reference-catalog.ts +162 -9
  57. package/src/describe-resources.ts +1 -1
  58. package/src/hcl/edges.test.ts +1 -1
  59. package/src/hcl/evaluate.test.ts +215 -0
  60. package/src/hcl/evaluate.ts +899 -0
  61. package/src/hcl/instances.ts +89 -3
  62. package/src/hcl/parse.test.ts +18 -0
  63. package/src/hcl/parse.ts +28 -3
  64. package/src/hcl/redact.ts +7 -3
  65. package/src/hcl/references.test.ts +68 -0
  66. package/src/hcl/references.ts +31 -15
  67. package/src/lint/post-synth/fixtures/TF001/negative-second-block.tf +17 -0
  68. package/src/lint/post-synth/fixtures/TF002/module-cycle/cdn/back/main.tf +3 -0
  69. package/src/lint/post-synth/fixtures/TF002/module-cycle/cdn/main.tf +3 -0
  70. package/src/lint/post-synth/fixtures/TF002/module-cycle/main.tf +14 -0
  71. package/src/lint/post-synth/fixtures/TF002/module-nested/cdn/edge/main.tf +8 -0
  72. package/src/lint/post-synth/fixtures/TF002/module-nested/cdn/main.tf +3 -0
  73. package/src/lint/post-synth/fixtures/TF002/module-nested/main.tf +14 -0
  74. package/src/lint/post-synth/fixtures/TF002/module-other-provider/cdn/main.tf +8 -0
  75. package/src/lint/post-synth/fixtures/TF002/module-other-provider/main.tf +14 -0
  76. package/src/lint/post-synth/fixtures/TF002/module-pinned/cdn/main.tf +12 -0
  77. package/src/lint/post-synth/fixtures/TF002/module-pinned/main.tf +14 -0
  78. package/src/lint/post-synth/fixtures/TF002/module-unpinned/cdn/main.tf +7 -0
  79. package/src/lint/post-synth/fixtures/TF002/module-unpinned/main.tf +14 -0
  80. package/src/lint/post-synth/fixtures/TF006/negative-null-default.tf +12 -0
  81. package/src/lint/post-synth/fixtures/TF006/positive-empty-default.tf +0 -6
  82. package/src/lint/post-synth/fixtures/TF007/negative-names.tf +25 -0
  83. package/src/lint/post-synth/fixtures/TF022/negative-secret-reference.tf +32 -0
  84. package/src/lint/post-synth/fixtures/TF022/positive-secret-data.tf +8 -0
  85. package/src/lint/post-synth/fixtures/TF024/live-backend-second-block/a-versions.tf +3 -0
  86. package/src/lint/post-synth/fixtures/TF024/live-backend-second-block/b-backend.tf +10 -0
  87. package/src/lint/post-synth/fixtures/TF024/live-cloud-second-block/a-versions.tf +3 -0
  88. package/src/lint/post-synth/fixtures/TF024/live-cloud-second-block/b-cloud.tf +9 -0
  89. package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/a-versions.tf +3 -0
  90. package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/b-backend.tf +6 -0
  91. package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/estate.chdf.hcl +1 -0
  92. package/src/lint/post-synth/module-rules.test.ts +1 -1
  93. package/src/lint/post-synth/post-synth.test.ts +39 -0
  94. package/src/lint/post-synth/single-block.test.ts +23 -2
  95. package/src/lint/post-synth/tf001.ts +21 -7
  96. package/src/lint/post-synth/tf002.test.ts +45 -1
  97. package/src/lint/post-synth/tf002.ts +44 -7
  98. package/src/lint/post-synth/tf006.ts +8 -3
  99. package/src/lint/post-synth/tf022.ts +18 -1
  100. package/src/lint/post-synth/tf024.test.ts +21 -0
  101. package/src/lint/secret-shape.test.ts +30 -0
  102. package/src/lint/secret-shape.ts +75 -0
  103. package/src/plugin.ts +6 -2
  104. package/src/redact.test.ts +8 -0
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Arguments evaluated from the files a plan reads (chant #5117).
3
+ *
4
+ * `src/__fixtures__/resolved-arguments/` holds trimmed copies of two cases
5
+ * from kubernetes/k8s.io (Apache-2.0): a Workload Identity service account
6
+ * made through a `for_each` module call over a local map, whose project id
7
+ * comes through another module's output, and a GKE cluster named through a
8
+ * local and a module variable, whose `count` compares a variable with `==`
9
+ * to pick the prod or the test block. `build-trusted/extras.tf` is not from
10
+ * that repository; it holds a second call of the cluster module and
11
+ * arguments that must stay unresolved.
12
+ */
13
+
14
+ import { join } from "node:path";
15
+ import { beforeAll, describe, expect, it } from "vitest";
16
+ import { buildGraphIr, withInstanceNodes, type GraphIR, type IRNode } from "@intentius/chant/graph-ir";
17
+ import type { Declarable } from "@intentius/chant/declarable";
18
+ import { renderTerraformRoots } from "./roots";
19
+ import { RootEvaluator, evaluateArguments, firstUnknown, isUnknown } from "./evaluate";
20
+ import { terraformDeclaredNode } from "../behaviour/reference-catalog";
21
+ import { terraformEntity, RESOURCE_TYPE, VARIABLE_TYPE, LOCALS_TYPE, MODULE_TYPE, OUTPUT_TYPE } from "./parse";
22
+
23
+ const TREE = join(import.meta.dirname, "..", "__fixtures__", "resolved-arguments");
24
+ const SA_BLOCK = "bt/module.workload_identity_service_accounts/google_service_account.serviceaccount";
25
+ const SA = (key: string) => `bt/module.workload_identity_service_accounts["${key}"]/google_service_account.serviceaccount`;
26
+ const CLUSTER = "bt/module.prow_build_cluster/google_container_cluster.prod_cluster";
27
+ const TEST_CLUSTER = "bt/module.prow_build_cluster/google_container_cluster.test_cluster";
28
+ const CANARY = (block: string) => `bt/module.canary_cluster/google_container_cluster.${block}`;
29
+
30
+ let ir: GraphIR;
31
+ let byId: Map<string, IRNode>;
32
+ const view = (id: string) => {
33
+ const n = byId.get(id);
34
+ expect(n, id).toBeDefined();
35
+ return terraformDeclaredNode(n!, n!.instanceOf ? byId.get(n!.instanceOf) : undefined);
36
+ };
37
+ const evaluatedOf = (id: string) => (byId.get(id)!.attrs as { evaluated?: Record<string, unknown> }).evaluated;
38
+
39
+ beforeAll(async () => {
40
+ const rendered = await renderTerraformRoots({ projectRoot: TREE, roots: { bt: { dir: "./build-trusted" } } });
41
+ expect(rendered.warnings).toEqual([]);
42
+ const entities: Map<string, Declarable> = rendered.entities;
43
+ ir = withInstanceNodes(buildGraphIr(entities), entities);
44
+ byId = new Map(ir.nodes.map((n) => [n.id, n]));
45
+ });
46
+
47
+ describe("the Workload Identity service account", () => {
48
+ it("expands the module call over a local map whose values interpolate another local", () => {
49
+ expect(ir.nodes.filter((n) => n.instanceOf === SA_BLOCK).map((n) => n.id).sort()).toEqual([
50
+ SA("gcb-builder"),
51
+ SA("kubernetes-external-secrets"),
52
+ SA("prow-build-trusted"),
53
+ ]);
54
+ });
55
+
56
+ it("resolves account_id from each.key and project through the gke-project module's output", () => {
57
+ expect(view(SA("prow-build-trusted"))).toEqual({
58
+ kind: "google_service_account",
59
+ attrs: { account_id: "prow-build-trusted", project: "k8s-infra-prow-build-trusted", display_name: "default service account for pods in prow-build-trusted" },
60
+ nonLiteral: 0,
61
+ });
62
+ expect(view(SA("gcb-builder"))?.attrs).toEqual({ account_id: "gcb-builder", project: "k8s-infra-prow-build-trusted", display_name: "trigger GCB builds in all k8s-staging projects" });
63
+ });
64
+
65
+ it("reads no arguments on the block node, so a value is matched once, on its instance", () => {
66
+ expect(view(SA_BLOCK)).toEqual({ kind: "google_service_account", attrs: {}, nonLiteral: 0 });
67
+ });
68
+
69
+ it("resolves display_name through a local that compares the variable with != (#5094)", () => {
70
+ const at = (evaluatedOf(SA_BLOCK)!.instances as Record<string, { arguments?: Record<string, unknown> }>)[
71
+ 'module.workload_identity_service_accounts["prow-build-trusted"].google_service_account.serviceaccount'
72
+ ];
73
+ expect(at?.arguments?.display_name).toBe("default service account for pods in prow-build-trusted");
74
+ });
75
+ });
76
+
77
+ describe("the GKE cluster", () => {
78
+ it("resolves name from a local through the gke-cluster module's variable, on the one instance its count picks", () => {
79
+ expect(view(`${CLUSTER}[0]`)).toEqual({
80
+ kind: "google_container_cluster",
81
+ attrs: { count: 1, name: "prow-build-trusted", location: "us-central1", project: "k8s-infra-prow-build-trusted" },
82
+ nonLiteral: 0,
83
+ });
84
+ expect(view(CLUSTER)).toEqual({ kind: "google_container_cluster", attrs: {}, nonLiteral: 0 });
85
+ });
86
+
87
+ it("gives the block whose count compares to zero no instance and nothing to match (#5094)", () => {
88
+ expect(ir.nodes.filter((n) => n.instanceOf === TEST_CLUSTER)).toEqual([]);
89
+ expect(byId.get(TEST_CLUSTER)?.attrs.evaluated).toEqual({ instances: {} });
90
+ expect(view(TEST_CLUSTER)).toEqual({ kind: "google_container_cluster", attrs: {}, nonLiteral: 0 });
91
+ });
92
+
93
+ it("picks per call: a second call with is_prod_cluster = \"false\" plans the test block", () => {
94
+ expect(ir.nodes.filter((n) => n.instanceOf === CANARY("prod_cluster"))).toEqual([]);
95
+ expect(view(`${CANARY("test_cluster")}[0]`)?.attrs).toEqual({ count: 1, name: "prow-build-trusted-canary", location: "us-central1", project: "k8s-infra-prow-build-trusted" });
96
+ });
97
+ });
98
+
99
+ describe("arguments that stay not literal", () => {
100
+ it("names a data source", () => {
101
+ expect(view("bt/google_storage_bucket.from_data")).toEqual({ kind: "google_storage_bucket", attrs: { location: "US" }, nonLiteral: 1 });
102
+ expect(evaluatedOf("bt/google_storage_bucket.from_data")).toEqual({ stopped: { name: "data.google_organization.org.org_id" } });
103
+ });
104
+
105
+ it("names the reference that stops a comparison, and counts a block whose comparison is known (#5094)", () => {
106
+ expect(ir.nodes.filter((n) => n.instanceOf === "bt/google_storage_bucket.compared").map((n) => n.id)).toEqual(["bt/google_storage_bucket.compared[0]"]);
107
+ const at = (evaluatedOf("bt/google_storage_bucket.compared")!.instances as Record<string, { stopped?: Record<string, string> }>)["google_storage_bucket.compared[0]"];
108
+ expect(at?.stopped).toEqual({ name: "data.google_organization.org.org_id" });
109
+ });
110
+
111
+ it("names timestamp()", () => {
112
+ expect(evaluatedOf("bt/google_storage_bucket.dated")).toEqual({ stopped: { name: "timestamp()" } });
113
+ });
114
+
115
+ it("names an attribute the provider computes, inside the module that has it", () => {
116
+ expect(evaluatedOf("bt/google_project_iam_member.computed")).toEqual({
117
+ arguments: { project: "k8s-infra-prow-build-trusted" },
118
+ stopped: { member: 'module.workload_identity_service_accounts["gcb-builder"].google_service_account.serviceaccount.email' },
119
+ });
120
+ expect(view("bt/google_project_iam_member.computed")?.nonLiteral).toBe(1);
121
+ });
122
+
123
+ it("names a data source in a child module with the module path in front", () => {
124
+ expect((evaluatedOf("bt/module.project/google_project.project")!.stopped as Record<string, string>).org_id).toBe(
125
+ "module.project.data.google_organization.org.org_id",
126
+ );
127
+ });
128
+ });
129
+
130
+ describe("RootEvaluator", () => {
131
+ const entity = (key: string, type: string, address: string, body: Record<string, unknown>) =>
132
+ [key, terraformEntity(type, address, body, "main.tf", "r")] as [string, Declarable];
133
+
134
+ it("reads tfvars over a default, leaves a sensitive variable unknown, and stops on a cycle", () => {
135
+ const entities = new Map<string, Declarable>([
136
+ entity("r/var.region", VARIABLE_TYPE, "var.region", { default: "us-east1" }),
137
+ entity("r/var.token", VARIABLE_TYPE, "var.token", { default: "(sensitive)", sensitive: true }),
138
+ entity("r/locals", LOCALS_TYPE, "locals", { a: "${local.b}", b: "${local.a}", m: { x: 1 } }),
139
+ ]);
140
+ const ev = new RootEvaluator(entities, "r", new Map([["region", "eu-west1"]]));
141
+ const place = ev.rootPlace();
142
+ expect(ev.value("${var.region}-${local.m.x}", { place })).toBe("eu-west1-1");
143
+ expect((ev.value("${var.token}", { place }) as { ref: string }).ref).toBe("var.token");
144
+ expect(isUnknown(ev.value("${local.a}", { place }))).toBe(true);
145
+ });
146
+
147
+ it("evaluates lookup with a default, a bool conditional, toset, length and an index step", () => {
148
+ const entities = new Map<string, Declarable>([
149
+ entity("r/var.on", VARIABLE_TYPE, "var.on", { default: true }),
150
+ entity("r/locals", LOCALS_TYPE, "locals", { m: { a: { n: "x" } }, l: ["b", "a", "b"] }),
151
+ ]);
152
+ const ev = new RootEvaluator(entities, "r");
153
+ const place = ev.rootPlace();
154
+ expect(ev.value('${lookup(local.m.a, "n", "d")}', { place })).toBe("x");
155
+ expect(ev.value('${lookup(local.m.a, "z", "d")}', { place })).toBe("d");
156
+ expect(ev.value("${var.on ? 1 : 0}", { place })).toBe(1);
157
+ expect(ev.value('${local.m.a.n == "x" ? 1 : 0}', { place })).toBe(1);
158
+ expect(ev.value('${local.m.a.n != "x"}', { place })).toBe(false);
159
+ expect(ev.value('${"1" == 1}', { place })).toBe(false);
160
+ expect(ev.value("${local.l == [\"b\", \"a\", \"b\"]}", { place })).toBe(true);
161
+ expect(ev.value('${local.m == { a = { n = "x" } }}', { place })).toBe(true);
162
+ expect((ev.value('${var.nope == "x" ? 1 : 0}', { place }) as { ref: string }).ref).toBe("var.nope");
163
+ expect((ev.value("${local.l[0] == local.l[1] + 1}", { place }) as { ref: string }).ref).toBe("local.l[0] == local.l[1] + 1");
164
+ expect(ev.value("${toset(local.l)}", { place })).toEqual(["a", "b"]);
165
+ expect(ev.value("${length(local.l)}", { place })).toBe(3);
166
+ expect(ev.value('${local.m["a"].n}', { place })).toBe("x");
167
+ expect(ev.value("a $${literal} b", { place })).toBe("a ${literal} b");
168
+ expect((ev.value("${merge(local.m, {})}", { place }) as { ref: string }).ref).toBe("merge()");
169
+ });
170
+
171
+ it("follows a module output to the child's resource argument, and a sensitive output stays unknown", () => {
172
+ const entities = new Map<string, Declarable>([
173
+ entity("r/module.c", MODULE_TYPE, "module.c", { source: "./c", n: "from-root" }),
174
+ entity("r/module.c/var.n", VARIABLE_TYPE, "var.n", {}),
175
+ entity("r/module.c/aws_s3_bucket.b", RESOURCE_TYPE, "aws_s3_bucket.b", { bucket: "${var.n}-bucket" }),
176
+ entity("r/module.c/output.name", OUTPUT_TYPE, "output.name", { value: "${aws_s3_bucket.b.bucket}" }),
177
+ entity("r/module.c/output.secret", OUTPUT_TYPE, "output.secret", { value: "${var.n}", sensitive: true }),
178
+ ]);
179
+ const ev = new RootEvaluator(entities, "r");
180
+ const place = ev.rootPlace();
181
+ expect(ev.value("${module.c.name}", { place })).toBe("from-root-bucket");
182
+ expect((ev.value("${module.c.secret}", { place }) as { ref: string }).ref).toBe("module.c.output.secret");
183
+ });
184
+
185
+ it("gives a variable the value its call passes, per count and for_each instance and through a nested call (#5094)", () => {
186
+ const entities = new Map<string, Declarable>([
187
+ entity("r/locals", LOCALS_TYPE, "locals", { envs: { prod: { zone: "a" }, test: { zone: "b" } } }),
188
+ entity("r/module.n", MODULE_TYPE, "module.n", { source: "./n", count: 2, name: "pool-${count.index}" }),
189
+ entity("r/module.n/var.name", VARIABLE_TYPE, "var.name", {}),
190
+ entity("r/module.e", MODULE_TYPE, "module.e", { source: "./e", for_each: "${local.envs}", cluster_name: "${each.key}-${each.value.zone}" }),
191
+ entity("r/module.e/var.cluster_name", VARIABLE_TYPE, "var.cluster_name", {}),
192
+ entity("r/module.e/module.inner", MODULE_TYPE, "module.inner", { source: "./inner", name: "${var.cluster_name}" }),
193
+ entity("r/module.e/module.inner/var.name", VARIABLE_TYPE, "var.name", { default: "unused" }),
194
+ ]);
195
+ const ev = new RootEvaluator(entities, "r");
196
+ const at = (scope: string) => {
197
+ const place = ev.placeOfInstanceScope(scope);
198
+ expect(place, scope).toBeDefined();
199
+ return place!;
200
+ };
201
+ expect(ev.value("${var.name}", { place: at("module.n[0]") })).toBe("pool-0");
202
+ expect(ev.value("${var.name}", { place: at("module.n[1]") })).toBe("pool-1");
203
+ expect(ev.value("${var.cluster_name}", { place: at('module.e["prod"]') })).toBe("prod-a");
204
+ expect(ev.value("${var.name}", { place: at('module.e["test"].module.inner') })).toBe("test-b");
205
+ // A block-level read stands for every instance, so the key stays unknown and is named.
206
+ expect((ev.value("${var.cluster_name}", { place: ev.placeOfScope("r/module.e")! }) as { ref: string }).ref).toBe("each.key");
207
+ });
208
+
209
+ it("keeps a map's known entries beside an unknown one, and reports the first unknown", () => {
210
+ const ev = new RootEvaluator(new Map(), "r");
211
+ const out = evaluateArguments({ tags: { a: "x", b: "${data.d.e.f}" }, plain: "${var.nope}" }, (raw) => ev.value(raw, { place: ev.rootPlace() }));
212
+ expect(out).toEqual({ stopped: { "tags.b": "data.d.e.f", plain: "var.nope" } });
213
+ expect(firstUnknown({ a: [1, ev.value("${data.x.y.z}", { place: ev.rootPlace() })] })?.ref).toBe("data.x.y.z");
214
+ });
215
+ });