@intentius/chant-lexicon-terraform 0.124.31 → 0.124.33

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 (149) hide show
  1. package/dist/__fixtures__/terragrunt/affected-cases.js +111 -0
  2. package/dist/__fixtures__/terragrunt/record.js +57 -0
  3. package/dist/behaviour/index.js +6 -0
  4. package/dist/behaviour/kinds.js +194 -0
  5. package/dist/behaviour/predict.js +297 -0
  6. package/dist/behaviour/reference-catalog.js +178 -0
  7. package/dist/behaviour/request.js +482 -0
  8. package/dist/change-set.js +234 -0
  9. package/dist/changed-roots.js +97 -0
  10. package/dist/codegen/docs-cli.js +3 -0
  11. package/dist/codegen/docs.js +44 -0
  12. package/dist/codegen/generate-cli.js +9 -0
  13. package/dist/codegen/generate.js +59 -0
  14. package/dist/codegen/package.js +43 -0
  15. package/dist/commands.js +106 -0
  16. package/dist/components/capability-plugin.d.ts +38 -2
  17. package/dist/components/capability-plugin.d.ts.map +1 -1
  18. package/dist/components/capability-plugin.js +260 -0
  19. package/dist/composites/terraform-adopt-op.d.ts +2 -0
  20. package/dist/composites/terraform-adopt-op.d.ts.map +1 -1
  21. package/dist/composites/terraform-adopt-op.js +153 -0
  22. package/dist/composites/terraform-apply-op.d.ts +2 -0
  23. package/dist/composites/terraform-apply-op.d.ts.map +1 -1
  24. package/dist/composites/terraform-apply-op.js +184 -0
  25. package/dist/composites/terraform-pin-rollout-op.js +45 -0
  26. package/dist/composites/terraform-watch-op.js +219 -0
  27. package/dist/config.js +118 -0
  28. package/dist/describe-resources.js +984 -0
  29. package/dist/generated/index.js +1 -0
  30. package/dist/hcl/descend.js +208 -0
  31. package/dist/hcl/edges.js +287 -0
  32. package/dist/hcl/jsonencode.js +442 -0
  33. package/dist/hcl/marker.js +212 -0
  34. package/dist/hcl/parse.js +372 -0
  35. package/dist/hcl/references.js +156 -0
  36. package/dist/hcl/roots.js +98 -0
  37. package/dist/hcl/suppressions.js +189 -0
  38. package/dist/hcl/value.js +72 -0
  39. package/dist/index.d.ts +1 -1
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +46 -0
  42. package/dist/integrity.json +2 -2
  43. package/dist/lint/audit-catalog.js +501 -0
  44. package/dist/lint/audit-lineage.js +442 -0
  45. package/dist/lint/post-synth/blocks.js +87 -0
  46. package/dist/lint/post-synth/fixtures/load.js +94 -0
  47. package/dist/lint/post-synth/flat-attributes.js +135 -0
  48. package/dist/lint/post-synth/index.js +90 -0
  49. package/dist/lint/post-synth/module-source.js +153 -0
  50. package/dist/lint/post-synth/scope.js +104 -0
  51. package/dist/lint/post-synth/terragrunt.js +63 -0
  52. package/dist/lint/post-synth/tf001.js +138 -0
  53. package/dist/lint/post-synth/tf002.js +143 -0
  54. package/dist/lint/post-synth/tf003.js +60 -0
  55. package/dist/lint/post-synth/tf004.js +54 -0
  56. package/dist/lint/post-synth/tf005.js +68 -0
  57. package/dist/lint/post-synth/tf006.js +43 -0
  58. package/dist/lint/post-synth/tf007.js +66 -0
  59. package/dist/lint/post-synth/tf008.js +64 -0
  60. package/dist/lint/post-synth/tf009.js +49 -0
  61. package/dist/lint/post-synth/tf010.js +40 -0
  62. package/dist/lint/post-synth/tf011.js +38 -0
  63. package/dist/lint/post-synth/tf012.js +36 -0
  64. package/dist/lint/post-synth/tf013.js +53 -0
  65. package/dist/lint/post-synth/tf014.js +61 -0
  66. package/dist/lint/post-synth/tf015.js +62 -0
  67. package/dist/lint/post-synth/tf016.js +66 -0
  68. package/dist/lint/post-synth/tf017.js +39 -0
  69. package/dist/lint/post-synth/tf018.js +63 -0
  70. package/dist/lint/post-synth/tf019.js +60 -0
  71. package/dist/lint/post-synth/tf020.js +103 -0
  72. package/dist/lint/post-synth/tf021.js +117 -0
  73. package/dist/lint/post-synth/tf022.js +51 -0
  74. package/dist/lint/post-synth/tf024.js +62 -0
  75. package/dist/lint/post-synth/tf025.js +83 -0
  76. package/dist/lint/post-synth/tf026.js +74 -0
  77. package/dist/lint/post-synth/tf027.js +66 -0
  78. package/dist/lint/post-synth/tf028.js +83 -0
  79. package/dist/lint/post-synth/tf029.js +73 -0
  80. package/dist/lint/post-synth/tf030.js +253 -0
  81. package/dist/lint/post-synth/tf031.js +332 -0
  82. package/dist/lint/post-synth/tf032.js +201 -0
  83. package/dist/lint/post-synth/tf033.js +126 -0
  84. package/dist/lint/post-synth/tf034.js +45 -0
  85. package/dist/lint/post-synth/tf035.js +63 -0
  86. package/dist/lint/post-synth/tf036.js +97 -0
  87. package/dist/lint/post-synth/tf037.js +80 -0
  88. package/dist/lint/post-synth/tf038.js +63 -0
  89. package/dist/lint/post-synth/tf039.js +55 -0
  90. package/dist/lint/post-synth/tf040.js +67 -0
  91. package/dist/lint/post-synth/tf041.js +53 -0
  92. package/dist/lint/post-synth/tf042.js +38 -0
  93. package/dist/lint/post-synth/tf043.js +45 -0
  94. package/dist/lint/post-synth/tf044.js +45 -0
  95. package/dist/lint/post-synth/tf045.js +44 -0
  96. package/dist/lint/rules/fixtures/load.js +50 -0
  97. package/dist/lint/rules/index.js +4 -0
  98. package/dist/lint/rules/plan-before-apply.js +136 -0
  99. package/dist/lint/secret-shape.js +149 -0
  100. package/dist/live-ownership.js +57 -0
  101. package/dist/lsp/completions.js +95 -0
  102. package/dist/lsp/context.js +358 -0
  103. package/dist/lsp/hover.js +45 -0
  104. package/dist/lsp/option-keys.js +93 -0
  105. package/dist/manifest.json +1 -1
  106. package/dist/op/activities/index.js +30 -0
  107. package/dist/op/activities/live-detect.js +143 -0
  108. package/dist/op/activities/pin-rollout.js +45 -0
  109. package/dist/op/activities/terraform.js +1270 -0
  110. package/dist/op/activity-contracts.js +162 -0
  111. package/dist/op/adoption.js +272 -0
  112. package/dist/op/builders.js +125 -0
  113. package/dist/op/resolve-root-mode.js +77 -0
  114. package/dist/package-cli.js +16 -0
  115. package/dist/pin/edit-ts.js +76 -0
  116. package/dist/pin/edit.js +118 -0
  117. package/dist/pin/forge.js +65 -0
  118. package/dist/pin/index.js +16 -0
  119. package/dist/pin/rollout.d.ts.map +1 -1
  120. package/dist/pin/rollout.js +382 -0
  121. package/dist/pin/scan.js +287 -0
  122. package/dist/pin/source.js +171 -0
  123. package/dist/pin/waves.js +109 -0
  124. package/dist/plan-digest.js +67 -0
  125. package/dist/plugin.js +323 -0
  126. package/dist/serializer.js +23 -0
  127. package/dist/state-ownership.js +29 -0
  128. package/dist/terragrunt/affected.d.ts.map +1 -1
  129. package/dist/terragrunt/affected.js +290 -0
  130. package/dist/terragrunt/index.js +10 -0
  131. package/dist/terragrunt/mocks.js +270 -0
  132. package/dist/terragrunt/run.js +264 -0
  133. package/dist/terragrunt/units.js +195 -0
  134. package/dist/terragrunt/wave.js +125 -0
  135. package/dist/validate-cli.js +4 -0
  136. package/dist/validate.js +31 -0
  137. package/package.json +10 -10
  138. package/src/codegen/package.ts +2 -2
  139. package/src/components/capability-plugin.test.ts +97 -2
  140. package/src/components/capability-plugin.ts +117 -12
  141. package/src/components/pr-loop.tofu.test.ts +51 -11
  142. package/src/composites/terraform-adopt-op.test.ts +6 -0
  143. package/src/composites/terraform-adopt-op.ts +7 -2
  144. package/src/composites/terraform-apply-op.test.ts +6 -0
  145. package/src/composites/terraform-apply-op.ts +7 -2
  146. package/src/index.ts +1 -0
  147. package/src/pin/rollout.ts +2 -1
  148. package/src/terragrunt/affected.test.ts +11 -0
  149. package/src/terragrunt/affected.ts +2 -1
