@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
@@ -24,12 +24,16 @@
24
24
  * asks every lexicon of a graph for its catalog (behold#1030's gate, before
25
25
  * behold#1005 retires behold's name matcher) finds terraform's. What it
26
26
  * matches depends on the nodes. A declared or live `chant graph` node is
27
- * `Terraform::Resource`, with its provider type in `attrs.type` and no
28
- * arguments in `attrs`, so no rule here reads it: those graphs keep the edges
29
- * the HCL references and the state dependencies give them, and gain none. A
30
- * node whose `kind` is the provider type and whose `attrs` are the block's
27
+ * `Terraform::Resource`, with its provider type in `attrs.type` and its
28
+ * arguments under `attrs.body`, so no rule here reads it as it is. A node
29
+ * whose `kind` is the provider type and whose `attrs` are the block's
31
30
  * arguments (`./request.ts`'s, an engine's instance roster) is what the rules
32
- * are for. `chant graph --live` merges a catalog's edges with the
31
+ * are for. `chant workspace graph` reads a declared block that way since
32
+ * #5097, through {@link terraformDeclaredNode}, with only the literal
33
+ * arguments, so a rule matches a value written in the HCL and an
34
+ * interpolation is counted as not read. Since #5117 an expression the files
35
+ * determine (a variable, a local, `each.key`, a module output) reads as its
36
+ * value, per instance node when the block has them. `chant graph --live` merges a catalog's edges with the
33
37
  * observation's own since #4535; before, any catalog replaced them, which is
34
38
  * what kept this one off the plugin.
35
39
  *
@@ -67,7 +71,8 @@
67
71
  * estate with a VPC in it is not `partial` forever and an estate with a queue
68
72
  * in it is not `complete` by omission.
69
73
  */
70
- import type { ReferenceCatalog } from "@intentius/chant/graph-refs";
74
+ import { type DeclaredCatalogNode, type ReferenceCatalog } from "@intentius/chant/graph-refs";
75
+ import type { IRNode } from "@intentius/chant/graph-ir";
71
76
  /**
72
77
  * Provider types this catalog has looked at and found to hold no argument
73
78
  * that identifies another resource. A node of one of these kinds is not an
@@ -78,6 +83,37 @@ import type { ReferenceCatalog } from "@intentius/chant/graph-refs";
78
83
  * references happen to be inconvenient to read.
79
84
  */
80
85
  export declare const REFERENCES_NOTHING: ReadonlySet<string>;
86
+ /**
87
+ * A block body with only its literal arguments, and how many it left out.
88
+ * A nested block (hcl2json writes it as an object, or a list of objects) and a
89
+ * map are descended, so each of their arguments counts on its own. A list of
90
+ * values keeps its literal elements and counts once when any element is not.
91
+ *
92
+ * `resolved` is the block's evaluated arguments (#5117, `../hcl/evaluate.ts`),
93
+ * shaped like the body: an argument that holds an expression and has a value
94
+ * there takes that value and is not counted.
95
+ */
96
+ export declare function literalArguments(body: Record<string, unknown>, resolved?: Record<string, unknown>): {
97
+ attrs: Record<string, unknown>;
98
+ nonLiteral: number;
99
+ };
100
+ /**
101
+ * The node the catalog reads for a declared terraform block (#5097): its
102
+ * provider type as `kind`, as `./request.ts` gives the behaviour engine, and
103
+ * its literal arguments as `attrs`. An argument whose value is an
104
+ * expression takes the value `../hcl/evaluate.ts` resolved for it (#5117):
105
+ * a variable, a local, `each.key`, a module output or a composed string the
106
+ * files determine. One that did not resolve is left out and counted; the
107
+ * reference that stopped it is in the node's `evaluated.stopped`. Any block
108
+ * but a `resource` is read as it is.
109
+ *
110
+ * A block with instance nodes is read per instance (#5117): each instance
111
+ * node, handed its block node, reads the block's arguments with that
112
+ * instance's values (`evaluated.instances`), and the block node itself reads
113
+ * no arguments, so a value is matched on the instance it belongs to and not
114
+ * a second time on the block.
115
+ */
116
+ export declare function terraformDeclaredNode(node: IRNode, block?: IRNode): DeclaredCatalogNode | undefined;
81
117
  /** The catalog. Rules are grouped by the relation they produce. */
82
118
  export declare const TERRAFORM_REFERENCE_CATALOG: ReferenceCatalog;
83
119
  /** Every provider type the catalog has at least one rule from. */
@@ -1 +1 @@
1
- {"version":3,"file":"reference-catalog.d.ts","sourceRoot":"","sources":["../../src/behaviour/reference-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEpE;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CAMjD,CAAC;AAEH,mEAAmE;AACnE,eAAO,MAAM,2BAA2B,EAAE,gBA8FzC,CAAC;AAEF,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,EAAE,WAAW,CAAC,MAAM,CAAgE,CAAC;AAElH;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEtD"}
1
+ {"version":3,"file":"reference-catalog.d.ts","sourceRoot":"","sources":["../../src/behaviour/reference-catalog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwEG;AAEH,OAAO,EAAY,KAAK,mBAAmB,EAAkB,KAAK,gBAAgB,EAAgB,MAAM,6BAA6B,CAAC;AACtI,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AAIxD;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,EAAE,WAAW,CAAC,MAAM,CAMjD,CAAC;AAIH;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CA0B1J;AAaD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,mBAAmB,GAAG,SAAS,CAsBnG;AAsCD,mEAAmE;AACnE,eAAO,MAAM,2BAA2B,EAAE,gBAiHzC,CAAC;AAEF,kEAAkE;AAClE,eAAO,MAAM,gBAAgB,EAAE,WAAW,CAAC,MAAM,CAAgE,CAAC;AAElH;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEtD"}
@@ -24,12 +24,16 @@
24
24
  * asks every lexicon of a graph for its catalog (behold#1030's gate, before
25
25
  * behold#1005 retires behold's name matcher) finds terraform's. What it
26
26
  * matches depends on the nodes. A declared or live `chant graph` node is
27
- * `Terraform::Resource`, with its provider type in `attrs.type` and no
28
- * arguments in `attrs`, so no rule here reads it: those graphs keep the edges
29
- * the HCL references and the state dependencies give them, and gain none. A
30
- * node whose `kind` is the provider type and whose `attrs` are the block's
27
+ * `Terraform::Resource`, with its provider type in `attrs.type` and its
28
+ * arguments under `attrs.body`, so no rule here reads it as it is. A node
29
+ * whose `kind` is the provider type and whose `attrs` are the block's
31
30
  * arguments (`./request.ts`'s, an engine's instance roster) is what the rules
32
- * are for. `chant graph --live` merges a catalog's edges with the
31
+ * are for. `chant workspace graph` reads a declared block that way since
32
+ * #5097, through {@link terraformDeclaredNode}, with only the literal
33
+ * arguments, so a rule matches a value written in the HCL and an
34
+ * interpolation is counted as not read. Since #5117 an expression the files
35
+ * determine (a variable, a local, `each.key`, a module output) reads as its
36
+ * value, per instance node when the block has them. `chant graph --live` merges a catalog's edges with the
33
37
  * observation's own since #4535; before, any catalog replaced them, which is
34
38
  * what kept this one off the plugin.
35
39
  *
@@ -67,6 +71,9 @@
67
71
  * estate with a VPC in it is not `partial` forever and an estate with a queue
68
72
  * in it is not `complete` by omission.
69
73
  */
74
+ import { readPath } from "@intentius/chant/graph-refs";
75
+ import { RESOURCE_TYPE } from "../hcl/parse.js";
76
+ import { interpolated } from "../hcl/evaluate.js";
70
77
  /**
71
78
  * Provider types this catalog has looked at and found to hold no argument
72
79
  * that identifies another resource. A node of one of these kinds is not an
@@ -83,12 +90,164 @@ export const REFERENCES_NOTHING = new Set([
83
90
  "aws_ecr_repository",
84
91
  "aws_route53_zone",
85
92
  ]);
93
+ const isBlock = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
94
+ /**
95
+ * A block body with only its literal arguments, and how many it left out.
96
+ * A nested block (hcl2json writes it as an object, or a list of objects) and a
97
+ * map are descended, so each of their arguments counts on its own. A list of
98
+ * values keeps its literal elements and counts once when any element is not.
99
+ *
100
+ * `resolved` is the block's evaluated arguments (#5117, `../hcl/evaluate.ts`),
101
+ * shaped like the body: an argument that holds an expression and has a value
102
+ * there takes that value and is not counted.
103
+ */
104
+ export function literalArguments(body, resolved) {
105
+ let nonLiteral = 0;
106
+ const walk = (obj, over, depth) => {
107
+ const out = {};
108
+ for (const [key, value] of Object.entries(obj)) {
109
+ if (depth > 12)
110
+ break;
111
+ const with_ = over?.[key];
112
+ if (isBlock(value)) {
113
+ out[key] = walk(value, isBlock(with_) ? with_ : undefined, depth + 1);
114
+ }
115
+ else if (Array.isArray(value) && value.length > 0 && value.every(isBlock)) {
116
+ out[key] = value.map((b, i) => walk(b, Array.isArray(with_) && isBlock(with_[i]) ? with_[i] : undefined, depth + 1));
117
+ }
118
+ else if (interpolated(value) && over !== undefined && key in over) {
119
+ out[key] = with_;
120
+ }
121
+ else if (Array.isArray(value)) {
122
+ const kept = value.filter((v) => !interpolated(v));
123
+ if (kept.length < value.length)
124
+ nonLiteral++;
125
+ out[key] = kept;
126
+ }
127
+ else if (interpolated(value)) {
128
+ nonLiteral++;
129
+ }
130
+ else {
131
+ out[key] = value;
132
+ }
133
+ }
134
+ return out;
135
+ };
136
+ return { attrs: walk(body, resolved, 0), nonLiteral };
137
+ }
138
+ /** A block node's `evaluated` attr (`TerraformEvaluated` in `../hcl/parse.ts`), when it has one. */
139
+ function evaluatedOf(attrs) {
140
+ return isBlock(attrs.evaluated) ? attrs.evaluated : undefined;
141
+ }
142
+ /** A declared resource node's provider type, from its `type` or its address. */
143
+ function providerType(attrs) {
144
+ const address = typeof attrs.address === "string" ? attrs.address : "";
145
+ return typeof attrs.type === "string" && attrs.type !== "" ? attrs.type : address.includes(".") ? address.split(".").slice(-2)[0] : undefined;
146
+ }
147
+ /**
148
+ * The node the catalog reads for a declared terraform block (#5097): its
149
+ * provider type as `kind`, as `./request.ts` gives the behaviour engine, and
150
+ * its literal arguments as `attrs`. An argument whose value is an
151
+ * expression takes the value `../hcl/evaluate.ts` resolved for it (#5117):
152
+ * a variable, a local, `each.key`, a module output or a composed string the
153
+ * files determine. One that did not resolve is left out and counted; the
154
+ * reference that stopped it is in the node's `evaluated.stopped`. Any block
155
+ * but a `resource` is read as it is.
156
+ *
157
+ * A block with instance nodes is read per instance (#5117): each instance
158
+ * node, handed its block node, reads the block's arguments with that
159
+ * instance's values (`evaluated.instances`), and the block node itself reads
160
+ * no arguments, so a value is matched on the instance it belongs to and not
161
+ * a second time on the block.
162
+ */
163
+ export function terraformDeclaredNode(node, block) {
164
+ if (node.instanceOf !== undefined) {
165
+ if (!block || block.kind !== RESOURCE_TYPE)
166
+ return undefined;
167
+ const attrs = block.attrs ?? {};
168
+ const instances = evaluatedOf(attrs)?.instances;
169
+ const type = providerType(attrs);
170
+ if (!instances || type === undefined)
171
+ return undefined;
172
+ const callers = Array.isArray(attrs.callers) ? attrs.callers.join(".") : "";
173
+ const path = node.instanceScope ?? callers;
174
+ const at = instances[`${path ? `${path}.` : ""}${String(attrs.address)}${node.instanceKey ?? ""}`];
175
+ if (!at)
176
+ return undefined;
177
+ const body = isBlock(attrs.body) ? attrs.body : {};
178
+ return { kind: type, ...literalArguments(body, at.arguments ?? {}) };
179
+ }
180
+ if (node.kind !== RESOURCE_TYPE)
181
+ return undefined;
182
+ const attrs = node.attrs ?? {};
183
+ const type = providerType(attrs);
184
+ if (type === undefined)
185
+ return undefined;
186
+ const evaluated = evaluatedOf(attrs);
187
+ if (evaluated?.instances)
188
+ return { kind: type, attrs: {}, nonLiteral: 0 };
189
+ const body = isBlock(attrs.body) ? attrs.body : {};
190
+ return { kind: type, ...literalArguments(body, evaluated?.arguments) };
191
+ }
192
+ const WORKLOAD_IDENTITY_USER = "roles/iam.workloadIdentityUser";
193
+ const WORKLOAD_MEMBER = /^serviceAccount:[^.]+\.svc\.id\.goog\[([^/\]]+)\/([^\]]+)\]$/;
194
+ /**
195
+ * A binding that lets a Kubernetes ServiceAccount act as a GCP service
196
+ * account (#5099): a `google_service_account_iam_member` or
197
+ * `google_service_account_iam_binding` with `role =
198
+ * "roles/iam.workloadIdentityUser"` and a literal member
199
+ * `serviceAccount:<project>.svc.id.goog[<namespace>/<name>]`. The edge goes
200
+ * to the ServiceAccount of that name in that namespace, found in any member.
201
+ * A binding for a cluster whose manifests are not in the workspace names no
202
+ * node, and that is not reported: the pool is the cluster's, not this
203
+ * workspace's. A member built with a local or a variable is not literal, so
204
+ * it is not read.
205
+ */
206
+ function workloadBindingRule(from, path) {
207
+ return {
208
+ from,
209
+ path,
210
+ targetKind: "K8s::Core::ServiceAccount",
211
+ relation: "reference",
212
+ label: "lets act as",
213
+ optional: true,
214
+ join(holder, find) {
215
+ if (!readPath(holder.attrs, "role").includes(WORKLOAD_IDENTITY_USER))
216
+ return [];
217
+ const refs = [];
218
+ for (const member of new Set(readPath(holder.attrs, path))) {
219
+ const m = WORKLOAD_MEMBER.exec(member);
220
+ if (!m)
221
+ continue;
222
+ refs.push({ value: member, targets: find(m[2], m[1]).filter((n) => n.kind === "K8s::Core::ServiceAccount"), via: "workload identity member" });
223
+ }
224
+ return refs;
225
+ },
226
+ };
227
+ }
86
228
  /** The catalog. Rules are grouped by the relation they produce. */
87
229
  export const TERRAFORM_REFERENCE_CATALOG = {
230
+ declaredNode: terraformDeclaredNode,
88
231
  // Every node is indexed by its own id and physical id (`reconstructEdges`
89
- // does that itself), and a block's body never carries a second identifier
90
- // for the block, so there are no identity rules to declare.
91
- identities: [],
232
+ // does that itself), and a block's body rarely carries a second identifier
233
+ // for the block. Two exceptions. A cloud secret (#5098): other systems
234
+ // name it by the id it is created with, an ExternalSecret's `remoteRef.key`
235
+ // among them. The k8s catalog's rule joins that key to the secret inside
236
+ // the store's project or region, so these only make the secret findable.
237
+ // A managed Kubernetes cluster is also named by its literal `name`, which
238
+ // is what an Argo CD cluster Secret carries (#5101).
239
+ identities: [
240
+ { kind: "google_secret_manager_secret", ids: ["secret_id"] },
241
+ { kind: "aws_secretsmanager_secret", ids: ["name"] },
242
+ { kind: "google_container_cluster", ids: ["name"] },
243
+ { kind: "aws_eks_cluster", ids: ["name"] },
244
+ { kind: "azurerm_kubernetes_cluster", ids: ["name"] },
245
+ // A ServiceAccount's annotation names a cloud identity by a value built
246
+ // from these (#5099): the k8s catalog's rule joins the email's account id
247
+ // and project, or the ARN's role name, so these only make them findable.
248
+ { kind: "google_service_account", ids: ["account_id"] },
249
+ { kind: "aws_iam_role", ids: ["name"] },
250
+ ],
92
251
  refs: [
93
252
  // ── containment (→ boundary pairs, and containment edges) ──
94
253
  { from: "aws_subnet", path: "vpc_id", targetKind: "aws_vpc", relation: "containment", label: "in VPC" },
@@ -173,6 +332,8 @@ export const TERRAFORM_REFERENCE_CATALOG = {
173
332
  { from: "aws_iam_role_policy_attachment", path: "policy_arn", targetKind: "aws_iam_policy", relation: "reference", label: "attaches" },
174
333
  { from: "aws_iam_role_policy", path: "role", targetKind: "aws_iam_role", relation: "reference", label: "on" },
175
334
  { from: "aws_iam_instance_profile", path: "role", targetKind: "aws_iam_role", relation: "reference", label: "holds" },
335
+ workloadBindingRule("google_service_account_iam_member", "member"),
336
+ workloadBindingRule("google_service_account_iam_binding", "members[]"),
176
337
  { from: "aws_route53_record", path: "zone_id", targetKind: "aws_route53_zone", relation: "reference", label: "in zone" },
177
338
  // A bucket's regional domain name or a load balancer's DNS name; no `targetKind`.
178
339
  { from: "aws_cloudfront_distribution", path: "origin[].domain_name", relation: "reference", label: "origin" },
@@ -1165,7 +1165,7 @@ function fromEntityName(name) {
1165
1165
  const last = name.lastIndexOf("/");
1166
1166
  return {
1167
1167
  root: name.slice(0, slash),
1168
- address: name.slice(last + 1).replace(/~\d+$/, ""),
1168
+ address: name.slice(last + 1).replace(/(~[^/~]+)+$/, ""),
1169
1169
  callers: callersOfKey(name),
1170
1170
  };
1171
1171
  }
@@ -0,0 +1,151 @@
1
+ /**
2
+ * Evaluate a block's arguments from the files a plan reads, per module
3
+ * instance (chant #5117).
4
+ *
5
+ * The reference catalog (`../behaviour/reference-catalog.ts`) matches values
6
+ * written on a node. A declared block mostly holds expressions instead:
7
+ * `account_id = var.name`, where the variable is set by a module call from
8
+ * `each.key`, and `project = var.project_id`, set from another module's
9
+ * output. This module follows those expressions back to a literal where the
10
+ * source alone says what it is, so the catalog has the value to match.
11
+ *
12
+ * It resolves, in one module instance:
13
+ *
14
+ * - `var.<x>` from the call's argument (evaluated in the caller), else the
15
+ * variable's default; in the root module from the tfvars files
16
+ * `./instances.ts` reads, then the default;
17
+ * - `local.<x>`, `each.key`, `each.value` and `count.index`;
18
+ * - `module.<call>.<output>`, when the output's value resolves in the child;
19
+ * - `<type>.<name>.<arg>` in the same module, when the block sets `<arg>`;
20
+ * - `"${...}"` interpolation whose every part resolves;
21
+ * - `lookup(map, key, default)`, `toset()`, `length()` and `cond ? a : b`
22
+ * over a known bool, plus list and object constructors and index steps;
23
+ * - `a == b` and `a != b` over two known values (#5094), so
24
+ * `count = var.is_prod_cluster == "true" ? 1 : 0` picks its count.
25
+ *
26
+ * Anything else is unknown, and an unknown value carries the first
27
+ * reference that stopped it: a data source (`data.google_organization.org.org_id`),
28
+ * a function (`timestamp()`), an attribute the provider computes
29
+ * (`google_service_account.sa.email`), a variable nothing sets, an operator
30
+ * this reader does not evaluate. A reference inside a child module is named
31
+ * with the module path in front, as Terraform prints addresses
32
+ * (`module.project.data.google_organization.org.org_id`). A sensitive
33
+ * variable or output is unknown, so a value marked sensitive never reaches
34
+ * the graph. No plan, state or provider is read.
35
+ *
36
+ * Pure apart from what it is handed: the parsed entities of one root, its
37
+ * tfvars values, and a module path.
38
+ */
39
+ import { type Declarable } from "@intentius/chant/declarable";
40
+ import { type BlockBody } from "./parse.js";
41
+ /** A value the source does not determine, and the reference that stopped it. */
42
+ export declare class Unknown {
43
+ readonly ref: string;
44
+ constructor(ref: string);
45
+ }
46
+ export declare const isUnknown: (v: unknown) => v is Unknown;
47
+ /** Whether a value is a set `toset()` made. */
48
+ export declare const isSetValue: (v: unknown) => boolean;
49
+ /** The first unknown inside a value, depth first, or undefined when the value is wholly known. */
50
+ export declare function firstUnknown(v: unknown, depth?: number): Unknown | undefined;
51
+ /** One module instance: the entity-key scope its blocks sit in and its Terraform path. */
52
+ export interface ModulePlace {
53
+ /** The entity-key scope, `estate` or `estate/module.a/module.b`. */
54
+ scope: string;
55
+ /** The module path as Terraform prints it, `""` for the root, `module.a["x"].module.b`. */
56
+ path: string;
57
+ parent?: ModulePlace;
58
+ /** The call's iteration for this instance, when the call is expanded. */
59
+ iteration?: Iteration;
60
+ }
61
+ /** `each` and `count` for one instance of an expanded block or call. */
62
+ export interface Iteration {
63
+ each?: {
64
+ key: unknown;
65
+ value: unknown;
66
+ } | Unknown;
67
+ count?: number | Unknown;
68
+ }
69
+ interface Env {
70
+ place: ModulePlace;
71
+ iteration?: Iteration;
72
+ }
73
+ /** The key text of an instance key, `[0]` or `["a"]`, as a number or a string. */
74
+ export declare function instanceKeyValue(key: string): number | string | undefined;
75
+ /**
76
+ * Evaluates expressions over one root's parsed entities. Build it once per
77
+ * root, after the descent into local modules.
78
+ */
79
+ export declare class RootEvaluator {
80
+ private readonly entities;
81
+ private readonly root;
82
+ private readonly tfvars;
83
+ private readonly vars;
84
+ private readonly locals;
85
+ private readonly memo;
86
+ private readonly places;
87
+ private readonly visiting;
88
+ private depth;
89
+ constructor(entities: ReadonlyMap<string, Declarable>, root: string, tfvars?: ReadonlyMap<string, unknown>);
90
+ /** The root module's place. */
91
+ rootPlace(): ModulePlace;
92
+ private placeAt;
93
+ /**
94
+ * The place an instance's scope names (`module.a["x"].module.b`, as
95
+ * `withInstanceNodes` carries it), or undefined when a call on the way is
96
+ * not a module call of this root.
97
+ */
98
+ placeOfInstanceScope(instanceScope: string): ModulePlace | undefined;
99
+ /**
100
+ * The place of a block-level read of an entity-key scope: every expanded
101
+ * call on the way has an unknown iteration, since the block stands for all
102
+ * of its instances.
103
+ */
104
+ placeOfScope(scope: string): ModulePlace | undefined;
105
+ /** A module call's instance place, or an unknown when its key is not known; undefined when there is no such call. */
106
+ private childPlace;
107
+ /** The iteration of one instance of an expanded block, by its key. */
108
+ iterationOf(body: BlockBody, env: Env, key: number | string): Iteration;
109
+ /** The block-level iteration of a block: unknown when it is expanded, none when it is not. */
110
+ blockIteration(body: BlockBody, place: ModulePlace): Iteration | undefined;
111
+ /**
112
+ * A value from a parsed body (a literal, a hcl2json `"${...}"` string, or a
113
+ * list or map of those) evaluated in a place. Unknown parts stay
114
+ * {@link Unknown}; a map keeps its known entries beside them.
115
+ */
116
+ value(raw: unknown, env: Env): unknown;
117
+ private expr;
118
+ private call;
119
+ /** Apply traversal steps to a value. `text` names the whole traversal when a step does not apply. */
120
+ private steps;
121
+ private ref;
122
+ /** `module.<call>[key]?.<output>...`. */
123
+ private moduleOutput;
124
+ /** `<type>.<name>[key]?.<arg>...` in the same module: the argument as the block sets it. */
125
+ private resourceArgument;
126
+ /** A variable's value in a place. */
127
+ private variable;
128
+ /** A local value in a place. */
129
+ private local;
130
+ /** Memoized, and unknown on a cycle. */
131
+ private guarded;
132
+ }
133
+ /**
134
+ * What evaluating one block's arguments gave: the arguments that held an
135
+ * expression and resolved, shaped like the body (a nested block keeps its
136
+ * place in its list), and per argument that did not, the reference that
137
+ * stopped it.
138
+ */
139
+ export interface EvaluatedArguments {
140
+ arguments?: Record<string, unknown>;
141
+ stopped?: Record<string, string>;
142
+ }
143
+ /** True when a value holds an interpolation, `${...}`, anywhere in it. */
144
+ export declare function interpolated(value: unknown, depth?: number): boolean;
145
+ /**
146
+ * Evaluate the arguments of a block body that hold an expression, walking
147
+ * nested blocks and maps the way the catalog's literal view does.
148
+ */
149
+ export declare function evaluateArguments(body: BlockBody, evaluate: (raw: unknown) => unknown): EvaluatedArguments;
150
+ export {};
151
+ //# sourceMappingURL=evaluate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"evaluate.d.ts","sourceRoot":"","sources":["../../src/hcl/evaluate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AAEH,OAAO,EAAwB,KAAK,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACpF,OAAO,EAOL,KAAK,SAAS,EAEf,MAAM,SAAS,CAAC;AAEjB,gFAAgF;AAChF,qBAAa,OAAO;IACN,QAAQ,CAAC,GAAG,EAAE,MAAM;IAAhC,YAAqB,GAAG,EAAE,MAAM,EAAI;CACrC;AAED,eAAO,MAAM,SAAS,MAAO,OAAO,KAAG,CAAC,IAAI,OAA+B,CAAC;AAK5E,+CAA+C;AAC/C,eAAO,MAAM,UAAU,MAAO,OAAO,KAAG,OAA0C,CAAC;AAEnF,kGAAkG;AAClG,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,SAAI,GAAG,OAAO,GAAG,SAAS,CAevE;AAkTD,0FAA0F;AAC1F,MAAM,WAAW,WAAW;IAC1B,oEAAoE;IACpE,KAAK,EAAE,MAAM,CAAC;IACd,2FAA2F;IAC3F,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,yEAAyE;IACzE,SAAS,CAAC,EAAE,SAAS,CAAC;CACvB;AAED,wEAAwE;A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