@intentius/chant-lexicon-terraform 0.124.97 → 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
@@ -21,11 +21,17 @@
21
21
  * A root with no `terraform` block at all is out of scope, same as TF001:
22
22
  * the missing block is that rule's finding, not this one's.
23
23
  *
24
- * Scope: root modules only (#2112). A child module's own `required_providers`
25
- * are the child's business, and its resources are constrained by the root's
26
- * block at plan time, so folding a descended module's blocks into this pass
27
- * would both credit the root with requirements it never declared and judge it
28
- * on providers it never named.
24
+ * Scope: the providers a root implies come from the root's own blocks (#2112);
25
+ * a child module's resources and provider blocks are not judged here. The
26
+ * constraints, though, come from the whole local module tree (#5108).
27
+ * Terraform combines the `required_providers` of every module in a
28
+ * configuration, so a root that gives only `source` is constrained when a
29
+ * local module it calls pins the `version`, and the reverse. A field the root
30
+ * entry lacks is taken from a descended module's entry for the same provider
31
+ * name; the root's own value always wins. A root entry in the legacy bare
32
+ * string form stays a finding. Registry and git modules are never fetched, so
33
+ * only the local modules `descend.ts` already read can contribute, and its
34
+ * cycle guard is what bounds the tree.
29
35
  */
30
36
  import { isResourceDeclarable } from "@intentius/chant/declarable";
31
37
  import { isRootScoped } from "./scope.js";
@@ -100,6 +106,22 @@ function entryProblem(entry) {
100
106
  missing.push("`version`");
101
107
  return missing.length > 0 ? `an entry missing ${missing.join(" and ")}` : undefined;
102
108
  }
109
+ /** Fill what a root entry lacks from the called modules' entries, or return `entry` unchanged. */
110
+ function withModuleFields(entry, fromModules) {
111
+ if (!fromModules || fromModules.length === 0)
112
+ return entry;
113
+ if (entry !== undefined && (typeof entry !== "object" || entry === null))
114
+ return entry;
115
+ const merged = { ...(entry ?? {}) };
116
+ for (const field of ["source", "version"]) {
117
+ if (typeof merged[field] === "string" && merged[field] !== "")
118
+ continue;
119
+ const found = fromModules.find((m) => typeof m[field] === "string" && m[field] !== "");
120
+ if (found)
121
+ merged[field] = found[field];
122
+ }
123
+ return Object.keys(merged).length === 0 ? entry : merged;
124
+ }
103
125
  export const tf002 = {
104
126
  id: "TF002",
105
127
  description: "A provider the root implies has no required_providers entry, or an incomplete one",
@@ -107,13 +129,25 @@ export const tf002 = {
107
129
  const diagnostics = [];
108
130
  const roots = new Set();
109
131
  const requiredByRoot = new Map();
132
+ const moduleEntriesByRoot = new Map();
110
133
  for (const entity of ctx.entities.values()) {
111
134
  if (entity.entityType !== TERRAFORM_TYPE || !isResourceDeclarable(entity))
112
135
  continue;
113
- if (!isRootScoped(entity))
114
- continue;
115
136
  const props = entity.props;
116
137
  const root = typeof props.root === "string" ? props.root : "";
138
+ if (!isRootScoped(entity)) {
139
+ const childBody = (typeof props.body === "object" && props.body !== null ? props.body : {});
140
+ const byName = moduleEntriesByRoot.get(root) ?? new Map();
141
+ for (const [name, entry] of Object.entries(requiredProvidersMap(childBody))) {
142
+ if (typeof entry !== "object" || entry === null)
143
+ continue;
144
+ const list = byName.get(name) ?? [];
145
+ list.push(entry);
146
+ byName.set(name, list);
147
+ }
148
+ moduleEntriesByRoot.set(root, byName);
149
+ continue;
150
+ }
117
151
  roots.add(root);
118
152
  const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
119
153
  const merged = requiredByRoot.get(root) ?? {};
@@ -124,7 +158,7 @@ export const tf002 = {
124
158
  const required = requiredByRoot.get(root) ?? {};
125
159
  const implied = impliedProviders(ctx.entities, root);
126
160
  for (const name of [...implied.keys()].sort()) {
127
- const problem = entryProblem(required[name]);
161
+ const problem = entryProblem(withModuleFields(required[name], moduleEntriesByRoot.get(root)?.get(name)));
128
162
  if (!problem)
129
163
  continue;
130
164
  diagnostics.push({
@@ -8,9 +8,13 @@
8
8
  * survives longest, because every environment that forgets to pass the variable
9
9
  * silently gets the committed one.
10
10
  *
11
- * Any default counts, not only a secret-shaped one. `default = ""` and
12
- * `default = null` are the two that look harmless, and both make a required
13
- * secret optional, which is the other half of the failure.
11
+ * Any default counts, not only a secret-shaped one. `default = ""` looks
12
+ * harmless and makes a required secret optional, which is the other half of
13
+ * the failure. `default = null` is the one exception (#5081): it commits no
14
+ * value, so nothing is in git, and it is how a variable is declared optional
15
+ * on purpose (a module that takes a credential only when a feature is on).
16
+ * The IR keeps a null default as null (see `../hcl/redact.ts`) so the rule can
17
+ * tell it apart from a literal that was redacted.
14
18
  *
15
19
  * Scope: root and child modules alike (#2112). The condition is a property
16
20
  * of the block itself, so a descended module's block is read exactly as a
@@ -1 +1 @@
1
- {"version":3,"file":"tf006.d.ts","sourceRoot":"","sources":["../../../src/lint/post-synth/tf006.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,kCAAkC,CAAC;AAI1C,eAAO,MAAM,KAAK,EAAE,cAyBnB,CAAC"}
1
+ {"version":3,"file":"tf006.d.ts","sourceRoot":"","sources":["../../../src/lint/post-synth/tf006.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,kCAAkC,CAAC;AAI1C,eAAO,MAAM,KAAK,EAAE,cA0BnB,CAAC"}
@@ -8,9 +8,13 @@
8
8
  * survives longest, because every environment that forgets to pass the variable
9
9
  * silently gets the committed one.
10
10
  *
11
- * Any default counts, not only a secret-shaped one. `default = ""` and
12
- * `default = null` are the two that look harmless, and both make a required
13
- * secret optional, which is the other half of the failure.
11
+ * Any default counts, not only a secret-shaped one. `default = ""` looks
12
+ * harmless and makes a required secret optional, which is the other half of
13
+ * the failure. `default = null` is the one exception (#5081): it commits no
14
+ * value, so nothing is in git, and it is how a variable is declared optional
15
+ * on purpose (a module that takes a credential only when a feature is on).
16
+ * The IR keeps a null default as null (see `../hcl/redact.ts`) so the rule can
17
+ * tell it apart from a literal that was redacted.
14
18
  *
15
19
  * Scope: root and child modules alike (#2112). The condition is a property
16
20
  * of the block itself, so a descended module's block is read exactly as a
@@ -28,6 +32,8 @@ export const tf006 = {
28
32
  continue;
29
33
  if (!("default" in block.body))
30
34
  continue;
35
+ if (block.body.default === null)
36
+ continue;
31
37
  diagnostics.push({
32
38
  checkId: "TF006",
33
39
  severity: "error",
@@ -15,6 +15,12 @@
15
15
  * shape, a reference rather than a constant, which is what the message says
16
16
  * rather than "remove this".
17
17
  *
18
+ * An argument that names a secret instead of holding one is skipped: the
19
+ * `secret` of a Secret Manager version data source is the id of the secret to
20
+ * read, and reading it at apply time is the fix (#5080). The skip is a table
21
+ * of types and attribute paths in `../secret-shape.ts`, not a rule about the
22
+ * word `secret`, so the same name on a type outside it is still judged.
23
+ *
18
24
  * Scope: root and child modules alike (#2112). The condition is a property
19
25
  * of the block itself, so a descended module's block is read exactly as a
20
26
  * root's is.
@@ -1 +1 @@
1
- {"version":3,"file":"tf022.d.ts","sourceRoot":"","sources":["../../../src/lint/post-synth/tf022.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,kCAAkC,CAAC;AAK1C,eAAO,MAAM,KAAK,EAAE,cA6BnB,CAAC"}
1
+ {"version":3,"file":"tf022.d.ts","sourceRoot":"","sources":["../../../src/lint/post-synth/tf022.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,OAAO,KAAK,EACV,cAAc,EAGf,MAAM,kCAAkC,CAAC;AAY1C,eAAO,MAAM,KAAK,EAAE,cAiCnB,CAAC"}
@@ -15,22 +15,39 @@
15
15
  * shape, a reference rather than a constant, which is what the message says
16
16
  * rather than "remove this".
17
17
  *
18
+ * An argument that names a secret instead of holding one is skipped: the
19
+ * `secret` of a Secret Manager version data source is the id of the secret to
20
+ * read, and reading it at apply time is the fix (#5080). The skip is a table
21
+ * of types and attribute paths in `../secret-shape.ts`, not a rule about the
22
+ * word `secret`, so the same name on a type outside it is still judged.
23
+ *
18
24
  * Scope: root and child modules alike (#2112). The condition is a property
19
25
  * of the block itself, so a descended module's block is read exactly as a
20
26
  * root's is.
21
27
  */
22
28
  import { DATA_TYPE, RESOURCE_TYPE } from "../../hcl/parse.js";
23
- import { isSecretName, secretShapedAssignment } from "../secret-shape.js";
29
+ import { isSecretName, isSecretReferenceArgument, secretShapedAssignment } from "../secret-shape.js";
24
30
  import { blocksOfTypes, walkAttributes } from "./blocks.js";
31
+ /** `data.google_x.y` and `google_x.y` both give `google_x`. */
32
+ function terraformTypeOf(address) {
33
+ const bare = address.startsWith("data.") ? address.slice("data.".length) : address;
34
+ const dot = bare.indexOf(".");
35
+ return dot === -1 ? bare : bare.slice(0, dot);
36
+ }
25
37
  export const tf022 = {
26
38
  id: "TF022",
27
39
  description: "Credential-named resource attribute holds a plaintext literal",
28
40
  check(ctx) {
29
41
  const diagnostics = [];
30
42
  for (const block of blocksOfTypes(ctx, [RESOURCE_TYPE, DATA_TYPE])) {
43
+ const blockType = terraformTypeOf(block.address);
31
44
  for (const attr of walkAttributes(block.body)) {
32
45
  if (!isSecretName(attr.name))
33
46
  continue;
47
+ // `secret` on a Secret Manager version data source is the id of the
48
+ // secret to read, which is the fix this rule asks for (#5080).
49
+ if (isSecretReferenceArgument(blockType, attr.path))
50
+ continue;
34
51
  const shape = secretShapedAssignment(attr.name, attr.value);
35
52
  if (!shape)
36
53
  continue;
@@ -20,6 +20,14 @@
20
20
  * characters, because an attribute value is already a single token rather
21
21
  * than a line of prose to fish tokens out of.
22
22
  */
23
+ /**
24
+ * Is this attribute, on a block of this resource or data source type, a
25
+ * reference to a secret (an id, a name or a resource path) instead of a
26
+ * secret value? `path` is the dotted path from the block body.
27
+ */
28
+ export declare function isSecretReferenceArgument(blockType: string, path: string): boolean;
29
+ /** Is this value one of the public-name shapes in {@link NAMED_SHAPES}? */
30
+ export declare function isPublicNameShape(value: string): boolean;
23
31
  /** Minimum length for a value to be worth judging at all. */
24
32
  export declare const MIN_SECRET_LENGTH = 8;
25
33
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"secret-shape.d.ts","sourceRoot":"","sources":["../../src/lint/secret-shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAyBH,6DAA6D;AAC7D,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAmBnC;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAIlD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE/D;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEzD;AAqBD;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAU/D;AAED,4EAA4E;AAC5E,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,OAAO,CAAC;AAEjD,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,iBAAiB,CAAC;IAC1B,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,WAAW,GAAG,SAAS,CAO5F"}
1
+ {"version":3,"file":"secret-shape.d.ts","sourceRoot":"","sources":["../../src/lint/secret-shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AA4CH;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAElF;AA2CD,2EAA2E;AAC3E,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAExD;AAED,6DAA6D;AAC7D,eAAO,MAAM,iBAAiB,IAAI,CAAC;AAmBnC;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAIlD;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAE/D;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAEzD;AAsBD;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAU/D;AAED,4EAA4E;AAC5E,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,OAAO,CAAC;AAEjD,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,iBAAiB,CAAC;IAC1B,4EAA4E;IAC5E,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,WAAW,GAAG,SAAS,CAO5F"}
@@ -35,6 +35,80 @@ const SECRET_NAME_RE = /(^|_)(password|passwd|secret|token|api_key|private_key|a
35
35
  * flag the remediation.
36
36
  */
37
37
  const LOCATOR_SUFFIX_RE = /_(file|path|arn|id|name)$/;
38
+ /**
39
+ * Arguments that name a secret rather than hold one, where the name alone
40
+ * would read as a credential. `secret` on a Secret Manager version data
41
+ * source is the id of the secret to read, so a literal there is the remedy the
42
+ * rules ask for (read the value at apply time), not a committed value (#5080).
43
+ * Keyed by resource or data source type, then by the dotted attribute path, so
44
+ * a bare `secret` on some other type is still judged by name.
45
+ */
46
+ const SECRET_REFERENCE_ARGUMENTS = [
47
+ // data "google_secret_manager_secret_version_access" / _version, and the
48
+ // regional variants and the resource form: `secret` is the parent secret.
49
+ { type: /^google_secret_manager_(regional_)?secret_version(_access)?$/, path: /^secret$/ },
50
+ // Cloud Functions and Cloud Run mount a secret by id, never by value.
51
+ {
52
+ type: /^google_(cloudfunctions2?_function|cloud_run(_v2)?_(service|job))$/,
53
+ path: /(^|\.)(secret_environment_variables|secret_volumes|secret_key_ref)\.secret$/,
54
+ },
55
+ ];
56
+ /**
57
+ * Is this attribute, on a block of this resource or data source type, a
58
+ * reference to a secret (an id, a name or a resource path) instead of a
59
+ * secret value? `path` is the dotted path from the block body.
60
+ */
61
+ export function isSecretReferenceArgument(blockType, path) {
62
+ return SECRET_REFERENCE_ARGUMENTS.some((r) => r.type.test(blockType) && r.path.test(path));
63
+ }
64
+ /**
65
+ * Shapes of values that are public names for a kind of object, and that the
66
+ * generic high-entropy test would otherwise read as random: a machine type
67
+ * (`db-custom-1-3840`, `n2-standard-4`, `bx2-4x16`), an image name
68
+ * (`ibm-ubuntu-24-04-3-minimal-s390x-2`, `debian-12-bookworm-v20240110`) and
69
+ * an Azure size (`Standard_D16ads_v5`). Each is matched on the shape of the
70
+ * whole value, a table of shapes and not of attribute names, so a secret that
71
+ * happens to sit in a variable called `image_name` is still judged by its
72
+ * value (#5081).
73
+ *
74
+ * The hyphenated shape is deliberately strict, so that a real token is not
75
+ * waved through: every segment is a lowercase word, a number, or a short
76
+ * letters-and-digits tag (`s390x`, `e2`, `4x16`, `v20240110`); there are at
77
+ * least three segments, and at least one is a word of three or more letters
78
+ * with a vowel. A UUID, a hex string, a base64 token and an `ami-` id all have
79
+ * a segment that is none of those, so they still reach the entropy test.
80
+ */
81
+ const NAMED_SHAPES = [
82
+ {
83
+ name: "hyphenated machine type or image name",
84
+ test(value) {
85
+ if (!/^[a-z0-9]+(-[a-z0-9]+)+$/.test(value))
86
+ return false;
87
+ const segments = value.split("-");
88
+ if (segments.length < 3)
89
+ return false;
90
+ let hasWord = false;
91
+ for (const seg of segments) {
92
+ if (/^[a-z]{2,16}$/.test(seg)) {
93
+ if (seg.length >= 3 && /[aeiouy]/.test(seg))
94
+ hasWord = true;
95
+ }
96
+ else if (!/^(\d+|[a-z]{1,2}\d+[a-z]{0,2}|\d+[a-z]{1,2}\d*)$/.test(seg)) {
97
+ return false;
98
+ }
99
+ }
100
+ return hasWord;
101
+ },
102
+ },
103
+ {
104
+ name: "Azure size",
105
+ test: (value) => /^(Standard|Basic|Premium)_[A-Za-z0-9]+(_[A-Za-z0-9]+)*$/.test(value),
106
+ },
107
+ ];
108
+ /** Is this value one of the public-name shapes in {@link NAMED_SHAPES}? */
109
+ export function isPublicNameShape(value) {
110
+ return NAMED_SHAPES.some((s) => s.test(value));
111
+ }
38
112
  /** Minimum length for a value to be worth judging at all. */
39
113
  export const MIN_SECRET_LENGTH = 8;
40
114
  /** Minimum length for the generic high-entropy shape (below it, entropy says nothing). */
@@ -101,6 +175,8 @@ function isHighEntropyToken(value) {
101
175
  return false;
102
176
  if (looksLikeIdentifierSlug(value))
103
177
  return false;
178
+ if (isPublicNameShape(value))
179
+ return false;
104
180
  return shannonEntropy(value) >= ENTROPY_THRESHOLD;
105
181
  }
106
182
  /**
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "terraform",
3
- "version": "0.124.97",
3
+ "version": "0.124.98",
4
4
  "chantVersion": ">=0.1.0",
5
5
  "namespace": "Terraform"
6
6
  }
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAsB,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAsElF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,EAAE,aAqT7B,CAAC"}
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAsB,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAsElF;;;;;;;GAOG;AACH,eAAO,MAAM,eAAe,EAAE,aAyT7B,CAAC"}
package/dist/plugin.js CHANGED
@@ -265,7 +265,10 @@ export const terraformPlugin = {
265
265
  * audited tree, a cycle) are warnings on a build; this hook returns
266
266
  * entities only, so on the audit path they are dropped rather than printed.
267
267
  *
268
- * Never throws: malformed HCL yields an empty map.
268
+ * Malformed HCL yields an empty map. The one error it lets through is a
269
+ * missing HCL parser (an error carrying `parserPackage`): returning an empty
270
+ * map for that would read as a clean directory, so `chant audit` catches it
271
+ * and reports the directories it could not read (#5083).
269
272
  */
270
273
  async auditEntities(content, input) {
271
274
  try {
@@ -294,7 +297,9 @@ export const terraformPlugin = {
294
297
  entities.set(key, entity);
295
298
  return entities;
296
299
  }
297
- catch {
300
+ catch (err) {
301
+ if (typeof err?.parserPackage === "string")
302
+ throw err;
298
303
  return new Map();
299
304
  }
300
305
  },
@@ -21,7 +21,10 @@
21
21
  * `# chant-ignore-block: TF001` on the line above its `terraform` block
22
22
  * (chant #2111), which is what the three shipped examples do.
23
23
  *
24
- * One diagnostic per root, fired from the root's `terraform` block. A root
24
+ * One diagnostic per root, fired from the root's first `terraform` block. The
25
+ * verdict reads every `terraform` block of the root, because Terraform merges
26
+ * them and a backend often sits in `backend.tf` or `provider.tf` beside a
27
+ * `versions.tf` block of its own (#5079). A root
25
28
  * with no `terraform` block at all is not flagged: it declares no version
26
29
  * constraints either, and the missing block is a different finding.
27
30
  *
@@ -98,8 +101,14 @@ export const tf001: PostSynthCheck = {
98
101
 
99
102
  check(ctx: PostSynthContext): PostSynthDiagnostic[] {
100
103
  const diagnostics: PostSynthDiagnostic[] = [];
101
- /** Root name to the entity key of the first `terraform` block seen for it. */
102
- const flagged = new Set<string>();
104
+ /**
105
+ * Per root: the entity key of the first `terraform` block seen, whether any
106
+ * block of the root configures remote state, and whether any block names
107
+ * `backend "local"`. Terraform merges the `terraform` blocks of a module,
108
+ * so the verdict belongs to the root, not to whichever block is read first
109
+ * (#5079).
110
+ */
111
+ const roots = new Map<string, { entity: string; remote: boolean; local: boolean }>();
103
112
 
104
113
  for (const [name, entity] of ctx.entities) {
105
114
  if (entity.entityType !== TERRAFORM_TYPE) continue;
@@ -107,18 +116,23 @@ export const tf001: PostSynthCheck = {
107
116
  const props = entity.props as { root?: unknown; body?: unknown; mode?: unknown };
108
117
  if (props.mode === "live") continue;
109
118
  const root = typeof props.root === "string" ? props.root : "";
110
- if (flagged.has(root)) continue;
111
119
 
112
120
  const body = (typeof props.body === "object" && props.body !== null ? props.body : {}) as BlockBody;
113
- if (hasRemoteBackend(body)) continue;
121
+ const seen = roots.get(root) ?? { entity: name, remote: false, local: false };
122
+ seen.remote ||= hasRemoteBackend(body);
123
+ seen.local ||= backendTypes(body).includes(LOCAL_BACKEND);
124
+ roots.set(root, seen);
125
+ }
126
+
127
+ for (const [root, { entity: name, remote, local }] of roots) {
128
+ if (remote) continue;
114
129
 
115
130
  // What the root actually declares, so the message names it rather than
116
131
  // saying "no remote backend" over a block the reader can see.
117
- const found = backendTypes(body).includes(LOCAL_BACKEND)
132
+ const found = local
118
133
  ? 'the backend it declares is `backend "local"`'
119
134
  : "there is no backend block at all";
120
135
 
121
- flagged.add(root);
122
136
  diagnostics.push({
123
137
  checkId: "TF001",
124
138
  severity: "warning",
@@ -21,11 +21,17 @@
21
21
  * A root with no `terraform` block at all is out of scope, same as TF001:
22
22
  * the missing block is that rule's finding, not this one's.
23
23
  *
24
- * Scope: root modules only (#2112). A child module's own `required_providers`
25
- * are the child's business, and its resources are constrained by the root's
26
- * block at plan time, so folding a descended module's blocks into this pass
27
- * would both credit the root with requirements it never declared and judge it
28
- * on providers it never named.
24
+ * Scope: the providers a root implies come from the root's own blocks (#2112);
25
+ * a child module's resources and provider blocks are not judged here. The
26
+ * constraints, though, come from the whole local module tree (#5108).
27
+ * Terraform combines the `required_providers` of every module in a
28
+ * configuration, so a root that gives only `source` is constrained when a
29
+ * local module it calls pins the `version`, and the reverse. A field the root
30
+ * entry lacks is taken from a descended module's entry for the same provider
31
+ * name; the root's own value always wins. A root entry in the legacy bare
32
+ * string form stays a finding. Registry and git modules are never fetched, so
33
+ * only the local modules `descend.ts` already read can contribute, and its
34
+ * cycle guard is what bounds the tree.
29
35
  */
30
36
 
31
37
  import type {
@@ -105,6 +111,25 @@ function entryProblem(entry: unknown): string | undefined {
105
111
  return missing.length > 0 ? `an entry missing ${missing.join(" and ")}` : undefined;
106
112
  }
107
113
 
114
+ /** The entries the descended modules of a root hold for each provider name, . */
115
+ type ModuleEntries = Map<string, Array<Record<string, unknown>>>;
116
+
117
+ /** Fill what a root entry lacks from the called modules' entries, or return `entry` unchanged. */
118
+ function withModuleFields(
119
+ entry: unknown,
120
+ fromModules: Array<Record<string, unknown>> | undefined,
121
+ ): unknown {
122
+ if (!fromModules || fromModules.length === 0) return entry;
123
+ if (entry !== undefined && (typeof entry !== "object" || entry === null)) return entry;
124
+ const merged: Record<string, unknown> = { ...((entry as Record<string, unknown> | undefined) ?? {}) };
125
+ for (const field of ["source", "version"]) {
126
+ if (typeof merged[field] === "string" && merged[field] !== "") continue;
127
+ const found = fromModules.find((m) => typeof m[field] === "string" && m[field] !== "");
128
+ if (found) merged[field] = found[field];
129
+ }
130
+ return Object.keys(merged).length === 0 ? entry : merged;
131
+ }
132
+
108
133
  export const tf002: PostSynthCheck = {
109
134
  id: "TF002",
110
135
  description: "A provider the root implies has no required_providers entry, or an incomplete one",
@@ -113,12 +138,24 @@ export const tf002: PostSynthCheck = {
113
138
  const diagnostics: PostSynthDiagnostic[] = [];
114
139
  const roots = new Set<string>();
115
140
  const requiredByRoot = new Map<string, Record<string, unknown>>();
141
+ const moduleEntriesByRoot = new Map<string, ModuleEntries>();
116
142
 
117
143
  for (const entity of ctx.entities.values()) {
118
144
  if (entity.entityType !== TERRAFORM_TYPE || !isResourceDeclarable(entity)) continue;
119
- if (!isRootScoped(entity)) continue;
120
145
  const props = entity.props as { root?: unknown; body?: unknown };
121
146
  const root = typeof props.root === "string" ? props.root : "";
147
+ if (!isRootScoped(entity)) {
148
+ const childBody = (typeof props.body === "object" && props.body !== null ? props.body : {}) as BlockBody;
149
+ const byName: ModuleEntries = moduleEntriesByRoot.get(root) ?? new Map();
150
+ for (const [name, entry] of Object.entries(requiredProvidersMap(childBody))) {
151
+ if (typeof entry !== "object" || entry === null) continue;
152
+ const list = byName.get(name) ?? [];
153
+ list.push(entry as Record<string, unknown>);
154
+ byName.set(name, list);
155
+ }
156
+ moduleEntriesByRoot.set(root, byName);
157
+ continue;
158
+ }
122
159
  roots.add(root);
123
160
  const body = (typeof props.body === "object" && props.body !== null ? props.body : {}) as BlockBody;
124
161
  const merged = requiredByRoot.get(root) ?? {};
@@ -131,7 +168,7 @@ export const tf002: PostSynthCheck = {
131
168
  const implied = impliedProviders(ctx.entities, root);
132
169
 
133
170
  for (const name of [...implied.keys()].sort()) {
134
- const problem = entryProblem(required[name]);
171
+ const problem = entryProblem(withModuleFields(required[name], moduleEntriesByRoot.get(root)?.get(name)));
135
172
  if (!problem) continue;
136
173
  diagnostics.push({
137
174
  checkId: "TF002",
@@ -8,9 +8,13 @@
8
8
  * survives longest, because every environment that forgets to pass the variable
9
9
  * silently gets the committed one.
10
10
  *
11
- * Any default counts, not only a secret-shaped one. `default = ""` and
12
- * `default = null` are the two that look harmless, and both make a required
13
- * secret optional, which is the other half of the failure.
11
+ * Any default counts, not only a secret-shaped one. `default = ""` looks
12
+ * harmless and makes a required secret optional, which is the other half of
13
+ * the failure. `default = null` is the one exception (#5081): it commits no
14
+ * value, so nothing is in git, and it is how a variable is declared optional
15
+ * on purpose (a module that takes a credential only when a feature is on).
16
+ * The IR keeps a null default as null (see `../hcl/redact.ts`) so the rule can
17
+ * tell it apart from a literal that was redacted.
14
18
  *
15
19
  * Scope: root and child modules alike (#2112). The condition is a property
16
20
  * of the block itself, so a descended module's block is read exactly as a
@@ -35,6 +39,7 @@ export const tf006: PostSynthCheck = {
35
39
  for (const block of blocksOfType(ctx, VARIABLE_TYPE)) {
36
40
  if (block.body.sensitive !== true) continue;
37
41
  if (!("default" in block.body)) continue;
42
+ if (block.body.default === null) continue;
38
43
 
39
44
  diagnostics.push({
40
45
  checkId: "TF006",
@@ -15,6 +15,12 @@
15
15
  * shape, a reference rather than a constant, which is what the message says
16
16
  * rather than "remove this".
17
17
  *
18
+ * An argument that names a secret instead of holding one is skipped: the
19
+ * `secret` of a Secret Manager version data source is the id of the secret to
20
+ * read, and reading it at apply time is the fix (#5080). The skip is a table
21
+ * of types and attribute paths in `../secret-shape.ts`, not a rule about the
22
+ * word `secret`, so the same name on a type outside it is still judged.
23
+ *
18
24
  * Scope: root and child modules alike (#2112). The condition is a property
19
25
  * of the block itself, so a descended module's block is read exactly as a
20
26
  * root's is.
@@ -26,9 +32,16 @@ import type {
26
32
  PostSynthDiagnostic,
27
33
  } from "@intentius/chant/lint/post-synth";
28
34
  import { DATA_TYPE, RESOURCE_TYPE } from "../../hcl/parse";
29
- import { isSecretName, secretShapedAssignment } from "../secret-shape";
35
+ import { isSecretName, isSecretReferenceArgument, secretShapedAssignment } from "../secret-shape";
30
36
  import { blocksOfTypes, walkAttributes } from "./blocks";
31
37
 
38
+ /** `data.google_x.y` and `google_x.y` both give `google_x`. */
39
+ function terraformTypeOf(address: string): string {
40
+ const bare = address.startsWith("data.") ? address.slice("data.".length) : address;
41
+ const dot = bare.indexOf(".");
42
+ return dot === -1 ? bare : bare.slice(0, dot);
43
+ }
44
+
32
45
  export const tf022: PostSynthCheck = {
33
46
  id: "TF022",
34
47
  description: "Credential-named resource attribute holds a plaintext literal",
@@ -37,8 +50,12 @@ export const tf022: PostSynthCheck = {
37
50
  const diagnostics: PostSynthDiagnostic[] = [];
38
51
 
39
52
  for (const block of blocksOfTypes(ctx, [RESOURCE_TYPE, DATA_TYPE])) {
53
+ const blockType = terraformTypeOf(block.address);
40
54
  for (const attr of walkAttributes(block.body)) {
41
55
  if (!isSecretName(attr.name)) continue;
56
+ // `secret` on a Secret Manager version data source is the id of the
57
+ // secret to read, which is the fix this rule asks for (#5080).
58
+ if (isSecretReferenceArgument(blockType, attr.path)) continue;
42
59
  const shape = secretShapedAssignment(attr.name, attr.value);
43
60
  if (!shape) continue;
44
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intentius/chant-lexicon-terraform",
3
- "version": "0.124.97",
3
+ "version": "0.124.98",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src/",
@@ -93,7 +93,7 @@
93
93
  "access": "public"
94
94
  },
95
95
  "peerDependencies": {
96
- "@intentius/chant": "^0.124.97",
96
+ "@intentius/chant": "^0.124.98",
97
97
  "zod": "^4.3.6"
98
98
  },
99
99
  "description": "terraform lexicon for chant, existing Terraform root modules as declarable entities",
@@ -0,0 +1,28 @@
1
+ resource "google_storage_bucket" "from_data" {
2
+ name = "${data.google_organization.org.org_id}-artifacts"
3
+ location = "US"
4
+ }
5
+
6
+ resource "google_storage_bucket" "dated" {
7
+ name = "logs-${timestamp()}"
8
+ location = "US"
9
+ }
10
+
11
+ resource "google_project_iam_member" "computed" {
12
+ project = module.project.project_id
13
+ role = "roles/viewer"
14
+ member = "serviceAccount:${module.workload_identity_service_accounts["gcb-builder"].email}"
15
+ }
16
+
17
+ module "canary_cluster" {
18
+ source = "../modules/gke-cluster"
19
+ project_name = local.project_id
20
+ cluster_name = "${local.cluster_name}-canary"
21
+ is_prod_cluster = "false"
22
+ }
23
+
24
+ resource "google_storage_bucket" "compared" {
25
+ count = local.project_id != "" ? 1 : 0
26
+ name = data.google_organization.org.org_id == "" ? "a" : "b"
27
+ location = "US"
28
+ }
@@ -0,0 +1,22 @@
1
+ locals {
2
+ project_id = "k8s-infra-prow-build-trusted"
3
+ cluster_name = "prow-build-trusted" // The name of the cluster defined in this file
4
+ pod_namespace = "test-pods"
5
+ }
6
+
7
+ data "google_organization" "org" {
8
+ domain = "kubernetes.io"
9
+ }
10
+
11
+ module "project" {
12
+ source = "../modules/gke-project"
13
+ project_id = local.project_id
14
+ project_name = local.project_id
15
+ }
16
+
17
+ module "prow_build_cluster" {
18
+ source = "../modules/gke-cluster"
19
+ project_name = module.project.project_id
20
+ cluster_name = local.cluster_name
21
+ is_prod_cluster = "true"
22
+ }