@@ -0,0 +1,138 @@
1
+ /**
2
+ * TF001: a root module keeps its state on local disk.
3
+ *
4
+ * Two ways a root gets there, and this reports both (#2218). A `terraform`
5
+ * block with no `backend "<type>"` and no `cloud {}` falls back to the local
6
+ * backend implicitly. A `backend "local"` block asks for the same backend by
7
+ * name. Either way `terraform.tfstate` lands in the working directory, and
8
+ * that file holds every attribute of every managed resource, secrets
9
+ * included, and it is not shared, not locked, and not versioned. The first
10
+ * apply from a second machine or a CI runner starts from an empty state and
11
+ * proposes to create the estate again.
12
+ *
13
+ * So the rule reads the backend block's TYPE LABEL rather than its presence:
14
+ * the id means "no remote backend", which is what its name, its message and
15
+ * its page have always argued. `cloud {}` counts as remote (it is Terraform
16
+ * Cloud / HCP state, held off the machine and locked). Every other backend
17
+ * type is taken as remote without a list to maintain, since `local` is the
18
+ * only backend Terraform ships that writes to the working directory.
19
+ *
20
+ * A root that keeps local state on purpose says so with a
21
+ * `# chant-ignore-block: TF001` on the line above its `terraform` block
22
+ * (chant #2111), which is what the three shipped examples do.
23
+ *
24
+ * One diagnostic per root, fired from the root's `terraform` block. A root
25
+ * with no `terraform` block at all is not flagged: it declares no version
26
+ * constraints either, and the missing block is a different finding.
27
+ *
28
+ * Scope: root modules only (#2112). A child module must NOT declare a
29
+ * backend, which is the mirror image and TF015's finding, so a descended
30
+ * module's `terraform` block is skipped here rather than read as if it were
31
+ * the root's.
32
+ *
33
+ * Does not fire on a live root (#2103): a `backend` block is exactly what
34
+ * choudoufu refuses there (TF024's territory), and the fallback to local
35
+ * state this check warns about does not apply, since a live root keeps no
36
+ * state file at all, local or remote.
37
+ */
38
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
39
+ import { TERRAFORM_TYPE } from "../../hcl/parse.js";
40
+ import { attr } from "../../hcl/value.js";
41
+ import { isRootScoped } from "./scope.js";
42
+ /**
43
+ * `backend`/`cloud` are blocks, so hcl2json encodes them as a value under the
44
+ * key, structurally the same "literal value under this key" shape `attr()`
45
+ * (`../../hcl/value.ts`, chant #2113) reads for an ordinary attribute, so
46
+ * this reads through it rather than re-deriving presence by hand.
47
+ */
48
+ export function hasBlock(body, key) {
49
+ const a = attr(body, key);
50
+ if (a.kind === "absent")
51
+ return false;
52
+ if (Array.isArray(a.value))
53
+ return a.value.length > 0;
54
+ return typeof a.value === "object" && a.value !== null ? Object.keys(a.value).length > 0 : true;
55
+ }
56
+ /** The one backend type that writes state to the working directory. */
57
+ export const LOCAL_BACKEND = "local";
58
+ /**
59
+ * The type labels of the `backend` blocks in a `terraform` block body.
60
+ *
61
+ * `backend` takes one label, so hcl2json nests it one level deeper than an
62
+ * unlabelled block: `backend "s3" { bucket = "b" }` is
63
+ * `{ backend: { s3: [{ bucket: "b" }] } }`, where `cloud { ... }` is just
64
+ * `{ cloud: [{ ... }] }`. The keys of that inner object are the labels, which
65
+ * is the only place the type survives the parse. A malformed body that puts
66
+ * an array or a scalar under `backend` yields no labels, and the caller then
67
+ * treats the root as having no remote backend, the same verdict a missing
68
+ * block gets.
69
+ */
70
+ export function backendTypes(body) {
71
+ const a = attr(body, "backend");
72
+ if (a.kind === "absent")
73
+ return [];
74
+ const value = a.value;
75
+ if (typeof value !== "object" || value === null || Array.isArray(value))
76
+ return [];
77
+ return Object.keys(value);
78
+ }
79
+ /**
80
+ * Whether the body configures state somewhere other than the working
81
+ * directory: any `backend` block whose type is not `local`, or a `cloud`
82
+ * block.
83
+ */
84
+ export function hasRemoteBackend(body) {
85
+ if (hasBlock(body, "cloud"))
86
+ return true;
87
+ return backendTypes(body).some((type) => type !== LOCAL_BACKEND);
88
+ }
89
+ export const tf001 = {
90
+ id: "TF001",
91
+ description: "Root module declares no remote backend",
92
+ check(ctx) {
93
+ const diagnostics = [];
94
+ /** Root name to the entity key of the first `terraform` block seen for it. */
95
+ const flagged = new Set();
96
+ for (const [name, entity] of ctx.entities) {
97
+ if (entity.entityType !== TERRAFORM_TYPE)
98
+ continue;
99
+ if (!isResourceDeclarable(entity) || !isRootScoped(entity))
100
+ continue;
101
+ const props = entity.props;
102
+ if (props.mode === "live")
103
+ continue;
104
+ const root = typeof props.root === "string" ? props.root : "";
105
+ if (flagged.has(root))
106
+ continue;
107
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
108
+ if (hasRemoteBackend(body))
109
+ continue;
110
+ // What the root actually declares, so the message names it rather than
111
+ // saying "no remote backend" over a block the reader can see.
112
+ const found = backendTypes(body).includes(LOCAL_BACKEND)
113
+ ? 'the backend it declares is `backend "local"`'
114
+ : "there is no backend block at all";
115
+ flagged.add(root);
116
+ diagnostics.push({
117
+ checkId: "TF001",
118
+ severity: "warning",
119
+ message: `Root module "${root}" declares no remote backend: ${found}. State is a ` +
120
+ "terraform.tfstate in the working directory, which is unshared, unlocked and holds every " +
121
+ 'resource attribute in plaintext. Declare a `backend "<type>"` naming a remote type (`s3`, ' +
122
+ "`gcs`, `azurerm`, `http`) or a `cloud {}` block, or keep the local state on purpose with a " +
123
+ "`# chant-ignore-block: TF001` above the terraform block.",
124
+ entity: name,
125
+ lexicon: "terraform",
126
+ // The missing-resource shape (chant #2113): there is no remote
127
+ // `backend`/`cloud` block to point at, only the root that lacks one.
128
+ // `entity` above still names the `terraform` block this fired from (by
129
+ // convenience, not because that block is what's wrong), so `missing`
130
+ // is what a suppression should key on instead. `kind` stays `backend`
131
+ // in both cases: what is absent is a remote backend, whether or not a
132
+ // `backend "local"` block is sitting where one should be.
133
+ missing: { kind: "backend", scope: root },
134
+ });
135
+ }
136
+ return diagnostics;
137
+ },
138
+ };
@@ -0,0 +1,143 @@
1
+ /**
2
+ * TF002: a provider a root implies has no `required_providers` entry, or an
3
+ * incomplete one.
4
+ *
5
+ * A provider is implied two ways: an explicit `provider "<name>" {}` block,
6
+ * or a resource/data type prefix (`aws_instance` implies `aws`, up to the
7
+ * first underscore, the same heuristic tflint's `terraform_required_providers`
8
+ * reads via `GetProviderRefs()`). The `terraform` builtin (the provider
9
+ * behind `terraform_remote_state`) is never implied: it has no registry
10
+ * entry to require. Every implied provider needs a `required_providers`
11
+ * entry that is an object carrying both `source` and `version`, since the old
12
+ * shorthand form (`aws = "~> 4.0"`, a bare string) has neither and fires the
13
+ * same as a missing entry.
14
+ *
15
+ * Fires once per incomplete or missing provider, not once per resource: two
16
+ * resources implying the same unconstrained provider produce one diagnostic.
17
+ * Anchored on whichever entity first implied the provider, preferring an
18
+ * explicit `provider` block over a resource/data type, so the diagnostic
19
+ * points at the most specific place a fix belongs.
20
+ *
21
+ * A root with no `terraform` block at all is out of scope, same as TF001:
22
+ * the missing block is that rule's finding, not this one's.
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.
29
+ */
30
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
31
+ import { isRootScoped } from "./scope.js";
32
+ import { DATA_TYPE, PROVIDER_TYPE, RESOURCE_TYPE, TERRAFORM_TYPE, } from "../../hcl/parse.js";
33
+ /** The `terraform` provider backs `terraform_remote_state`; it ships with the binary, not the registry. */
34
+ const BUILTIN_PROVIDERS = new Set(["terraform"]);
35
+ function providerNameFromType(type) {
36
+ return type.split("_")[0];
37
+ }
38
+ /** One implied provider name to the entity key that first implied it in file order. */
39
+ export function impliedProviders(entities, root) {
40
+ const implied = new Map();
41
+ const fromProviderBlock = new Set();
42
+ for (const [key, entity] of entities) {
43
+ if (!isResourceDeclarable(entity) || !isRootScoped(entity))
44
+ continue;
45
+ const props = entity.props;
46
+ if (props.root !== root)
47
+ continue;
48
+ const address = typeof props.address === "string" ? props.address : "";
49
+ if (entity.entityType === PROVIDER_TYPE) {
50
+ const name = address.slice("provider.".length);
51
+ if (BUILTIN_PROVIDERS.has(name))
52
+ continue;
53
+ implied.set(name, key);
54
+ fromProviderBlock.add(name);
55
+ }
56
+ else if (entity.entityType === RESOURCE_TYPE) {
57
+ const type = address.split(".")[0] ?? "";
58
+ const name = providerNameFromType(type);
59
+ if (BUILTIN_PROVIDERS.has(name) || fromProviderBlock.has(name))
60
+ continue;
61
+ if (!implied.has(name))
62
+ implied.set(name, key);
63
+ }
64
+ else if (entity.entityType === DATA_TYPE) {
65
+ const type = address.split(".")[1] ?? "";
66
+ const name = providerNameFromType(type);
67
+ if (BUILTIN_PROVIDERS.has(name) || fromProviderBlock.has(name))
68
+ continue;
69
+ if (!implied.has(name))
70
+ implied.set(name, key);
71
+ }
72
+ }
73
+ return implied;
74
+ }
75
+ /** `required_providers` is a nested block: hcl2json encodes it as a one-element array of `{name: entry}`. */
76
+ function requiredProvidersMap(body) {
77
+ const merged = {};
78
+ const blocks = body.required_providers;
79
+ if (!Array.isArray(blocks))
80
+ return merged;
81
+ for (const block of blocks) {
82
+ if (typeof block === "object" && block !== null)
83
+ Object.assign(merged, block);
84
+ }
85
+ return merged;
86
+ }
87
+ /** Why a `required_providers` entry is incomplete, or `undefined` if it's fine. */
88
+ function entryProblem(entry) {
89
+ if (entry === undefined)
90
+ return "no `required_providers` entry";
91
+ if (typeof entry === "string")
92
+ return "the legacy shorthand form (a bare version string), which carries no `source`";
93
+ if (typeof entry !== "object" || entry === null)
94
+ return "an entry that is not a valid object";
95
+ const obj = entry;
96
+ const missing = [];
97
+ if (typeof obj.source !== "string" || obj.source === "")
98
+ missing.push("`source`");
99
+ if (typeof obj.version !== "string" || obj.version === "")
100
+ missing.push("`version`");
101
+ return missing.length > 0 ? `an entry missing ${missing.join(" and ")}` : undefined;
102
+ }
103
+ export const tf002 = {
104
+ id: "TF002",
105
+ description: "A provider the root implies has no required_providers entry, or an incomplete one",
106
+ check(ctx) {
107
+ const diagnostics = [];
108
+ const roots = new Set();
109
+ const requiredByRoot = new Map();
110
+ for (const entity of ctx.entities.values()) {
111
+ if (entity.entityType !== TERRAFORM_TYPE || !isResourceDeclarable(entity))
112
+ continue;
113
+ if (!isRootScoped(entity))
114
+ continue;
115
+ const props = entity.props;
116
+ const root = typeof props.root === "string" ? props.root : "";
117
+ roots.add(root);
118
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
119
+ const merged = requiredByRoot.get(root) ?? {};
120
+ Object.assign(merged, requiredProvidersMap(body));
121
+ requiredByRoot.set(root, merged);
122
+ }
123
+ for (const root of [...roots].sort()) {
124
+ const required = requiredByRoot.get(root) ?? {};
125
+ const implied = impliedProviders(ctx.entities, root);
126
+ for (const name of [...implied.keys()].sort()) {
127
+ const problem = entryProblem(required[name]);
128
+ if (!problem)
129
+ continue;
130
+ diagnostics.push({
131
+ checkId: "TF002",
132
+ severity: "warning",
133
+ message: `Provider "${name}" is used in root module "${root}" but has ${problem} in ` +
134
+ "`required_providers`. Add `source` and `version` for it in the terraform block's " +
135
+ "`required_providers`, so a future provider release can't silently change behavior.",
136
+ entity: implied.get(name),
137
+ lexicon: "terraform",
138
+ });
139
+ }
140
+ }
141
+ return diagnostics;
142
+ },
143
+ };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * TF003: a root's `terraform` block has no `required_version`.
3
+ *
4
+ * Sits on the same `terraform` block TF001 already walks. Without
5
+ * `required_version`, a root can be applied by whatever Terraform binary
6
+ * happens to be on the runner's `PATH`, and a binary upgrade that changes
7
+ * behavior (a provider protocol bump, a changed default) surfaces as a
8
+ * surprise plan diff instead of a version-check failure before anything runs.
9
+ *
10
+ * One diagnostic per root, fired once even if the root spreads its
11
+ * `terraform` block across several files (a legacy-config split into
12
+ * `terraform.tf` and `versions.tf`, say). A root with no `terraform` block
13
+ * at all is out of scope, same as TF001 and TF002: the missing block is
14
+ * their finding, not this one's.
15
+ *
16
+ * Scope: root modules only (#2112). A child module may legitimately leave
17
+ * `required_version` to the root that calls it, so a descended module's
18
+ * `terraform` block is skipped here.
19
+ */
20
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
21
+ import { isRootScoped } from "./scope.js";
22
+ import { TERRAFORM_TYPE } from "../../hcl/parse.js";
23
+ function hasRequiredVersion(body) {
24
+ return typeof body.required_version === "string" && body.required_version.trim() !== "";
25
+ }
26
+ export const tf003 = {
27
+ id: "TF003",
28
+ description: "Root module's terraform block has no required_version",
29
+ check(ctx) {
30
+ const diagnostics = [];
31
+ /** Root name to the entity key of its first `terraform` block, and whether any block for it declares `required_version`. */
32
+ const seen = new Map();
33
+ for (const [key, entity] of ctx.entities) {
34
+ if (entity.entityType !== TERRAFORM_TYPE || !isResourceDeclarable(entity))
35
+ continue;
36
+ if (!isRootScoped(entity))
37
+ continue;
38
+ const props = entity.props;
39
+ const root = typeof props.root === "string" ? props.root : "";
40
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
41
+ const existing = seen.get(root);
42
+ const hasVersion = (existing?.hasVersion ?? false) || hasRequiredVersion(body);
43
+ seen.set(root, { anchor: existing?.anchor ?? key, hasVersion });
44
+ }
45
+ for (const [root, { anchor, hasVersion }] of [...seen].sort(([a], [b]) => a.localeCompare(b))) {
46
+ if (hasVersion)
47
+ continue;
48
+ diagnostics.push({
49
+ checkId: "TF003",
50
+ severity: "warning",
51
+ message: `Root module "${root}"'s terraform block has no \`required_version\`. Without it, ` +
52
+ "an upgraded Terraform binary can change behavior with no version-check failure to " +
53
+ "flag it first. Add `required_version = \">= <lowest supported version>\"`.",
54
+ entity: anchor,
55
+ lexicon: "terraform",
56
+ });
57
+ }
58
+ return diagnostics;
59
+ },
60
+ };
@@ -0,0 +1,54 @@
1
+ /**
2
+ * TF004: a registry-sourced `module` block has no `version`.
3
+ *
4
+ * "Registry-sourced" means `source` parses as a Terraform module registry
5
+ * address (`classifyModuleSource`, `./module-source.ts`): a bare
6
+ * `namespace/name/target-system`, or a hostname-prefixed
7
+ * `host/namespace/name/target-system`. Without `version`, `terraform init`
8
+ * always resolves to the newest release that satisfies no constraint at
9
+ * all, so an upstream module release can change what a root provisions with
10
+ * no local edit and no diff to review first.
11
+ *
12
+ * A local (`./`, `../`) or git/hg source is never flagged here: git/hg is
13
+ * TF005's territory, and a local source cannot carry a registry version at
14
+ * all. One diagnostic per module block.
15
+ *
16
+ * Scope: root and child modules alike (#2112). The condition is a property
17
+ * of the block itself, so a descended module's block is read exactly as a
18
+ * root's is.
19
+ */
20
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
21
+ import { MODULE_TYPE } from "../../hcl/parse.js";
22
+ import { classifyModuleSource } from "./module-source.js";
23
+ export const tf004 = {
24
+ id: "TF004",
25
+ description: "Registry-sourced module block has no version",
26
+ check(ctx) {
27
+ const diagnostics = [];
28
+ for (const [key, entity] of ctx.entities) {
29
+ if (entity.entityType !== MODULE_TYPE || !isResourceDeclarable(entity))
30
+ continue;
31
+ const props = entity.props;
32
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
33
+ const source = typeof body.source === "string" ? body.source : undefined;
34
+ if (source === undefined)
35
+ continue;
36
+ if (classifyModuleSource(source).kind !== "registry")
37
+ continue;
38
+ const version = body.version;
39
+ if (typeof version === "string" && version.trim() !== "")
40
+ continue;
41
+ const address = typeof props.address === "string" ? props.address : key;
42
+ diagnostics.push({
43
+ checkId: "TF004",
44
+ severity: "warning",
45
+ message: `Module "${address}" sources "${source}" from a registry with no \`version\`. ` +
46
+ "`terraform init` resolves to the newest release with no constraint to hold it back. " +
47
+ "Add a `version` constraint (e.g. `~> 5.0`).",
48
+ entity: key,
49
+ lexicon: "terraform",
50
+ });
51
+ }
52
+ return diagnostics;
53
+ },
54
+ };
@@ -0,0 +1,68 @@
1
+ /**
2
+ * TF005: a git or hg `module` source with no `?ref=`, or a `?ref=` that
3
+ * isn't tight enough to call pinned.
4
+ *
5
+ * "Git or hg" is `classifyModuleSource`'s `"git"` kind (`./module-source.ts`):
6
+ * an explicit `git::`/`hg::` prefix, the bare `github.com`/`bitbucket.org`
7
+ * shorthands, an scp-style `git@host:path`, or anything ending in `.git`.
8
+ * A ref counts as pinned only if it is a semver-shaped tag or a full 40-hex
9
+ * commit SHA (`isPinnedRef`), stricter than checkov's `CKV_TF_1`/`CKV_TF_2`,
10
+ * which accept any `?ref=` containing `\d\.\d` (so `v1.2-dev` passes there;
11
+ * it does not here, and the rule's page section says so). A ref matching a
12
+ * well-known mutable branch name (`main`, `master`, `develop`, `trunk`) gets
13
+ * its own, more specific message, since that's the exact case tflint's
14
+ * `flexible` style (the style this rule ships; see the page section for what
15
+ * `semver` style would add) already reports on.
16
+ *
17
+ * A local (`./`, `../`) source is never flagged: it can't be pinned to a
18
+ * ref at all, and checkov's own module-pinning checks return `UNKNOWN`
19
+ * (not a failure) for the same reason. A registry source is TF004's
20
+ * territory, not this rule's; the two never fire on the same module block.
21
+ *
22
+ * Scope: root and child modules alike (#2112). The condition is a property
23
+ * of the block itself, so a descended module's block is read exactly as a
24
+ * root's is.
25
+ */
26
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
27
+ import { MODULE_TYPE } from "../../hcl/parse.js";
28
+ import { classifyModuleSource, isDefaultBranch, isPinnedRef } from "./module-source.js";
29
+ export const tf005 = {
30
+ id: "TF005",
31
+ description: "Git/hg module source is unpinned, or pinned to a mutable ref",
32
+ check(ctx) {
33
+ const diagnostics = [];
34
+ for (const [key, entity] of ctx.entities) {
35
+ if (entity.entityType !== MODULE_TYPE || !isResourceDeclarable(entity))
36
+ continue;
37
+ const props = entity.props;
38
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
39
+ const source = typeof body.source === "string" ? body.source : undefined;
40
+ if (source === undefined)
41
+ continue;
42
+ const classified = classifyModuleSource(source);
43
+ if (classified.kind !== "git")
44
+ continue;
45
+ const address = typeof props.address === "string" ? props.address : key;
46
+ let message;
47
+ if (!classified.ref) {
48
+ message = `Module "${address}" sources "${source}" from git/hg with no \`?ref=\`. Add one pinned to a tag or a full commit SHA.`;
49
+ }
50
+ else if (isDefaultBranch(classified.ref)) {
51
+ message = `Module "${address}" sources "${source}" pinned to "${classified.ref}", a mutable branch. Pin to a tag or a full commit SHA instead.`;
52
+ }
53
+ else if (!isPinnedRef(classified.ref)) {
54
+ message = `Module "${address}" sources "${source}" pinned to "${classified.ref}", which is neither a tag nor a full commit SHA and can move. Pin to a tag or a full commit SHA instead.`;
55
+ }
56
+ if (!message)
57
+ continue;
58
+ diagnostics.push({
59
+ checkId: "TF005",
60
+ severity: "warning",
61
+ message,
62
+ entity: key,
63
+ lexicon: "terraform",
64
+ });
65
+ }
66
+ return diagnostics;
67
+ },
68
+ };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * TF006: a `sensitive = true` variable carries a `default`.
3
+ *
4
+ * Marking a variable sensitive is a statement that its value must not be seen.
5
+ * A default is a value committed to the repository, so the two together are a
6
+ * contradiction: the secret is in git, and the `sensitive` flag only hides it
7
+ * from plan output, where it was never the exposure. It is also the shape that
8
+ * survives longest, because every environment that forgets to pass the variable
9
+ * silently gets the committed one.
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.
14
+ *
15
+ * Scope: root and child modules alike (#2112). The condition is a property
16
+ * of the block itself, so a descended module's block is read exactly as a
17
+ * root's is.
18
+ */
19
+ import { VARIABLE_TYPE } from "../../hcl/parse.js";
20
+ import { blockName, blocksOfType } from "./blocks.js";
21
+ export const tf006 = {
22
+ id: "TF006",
23
+ description: "Sensitive variable declares a default value",
24
+ check(ctx) {
25
+ const diagnostics = [];
26
+ for (const block of blocksOfType(ctx, VARIABLE_TYPE)) {
27
+ if (block.body.sensitive !== true)
28
+ continue;
29
+ if (!("default" in block.body))
30
+ continue;
31
+ diagnostics.push({
32
+ checkId: "TF006",
33
+ severity: "error",
34
+ message: `Variable "${blockName(block.address)}" is marked \`sensitive = true\` and declares a ` +
35
+ "`default`. A default is committed to the repository, so the value the variable is meant " +
36
+ "to protect is already public. Drop the default and require the value to be passed in.",
37
+ entity: block.key,
38
+ lexicon: "terraform",
39
+ });
40
+ }
41
+ return diagnostics;
42
+ },
43
+ };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * TF007: a variable default or a `locals` value holds a secret-shaped literal.
3
+ *
4
+ * This is the check tfsec shipped as `general-secrets-sensitive-in-variable`
5
+ * and `general-secrets-sensitive-in-local`, and that trivy dropped when it
6
+ * absorbed tfsec. #2107's survey found no maintained tool that has it today.
7
+ *
8
+ * A committed default or local is a credential in git history, which no later
9
+ * edit removes, and Terraform copies it into state and into plan output on
10
+ * every run. Both heuristics in `../secret-shape.ts` count: the name (a
11
+ * variable called `db_password`) and the value (something shaped like a live
12
+ * credential whatever it is called). References and interpolations are never
13
+ * flagged, because passing the value in is the fix.
14
+ *
15
+ * Scope: root and child modules alike (#2112). The condition is a property
16
+ * of the block itself, so a descended module's block is read exactly as a
17
+ * root's is.
18
+ */
19
+ import { LOCALS_TYPE, VARIABLE_TYPE } from "../../hcl/parse.js";
20
+ import { secretShapedAssignment } from "../secret-shape.js";
21
+ import { blockName, blocksOfType } from "./blocks.js";
22
+ /** Why the value was flagged, in the words of the heuristic that fired. */
23
+ function because(shape) {
24
+ return shape.reason === "value"
25
+ ? `the value (${shape.length} characters, redacted here) matches a credential shape`
26
+ : "the name reads like a credential and the value is a committed literal";
27
+ }
28
+ export const tf007 = {
29
+ id: "TF007",
30
+ description: "Secret-shaped literal in a variable default or a locals value",
31
+ check(ctx) {
32
+ const diagnostics = [];
33
+ for (const block of blocksOfType(ctx, VARIABLE_TYPE)) {
34
+ const name = blockName(block.address);
35
+ const shape = secretShapedAssignment(name, block.body.default);
36
+ if (!shape)
37
+ continue;
38
+ diagnostics.push({
39
+ checkId: "TF007",
40
+ severity: "error",
41
+ message: `Variable "${name}" defaults to what looks like a secret: ${because(shape)}. A default is ` +
42
+ "committed to the repository and copied into state on every run. Remove it, mark the " +
43
+ "variable `sensitive = true`, and pass the value from a secret store at apply time.",
44
+ entity: block.key,
45
+ lexicon: "terraform",
46
+ });
47
+ }
48
+ for (const block of blocksOfType(ctx, LOCALS_TYPE)) {
49
+ for (const [name, value] of Object.entries(block.body)) {
50
+ const shape = secretShapedAssignment(name, value);
51
+ if (!shape)
52
+ continue;
53
+ diagnostics.push({
54
+ checkId: "TF007",
55
+ severity: "error",
56
+ message: `Local value "${name}" looks like a secret: ${because(shape)}. A local is a committed ` +
57
+ "constant with no way to override it per environment. Read the value from a data source " +
58
+ "or a sensitive variable instead.",
59
+ entity: `${block.key}.${name}`,
60
+ lexicon: "terraform",
61
+ });
62
+ }
63
+ }
64
+ return diagnostics;
65
+ },
66
+ };
@@ -0,0 +1,64 @@
1
+ /**
2
+ * TF008: a `provider` block configures a credential inline.
3
+ *
4
+ * The most corroborated rule in #2107's survey: checkov (`CKV_AWS_41` and its
5
+ * `credentials.py` siblings), KICS, Snyk and semgrep all ship a version of it,
6
+ * and it is the only Terraform-language-shaped check any of Snyk or semgrep
7
+ * ships at all. Every one of them is written per provider. This one is written
8
+ * against attribute NAMES, so it covers the providers those tools cover and
9
+ * the several hundred they do not.
10
+ *
11
+ * A credential in a provider block is used by every resource that provider
12
+ * manages, so it is the highest-value one in the root, and it is read from the
13
+ * repository by everyone who can clone it. Providers all accept the same
14
+ * credential through the environment or a shared config file instead.
15
+ *
16
+ * Scope: root and child modules alike (#2112). The condition is a property
17
+ * of the block itself, so a descended module's block is read exactly as a
18
+ * root's is.
19
+ */
20
+ import { PROVIDER_TYPE } from "../../hcl/parse.js";
21
+ import { isLiteralString, isPlaceholderValue, isSecretName } from "../secret-shape.js";
22
+ import { blockName, blocksOfType, walkAttributes } from "./blocks.js";
23
+ /**
24
+ * Credential attributes the name heuristic in `../secret-shape.ts` does not
25
+ * cover on its own. `client_secret` and the `*_key`/`*_token` names it does
26
+ * cover are not repeated here.
27
+ */
28
+ const PROVIDER_CREDENTIAL_ATTRS = new Set(["credentials", "client_certificate", "client_key", "shared_credentials_file"]);
29
+ /**
30
+ * A path, not a credential: `credentials = "keys/service-account.json"` and
31
+ * `shared_credentials_file = "~/.aws/credentials"` name where the secret lives,
32
+ * which is what the fix looks like.
33
+ */
34
+ const FILE_PATH_RE = /^[~./]?[\w./\\-]*\.(json|pem|key|p12|pfx|crt|cer|ini|conf|credentials)$/i;
35
+ export const tf008 = {
36
+ id: "TF008",
37
+ description: "Provider block configures a hardcoded credential",
38
+ check(ctx) {
39
+ const diagnostics = [];
40
+ for (const block of blocksOfType(ctx, PROVIDER_TYPE)) {
41
+ for (const attr of walkAttributes(block.body)) {
42
+ if (!PROVIDER_CREDENTIAL_ATTRS.has(attr.name) && !isSecretName(attr.name))
43
+ continue;
44
+ if (!isLiteralString(attr.value))
45
+ continue;
46
+ const value = attr.value.trim();
47
+ if (value === "" || isPlaceholderValue(value) || FILE_PATH_RE.test(value))
48
+ continue;
49
+ diagnostics.push({
50
+ checkId: "TF008",
51
+ severity: "error",
52
+ message: `Provider "${blockName(block.address)}" sets \`${attr.path}\` to a literal value ` +
53
+ `(${value.length} characters, redacted here). A credential in a provider block is ` +
54
+ "committed to the repository and used by every resource the provider manages. Remove it " +
55
+ "and let the provider read the credential from the environment, a shared config file, or " +
56
+ "a sensitive variable.",
57
+ entity: `${block.key}.${attr.path}`,
58
+ lexicon: "terraform",
59
+ });
60
+ }
61
+ }
62
+ return diagnostics;
63
+ },
64
+ };