@intentius/chant-lexicon-terraform 0.124.96 → 0.124.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/behaviour/reference-catalog.d.ts +42 -6
  2. package/dist/behaviour/reference-catalog.d.ts.map +1 -1
  3. package/dist/behaviour/reference-catalog.js +169 -8
  4. package/dist/describe-resources.js +1 -1
  5. package/dist/hcl/evaluate.d.ts +151 -0
  6. package/dist/hcl/evaluate.d.ts.map +1 -0
  7. package/dist/hcl/evaluate.js +940 -0
  8. package/dist/hcl/instances.d.ts +11 -1
  9. package/dist/hcl/instances.d.ts.map +1 -1
  10. package/dist/hcl/instances.js +89 -3
  11. package/dist/hcl/parse.d.ts +23 -1
  12. package/dist/hcl/parse.d.ts.map +1 -1
  13. package/dist/hcl/parse.js +9 -3
  14. package/dist/hcl/redact.d.ts +5 -1
  15. package/dist/hcl/redact.d.ts.map +1 -1
  16. package/dist/hcl/redact.js +7 -3
  17. package/dist/hcl/references.d.ts +12 -1
  18. package/dist/hcl/references.d.ts.map +1 -1
  19. package/dist/hcl/references.js +33 -16
  20. package/dist/integrity.json +6 -6
  21. package/dist/lint/post-synth/tf001.d.ts +4 -1
  22. package/dist/lint/post-synth/tf001.d.ts.map +1 -1
  23. package/dist/lint/post-synth/tf001.js +20 -8
  24. package/dist/lint/post-synth/tf002.d.ts +11 -5
  25. package/dist/lint/post-synth/tf002.d.ts.map +1 -1
  26. package/dist/lint/post-synth/tf002.js +42 -8
  27. package/dist/lint/post-synth/tf006.d.ts +7 -3
  28. package/dist/lint/post-synth/tf006.d.ts.map +1 -1
  29. package/dist/lint/post-synth/tf006.js +9 -3
  30. package/dist/lint/post-synth/tf022.d.ts +6 -0
  31. package/dist/lint/post-synth/tf022.d.ts.map +1 -1
  32. package/dist/lint/post-synth/tf022.js +18 -1
  33. package/dist/lint/secret-shape.d.ts +8 -0
  34. package/dist/lint/secret-shape.d.ts.map +1 -1
  35. package/dist/lint/secret-shape.js +76 -0
  36. package/dist/manifest.json +1 -1
  37. package/dist/plugin.d.ts.map +1 -1
  38. package/dist/plugin.js +7 -2
  39. package/dist/rules/tf001.ts +21 -7
  40. package/dist/rules/tf002.ts +44 -7
  41. package/dist/rules/tf006.ts +8 -3
  42. package/dist/rules/tf022.ts +18 -1
  43. package/package.json +2 -2
  44. package/src/__fixtures__/resolved-arguments/build-trusted/extras.tf +28 -0
  45. package/src/__fixtures__/resolved-arguments/build-trusted/main.tf +22 -0
  46. package/src/__fixtures__/resolved-arguments/build-trusted/serviceaccounts.tf +26 -0
  47. package/src/__fixtures__/resolved-arguments/modules/gke-cluster/main.tf +15 -0
  48. package/src/__fixtures__/resolved-arguments/modules/gke-cluster/variables.tf +12 -0
  49. package/src/__fixtures__/resolved-arguments/modules/gke-project/main.tf +9 -0
  50. package/src/__fixtures__/resolved-arguments/modules/gke-project/outputs.tf +9 -0
  51. package/src/__fixtures__/resolved-arguments/modules/gke-project/variables.tf +7 -0
  52. package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/main.tf +16 -0
  53. package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/outputs.tf +4 -0
  54. package/src/__fixtures__/resolved-arguments/modules/workload-identity-service-account/variables.tf +21 -0
  55. package/src/behaviour/reference-catalog.plugin.test.ts +101 -2
  56. package/src/behaviour/reference-catalog.ts +162 -9
  57. package/src/describe-resources.ts +1 -1
  58. package/src/hcl/edges.test.ts +1 -1
  59. package/src/hcl/evaluate.test.ts +215 -0
  60. package/src/hcl/evaluate.ts +899 -0
  61. package/src/hcl/instances.ts +89 -3
  62. package/src/hcl/parse.test.ts +18 -0
  63. package/src/hcl/parse.ts +28 -3
  64. package/src/hcl/redact.ts +7 -3
  65. package/src/hcl/references.test.ts +68 -0
  66. package/src/hcl/references.ts +31 -15
  67. package/src/lint/post-synth/fixtures/TF001/negative-second-block.tf +17 -0
  68. package/src/lint/post-synth/fixtures/TF002/module-cycle/cdn/back/main.tf +3 -0
  69. package/src/lint/post-synth/fixtures/TF002/module-cycle/cdn/main.tf +3 -0
  70. package/src/lint/post-synth/fixtures/TF002/module-cycle/main.tf +14 -0
  71. package/src/lint/post-synth/fixtures/TF002/module-nested/cdn/edge/main.tf +8 -0
  72. package/src/lint/post-synth/fixtures/TF002/module-nested/cdn/main.tf +3 -0
  73. package/src/lint/post-synth/fixtures/TF002/module-nested/main.tf +14 -0
  74. package/src/lint/post-synth/fixtures/TF002/module-other-provider/cdn/main.tf +8 -0
  75. package/src/lint/post-synth/fixtures/TF002/module-other-provider/main.tf +14 -0
  76. package/src/lint/post-synth/fixtures/TF002/module-pinned/cdn/main.tf +12 -0
  77. package/src/lint/post-synth/fixtures/TF002/module-pinned/main.tf +14 -0
  78. package/src/lint/post-synth/fixtures/TF002/module-unpinned/cdn/main.tf +7 -0
  79. package/src/lint/post-synth/fixtures/TF002/module-unpinned/main.tf +14 -0
  80. package/src/lint/post-synth/fixtures/TF006/negative-null-default.tf +12 -0
  81. package/src/lint/post-synth/fixtures/TF006/positive-empty-default.tf +0 -6
  82. package/src/lint/post-synth/fixtures/TF007/negative-names.tf +25 -0
  83. package/src/lint/post-synth/fixtures/TF022/negative-secret-reference.tf +32 -0
  84. package/src/lint/post-synth/fixtures/TF022/positive-secret-data.tf +8 -0
  85. package/src/lint/post-synth/fixtures/TF024/live-backend-second-block/a-versions.tf +3 -0
  86. package/src/lint/post-synth/fixtures/TF024/live-backend-second-block/b-backend.tf +10 -0
  87. package/src/lint/post-synth/fixtures/TF024/live-cloud-second-block/a-versions.tf +3 -0
  88. package/src/lint/post-synth/fixtures/TF024/live-cloud-second-block/b-cloud.tf +9 -0
  89. package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/a-versions.tf +3 -0
  90. package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/b-backend.tf +6 -0
  91. package/src/lint/post-synth/fixtures/TF024/sidecar-backend-second-block/estate.chdf.hcl +1 -0
  92. package/src/lint/post-synth/module-rules.test.ts +1 -1
  93. package/src/lint/post-synth/post-synth.test.ts +39 -0
  94. package/src/lint/post-synth/single-block.test.ts +23 -2
  95. package/src/lint/post-synth/tf001.ts +21 -7
  96. package/src/lint/post-synth/tf002.test.ts +45 -1
  97. package/src/lint/post-synth/tf002.ts +44 -7
  98. package/src/lint/post-synth/tf006.ts +8 -3
  99. package/src/lint/post-synth/tf022.ts +18 -1
  100. package/src/lint/post-synth/tf024.test.ts +21 -0
  101. package/src/lint/secret-shape.test.ts +30 -0
  102. package/src/lint/secret-shape.ts +75 -0
  103. package/src/plugin.ts +6 -2
  104. package/src/redact.test.ts +8 -0
