@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.
- package/dist/behaviour/reference-catalog.d.ts +42 -6
- package/dist/behaviour/reference-catalog.d.ts.map +1 -1
- package/dist/behaviour/reference-catalog.js +169 -8
- package/dist/describe-resources.js +1 -1
- package/dist/hcl/evaluate.d.ts +151 -0
- package/dist/hcl/evaluate.d.ts.map +1 -0
- package/dist/hcl/evaluate.js +940 -0
- package/dist/hcl/instances.d.ts +11 -1
- package/dist/hcl/instances.d.ts.map +1 -1
- package/dist/hcl/instances.js +89 -3
- package/dist/hcl/parse.d.ts +23 -1
- package/dist/hcl/parse.d.ts.map +1 -1
- package/dist/hcl/parse.js +9 -3
- package/dist/hcl/redact.d.ts +5 -1
- package/dist/hcl/redact.d.ts.map +1 -1
- package/dist/hcl/redact.js +7 -3
- package/dist/hcl/references.d.ts +12 -1
- package/dist/hcl/references.d.ts.map +1 -1
- package/dist/hcl/references.js +33 -16
- package/dist/integrity.json +6 -6
- package/dist/lint/post-synth/tf001.d.ts +4 -1
- package/dist/lint/post-synth/tf001.d.ts.map +1 -1
- package/dist/lint/post-synth/tf001.js +20 -8
- package/dist/lint/post-synth/tf002.d.ts +11 -5
- package/dist/lint/post-synth/tf002.d.ts.map +1 -1
- package/dist/lint/post-synth/tf002.js +42 -8
- package/dist/lint/post-synth/tf006.d.ts +7 -3
- package/dist/lint/post-synth/tf006.d.ts.map +1 -1
- package/dist/lint/post-synth/tf006.js +9 -3
- package/dist/lint/post-synth/tf022.d.ts +6 -0
- package/dist/lint/post-synth/tf022.d.ts.map +1 -1
- package/dist/lint/post-synth/tf022.js +18 -1
- package/dist/lint/secret-shape.d.ts +8 -0
- package/dist/lint/secret-shape.d.ts.map +1 -1
- package/dist/lint/secret-shape.js +76 -0
- package/dist/manifest.json +1 -1
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +7 -2
- package/dist/rules/tf001.ts +21 -7
- package/dist/rules/tf002.ts +44 -7
- package/dist/rules/tf006.ts +8 -3
- package/dist/rules/tf022.ts +18 -1
- package/package.json +2 -2
- package/src/__fixtures__/resolved-arguments/build-trusted/extras.tf +28 -0
- package/src/__fixtures__/resolved-arguments/build-trusted/main.tf +22 -0
- package/src/__fixtures__/resolved-arguments/build-trusted/serviceaccounts.tf +26 -0
- package/src/__fixtures__/resolved-arguments/modules/gke-cluster/main.tf +15 -0
- package/src/__fixtures__/resolved-arguments/modules/gke-cluster/variables.tf +12 -0
- package/src/__fixtures__/resolved-arguments/modules/gke-project/main.tf +9 -0
- package/src/__fixtures__/resolved-arguments/modules/gke-project/outputs.tf +9 -0
- package/src/__fixtures__/resolved-arguments/modules/gke-project/variables.tf +7 -0
- package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/main.tf +16 -0
- package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/outputs.tf +4 -0
- package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/variables.tf +21 -0
- package/src/behaviour/reference-catalog.plugin.test.ts +101 -2
- package/src/behaviour/reference-catalog.ts +162 -9
- package/src/describe-resources.ts +1 -1
- package/src/hcl/edges.test.ts +1 -1
- package/src/hcl/evaluate.test.ts +215 -0
- package/src/hcl/evaluate.ts +899 -0
- package/src/hcl/instances.ts +89 -3
- package/src/hcl/parse.test.ts +18 -0
- package/src/hcl/parse.ts +28 -3
- package/src/hcl/redact.ts +7 -3
- package/src/hcl/references.test.ts +68 -0
- package/src/hcl/references.ts +31 -15
- package/src/lint/post-synth/fixtures/TF001/negative-second-block.tf +17 -0
- package/src/lint/post-synth/fixtures/TF002/module-cycle/cdn/back/main.tf +3 -0
- package/src/lint/post-synth/fixtures/TF002/module-cycle/cdn/main.tf +3 -0
- package/src/lint/post-synth/fixtures/TF002/module-cycle/main.tf +14 -0
- package/src/lint/post-synth/fixtures/TF002/module-nested/cdn/edge/main.tf +8 -0
- package/src/lint/post-synth/fixtures/TF002/module-nested/cdn/main.tf +3 -0
- package/src/lint/post-synth/fixtures/TF002/module-nested/main.tf +14 -0
- package/src/lint/post-synth/fixtures/TF002/module-other-provider/cdn/main.tf +8 -0
- package/src/lint/post-synth/fixtures/TF002/module-other-provider/main.tf +14 -0
- package/src/lint/post-synth/fixtures/TF002/module-pinned/cdn/main.tf +12 -0
- package/src/lint/post-synth/fixtures/TF002/module-pinned/main.tf +14 -0
- package/src/lint/post-synth/fixtures/TF002/module-unpinned/cdn/main.tf +7 -0
- package/src/lint/post-synth/fixtures/TF002/module-unpinned/main.tf +14 -0
- package/src/lint/post-synth/fixtures/TF006/negative-null-default.tf +12 -0
- package/src/lint/post-synth/fixtures/TF006/positive-empty-default.tf +0 -6
- package/src/lint/post-synth/fixtures/TF007/negative-names.tf +25 -0
- package/src/lint/post-synth/fixtures/TF022/negative-secret-reference.tf +32 -0
- package/src/lint/post-synth/fixtures/TF022/positive-secret-data.tf +8 -0
- package/src/lint/post-synth/fixtures/TF024/live-backend-second-block/a-versions.tf +3 -0
- package/src/lint/post-synth/fixtures/TF024/live-backend-second-block/b-backend.tf +10 -0
- package/src/lint/post-synth/fixtures/TF024/live-cloud-second-block/a-versions.tf +3 -0
- package/src/lint/post-synth/fixtures/TF024/live-cloud-second-block/b-cloud.tf +9 -0
- package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/a-versions.tf +3 -0
- package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/b-backend.tf +6 -0
- package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/estate.chdf.hcl +1 -0
- package/src/lint/post-synth/module-rules.test.ts +1 -1
- package/src/lint/post-synth/post-synth.test.ts +39 -0
- package/src/lint/post-synth/single-block.test.ts +23 -2
- package/src/lint/post-synth/tf001.ts +21 -7
- package/src/lint/post-synth/tf002.test.ts +45 -1
- package/src/lint/post-synth/tf002.ts +44 -7
- package/src/lint/post-synth/tf006.ts +8 -3
- package/src/lint/post-synth/tf022.ts +18 -1
- package/src/lint/post-synth/tf024.test.ts +21 -0
- package/src/lint/secret-shape.test.ts +30 -0
- package/src/lint/secret-shape.ts +75 -0
- package/src/plugin.ts +6 -2
- package/src/redact.test.ts +8 -0
package/src/hcl/instances.ts
CHANGED
|
@@ -34,6 +34,16 @@
|
|
|
34
34
|
* rolled up by ruling. A block under an expanded call whose own expansion
|
|
35
35
|
* or whose call's expansion cannot be counted gets no instances at all,
|
|
36
36
|
* since a partial list would name ids the plan never prints.
|
|
37
|
+
*
|
|
38
|
+
* Since #5117 an expansion none of those forms counts is read once more with
|
|
39
|
+
* `./evaluate.ts`, which follows locals that read locals, interpolation,
|
|
40
|
+
* `lookup()` and module outputs: a `for_each` over a local map whose values
|
|
41
|
+
* interpolate another local gets its keys, since a map's keys are known when
|
|
42
|
+
* some of its values are not. The same evaluator then gives each resource
|
|
43
|
+
* block its `evaluated` arguments, per instance when it has instance nodes,
|
|
44
|
+
* for the reference catalog. Since #5094 that reading includes `==` and
|
|
45
|
+
* `!=`, and a block whose expansion comes to zero instances gets
|
|
46
|
+
* `{ instances: {} }`, so the catalog reads nothing on it.
|
|
37
47
|
*/
|
|
38
48
|
|
|
39
49
|
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
@@ -41,6 +51,7 @@ import { join, resolve } from "node:path";
|
|
|
41
51
|
import { isResourceDeclarable, type Declarable } from "@intentius/chant/declarable";
|
|
42
52
|
import type { InstanceRollup, InstanceRollupCode } from "@intentius/chant/instance-nodes";
|
|
43
53
|
import { loadHcl2json, type Hcl2Json } from "@intentius/chant/terraform/hcl2json";
|
|
54
|
+
import { RootEvaluator, evaluateArguments, instanceKeyValue, isSetValue, isUnknown, type EvaluatedArguments } from "./evaluate";
|
|
44
55
|
import {
|
|
45
56
|
DATA_TYPE,
|
|
46
57
|
LOCALS_TYPE,
|
|
@@ -52,6 +63,7 @@ import {
|
|
|
52
63
|
scopeOfKey,
|
|
53
64
|
type BlockBody,
|
|
54
65
|
type TerraformEntity,
|
|
66
|
+
type TerraformEvaluated,
|
|
55
67
|
} from "./parse";
|
|
56
68
|
|
|
57
69
|
/** The most instance nodes one block gets; past it the block draws alone. */
|
|
@@ -172,7 +184,11 @@ function rollupCodeOf(value: unknown): InstanceRollupCode {
|
|
|
172
184
|
* A block's expansion in a scope: its instance keys as Terraform prints them,
|
|
173
185
|
* `null` when the block is not expanded, or why the keys are not known.
|
|
174
186
|
*/
|
|
175
|
-
export function expansionIn(
|
|
187
|
+
export function expansionIn(
|
|
188
|
+
body: BlockBody,
|
|
189
|
+
scope: Scope,
|
|
190
|
+
evaluateFully?: (raw: unknown) => unknown,
|
|
191
|
+
): { keys: string[] } | { rollup: InstanceRollup } | null {
|
|
176
192
|
if (!("count" in body) && !("for_each" in body)) return null;
|
|
177
193
|
const argument = "count" in body ? "count" : "for_each";
|
|
178
194
|
const expr = body[argument];
|
|
@@ -193,11 +209,28 @@ export function expansionIn(body: BlockBody, scope: Scope): { keys: string[] } |
|
|
|
193
209
|
keys = Object.keys(v.value).sort().map((k) => `[${JSON.stringify(k)}]`);
|
|
194
210
|
}
|
|
195
211
|
}
|
|
212
|
+
if (keys === undefined && evaluateFully) keys = keysOfValue(argument, evaluateFully(expr));
|
|
196
213
|
if (keys === undefined) return rolled(rollupCodeOf(expr));
|
|
197
214
|
if (keys.length > MAX_INSTANCES) return rolled("instances-over-limit");
|
|
198
215
|
return { keys };
|
|
199
216
|
}
|
|
200
217
|
|
|
218
|
+
/**
|
|
219
|
+
* The instance keys of an evaluated `count` or `for_each` (#5117): a whole
|
|
220
|
+
* number, a map (whose keys are known even when some of its values are not),
|
|
221
|
+
* or a set of strings `toset()` made. Undefined for anything else.
|
|
222
|
+
*/
|
|
223
|
+
function keysOfValue(argument: "count" | "for_each", v: unknown): string[] | undefined {
|
|
224
|
+
if (isUnknown(v)) return undefined;
|
|
225
|
+
if (argument === "count") return typeof v === "number" && Number.isInteger(v) && v >= 0 ? Array.from({ length: v }, (_, i) => `[${i}]`) : undefined;
|
|
226
|
+
if (isSetValue(v)) {
|
|
227
|
+
const list = v as unknown[];
|
|
228
|
+
return list.every((s) => typeof s === "string") ? [...new Set(list as string[])].sort().map((k) => `[${JSON.stringify(k)}]`) : undefined;
|
|
229
|
+
}
|
|
230
|
+
if (typeof v === "object" && v !== null && !Array.isArray(v)) return Object.keys(v).sort().map((k) => `[${JSON.stringify(k)}]`);
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
201
234
|
/**
|
|
202
235
|
* The instance keys a block's `count` or `for_each` gives in `vars`, as
|
|
203
236
|
* Terraform prints them. `null` when the block is not expanded, undefined
|
|
@@ -292,6 +325,12 @@ export async function resolveInstanceNodes(
|
|
|
292
325
|
}
|
|
293
326
|
|
|
294
327
|
const tfvars = await rootTfvars(opts);
|
|
328
|
+
const evaluator = new RootEvaluator(entities, root, tfvars);
|
|
329
|
+
/** The full evaluator over a block-level read of a scope (#5117), where the narrow forms above give no keys. */
|
|
330
|
+
const evaluateIn = (scope: string) => (raw: unknown): unknown => {
|
|
331
|
+
const place = evaluator.placeOfScope(scope);
|
|
332
|
+
return place ? evaluator.value(raw, { place }) : undefined;
|
|
333
|
+
};
|
|
295
334
|
const scopes = new Map<string, ScopeVars>();
|
|
296
335
|
const localScopes = new Map<string, ScopeVars>();
|
|
297
336
|
|
|
@@ -350,7 +389,7 @@ export async function resolveInstanceNodes(
|
|
|
350
389
|
const callExpansion = (callKey: string): ReturnType<typeof expansionIn> | undefined => {
|
|
351
390
|
const call = entities.get(callKey) as TerraformEntity | undefined;
|
|
352
391
|
if (!call || call.entityType !== MODULE_TYPE) return undefined;
|
|
353
|
-
return expansionIn(bodyOf(call), scopeOf(scopeOfKey(callKey)));
|
|
392
|
+
return expansionIn(bodyOf(call), scopeOf(scopeOfKey(callKey)), evaluateIn(scopeOfKey(callKey)));
|
|
354
393
|
};
|
|
355
394
|
const keysOf = (e: ReturnType<typeof expansionIn> | undefined): string[] | null | undefined =>
|
|
356
395
|
e === undefined ? undefined : e === null ? null : "keys" in e ? e.keys : undefined;
|
|
@@ -360,7 +399,7 @@ export async function resolveInstanceNodes(
|
|
|
360
399
|
const callers = callersOfKey(key);
|
|
361
400
|
const calls = callers.map((_, i) => callExpansion(`${root}/${callers.slice(0, i + 1).join("/")}`));
|
|
362
401
|
const chain: Array<string[] | null | undefined> = calls.map(keysOf);
|
|
363
|
-
const own = expansionIn(bodyOf(entity), scopeOf(scopeOfKey(key)));
|
|
402
|
+
const own = expansionIn(bodyOf(entity), scopeOf(scopeOfKey(key)), evaluateIn(scopeOfKey(key)));
|
|
364
403
|
const ownKeys = keysOf(own);
|
|
365
404
|
const ownRollup = own !== null && "rollup" in own ? own.rollup : undefined;
|
|
366
405
|
const expandedChain = chain.some((k) => k !== null);
|
|
@@ -419,4 +458,51 @@ export async function resolveInstanceNodes(
|
|
|
419
458
|
}
|
|
420
459
|
entities.set(key, { ...rest, instances } as TerraformEntity);
|
|
421
460
|
}
|
|
461
|
+
|
|
462
|
+
// Each resource block's expressions, per instance when it has instance
|
|
463
|
+
// nodes (#5117), for the reference catalog.
|
|
464
|
+
for (const [key] of own) {
|
|
465
|
+
const entity = entities.get(key) as TerraformEntity | undefined;
|
|
466
|
+
if (!entity || entity.entityType !== RESOURCE_TYPE) continue;
|
|
467
|
+
const evaluated = evaluateBlock(evaluator, entity, key);
|
|
468
|
+
if (evaluated) entities.set(key, { ...entity, props: { ...entity.props, evaluated } } as TerraformEntity);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const hasAny = (e: EvaluatedArguments): boolean => e.arguments !== undefined || e.stopped !== undefined;
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* A resource block's evaluated arguments (#5117): one entry per instance node
|
|
476
|
+
* the graph will list, keyed by the instance address, or the block's own when
|
|
477
|
+
* it has none. Undefined when nothing in it holds an expression and it has no
|
|
478
|
+
* instance nodes.
|
|
479
|
+
*/
|
|
480
|
+
function evaluateBlock(evaluator: RootEvaluator, entity: TerraformEntity, key: string): TerraformEvaluated | undefined {
|
|
481
|
+
const body = bodyOf(entity);
|
|
482
|
+
const scope = scopeOfKey(key);
|
|
483
|
+
const address = entity.props.address;
|
|
484
|
+
const callers = callersOfKey(key);
|
|
485
|
+
const listed = (entity as { instances?: readonly ListedInstance[] }).instances;
|
|
486
|
+
const keys = entity.instanceKeys;
|
|
487
|
+
// A count or for_each the files settle at zero (`count = var.on == "true" ? 1 : 0` with it off, #5094)
|
|
488
|
+
// plans no instance, so nothing is read for the catalog to match.
|
|
489
|
+
if ((listed && listed.length === 0) || (!listed && keys && keys.length === 0 && ("count" in body || "for_each" in body))) return { instances: {} };
|
|
490
|
+
if ((listed && listed.length > 0) || (keys && keys.length > 0)) {
|
|
491
|
+
const entries: Array<{ scope?: string; key?: string }> = listed && listed.length > 0 ? [...listed] : keys!.map((k) => ({ key: k }));
|
|
492
|
+
const instances: Record<string, EvaluatedArguments> = {};
|
|
493
|
+
for (const inst of entries) {
|
|
494
|
+
const place = inst.scope !== undefined ? evaluator.placeOfInstanceScope(inst.scope) : evaluator.placeOfScope(scope);
|
|
495
|
+
if (!place) continue;
|
|
496
|
+
const k = inst.key !== undefined ? instanceKeyValue(inst.key) : undefined;
|
|
497
|
+
const iteration = k !== undefined ? evaluator.iterationOf(body, { place }, k) : evaluator.blockIteration(body, place);
|
|
498
|
+
const path = inst.scope ?? callers.join(".");
|
|
499
|
+
instances[`${path ? `${path}.` : ""}${address}${inst.key ?? ""}`] = evaluateArguments(body, (raw) => evaluator.value(raw, { place, ...(iteration ? { iteration } : {}) }));
|
|
500
|
+
}
|
|
501
|
+
return { instances };
|
|
502
|
+
}
|
|
503
|
+
const place = evaluator.placeOfScope(scope);
|
|
504
|
+
if (!place) return undefined;
|
|
505
|
+
const iteration = evaluator.blockIteration(body, place);
|
|
506
|
+
const out = evaluateArguments(body, (raw) => evaluator.value(raw, { place, ...(iteration ? { iteration } : {}) }));
|
|
507
|
+
return hasAny(out) ? out : undefined;
|
|
422
508
|
}
|
package/src/hcl/parse.test.ts
CHANGED
|
@@ -186,6 +186,24 @@ describe("configuration files beyond `.tf` (chant#4457)", () => {
|
|
|
186
186
|
});
|
|
187
187
|
});
|
|
188
188
|
|
|
189
|
+
describe("blocksToEntities: aliased providers (#5106)", () => {
|
|
190
|
+
const provider = (alias: string) => `provider "ibm" {\n alias = "${alias}"\n region = "${alias}"\n}\n`;
|
|
191
|
+
|
|
192
|
+
test("an aliased provider is keyed by its alias, so a block added before it does not renumber it", async () => {
|
|
193
|
+
const files = (sources: string[]) => sources.map((source, i) => ({ name: `f${i}.tf`, source }));
|
|
194
|
+
const before = await blocksToEntities(files([provider("eu"), provider("us")]), "app");
|
|
195
|
+
const after = await blocksToEntities(files([provider("ap"), provider("eu"), provider("us")]), "app");
|
|
196
|
+
expect([...before.keys()].sort()).toEqual(["app/provider.ibm~eu", "app/provider.ibm~us"]);
|
|
197
|
+
expect([...after.keys()].sort()).toEqual(["app/provider.ibm~ap", "app/provider.ibm~eu", "app/provider.ibm~us"]);
|
|
198
|
+
expect((after.get("app/provider.ibm~eu") as TerraformEntity).props.address).toBe("provider.ibm");
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("an unaliased provider keeps the plain key", async () => {
|
|
202
|
+
const entities = await blocksToEntities([{ name: "main.tf", source: `provider "ibm" {}\n${provider("eu")}` }], "app");
|
|
203
|
+
expect([...entities.keys()].sort()).toEqual(["app/provider.ibm", "app/provider.ibm~eu"]);
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
|
|
189
207
|
describe("jsonConfigTree: Terraform's JSON syntax in hcl2json's shape (chant#4457)", () => {
|
|
190
208
|
test("wraps each block body in an array, one object level per label", () => {
|
|
191
209
|
const tree = jsonConfigTree(
|
package/src/hcl/parse.ts
CHANGED
|
@@ -66,6 +66,13 @@ export type TerraformExpansion = "count" | "for_each";
|
|
|
66
66
|
/** Whether a root runs under choudoufu with a declared estate (#2103). */
|
|
67
67
|
export type TerraformRootMode = "live" | "state";
|
|
68
68
|
|
|
69
|
+
/** What `./evaluate.ts` made of a block's expressions (chant #5117). */
|
|
70
|
+
export interface TerraformEvaluated {
|
|
71
|
+
readonly arguments?: Record<string, unknown>;
|
|
72
|
+
readonly stopped?: Record<string, string>;
|
|
73
|
+
readonly instances?: Record<string, { readonly arguments?: Record<string, unknown>; readonly stopped?: Record<string, string> }>;
|
|
74
|
+
}
|
|
75
|
+
|
|
69
76
|
/** The entity every block becomes. `props` is what post-synth checks read. */
|
|
70
77
|
export interface TerraformEntity extends Declarable {
|
|
71
78
|
readonly lexicon: "terraform";
|
|
@@ -156,6 +163,18 @@ export interface TerraformEntity extends Declarable {
|
|
|
156
163
|
* expansion.
|
|
157
164
|
*/
|
|
158
165
|
readonly expansion?: TerraformExpansion;
|
|
166
|
+
/**
|
|
167
|
+
* A `resource` block's arguments that hold an expression, evaluated from
|
|
168
|
+
* the files a plan reads (chant #5117, `./evaluate.ts`): `arguments` has
|
|
169
|
+
* the ones that resolved, shaped like `body`, and `stopped` names, per
|
|
170
|
+
* argument that did not, the first reference that stopped it. A block
|
|
171
|
+
* with instance nodes has one such pair per instance instead, under
|
|
172
|
+
* `instances`, keyed by the instance's address as Terraform prints it
|
|
173
|
+
* (`module.sa["a"].google_service_account.this`). The reference catalog
|
|
174
|
+
* reads it through `terraformDeclaredNode`. Set by `./instances.ts`;
|
|
175
|
+
* absent when no argument holds an expression.
|
|
176
|
+
*/
|
|
177
|
+
readonly evaluated?: TerraformEvaluated;
|
|
159
178
|
};
|
|
160
179
|
/**
|
|
161
180
|
* The deployable unit this entity belongs to: the root name, which is what
|
|
@@ -495,7 +514,8 @@ export const LOCK_FILENAME = ".terraform.lock.hcl";
|
|
|
495
514
|
/**
|
|
496
515
|
* Parse a set of files into entities keyed `<root>/<address>`. Two blocks that
|
|
497
516
|
* genuinely share an address (two `locals` blocks, or the same address in two
|
|
498
|
-
* files of one root) are numbered `~2`, `~3` rather than overwriting.
|
|
517
|
+
* files of one root) are numbered `~2`, `~3` rather than overwriting. An aliased
|
|
518
|
+
* `provider` block is keyed `provider.<name>~<alias>` instead of by number.
|
|
499
519
|
*
|
|
500
520
|
* Recognises both ways an estate is declared: a `live { }` block nested in a
|
|
501
521
|
* `terraform { }` block of any file here, and, when `sidecar` is given, the
|
|
@@ -559,8 +579,13 @@ export async function blocksToEntities(
|
|
|
559
579
|
line,
|
|
560
580
|
suppressions,
|
|
561
581
|
);
|
|
562
|
-
|
|
563
|
-
|
|
582
|
+
// An aliased provider shares its address with the other blocks of that
|
|
583
|
+
// provider, so it is keyed by its alias, not by its place among them: a
|
|
584
|
+
// block added elsewhere in the root no longer renumbers it (#5106).
|
|
585
|
+
const alias = entityType === PROVIDER_TYPE && typeof plain.alias === "string" && plain.alias.trim() !== "" ? plain.alias.trim() : undefined;
|
|
586
|
+
const base = alias !== undefined ? `${address}~${alias}` : address;
|
|
587
|
+
let key = `${prefix}/${base}`;
|
|
588
|
+
for (let n = 2; entities.has(key); n++) key = `${prefix}/${base}~${n}`;
|
|
564
589
|
entities.set(key, entity);
|
|
565
590
|
};
|
|
566
591
|
|
package/src/hcl/redact.ts
CHANGED
|
@@ -10,7 +10,11 @@
|
|
|
10
10
|
* redacted form.
|
|
11
11
|
*
|
|
12
12
|
* `body` keeps its `default` key: TF006 asks whether a sensitive variable
|
|
13
|
-
* declares a default, and that fact survives. Only the value goes.
|
|
13
|
+
* declares a default, and that fact survives. Only the value goes. A
|
|
14
|
+
* `default = null` is the exception (#5081): it holds no value, so there is
|
|
15
|
+
* nothing to leave out, and it is the way to say "optional, nothing committed".
|
|
16
|
+
* Replacing it would make it look like a committed default to the rules that
|
|
17
|
+
* read the body.
|
|
14
18
|
*
|
|
15
19
|
* The source scanner skips strings (with `${}`), heredocs and comments, finds
|
|
16
20
|
* each top-level `variable` block, and inside it the `sensitive` and `default`
|
|
@@ -154,7 +158,7 @@ export function redactSensitiveSource(source: string): string {
|
|
|
154
158
|
if (variable) {
|
|
155
159
|
const attrs = attributes(source, i + 1, end - 1);
|
|
156
160
|
const sensitive = attrs.some((a) => a.name === "sensitive" && source.slice(a.from, a.to) === "true");
|
|
157
|
-
if (sensitive) for (const a of attrs) if (a.name === "default") spans.push([a.from, a.to]);
|
|
161
|
+
if (sensitive) for (const a of attrs) if (a.name === "default" && source.slice(a.from, a.to) !== "null") spans.push([a.from, a.to]);
|
|
158
162
|
}
|
|
159
163
|
variable = false;
|
|
160
164
|
i = end;
|
|
@@ -176,6 +180,6 @@ export function redactSensitiveSource(source: string): string {
|
|
|
176
180
|
|
|
177
181
|
/** A variable body with its `default` replaced, when the variable is a literal `sensitive = true` with a default. Otherwise the body itself. */
|
|
178
182
|
export function redactSensitiveBody<T extends Record<string, unknown>>(body: T): T {
|
|
179
|
-
if (body.sensitive !== true || !("default" in body)) return body;
|
|
183
|
+
if (body.sensitive !== true || !("default" in body) || body.default === null) return body;
|
|
180
184
|
return { ...body, default: SENSITIVE_PLACEHOLDER };
|
|
181
185
|
}
|
|
@@ -75,6 +75,74 @@ describe("referencesInBody", () => {
|
|
|
75
75
|
it("descends through nested blocks", () => {
|
|
76
76
|
expect(referencesInBody({ tags: { Name: "${var.name}" } })).toContain("var.name");
|
|
77
77
|
});
|
|
78
|
+
|
|
79
|
+
it("reaches the expression of a Cloud Armor rule, seven levels down", () => {
|
|
80
|
+
// hcl2json shape of `rule { match { expr { expression = "..." } } }`.
|
|
81
|
+
const body = {
|
|
82
|
+
name: "p",
|
|
83
|
+
rule: [{ match: [{ expr: [{ expression: "${\"!request.path.matches('${join(\"|\", local.deep)}')\"}" }] }] }],
|
|
84
|
+
} as never;
|
|
85
|
+
expect(referencesInBody(body)).toContain("local.deep");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("reaches a reference at any depth and keeps the providers rule there", () => {
|
|
89
|
+
let deep: unknown = { x: "${var.bottom}", provider: "${aws.west}" };
|
|
90
|
+
for (let i = 0; i < 200; i++) deep = { block: [deep] };
|
|
91
|
+
const refs = referencesInBody(deep as never);
|
|
92
|
+
expect(refs).toContain("var.bottom");
|
|
93
|
+
expect(refs).toContain("provider.aws.west");
|
|
94
|
+
let mapped: unknown = { providers: { aws: "${aws.east}" } };
|
|
95
|
+
for (let i = 0; i < 50; i++) mapped = { block: [mapped] };
|
|
96
|
+
expect(referencesInBody(mapped as never)).toContain("provider.aws.east");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("survives a pathological body: 100000 levels, a cycle, a shared sub-object, a wide block", () => {
|
|
100
|
+
let deep: unknown = { x: "${local.bottom}" };
|
|
101
|
+
for (let i = 0; i < 100_000; i++) deep = { block: [deep] };
|
|
102
|
+
expect(referencesInBody(deep as never)).toEqual(["local.bottom"]);
|
|
103
|
+
|
|
104
|
+
const cyclic: Record<string, unknown> = { a: "${var.a}" };
|
|
105
|
+
cyclic.self = cyclic;
|
|
106
|
+
cyclic.list = [cyclic, { back: cyclic, b: "${var.b}" }];
|
|
107
|
+
expect(referencesInBody(cyclic as never).sort()).toEqual(["var.a", "var.b"]);
|
|
108
|
+
|
|
109
|
+
const shared = { s: "${local.shared}" };
|
|
110
|
+
expect(referencesInBody({ one: shared, two: [shared, shared] } as never)).toEqual(["local.shared"]);
|
|
111
|
+
|
|
112
|
+
const wide: Record<string, unknown> = {};
|
|
113
|
+
for (let i = 0; i < 50_000; i++) wide[`k${i}`] = [{ v: `\${var.v${i % 100}}` }];
|
|
114
|
+
const start = performance.now();
|
|
115
|
+
expect(referencesInBody(wide as never)).toHaveLength(100);
|
|
116
|
+
expect(performance.now() - start).toBeLessThan(2000);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
describe("a reference inside a nested block, parsed from HCL", () => {
|
|
121
|
+
it("is found for the Cloud Armor rule shape", async () => {
|
|
122
|
+
const source = [
|
|
123
|
+
'locals {',
|
|
124
|
+
' shallow = ["a", "b"]',
|
|
125
|
+
' deep = ["/", "/v2/.*"]',
|
|
126
|
+
'}',
|
|
127
|
+
'resource "google_compute_security_policy" "p" {',
|
|
128
|
+
' name = "p-${join("-", local.shallow)}"',
|
|
129
|
+
' rule {',
|
|
130
|
+
' action = "deny(404)"',
|
|
131
|
+
' priority = "1"',
|
|
132
|
+
' match {',
|
|
133
|
+
' expr {',
|
|
134
|
+
' expression = "!request.path.matches(\'${join("|", local.deep)}\')"',
|
|
135
|
+
' }',
|
|
136
|
+
' }',
|
|
137
|
+
' }',
|
|
138
|
+
'}',
|
|
139
|
+
'',
|
|
140
|
+
].join("\n");
|
|
141
|
+
const ents = await blocksToEntities([{ name: "main.tf", source }], "app");
|
|
142
|
+
const idx = buildReferenceIndex(ents);
|
|
143
|
+
expect(isReferenced(idx, "app", "local.deep", "app/locals")).toBe(true);
|
|
144
|
+
expect(isReferenced(idx, "app", "local.shallow", "app/locals")).toBe(true);
|
|
145
|
+
});
|
|
78
146
|
});
|
|
79
147
|
|
|
80
148
|
describe("buildReferenceIndex over the fixture", () => {
|
package/src/hcl/references.ts
CHANGED
|
@@ -77,35 +77,51 @@ export function providerReference(value: unknown): string | undefined {
|
|
|
77
77
|
return m ? `provider.${m[1]}.${m[2]}` : undefined;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
/**
|
|
80
|
+
/**
|
|
81
|
+
* Every reference in one block body, provider meta-arguments included.
|
|
82
|
+
*
|
|
83
|
+
* The walk reaches every string in the body at any depth. hcl2json wraps each
|
|
84
|
+
* nested block in an array, so a provider's `rule { match { expr { ... } } }`
|
|
85
|
+
* is already six levels down before its attribute, and real providers nest
|
|
86
|
+
* further; a depth cap would have to be guessed and would still miss someone's
|
|
87
|
+
* block. Instead the walk is iterative, so a deeply nested body cannot
|
|
88
|
+
* overflow the call stack, and it marks each container it has opened, so a
|
|
89
|
+
* body that shares a sub-object or points back at an ancestor is read once and
|
|
90
|
+
* cannot loop. Cost is linear in the number of values in the body.
|
|
91
|
+
*/
|
|
81
92
|
export function referencesInBody(body: BlockBody): string[] {
|
|
82
93
|
const found = new Set<string>();
|
|
94
|
+
const opened = new Set<object>();
|
|
95
|
+
const stack: Array<[unknown, string]> = [[body, ""]];
|
|
83
96
|
|
|
84
|
-
|
|
85
|
-
|
|
97
|
+
while (stack.length > 0) {
|
|
98
|
+
const [value, key] = stack.pop()!;
|
|
86
99
|
if (typeof value === "string") {
|
|
87
100
|
for (const ref of collectReferences(value)) found.add(ref);
|
|
88
101
|
if (key === "provider") {
|
|
89
102
|
const provider = providerReference(value);
|
|
90
103
|
if (provider) found.add(provider);
|
|
91
104
|
}
|
|
92
|
-
|
|
105
|
+
continue;
|
|
93
106
|
}
|
|
107
|
+
if (typeof value !== "object" || value === null) continue;
|
|
108
|
+
// A key is passed down unchanged through arrays, so the same container
|
|
109
|
+
// reached under two different keys could in principle matter. A body is a
|
|
110
|
+
// tree, so it is not, and a repeat visit only repeats what was found.
|
|
111
|
+
if (opened.has(value)) continue;
|
|
112
|
+
opened.add(value);
|
|
94
113
|
if (Array.isArray(value)) {
|
|
95
|
-
for (const item of value)
|
|
96
|
-
|
|
114
|
+
for (const item of value) stack.push([item, key]);
|
|
115
|
+
continue;
|
|
97
116
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
visit(v, key === "providers" ? "provider" : k, depth + 1);
|
|
104
|
-
}
|
|
117
|
+
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
|
118
|
+
// `providers = { aws = aws.west }` maps a child module's provider name
|
|
119
|
+
// to the caller's block, so every VALUE under `providers` is a
|
|
120
|
+
// provider reference, whatever its key.
|
|
121
|
+
stack.push([v, key === "providers" ? "provider" : k]);
|
|
105
122
|
}
|
|
106
|
-
}
|
|
123
|
+
}
|
|
107
124
|
|
|
108
|
-
visit(body, "", 0);
|
|
109
125
|
return [...found];
|
|
110
126
|
}
|
|
111
127
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
variable "access_key" {
|
|
2
|
+
description = "HMAC access key ID used to sign origin requests"
|
|
3
|
+
type = string
|
|
4
|
+
default = null
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
variable "secret_key" {
|
|
8
|
+
type = string
|
|
9
|
+
default = null
|
|
10
|
+
sensitive = true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
variable "cloudsql_tier" {
|
|
14
|
+
type = string
|
|
15
|
+
default = "db-custom-1-3840"
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
variable "image_name" {
|
|
19
|
+
type = string
|
|
20
|
+
default = "ibm-ubuntu-24-04-3-minimal-s390x-2"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
locals {
|
|
24
|
+
machine_type = "n2-standard-4"
|
|
25
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
data "google_secret_manager_secret_version_access" "fastly_api_key" {
|
|
2
|
+
secret = "fastly_api_key"
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
data "google_secret_manager_secret_version" "datadog" {
|
|
6
|
+
secret = "projects/example/secrets/datadog_api_key"
|
|
7
|
+
version = "3"
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
data "aws_secretsmanager_secret_version" "db" {
|
|
11
|
+
secret_id = "prod/app/db"
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
resource "google_cloud_run_v2_service" "app" {
|
|
15
|
+
name = "app"
|
|
16
|
+
location = "us-central1"
|
|
17
|
+
|
|
18
|
+
template {
|
|
19
|
+
containers {
|
|
20
|
+
image = "us-docker.pkg.dev/example/app:1"
|
|
21
|
+
env {
|
|
22
|
+
name = "API_KEY"
|
|
23
|
+
value_source {
|
|
24
|
+
secret_key_ref {
|
|
25
|
+
secret = "app_api_key"
|
|
26
|
+
version = "latest"
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|