@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,49 @@
1
+ /**
2
+ * TF009: a credential-named variable is not marked `sensitive`.
3
+ *
4
+ * No tool in #2107's survey checks this. The authority is HashiCorp's own style
5
+ * guide, whose variables section says plainly: "For sensitive variables, such
6
+ * as passwords and private keys, set the sensitive parameter to true."
7
+ *
8
+ * Without the flag the value is echoed in `terraform plan` and
9
+ * `terraform apply` output, which means it lands in CI logs, in the pull
10
+ * request comment a plan bot posts, and in every terminal scrollback. The flag
11
+ * does not keep the value out of state (nothing does), which is why the
12
+ * remediation says both things.
13
+ *
14
+ * Only the name is read. A variable with no default and no value in the
15
+ * repository is still flagged, because the finding is about where the value
16
+ * goes at run time, not about what is committed. That is TF007's question.
17
+ *
18
+ * Scope: root and child modules alike (#2112). The condition is a property
19
+ * of the block itself, so a descended module's block is read exactly as a
20
+ * root's is.
21
+ */
22
+ import { VARIABLE_TYPE } from "../../hcl/parse.js";
23
+ import { isSecretName } from "../secret-shape.js";
24
+ import { blockName, blocksOfType } from "./blocks.js";
25
+ export const tf009 = {
26
+ id: "TF009",
27
+ description: "Credential-named variable is not marked sensitive",
28
+ check(ctx) {
29
+ const diagnostics = [];
30
+ for (const block of blocksOfType(ctx, VARIABLE_TYPE)) {
31
+ const name = blockName(block.address);
32
+ if (!isSecretName(name))
33
+ continue;
34
+ if (block.body.sensitive === true)
35
+ continue;
36
+ diagnostics.push({
37
+ checkId: "TF009",
38
+ severity: "error",
39
+ message: `Variable "${name}" is named like a credential but is not marked \`sensitive = true\`, so ` +
40
+ "Terraform prints its value in plan and apply output, and from there into CI logs and plan " +
41
+ "comments. Add `sensitive = true`, and remember the value is still stored in plaintext in " +
42
+ "state either way.",
43
+ entity: block.key,
44
+ lexicon: "terraform",
45
+ });
46
+ }
47
+ return diagnostics;
48
+ },
49
+ };
@@ -0,0 +1,40 @@
1
+ /**
2
+ * TF010: a `variable` declares no `type`.
3
+ *
4
+ * Without a type constraint Terraform accepts whatever it is given and infers
5
+ * the type from the value, so a variable meant to be a list arrives as a
6
+ * string from a `TF_VAR_` environment variable, or a number arrives as `"3"`
7
+ * from a tfvars file, and the failure surfaces deep inside a resource
8
+ * argument rather than at the module boundary where it was introduced.
9
+ *
10
+ * A blank `type` counts as absent, matching KICS, which fails a `type` that
11
+ * trims to empty.
12
+ *
13
+ * Scope: root and child modules alike (#2112). The condition is a property
14
+ * of the block itself, so a descended module's block is read exactly as a
15
+ * root's is.
16
+ */
17
+ import { VARIABLE_TYPE } from "../../hcl/parse.js";
18
+ import { blockName, blocksOfType, isBlank } from "./blocks.js";
19
+ export const tf010 = {
20
+ id: "TF010",
21
+ description: "Variable declares no type constraint",
22
+ check(ctx) {
23
+ const diagnostics = [];
24
+ for (const block of blocksOfType(ctx, VARIABLE_TYPE)) {
25
+ if (!isBlank(block.body.type))
26
+ continue;
27
+ diagnostics.push({
28
+ checkId: "TF010",
29
+ severity: "info",
30
+ message: `Variable "${blockName(block.address)}" declares no \`type\`, so Terraform accepts any ` +
31
+ "value and infers the type from whatever it is given. Add a type constraint " +
32
+ "(`string`, `number`, `bool`, `list(string)`, an `object({...})`) so a wrong value is " +
33
+ "rejected at the module boundary.",
34
+ entity: block.key,
35
+ lexicon: "terraform",
36
+ });
37
+ }
38
+ return diagnostics;
39
+ },
40
+ };
@@ -0,0 +1,38 @@
1
+ /**
2
+ * TF011: a `variable` declares no `description`.
3
+ *
4
+ * A module's variables are its API. The description is what `terraform-docs`
5
+ * renders into the README, what the HCP Terraform and Terraform Cloud variable
6
+ * UI shows next to the input box, and what a caller reads before deciding what
7
+ * to pass. Without it the only documentation is the variable's name.
8
+ *
9
+ * Report-only: this is documentation hygiene, filed the way KICS files it (as
10
+ * INFO) and the way tflint files it (in the `all` preset, not `recommended`).
11
+ *
12
+ * Scope: root and child modules alike (#2112). The condition is a property
13
+ * of the block itself, so a descended module's block is read exactly as a
14
+ * root's is.
15
+ */
16
+ import { VARIABLE_TYPE } from "../../hcl/parse.js";
17
+ import { blockName, blocksOfType, isBlank } from "./blocks.js";
18
+ export const tf011 = {
19
+ id: "TF011",
20
+ description: "Variable declares no description",
21
+ check(ctx) {
22
+ const diagnostics = [];
23
+ for (const block of blocksOfType(ctx, VARIABLE_TYPE)) {
24
+ if (!isBlank(block.body.description))
25
+ continue;
26
+ diagnostics.push({
27
+ checkId: "TF011",
28
+ severity: "info",
29
+ message: `Variable "${blockName(block.address)}" declares no \`description\`, so the module's ` +
30
+ "generated documentation and the variable prompt have nothing to show but the name. Add a " +
31
+ "description saying what the value is for and what a valid one looks like.",
32
+ entity: block.key,
33
+ lexicon: "terraform",
34
+ });
35
+ }
36
+ return diagnostics;
37
+ },
38
+ };
@@ -0,0 +1,36 @@
1
+ /**
2
+ * TF012: an `output` declares no `description`.
3
+ *
4
+ * The other half of a module's API. An output is what a caller wires into the
5
+ * next module, and the description is the only place to say what the value
6
+ * actually is (an id or an ARN, a full URL or a bare hostname) and whether it
7
+ * is stable enough to depend on.
8
+ *
9
+ * Report-only, for the same reason TF011 is.
10
+ *
11
+ * Scope: root and child modules alike (#2112). The condition is a property
12
+ * of the block itself, so a descended module's block is read exactly as a
13
+ * root's is.
14
+ */
15
+ import { OUTPUT_TYPE } from "../../hcl/parse.js";
16
+ import { blockName, blocksOfType, isBlank } from "./blocks.js";
17
+ export const tf012 = {
18
+ id: "TF012",
19
+ description: "Output declares no description",
20
+ check(ctx) {
21
+ const diagnostics = [];
22
+ for (const block of blocksOfType(ctx, OUTPUT_TYPE)) {
23
+ if (!isBlank(block.body.description))
24
+ continue;
25
+ diagnostics.push({
26
+ checkId: "TF012",
27
+ severity: "info",
28
+ message: `Output "${blockName(block.address)}" declares no \`description\`. Outputs are a module's ` +
29
+ "public interface, so say what the value is and what a caller can rely on it for.",
30
+ entity: block.key,
31
+ lexicon: "terraform",
32
+ });
33
+ }
34
+ return diagnostics;
35
+ },
36
+ };
@@ -0,0 +1,53 @@
1
+ /**
2
+ * TF013: `lifecycle { ignore_changes = all }`.
3
+ *
4
+ * `all` tells Terraform to ignore drift on every attribute of the resource
5
+ * after it is created. Nothing about that resource is ever reconciled again:
6
+ * a manual console edit, a deleted security-group rule, a downgraded instance
7
+ * class, all of it plans clean forever. It is also silent, because a plan that
8
+ * finds nothing looks exactly like a plan that was told to look at nothing.
9
+ *
10
+ * That is the observe half of chant's lifecycle switched off, so this is
11
+ * merge-worthy rather than hygiene. A list of specific attributes is fine and
12
+ * is not reported: ignoring `tags` an external tool writes is a real need, and
13
+ * naming them is what makes the intent auditable.
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 { DATA_TYPE, RESOURCE_TYPE } from "../../hcl/parse.js";
20
+ import { blocksOfTypes, nestedBodies } from "./blocks.js";
21
+ /**
22
+ * `ignore_changes = all` parses to the string `"${all}"`: hcl2json renders the
23
+ * bare keyword as an expression template. A list of attributes parses to an
24
+ * array, so an array is only a match if `all` is the whole of it.
25
+ */
26
+ function ignoresEverything(value) {
27
+ if (value === "${all}")
28
+ return true;
29
+ return Array.isArray(value) && value.length === 1 && value[0] === "${all}";
30
+ }
31
+ export const tf013 = {
32
+ id: "TF013",
33
+ description: "lifecycle ignore_changes is set to all",
34
+ check(ctx) {
35
+ const diagnostics = [];
36
+ for (const block of blocksOfTypes(ctx, [RESOURCE_TYPE, DATA_TYPE])) {
37
+ for (const lifecycle of nestedBodies(block.body, "lifecycle")) {
38
+ if (!ignoresEverything(lifecycle.ignore_changes))
39
+ continue;
40
+ diagnostics.push({
41
+ checkId: "TF013",
42
+ severity: "warning",
43
+ message: `"${block.address}" sets \`ignore_changes = all\`, so Terraform stops reconciling every ` +
44
+ "attribute of the resource after it is created. Drift becomes invisible: a manual change " +
45
+ "in the console plans clean forever. List the specific attributes to ignore instead.",
46
+ entity: block.key,
47
+ lexicon: "terraform",
48
+ });
49
+ }
50
+ }
51
+ return diagnostics;
52
+ },
53
+ };
@@ -0,0 +1,61 @@
1
+ /**
2
+ * TF014: a `provider` block inside a child module that configures anything.
3
+ *
4
+ * Scope: child modules only. A `provider` block in the ROOT module is where
5
+ * providers are supposed to be configured, and TF008 is what checks its
6
+ * contents.
7
+ *
8
+ * A child module that configures its own provider takes the decision away
9
+ * from its caller: the caller can no longer point the module at a different
10
+ * region, account or endpoint, and two calls of the module in one root cannot
11
+ * differ. Worse, Terraform cannot remove a module whose provider is declared
12
+ * inside it, because destroying the resources needs a provider configuration
13
+ * that the removal itself deletes. That is the "Provider configuration not
14
+ * present" failure that leaves a root unable to plan.
15
+ *
16
+ * The one legal form is a provider block that declares only `alias`, which
17
+ * names a slot the caller fills through `providers = { aws.replica =
18
+ * aws.west }`. That is a declaration, not a configuration, so a block whose
19
+ * body has nothing but `alias` is not reported. Everything else is: a
20
+ * `region`, a `profile`, an `assume_role` block, and `version` too, which has
21
+ * been deprecated in favour of `required_providers` since Terraform 0.13.
22
+ */
23
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
24
+ import { PROVIDER_TYPE } from "../../hcl/parse.js";
25
+ import { isChildScoped } from "./scope.js";
26
+ /** Everything in a provider body that is not the `alias` slot declaration. */
27
+ function configuredKeys(body) {
28
+ return Object.keys(body).filter((key) => key !== "alias");
29
+ }
30
+ export const tf014 = {
31
+ id: "TF014",
32
+ description: "Child module configures a provider block",
33
+ check(ctx) {
34
+ const diagnostics = [];
35
+ for (const [key, entity] of ctx.entities) {
36
+ if (entity.entityType !== PROVIDER_TYPE || !isResourceDeclarable(entity))
37
+ continue;
38
+ if (!isChildScoped(entity))
39
+ continue;
40
+ const props = entity.props;
41
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
42
+ const configured = configuredKeys(body);
43
+ if (configured.length === 0)
44
+ continue;
45
+ const address = typeof props.address === "string" ? props.address : key;
46
+ diagnostics.push({
47
+ checkId: "TF014",
48
+ severity: "warning",
49
+ message: `Child module block "${address}" configures a provider (${configured.sort().join(", ")}). ` +
50
+ "A module that configures its own provider cannot be pointed at another region or account by " +
51
+ "its caller, cannot be called twice with different settings, and cannot be removed, since " +
52
+ "destroying its resources needs the provider configuration the removal deletes. Configure the " +
53
+ "provider in the root module and pass it in with `providers = { ... }`, leaving at most an " +
54
+ "`alias`-only block here.",
55
+ entity: key,
56
+ lexicon: "terraform",
57
+ });
58
+ }
59
+ return diagnostics;
60
+ },
61
+ };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * TF015: a `backend` or `cloud` block inside a child module.
3
+ *
4
+ * Scope: child modules only. This is TF001's mirror image. TF001 wants a root
5
+ * module to declare a REMOTE backend, because a root without one keeps its
6
+ * state on local disk. A child module must declare neither: state belongs to
7
+ * the root,
8
+ * one state per root however many modules it calls, and Terraform says so
9
+ * directly, "A backend block can only appear in the root module" and the
10
+ * same for `cloud`. Depending on the version, the block is ignored with a
11
+ * warning or the root refuses to initialize, and either way the block is a
12
+ * claim about where state lives that the module does not get to make.
13
+ *
14
+ * The usual cause is a directory that used to be a root module and became a
15
+ * child when someone factored it out, with its `terraform` block carried
16
+ * along. The rest of that block is fine to keep: `required_version` and
17
+ * `required_providers` are meaningful in a child module and are not reported
18
+ * here.
19
+ *
20
+ * Label-blind on purpose (#2218). TF001 had to start reading the backend
21
+ * block's type label, since `local` is the state placement it warns about
22
+ * rather than a cure for it. Nothing of the sort applies here: what a child
23
+ * module may not do is name a backend at all, `local` included, and
24
+ * Terraform's own refusal ("A backend block can only appear in the root
25
+ * module") does not read the label either. `hasBlock` is the right test, and
26
+ * the `positive` fixture's child declares `backend "local"` for that reason.
27
+ */
28
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
29
+ import { TERRAFORM_TYPE } from "../../hcl/parse.js";
30
+ import { hasBlock } from "./tf001.js";
31
+ import { isChildScoped } from "./scope.js";
32
+ export const tf015 = {
33
+ id: "TF015",
34
+ description: "Child module declares a backend or cloud block",
35
+ check(ctx) {
36
+ const diagnostics = [];
37
+ for (const [key, entity] of ctx.entities) {
38
+ if (entity.entityType !== TERRAFORM_TYPE || !isResourceDeclarable(entity))
39
+ continue;
40
+ if (!isChildScoped(entity))
41
+ continue;
42
+ const props = entity.props;
43
+ const body = (typeof props.body === "object" && props.body !== null ? props.body : {});
44
+ const backend = hasBlock(body, "backend");
45
+ const cloud = hasBlock(body, "cloud");
46
+ if (!backend && !cloud)
47
+ continue;
48
+ const blockKind = backend ? "backend" : "cloud";
49
+ diagnostics.push({
50
+ checkId: "TF015",
51
+ severity: "warning",
52
+ message: `A child module's terraform block declares a ${blockKind} block. Only a root module may: ` +
53
+ "state belongs to the root, one state file for every module it calls, so this block is either " +
54
+ `ignored with a warning or refused at init. Delete the ${blockKind} block and leave the state ` +
55
+ "configuration to the root module that calls this one.",
56
+ entity: key,
57
+ lexicon: "terraform",
58
+ });
59
+ }
60
+ return diagnostics;
61
+ },
62
+ };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * TF016: an attribute whose whole value is one quoted interpolation,
3
+ * `x = "${var.y}"`, the pre-0.12 style Terraform deprecated in 2019.
4
+ *
5
+ * The quotes buy nothing and cost readability: the expression is already an
6
+ * expression, and wrapping it in a template makes every value in the file look
7
+ * like a string whether it is one or not, which hides the actual type of an
8
+ * argument that takes a list or a bool.
9
+ *
10
+ * This is the one rule in the family that reads the block's SOURCE rather than
11
+ * its parsed body, and it has to. `@cdktn/hcl2json` renders both `x = var.y`
12
+ * and `x = "${var.y}"` as the string `"${var.y}"`, so a check over the parse
13
+ * cannot tell the deprecated form from the idiomatic one and would report every
14
+ * reference in the root. The quotes only survive in the text, so the text is
15
+ * what is scanned, one pass per file, through the same
16
+ * `interpolationOnlyLine` the deterministic fix uses, so the finding and the
17
+ * patch can never disagree.
18
+ *
19
+ * Out of reach, and deliberately not guessed at: tflint also reports the
20
+ * deprecated interpolation inside an object KEY (`"${var.k}" = "value"`).
21
+ * That is a different line shape and its fix is not a simple unquote, so this
22
+ * rule leaves it alone rather than emit a diff it cannot justify.
23
+ *
24
+ * Scope: root and child modules alike (#2112). The condition is a property
25
+ * of the block itself, so a descended module's block is read exactly as a
26
+ * root's is.
27
+ */
28
+ import { isResourceDeclarable } from "@intentius/chant/declarable";
29
+ import { interpolationOnlyLine } from "@intentius/chant/audit/proof";
30
+ export const tf016 = {
31
+ id: "TF016",
32
+ description: "Attribute value is a quoted interpolation of a single expression",
33
+ check(ctx) {
34
+ const diagnostics = [];
35
+ /** `<root>/<file>` to that file's source, so a file is scanned once however many blocks it holds. */
36
+ const sources = new Map();
37
+ for (const entity of ctx.entities.values()) {
38
+ if (entity.lexicon !== "terraform")
39
+ continue;
40
+ if (!isResourceDeclarable(entity))
41
+ continue;
42
+ const props = entity.props;
43
+ if (typeof props.source !== "string" || props.source === "")
44
+ continue;
45
+ sources.set(`${props.root ?? ""}/${props.file ?? ""}`, props.source);
46
+ }
47
+ for (const [key, source] of sources) {
48
+ const lines = source.split("\n");
49
+ for (let i = 0; i < lines.length; i++) {
50
+ const parsed = interpolationOnlyLine(lines[i]);
51
+ if (!parsed)
52
+ continue;
53
+ diagnostics.push({
54
+ checkId: "TF016",
55
+ severity: "info",
56
+ message: `${key}:${i + 1}: \`${parsed.name}\` wraps a single expression in a template ` +
57
+ `("\${${parsed.expr}}"). Terraform deprecated that spelling in 0.12. Write ` +
58
+ `\`${parsed.name} = ${parsed.expr}\` instead, so the argument keeps its own type.`,
59
+ entity: `${key}:${i + 1}`,
60
+ lexicon: "terraform",
61
+ });
62
+ }
63
+ }
64
+ return diagnostics;
65
+ },
66
+ };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * TF017: a `module` block carries `depends_on`.
3
+ *
4
+ * `depends_on` on a module applies to every resource inside it, including the
5
+ * ones that had no reason to wait, so the whole module serializes behind the
6
+ * named dependency and a plan that could have run in parallel does not. It
7
+ * also makes the dependency invisible from inside the module, where the
8
+ * resource that actually needed the ordering lives.
9
+ *
10
+ * Passing an attribute of the dependency into a module input creates the same
11
+ * edge implicitly, only narrower and self-documenting.
12
+ *
13
+ * Scope: root and child modules alike (#2112). The condition is a property
14
+ * of the block itself, so a descended module's block is read exactly as a
15
+ * root's is.
16
+ */
17
+ import { MODULE_TYPE } from "../../hcl/parse.js";
18
+ import { blocksOfType } from "./blocks.js";
19
+ export const tf017 = {
20
+ id: "TF017",
21
+ description: "Module block uses depends_on",
22
+ check(ctx) {
23
+ const diagnostics = [];
24
+ for (const block of blocksOfType(ctx, MODULE_TYPE)) {
25
+ if (!("depends_on" in block.body))
26
+ continue;
27
+ diagnostics.push({
28
+ checkId: "TF017",
29
+ severity: "info",
30
+ message: `"${block.address}" uses \`depends_on\`, which orders every resource in the module behind ` +
31
+ "the dependency, not just the one that needs it. Pass an attribute of the dependency into " +
32
+ "a module input instead, so Terraform derives the edge itself.",
33
+ entity: block.key,
34
+ lexicon: "terraform",
35
+ });
36
+ }
37
+ return diagnostics;
38
+ },
39
+ };
@@ -0,0 +1,63 @@
1
+ /**
2
+ * TF018: an `output` returns a whole resource or data source.
3
+ *
4
+ * `value = aws_instance.web` exports every attribute the provider schema
5
+ * happens to have, which is a much larger promise than the module meant to
6
+ * make. Consumers reach into whatever they find, the module can no longer
7
+ * change any of it, and a provider upgrade that adds or renames an attribute
8
+ * moves the module's public interface without anyone editing it. Sensitive
9
+ * attributes ride along too.
10
+ *
11
+ * A bare `module.x` reference is not reported: a module's outputs are already
12
+ * a curated surface, which is the property this rule is asking for.
13
+ *
14
+ * Scope: root and child modules alike (#2112). The condition is a property
15
+ * of the block itself, so a descended module's block is read exactly as a
16
+ * root's is.
17
+ */
18
+ import { OUTPUT_TYPE } from "../../hcl/parse.js";
19
+ import { blockName, blocksOfType } from "./blocks.js";
20
+ /** Reference prefixes that are not a managed resource type. */
21
+ const NOT_A_RESOURCE = new Set(["var", "local", "module", "each", "count", "self", "path", "terraform"]);
22
+ /**
23
+ * Is this value a reference to a whole resource (`type.name`) or a whole data
24
+ * source (`data.type.name`), with no attribute selected? Anything with a third
25
+ * segment, an index, or a function call around it has already narrowed the
26
+ * value down, which is the fix.
27
+ */
28
+ function wholeBlockReference(value) {
29
+ if (typeof value !== "string")
30
+ return undefined;
31
+ const m = /^\$\{([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_-]*)+)\}$/.exec(value);
32
+ if (!m)
33
+ return undefined;
34
+ const parts = m[1].split(".");
35
+ if (parts[0] === "data")
36
+ return parts.length === 3 ? m[1] : undefined;
37
+ if (NOT_A_RESOURCE.has(parts[0]))
38
+ return undefined;
39
+ return parts.length === 2 ? m[1] : undefined;
40
+ }
41
+ export const tf018 = {
42
+ id: "TF018",
43
+ description: "Output value is a whole resource or data source",
44
+ check(ctx) {
45
+ const diagnostics = [];
46
+ for (const block of blocksOfType(ctx, OUTPUT_TYPE)) {
47
+ const reference = wholeBlockReference(block.body.value);
48
+ if (!reference)
49
+ continue;
50
+ diagnostics.push({
51
+ checkId: "TF018",
52
+ severity: "info",
53
+ message: `Output "${blockName(block.address)}" returns the whole of \`${reference}\`, so every ` +
54
+ "attribute the provider schema has becomes part of this module's public interface, " +
55
+ "including the ones a provider upgrade adds later. Return the attribute the caller needs " +
56
+ `(\`${reference}.id\`, \`${reference}.arn\`) instead.`,
57
+ entity: block.key,
58
+ lexicon: "terraform",
59
+ });
60
+ }
61
+ return diagnostics;
62
+ },
63
+ };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * TF019: a meta-argument is explicitly set to its own default, `false`.
3
+ *
4
+ * `sensitive` and `ephemeral` on a `variable` or an `output`, `prevent_destroy`
5
+ * and `create_before_destroy` inside a `lifecycle` block. All four default to
6
+ * false, so writing it changes nothing and reads as though someone considered
7
+ * the setting and turned it off, which is exactly the impression a reviewer
8
+ * should not get from a variable called `password`.
9
+ *
10
+ * The first TF rule with a mechanical fix: the remediation is the deletion of
11
+ * the line, so this ships `fixKind: "deterministic"` and `chant audit` renders
12
+ * the diff in its quick-wins section. `packages/core/src/audit/proof.ts` owns
13
+ * the patch, as it does for every other deterministic rule.
14
+ *
15
+ * Only a literal `false` counts. `prevent_destroy = var.protect` is a real
16
+ * decision expressed as a variable, and hcl2json leaves it a `"${var.protect}"`
17
+ * string rather than a boolean, so it never reaches the comparison.
18
+ *
19
+ * Scope: root and child modules alike (#2112). The condition is a property
20
+ * of the block itself, so a descended module's block is read exactly as a
21
+ * root's is.
22
+ */
23
+ import { DATA_TYPE, OUTPUT_TYPE, RESOURCE_TYPE, VARIABLE_TYPE } from "../../hcl/parse.js";
24
+ import { blocksOfTypes, nestedBodies } from "./blocks.js";
25
+ /** Meta-arguments that default to false on a `variable` or an `output`. */
26
+ const BLOCK_ARGS = ["sensitive", "ephemeral"];
27
+ /** Meta-arguments that default to false inside a `lifecycle` block. */
28
+ const LIFECYCLE_ARGS = ["prevent_destroy", "create_before_destroy"];
29
+ function redundant(block, arg, where) {
30
+ return {
31
+ checkId: "TF019",
32
+ severity: "info",
33
+ message: `"${block.address}" sets \`${arg} = false\`${where}, which is already the default. Declaring a ` +
34
+ "default reads as a deliberate decision to turn the behaviour off. Delete the line.",
35
+ entity: `${block.key}.${arg}`,
36
+ lexicon: "terraform",
37
+ };
38
+ }
39
+ export const tf019 = {
40
+ id: "TF019",
41
+ description: "Meta-argument explicitly set to its default of false",
42
+ check(ctx) {
43
+ const diagnostics = [];
44
+ for (const block of blocksOfTypes(ctx, [VARIABLE_TYPE, OUTPUT_TYPE])) {
45
+ for (const arg of BLOCK_ARGS) {
46
+ if (block.body[arg] === false)
47
+ diagnostics.push(redundant(block, arg, ""));
48
+ }
49
+ }
50
+ for (const block of blocksOfTypes(ctx, [RESOURCE_TYPE, DATA_TYPE])) {
51
+ for (const lifecycle of nestedBodies(block.body, "lifecycle")) {
52
+ for (const arg of LIFECYCLE_ARGS) {
53
+ if (lifecycle[arg] === false)
54
+ diagnostics.push(redundant(block, arg, " in its `lifecycle` block"));
55
+ }
56
+ }
57
+ }
58
+ return diagnostics;
59
+ },
60
+ };