@@ -0,0 +1,26 @@
1
+ locals {
2
+ workload_identity_service_accounts = {
3
+ prow-build-trusted = {
4
+ description = "default service account for pods in ${local.cluster_name}"
5
+ }
6
+ gcb-builder = {
7
+ name = "gcb-builder"
8
+ description = "trigger GCB builds in all k8s-staging projects"
9
+ }
10
+ kubernetes-external-secrets = {
11
+ description = "sync K8s secrets from GSM in this and other projects"
12
+ project_roles = ["roles/secretmanager.secretAccessor"]
13
+ cluster_namespace = "kubernetes-external-secrets"
14
+ }
15
+ }
16
+ }
17
+
18
+ module "workload_identity_service_accounts" {
19
+ for_each = local.workload_identity_service_accounts
20
+ source = "../modules/workload-identity-service-account"
21
+ project_id = module.project.project_id
22
+ name = each.key
23
+ description = each.value.description
24
+ cluster_namespace = lookup(each.value, "cluster_namespace", local.pod_namespace)
25
+ project_roles = lookup(each.value, "project_roles", [])
26
+ }
@@ -0,0 +1,15 @@
1
+ resource "google_container_cluster" "prod_cluster" {
2
+ count = var.is_prod_cluster == "true" ? 1 : 0
3
+
4
+ name = var.cluster_name
5
+ location = "us-central1"
6
+ project = var.project_name
7
+ }
8
+
9
+ resource "google_container_cluster" "test_cluster" {
10
+ count = var.is_prod_cluster == "true" ? 0 : 1
11
+
12
+ name = var.cluster_name
13
+ location = "us-central1"
14
+ project = var.project_name
15
+ }
@@ -0,0 +1,12 @@
1
+ variable "project_name" {
2
+ type = string
3
+ }
4
+
5
+ variable "cluster_name" {
6
+ type = string
7
+ }
8
+
9
+ variable "is_prod_cluster" {
10
+ type = string
11
+ default = "false"
12
+ }
@@ -0,0 +1,9 @@
1
+ data "google_organization" "org" {
2
+ domain = "kubernetes.io"
3
+ }
4
+
5
+ resource "google_project" "project" {
6
+ name = var.project_name
7
+ project_id = var.project_name
8
+ org_id = data.google_organization.org.org_id
9
+ }
@@ -0,0 +1,9 @@
1
+ output "project_id" {
2
+ description = "The project_id of the project that was created"
3
+ value = google_project.project.project_id
4
+ }
5
+
6
+ output "project_number" {
7
+ description = "Numeric identifier for the project"
8
+ value = google_project.project.number
9
+ }
@@ -0,0 +1,7 @@
1
+ variable "project_id" {
2
+ type = string
3
+ }
4
+
5
+ variable "project_name" {
6
+ type = string
7
+ }
@@ -0,0 +1,16 @@
1
+ locals {
2
+ description = var.description != "" ? var.description : var.name
3
+ }
4
+
5
+ resource "google_service_account" "serviceaccount" {
6
+ project = var.project_id
7
+ account_id = var.name
8
+ display_name = local.description
9
+ }
10
+
11
+ resource "google_project_iam_member" "project_roles" {
12
+ for_each = toset(var.project_roles)
13
+ project = var.project_id
14
+ role = each.value
15
+ member = "serviceAccount:${google_service_account.serviceaccount.email}"
16
+ }
@@ -0,0 +1,4 @@
1
+ output "email" {
2
+ description = "The email of the serviceaccount that was created"
3
+ value = google_service_account.serviceaccount.email
4
+ }
@@ -0,0 +1,21 @@
1
+ variable "project_id" {
2
+ type = string
3
+ }
4
+
5
+ variable "name" {
6
+ type = string
7
+ }
8
+
9
+ variable "description" {
10
+ type = string
11
+ default = ""
12
+ }
13
+
14
+ variable "cluster_namespace" {
15
+ type = string
16
+ }
17
+
18
+ variable "project_roles" {
19
+ type = list(string)
20
+ default = []
21
+ }
@@ -14,7 +14,8 @@ import { join } from "node:path";
14
14
  import { describe, expect, it } from "vitest";
15
15
  import type { IRNode } from "@intentius/chant/graph-ir";
16
16
  import { reconstructIndexedEdges, refRuleId } from "@intentius/chant/graph-refs";
17
- import { TERRAFORM_REFERENCE_CATALOG } from "./reference-catalog";
17
+ import { literalArguments, TERRAFORM_REFERENCE_CATALOG, terraformDeclaredNode } from "./reference-catalog";
18
+ import { runCatalogPass } from "@intentius/chant/workspace/catalog-edges";
18
19
  import { terraformPlugin } from "../plugin";
19
20
 
20
21
  const LEXICON = "terraform";
@@ -38,7 +39,8 @@ describe("terraform reference catalog on the plugin", () => {
38
39
 
39
40
  describe("each targeted rule draws its edge on provider-typed nodes", () => {
40
41
  for (const [i, rule] of TERRAFORM_REFERENCE_CATALOG.refs.entries()) {
41
- if (!rule.targetKind) continue;
42
+ // A rule with a `join` spans two values and has its own fixtures (packages/core/src/workspace/identity-annotation-catalog.test.ts).
43
+ if (!rule.targetKind || rule.join) continue;
42
44
  it(`${rule.from} ${rule.path} -> ${rule.targetKind}`, () => {
43
45
  const value = `id-${i}`;
44
46
  const nodes: IRNode[] = [
@@ -63,3 +65,100 @@ describe("terraform reference catalog on the plugin", () => {
63
65
  });
64
66
  });
65
67
  });
68
+
69
+ describe("a declared block, as chant workspace graph's catalog pass reads it (#5097)", () => {
70
+ const block = (member: string, address: string, body: Record<string, unknown>) => ({
71
+ id: `${member}/r/${address}`,
72
+ kind: "Terraform::Resource",
73
+ lexicon: LEXICON,
74
+ member,
75
+ attrs: { address, type: address.split(".")[0], name: address.split(".")[1], body, source: "..." },
76
+ });
77
+
78
+ it("has the provider type as its kind and its literal arguments as attrs, counting the rest", () => {
79
+ const view = terraformDeclaredNode(block("net", "aws_subnet.a", {
80
+ vpc_id: "vpc-1",
81
+ cidr_block: "${var.cidr}",
82
+ tags: { Name: "a", Env: "${var.env}" },
83
+ ids: ["sg-1", "${aws_security_group.x.id}"],
84
+ ebs_block_device: [{ volume_size: 8, kms_key_id: "${aws_kms_key.k.arn}" }],
85
+ }));
86
+ expect(view).toEqual({
87
+ kind: "aws_subnet",
88
+ attrs: { vpc_id: "vpc-1", tags: { Name: "a" }, ids: ["sg-1"], ebs_block_device: [{ volume_size: 8 }] },
89
+ nonLiteral: 4,
90
+ });
91
+ expect(literalArguments({}).nonLiteral).toBe(0);
92
+ expect(terraformDeclaredNode({ id: "x", kind: "Terraform::Variable", lexicon: LEXICON, attrs: {} })).toBeUndefined();
93
+ });
94
+
95
+ it("reads a literal argument with the provider type's rules across members, and counts what it could not read", () => {
96
+ const rule = refRuleId(LEXICON, "aws_subnet", "vpc_id");
97
+ const out = runCatalogPass(
98
+ [
99
+ // The catalog has no identity rules yet, so a node is named by its id here.
100
+ block("net", "aws_subnet.a", { vpc_id: "core/r/aws_vpc.main", cidr_block: "${var.cidr}" }),
101
+ block("net", "aws_subnet.b", { vpc_id: "vpc-0abc" }),
102
+ block("core", "aws_vpc.main", {}),
103
+ ],
104
+ [{ lexicon: LEXICON, catalog: TERRAFORM_REFERENCE_CATALOG }],
105
+ );
106
+ expect(out.edges).toEqual([expect.objectContaining({ from: "net/r/aws_subnet.a", to: "core/r/aws_vpc.main", rule, relation: "containment", member: "net" })]);
107
+ expect(out.dangling).toEqual([expect.objectContaining({ from: "net/r/aws_subnet.b", rule, value: "vpc-0abc", targetKind: "aws_vpc" })]);
108
+ expect(out.nonLiteral).toEqual({ "net/r/aws_subnet.a": 1 });
109
+ });
110
+ });
111
+
112
+ describe("evaluated arguments in the catalog pass (#5117)", () => {
113
+ // Stand-ins for the rules #5099 and #5101 add: an identity that names a
114
+ // cluster and a service account by an argument, and a rule from an Argo CD
115
+ // cluster Secret's name. Only the values come from this lexicon.
116
+ const withIdentities = {
117
+ ...TERRAFORM_REFERENCE_CATALOG,
118
+ identities: [
119
+ { kind: "google_container_cluster", ids: ["name"] },
120
+ { kind: "google_service_account", ids: ["account_id"] },
121
+ ],
122
+ };
123
+ const standIn = {
124
+ identities: [],
125
+ refs: [
126
+ { from: "Secret", path: "stringData.name", targetKind: "google_container_cluster", relation: "reference" as const, label: "cluster" },
127
+ { from: "ConfigMap", path: "data.account", targetKind: "google_service_account", relation: "reference" as const, label: "as" },
128
+ ],
129
+ };
130
+
131
+ it("joins a cluster Secret to the cluster instance its count picks by its resolved name, and an instance by its resolved account_id", async () => {
132
+ const { renderTerraformRoots } = await import("../hcl/roots");
133
+ const { buildGraphIr, withInstanceNodes } = await import("@intentius/chant/graph-ir");
134
+ const rendered = await renderTerraformRoots({ projectRoot: join(import.meta.dirname, "..", "__fixtures__", "resolved-arguments"), roots: { bt: { dir: "./build-trusted" } } });
135
+ const ir = withInstanceNodes(buildGraphIr(rendered.entities), rendered.entities);
136
+ const prefix = (id: string) => `infra/${id}`;
137
+ const nodes = [
138
+ ...ir.nodes.map((n) => ({ ...n, id: prefix(n.id), member: "infra", ...(n.instanceOf ? { instanceOf: prefix(n.instanceOf) } : {}) })),
139
+ { id: "apps/Secret/gke-prow-build-trusted", kind: "Secret", lexicon: "k8s", member: "apps", attrs: { stringData: { name: "prow-build-trusted" } } },
140
+ { id: "apps/ConfigMap/sa", kind: "ConfigMap", lexicon: "k8s", member: "apps", attrs: { data: { account: "gcb-builder" } } },
141
+ ];
142
+ const out = runCatalogPass(nodes, [
143
+ { lexicon: LEXICON, catalog: withIdentities },
144
+ { lexicon: "k8s", catalog: standIn },
145
+ ]);
146
+ // The module's prod and test blocks share a name; `count = var.is_prod_cluster == "true" ? 1 : 0` plans only the prod one (#5094).
147
+ expect(out.ambiguous).toEqual([]);
148
+ expect(out.edges.map((e) => [e.from, e.to])).toEqual(expect.arrayContaining([
149
+ ["apps/Secret/gke-prow-build-trusted", "infra/bt/module.prow_build_cluster/google_container_cluster.prod_cluster[0]"],
150
+ ["apps/ConfigMap/sa", 'infra/bt/module.workload_identity_service_accounts["gcb-builder"]/google_service_account.serviceaccount'],
151
+ ]));
152
+ // What stays unread: the data source, timestamp() and the computed email in extras.tf among them.
153
+ expect(out.nonLiteral["infra/bt/google_storage_bucket.from_data"]).toBe(1);
154
+ expect(out.nonLiteral["infra/bt/google_storage_bucket.dated"]).toBe(1);
155
+ expect(out.nonLiteral["infra/bt/google_project_iam_member.computed"]).toBe(1);
156
+ });
157
+
158
+ it("reads an instance node with no evaluated entry on its block as it is", () => {
159
+ const blockNode: IRNode = { id: "m/r/aws_eip.pool", kind: "Terraform::Resource", lexicon: LEXICON, attrs: { address: "aws_eip.pool", type: "aws_eip", body: { domain: "vpc" } } };
160
+ const inst: IRNode = { id: "m/r/aws_eip.pool[0]", kind: "Terraform::Resource", lexicon: LEXICON, attrs: {}, instanceOf: blockNode.id, instanceKey: "[0]" };
161
+ expect(terraformDeclaredNode(inst, blockNode)).toBeUndefined();
162
+ expect(terraformDeclaredNode(blockNode)).toEqual({ kind: "aws_eip", attrs: { domain: "vpc" }, nonLiteral: 0 });
163
+ });
164
+ });
@@ -24,12 +24,16 @@
24
24
  * asks every lexicon of a graph for its catalog (behold#1030's gate, before
25
25
  * behold#1005 retires behold's name matcher) finds terraform's. What it
26
26
  * matches depends on the nodes. A declared or live `chant graph` node is
27
- * `Terraform::Resource`, with its provider type in `attrs.type` and no
28
- * arguments in `attrs`, so no rule here reads it: those graphs keep the edges
29
- * the HCL references and the state dependencies give them, and gain none. A
30
- * node whose `kind` is the provider type and whose `attrs` are the block's
27
+ * `Terraform::Resource`, with its provider type in `attrs.type` and its
28
+ * arguments under `attrs.body`, so no rule here reads it as it is. A node
29
+ * whose `kind` is the provider type and whose `attrs` are the block's
31
30
  * arguments (`./request.ts`'s, an engine's instance roster) is what the rules
32
- * are for. `chant graph --live` merges a catalog's edges with the
31
+ * are for. `chant workspace graph` reads a declared block that way since
32
+ * #5097, through {@link terraformDeclaredNode}, with only the literal
33
+ * arguments, so a rule matches a value written in the HCL and an
34
+ * interpolation is counted as not read. Since #5117 an expression the files
35
+ * determine (a variable, a local, `each.key`, a module output) reads as its
36
+ * value, per instance node when the block has them. `chant graph --live` merges a catalog's edges with the
33
37
  * observation's own since #4535; before, any catalog replaced them, which is
34
38
  * what kept this one off the plugin.
35
39
  *
@@ -68,7 +72,10 @@
68
72
  * in it is not `complete` by omission.
69
73
  */
70
74
 
71
- import type { ReferenceCatalog } from "@intentius/chant/graph-refs";
75
+ import { readPath, type DeclaredCatalogNode, type JoinedRef, type ReferenceCatalog, type RefRule } from "@intentius/chant/graph-refs";
76
+ import type { IRNode } from "@intentius/chant/graph-ir";
77
+ import { RESOURCE_TYPE, type TerraformEvaluated } from "../hcl/parse";
78
+ import { interpolated } from "../hcl/evaluate";
72
79
 
73
80
  /**
74
81
  * Provider types this catalog has looked at and found to hold no argument
@@ -87,12 +94,156 @@ export const REFERENCES_NOTHING: ReadonlySet<string> = new Set([
87
94
  "aws_route53_zone",
88
95
  ]);
89
96
 
97
+ const isBlock = (v: unknown): v is Record<string, unknown> => typeof v === "object" && v !== null && !Array.isArray(v);
98
+
99
+ /**
100
+ * A block body with only its literal arguments, and how many it left out.
101
+ * A nested block (hcl2json writes it as an object, or a list of objects) and a
102
+ * map are descended, so each of their arguments counts on its own. A list of
103
+ * values keeps its literal elements and counts once when any element is not.
104
+ *
105
+ * `resolved` is the block's evaluated arguments (#5117, `../hcl/evaluate.ts`),
106
+ * shaped like the body: an argument that holds an expression and has a value
107
+ * there takes that value and is not counted.
108
+ */
109
+ export function literalArguments(body: Record<string, unknown>, resolved?: Record<string, unknown>): { attrs: Record<string, unknown>; nonLiteral: number } {
110
+ let nonLiteral = 0;
111
+ const walk = (obj: Record<string, unknown>, over: Record<string, unknown> | undefined, depth: number): Record<string, unknown> => {
112
+ const out: Record<string, unknown> = {};
113
+ for (const [key, value] of Object.entries(obj)) {
114
+ if (depth > 12) break;
115
+ const with_ = over?.[key];
116
+ if (isBlock(value)) {
117
+ out[key] = walk(value, isBlock(with_) ? with_ : undefined, depth + 1);
118
+ } else if (Array.isArray(value) && value.length > 0 && value.every(isBlock)) {
119
+ out[key] = value.map((b, i) => walk(b, Array.isArray(with_) && isBlock(with_[i]) ? with_[i] : undefined, depth + 1));
120
+ } else if (interpolated(value) && over !== undefined && key in over) {
121
+ out[key] = with_;
122
+ } else if (Array.isArray(value)) {
123
+ const kept = value.filter((v) => !interpolated(v));
124
+ if (kept.length < value.length) nonLiteral++;
125
+ out[key] = kept;
126
+ } else if (interpolated(value)) {
127
+ nonLiteral++;
128
+ } else {
129
+ out[key] = value;
130
+ }
131
+ }
132
+ return out;
133
+ };
134
+ return { attrs: walk(body, resolved, 0), nonLiteral };
135
+ }
136
+
137
+ /** A block node's `evaluated` attr (`TerraformEvaluated` in `../hcl/parse.ts`), when it has one. */
138
+ function evaluatedOf(attrs: Record<string, unknown>): TerraformEvaluated | undefined {
139
+ return isBlock(attrs.evaluated) ? (attrs.evaluated as TerraformEvaluated) : undefined;
140
+ }
141
+
142
+ /** A declared resource node's provider type, from its `type` or its address. */
143
+ function providerType(attrs: Record<string, unknown>): string | undefined {
144
+ const address = typeof attrs.address === "string" ? attrs.address : "";
145
+ return typeof attrs.type === "string" && attrs.type !== "" ? attrs.type : address.includes(".") ? address.split(".").slice(-2)[0] : undefined;
146
+ }
147
+
148
+ /**
149
+ * The node the catalog reads for a declared terraform block (#5097): its
150
+ * provider type as `kind`, as `./request.ts` gives the behaviour engine, and
151
+ * its literal arguments as `attrs`. An argument whose value is an
152
+ * expression takes the value `../hcl/evaluate.ts` resolved for it (#5117):
153
+ * a variable, a local, `each.key`, a module output or a composed string the
154
+ * files determine. One that did not resolve is left out and counted; the
155
+ * reference that stopped it is in the node's `evaluated.stopped`. Any block
156
+ * but a `resource` is read as it is.
157
+ *
158
+ * A block with instance nodes is read per instance (#5117): each instance
159
+ * node, handed its block node, reads the block's arguments with that
160
+ * instance's values (`evaluated.instances`), and the block node itself reads
161
+ * no arguments, so a value is matched on the instance it belongs to and not
162
+ * a second time on the block.
163
+ */
164
+ export function terraformDeclaredNode(node: IRNode, block?: IRNode): DeclaredCatalogNode | undefined {
165
+ if (node.instanceOf !== undefined) {
166
+ if (!block || block.kind !== RESOURCE_TYPE) return undefined;
167
+ const attrs = block.attrs ?? {};
168
+ const instances = evaluatedOf(attrs)?.instances;
169
+ const type = providerType(attrs);
170
+ if (!instances || type === undefined) return undefined;
171
+ const callers = Array.isArray(attrs.callers) ? (attrs.callers as string[]).join(".") : "";
172
+ const path = node.instanceScope ?? callers;
173
+ const at = instances[`${path ? `${path}.` : ""}${String(attrs.address)}${node.instanceKey ?? ""}`];
174
+ if (!at) return undefined;
175
+ const body = isBlock(attrs.body) ? attrs.body : {};
176
+ return { kind: type, ...literalArguments(body, at.arguments ?? {}) };
177
+ }
178
+ if (node.kind !== RESOURCE_TYPE) return undefined;
179
+ const attrs = node.attrs ?? {};
180
+ const type = providerType(attrs);
181
+ if (type === undefined) return undefined;
182
+ const evaluated = evaluatedOf(attrs);
183
+ if (evaluated?.instances) return { kind: type, attrs: {}, nonLiteral: 0 };
184
+ const body = isBlock(attrs.body) ? attrs.body : {};
185
+ return { kind: type, ...literalArguments(body, evaluated?.arguments) };
186
+ }
187
+
188
+ const WORKLOAD_IDENTITY_USER = "roles/iam.workloadIdentityUser";
189
+ const WORKLOAD_MEMBER = /^serviceAccount:[^.]+\.svc\.id\.goog\[([^/\]]+)\/([^\]]+)\]$/;
190
+
191
+ /**
192
+ * A binding that lets a Kubernetes ServiceAccount act as a GCP service
193
+ * account (#5099): a `google_service_account_iam_member` or
194
+ * `google_service_account_iam_binding` with `role =
195
+ * "roles/iam.workloadIdentityUser"` and a literal member
196
+ * `serviceAccount:<project>.svc.id.goog[<namespace>/<name>]`. The edge goes
197
+ * to the ServiceAccount of that name in that namespace, found in any member.
198
+ * A binding for a cluster whose manifests are not in the workspace names no
199
+ * node, and that is not reported: the pool is the cluster's, not this
200
+ * workspace's. A member built with a local or a variable is not literal, so
201
+ * it is not read.
202
+ */
203
+ function workloadBindingRule(from: string, path: string): RefRule {
204
+ return {
205
+ from,
206
+ path,
207
+ targetKind: "K8s::Core::ServiceAccount",
208
+ relation: "reference",
209
+ label: "lets act as",
210
+ optional: true,
211
+ join(holder, find) {
212
+ if (!readPath(holder.attrs, "role").includes(WORKLOAD_IDENTITY_USER)) return [];
213
+ const refs: JoinedRef[] = [];
214
+ for (const member of new Set(readPath(holder.attrs, path))) {
215
+ const m = WORKLOAD_MEMBER.exec(member);
216
+ if (!m) continue;
217
+ refs.push({ value: member, targets: find(m[2], m[1]).filter((n) => n.kind === "K8s::Core::ServiceAccount"), via: "workload identity member" });
218
+ }
219
+ return refs;
220
+ },
221
+ };
222
+ }
223
+
90
224
  /** The catalog. Rules are grouped by the relation they produce. */
91
225
  export const TERRAFORM_REFERENCE_CATALOG: ReferenceCatalog = {
226
+ declaredNode: terraformDeclaredNode,
92
227
  // Every node is indexed by its own id and physical id (`reconstructEdges`
93
- // does that itself), and a block's body never carries a second identifier
94
- // for the block, so there are no identity rules to declare.
95
- identities: [],
228
+ // does that itself), and a block's body rarely carries a second identifier
229
+ // for the block. Two exceptions. A cloud secret (#5098): other systems
230
+ // name it by the id it is created with, an ExternalSecret's `remoteRef.key`
231
+ // among them. The k8s catalog's rule joins that key to the secret inside
232
+ // the store's project or region, so these only make the secret findable.
233
+ // A managed Kubernetes cluster is also named by its literal `name`, which
234
+ // is what an Argo CD cluster Secret carries (#5101).
235
+ identities: [
236
+ { kind: "google_secret_manager_secret", ids: ["secret_id"] },
237
+ { kind: "aws_secretsmanager_secret", ids: ["name"] },
238
+ { kind: "google_container_cluster", ids: ["name"] },
239
+ { kind: "aws_eks_cluster", ids: ["name"] },
240
+ { kind: "azurerm_kubernetes_cluster", ids: ["name"] },
241
+ // A ServiceAccount's annotation names a cloud identity by a value built
242
+ // from these (#5099): the k8s catalog's rule joins the email's account id
243
+ // and project, or the ARN's role name, so these only make them findable.
244
+ { kind: "google_service_account", ids: ["account_id"] },
245
+ { kind: "aws_iam_role", ids: ["name"] },
246
+ ],
96
247
  refs: [
97
248
  // ── containment (→ boundary pairs, and containment edges) ──
98
249
  { from: "aws_subnet", path: "vpc_id", targetKind: "aws_vpc", relation: "containment", label: "in VPC" },
@@ -178,6 +329,8 @@ export const TERRAFORM_REFERENCE_CATALOG: ReferenceCatalog = {
178
329
  { from: "aws_iam_role_policy_attachment", path: "policy_arn", targetKind: "aws_iam_policy", relation: "reference", label: "attaches" },
179
330
  { from: "aws_iam_role_policy", path: "role", targetKind: "aws_iam_role", relation: "reference", label: "on" },
180
331
  { from: "aws_iam_instance_profile", path: "role", targetKind: "aws_iam_role", relation: "reference", label: "holds" },
332
+ workloadBindingRule("google_service_account_iam_member", "member"),
333
+ workloadBindingRule("google_service_account_iam_binding", "members[]"),
181
334
  { from: "aws_route53_record", path: "zone_id", targetKind: "aws_route53_zone", relation: "reference", label: "in zone" },
182
335
  // A bucket's regional domain name or a load balancer's DNS name; no `targetKind`.
183
336
  { from: "aws_cloudfront_distribution", path: "origin[].domain_name", relation: "reference", label: "origin" },
@@ -1454,7 +1454,7 @@ function fromEntityName(name: string): { root: string; address: string; callers:
1454
1454
  const last = name.lastIndexOf("/");
1455
1455
  return {
1456
1456
  root: name.slice(0, slash),
1457
- address: name.slice(last + 1).replace(/~\d+$/, ""),
1457
+ address: name.slice(last + 1).replace(/(~[^/~]+)+$/, ""),
1458
1458
  callers: callersOfKey(name),
1459
1459
  };
1460
1460
  }
@@ -76,7 +76,7 @@ describe("edges out of a parsed estate (#2265)", () => {
76
76
  "app/module.cdn/aws_cloudfront_distribution.cdn app/module.cdn/var.bucket origin_id ",
77
77
  "app/module.cdn app/aws_s3_bucket.assets bucket id",
78
78
  "app/output.cdn_url app/module.cdn value url",
79
- "app/provider.aws app/var.region region ",
79
+ "app/provider.aws~replica app/var.region region ",
80
80
  "network/aws_vpc.main network/locals tags ",
81
81
  "network/aws_vpc.main network/var.cidr cidr_block ",
82
82
  "network/output.vpc_id network/aws_vpc.main value id",