|
b45288f…
|
lmata
|
1 |
""" |
|
b45288f…
|
lmata
|
2 |
Terraform enricher for cross-file module resolution and resource linking. |
|
b45288f…
|
lmata
|
3 |
|
|
b45288f…
|
lmata
|
4 |
Promotes and links Terraform graph nodes: |
|
b45288f…
|
lmata
|
5 |
- Cross-file variable references (REFERENCES edges) |
|
b45288f…
|
lmata
|
6 |
- Module source resolution (DEPENDS_ON edges to local source dirs) |
|
b45288f…
|
lmata
|
7 |
- Provider grouping (BELONGS_TO edges to provider nodes) |
|
b45288f…
|
lmata
|
8 |
""" |
|
b45288f…
|
lmata
|
9 |
|
|
b45288f…
|
lmata
|
10 |
from navegador.enrichment.base import EnrichmentResult, FrameworkEnricher |
|
b45288f…
|
lmata
|
11 |
from navegador.graph.store import GraphStore |
|
b45288f…
|
lmata
|
12 |
|
|
b45288f…
|
lmata
|
13 |
# Common Terraform provider prefixes and their canonical provider names |
|
b45288f…
|
lmata
|
14 |
_PROVIDER_PREFIXES = { |
|
b45288f…
|
lmata
|
15 |
"aws_": "aws", |
|
b45288f…
|
lmata
|
16 |
"google_": "google", |
|
b45288f…
|
lmata
|
17 |
"azurerm_": "azurerm", |
|
b45288f…
|
lmata
|
18 |
"azuread_": "azuread", |
|
b45288f…
|
lmata
|
19 |
"kubernetes_": "kubernetes", |
|
b45288f…
|
lmata
|
20 |
"helm_": "helm", |
|
b45288f…
|
lmata
|
21 |
"vault_": "vault", |
|
b45288f…
|
lmata
|
22 |
"datadog_": "datadog", |
|
b45288f…
|
lmata
|
23 |
"cloudflare_": "cloudflare", |
|
b45288f…
|
lmata
|
24 |
"digitalocean_": "digitalocean", |
|
b45288f…
|
lmata
|
25 |
"github_": "github", |
|
b45288f…
|
lmata
|
26 |
"null_": "null", |
|
b45288f…
|
lmata
|
27 |
"random_": "random", |
|
b45288f…
|
lmata
|
28 |
"local_": "local", |
|
b45288f…
|
lmata
|
29 |
"tls_": "tls", |
|
b45288f…
|
lmata
|
30 |
"archive_": "archive", |
|
b45288f…
|
lmata
|
31 |
"external_": "external", |
|
b45288f…
|
lmata
|
32 |
"template_": "template", |
|
b45288f…
|
lmata
|
33 |
"time_": "time", |
|
b45288f…
|
lmata
|
34 |
} |
|
b45288f…
|
lmata
|
35 |
|
|
b45288f…
|
lmata
|
36 |
|
|
b45288f…
|
lmata
|
37 |
class TerraformEnricher(FrameworkEnricher): |
|
b45288f…
|
lmata
|
38 |
"""Enriches a navegador graph with Terraform-specific semantics.""" |
|
b45288f…
|
lmata
|
39 |
|
|
b45288f…
|
lmata
|
40 |
def __init__(self, store: GraphStore) -> None: |
|
b45288f…
|
lmata
|
41 |
super().__init__(store) |
|
b45288f…
|
lmata
|
42 |
|
|
b45288f…
|
lmata
|
43 |
# ── Identity ────────────────────────────────────────────────────────────── |
|
b45288f…
|
lmata
|
44 |
|
|
b45288f…
|
lmata
|
45 |
@property |
|
b45288f…
|
lmata
|
46 |
def framework_name(self) -> str: |
|
b45288f…
|
lmata
|
47 |
return "terraform" |
|
b45288f…
|
lmata
|
48 |
|
|
b45288f…
|
lmata
|
49 |
@property |
|
b45288f…
|
lmata
|
50 |
def detection_patterns(self) -> list[str]: |
|
b45288f…
|
lmata
|
51 |
return [] # No import nodes for Terraform |
|
b45288f…
|
lmata
|
52 |
|
|
b45288f…
|
lmata
|
53 |
@property |
|
b45288f…
|
lmata
|
54 |
def detection_files(self) -> list[str]: |
|
b45288f…
|
lmata
|
55 |
return ["main.tf", "variables.tf", "outputs.tf", "providers.tf"] |
|
b45288f…
|
lmata
|
56 |
|
|
b45288f…
|
lmata
|
57 |
# ── Enrichment ──────────────────────────────────────────────────────────── |
|
b45288f…
|
lmata
|
58 |
|
|
b45288f…
|
lmata
|
59 |
def enrich(self) -> EnrichmentResult: |
|
b45288f…
|
lmata
|
60 |
result = EnrichmentResult() |
|
b45288f…
|
lmata
|
61 |
|
|
b45288f…
|
lmata
|
62 |
var_refs = self._enrich_variable_references() |
|
b45288f…
|
lmata
|
63 |
result.edges_added += var_refs |
|
b45288f…
|
lmata
|
64 |
result.patterns_found["variable_references"] = var_refs |
|
b45288f…
|
lmata
|
65 |
|
|
b45288f…
|
lmata
|
66 |
module_deps = self._enrich_module_sources() |
|
b45288f…
|
lmata
|
67 |
result.edges_added += module_deps |
|
b45288f…
|
lmata
|
68 |
result.patterns_found["module_sources"] = module_deps |
|
b45288f…
|
lmata
|
69 |
|
|
b45288f…
|
lmata
|
70 |
provider_links = self._enrich_provider_grouping() |
|
b45288f…
|
lmata
|
71 |
result.edges_added += provider_links |
|
b45288f…
|
lmata
|
72 |
result.patterns_found["provider_grouping"] = provider_links |
|
b45288f…
|
lmata
|
73 |
|
|
b45288f…
|
lmata
|
74 |
return result |
|
b45288f…
|
lmata
|
75 |
|
|
b45288f…
|
lmata
|
76 |
# ── Pattern helpers ─────────────────────────────────────────────────────── |
|
b45288f…
|
lmata
|
77 |
|
|
b45288f…
|
lmata
|
78 |
def _enrich_variable_references(self) -> int: |
|
b45288f…
|
lmata
|
79 |
"""Find terraform_variable and terraform_output nodes that reference |
|
b45288f…
|
lmata
|
80 |
variables defined in other files, and create REFERENCES edges.""" |
|
b45288f…
|
lmata
|
81 |
edges_added = 0 |
|
b45288f…
|
lmata
|
82 |
|
|
b45288f…
|
lmata
|
83 |
# Find all terraform_variable nodes |
|
b45288f…
|
lmata
|
84 |
var_result = self.store.query( |
|
b45288f…
|
lmata
|
85 |
"MATCH (v) WHERE v.semantic_type = $var_type RETURN v.name, v.file_path", |
|
b45288f…
|
lmata
|
86 |
{"var_type": "terraform_variable"}, |
|
b45288f…
|
lmata
|
87 |
) |
|
b45288f…
|
lmata
|
88 |
var_rows = var_result.result_set or [] |
|
b45288f…
|
lmata
|
89 |
var_by_name: dict[str, list[str]] = {} |
|
b45288f…
|
lmata
|
90 |
for row in var_rows: |
|
b45288f…
|
lmata
|
91 |
name, file_path = row[0], row[1] |
|
b45288f…
|
lmata
|
92 |
if name and file_path: |
|
b45288f…
|
lmata
|
93 |
var_by_name.setdefault(name, []).append(file_path) |
|
b45288f…
|
lmata
|
94 |
|
|
b45288f…
|
lmata
|
95 |
# Find terraform_output nodes and check if they reference variables |
|
b45288f…
|
lmata
|
96 |
# from other files (outputs often reference var.xxx) |
|
b45288f…
|
lmata
|
97 |
output_result = self.store.query( |
|
b45288f…
|
lmata
|
98 |
"MATCH (o) WHERE o.semantic_type = $out_type RETURN o.name, o.file_path", |
|
b45288f…
|
lmata
|
99 |
{"out_type": "terraform_output"}, |
|
b45288f…
|
lmata
|
100 |
) |
|
b45288f…
|
lmata
|
101 |
output_rows = output_result.result_set or [] |
|
b45288f…
|
lmata
|
102 |
for row in output_rows: |
|
b45288f…
|
lmata
|
103 |
out_name, out_file = row[0], row[1] |
|
b45288f…
|
lmata
|
104 |
if not (out_name and out_file): |
|
b45288f…
|
lmata
|
105 |
continue |
|
b45288f…
|
lmata
|
106 |
|
|
b45288f…
|
lmata
|
107 |
# Check CALLS or REFERENCES edges from this output to variables |
|
b45288f…
|
lmata
|
108 |
ref_result = self.store.query( |
|
b45288f…
|
lmata
|
109 |
"MATCH (o)-[:CALLS]->(target) " |
|
b45288f…
|
lmata
|
110 |
"WHERE o.name = $name AND o.file_path = $path " |
|
b45288f…
|
lmata
|
111 |
"RETURN target.name, target.file_path", |
|
b45288f…
|
lmata
|
112 |
{"name": out_name, "path": out_file}, |
|
b45288f…
|
lmata
|
113 |
) |
|
b45288f…
|
lmata
|
114 |
ref_rows = ref_result.result_set or [] |
|
b45288f…
|
lmata
|
115 |
for ref_row in ref_rows: |
|
b45288f…
|
lmata
|
116 |
target_name, target_path = ref_row[0], ref_row[1] |
|
b45288f…
|
lmata
|
117 |
if target_name and target_path and target_path != out_file: |
|
b45288f…
|
lmata
|
118 |
self._add_semantic_edge(out_name, "REFERENCES", target_name) |
|
b45288f…
|
lmata
|
119 |
edges_added += 1 |
|
b45288f…
|
lmata
|
120 |
|
|
b45288f…
|
lmata
|
121 |
# Also link variables in different files that share the same name |
|
b45288f…
|
lmata
|
122 |
# (e.g. variables.tf defines var, main.tf uses it) |
|
b45288f…
|
lmata
|
123 |
for var_name, paths in var_by_name.items(): |
|
b45288f…
|
lmata
|
124 |
if len(paths) <= 1: |
|
b45288f…
|
lmata
|
125 |
continue |
|
b45288f…
|
lmata
|
126 |
# Find nodes in other files that reference this variable |
|
b45288f…
|
lmata
|
127 |
for path in paths: |
|
b45288f…
|
lmata
|
128 |
ref_result = self.store.query( |
|
b45288f…
|
lmata
|
129 |
"MATCH (n) WHERE n.file_path <> $path " |
|
b45288f…
|
lmata
|
130 |
"AND n.name = $name " |
|
b45288f…
|
lmata
|
131 |
"AND n.semantic_type = $var_type " |
|
b45288f…
|
lmata
|
132 |
"RETURN n.name, n.file_path", |
|
b45288f…
|
lmata
|
133 |
{"path": path, "name": var_name, "var_type": "terraform_variable"}, |
|
b45288f…
|
lmata
|
134 |
) |
|
b45288f…
|
lmata
|
135 |
ref_rows = ref_result.result_set or [] |
|
b45288f…
|
lmata
|
136 |
for ref_row in ref_rows: |
|
b45288f…
|
lmata
|
137 |
ref_name = ref_row[0] |
|
b45288f…
|
lmata
|
138 |
if ref_name: |
|
b45288f…
|
lmata
|
139 |
self._add_semantic_edge(var_name, "REFERENCES", ref_name) |
|
b45288f…
|
lmata
|
140 |
edges_added += 1 |
|
b45288f…
|
lmata
|
141 |
|
|
b45288f…
|
lmata
|
142 |
return edges_added |
|
b45288f…
|
lmata
|
143 |
|
|
b45288f…
|
lmata
|
144 |
def _enrich_module_sources(self) -> int: |
|
b45288f…
|
lmata
|
145 |
"""Find terraform_module nodes with local source paths and create |
|
b45288f…
|
lmata
|
146 |
DEPENDS_ON edges to File nodes in the referenced directory.""" |
|
b45288f…
|
lmata
|
147 |
edges_added = 0 |
|
b45288f…
|
lmata
|
148 |
|
|
b45288f…
|
lmata
|
149 |
# Find Module nodes with terraform_module semantic type |
|
b45288f…
|
lmata
|
150 |
module_result = self.store.query( |
|
b45288f…
|
lmata
|
151 |
"MATCH (m) WHERE m.semantic_type = $mod_type RETURN m.name, m.file_path", |
|
b45288f…
|
lmata
|
152 |
{"mod_type": "terraform_module"}, |
|
b45288f…
|
lmata
|
153 |
) |
|
b45288f…
|
lmata
|
154 |
module_rows = module_result.result_set or [] |
|
b45288f…
|
lmata
|
155 |
|
|
b45288f…
|
lmata
|
156 |
for row in module_rows: |
|
b45288f…
|
lmata
|
157 |
mod_name, mod_file = row[0], row[1] |
|
b45288f…
|
lmata
|
158 |
if not (mod_name and mod_file): |
|
b45288f…
|
lmata
|
159 |
continue |
|
b45288f…
|
lmata
|
160 |
|
|
b45288f…
|
lmata
|
161 |
# Check for CALLS edges that may point to the source path, |
|
b45288f…
|
lmata
|
162 |
# or look for a source property on the node |
|
b45288f…
|
lmata
|
163 |
source_result = self.store.query( |
|
b45288f…
|
lmata
|
164 |
"MATCH (m)-[:CALLS]->(target) " |
|
b45288f…
|
lmata
|
165 |
"WHERE m.name = $name AND m.file_path = $path " |
|
b45288f…
|
lmata
|
166 |
"RETURN target.name, target.file_path", |
|
b45288f…
|
lmata
|
167 |
{"name": mod_name, "path": mod_file}, |
|
b45288f…
|
lmata
|
168 |
) |
|
b45288f…
|
lmata
|
169 |
source_rows = source_result.result_set or [] |
|
b45288f…
|
lmata
|
170 |
|
|
b45288f…
|
lmata
|
171 |
for source_row in source_rows: |
|
b45288f…
|
lmata
|
172 |
target_name, target_path = source_row[0], source_row[1] |
|
b45288f…
|
lmata
|
173 |
if target_name and target_path: |
|
b45288f…
|
lmata
|
174 |
self._add_semantic_edge(mod_name, "DEPENDS_ON", target_name) |
|
b45288f…
|
lmata
|
175 |
edges_added += 1 |
|
b45288f…
|
lmata
|
176 |
continue |
|
b45288f…
|
lmata
|
177 |
|
|
b45288f…
|
lmata
|
178 |
# Fallback: look for File nodes whose path contains the module name |
|
b45288f…
|
lmata
|
179 |
# (local modules are often in ./modules/<name>/) |
|
b45288f…
|
lmata
|
180 |
file_result = self.store.query( |
|
b45288f…
|
lmata
|
181 |
"MATCH (f:File) WHERE f.file_path CONTAINS $fragment RETURN f.name", |
|
b45288f…
|
lmata
|
182 |
{"fragment": f"/modules/{mod_name}/"}, |
|
b45288f…
|
lmata
|
183 |
) |
|
b45288f…
|
lmata
|
184 |
file_rows = file_result.result_set or [] |
|
b45288f…
|
lmata
|
185 |
for file_row in file_rows: |
|
b45288f…
|
lmata
|
186 |
target_name = file_row[0] |
|
b45288f…
|
lmata
|
187 |
if target_name: |
|
b45288f…
|
lmata
|
188 |
self._add_semantic_edge(mod_name, "DEPENDS_ON", target_name) |
|
b45288f…
|
lmata
|
189 |
edges_added += 1 |
|
b45288f…
|
lmata
|
190 |
|
|
b45288f…
|
lmata
|
191 |
return edges_added |
|
b45288f…
|
lmata
|
192 |
|
|
b45288f…
|
lmata
|
193 |
def _enrich_provider_grouping(self) -> int: |
|
b45288f…
|
lmata
|
194 |
"""Group Terraform resources by their provider prefix and create |
|
b45288f…
|
lmata
|
195 |
BELONGS_TO edges from resources to provider nodes.""" |
|
b45288f…
|
lmata
|
196 |
edges_added = 0 |
|
b45288f…
|
lmata
|
197 |
|
|
b45288f…
|
lmata
|
198 |
# Find all terraform_resource nodes |
|
b45288f…
|
lmata
|
199 |
resource_result = self.store.query( |
|
b45288f…
|
lmata
|
200 |
"MATCH (r) WHERE r.semantic_type = $res_type RETURN r.name, r.file_path", |
|
b45288f…
|
lmata
|
201 |
{"res_type": "terraform_resource"}, |
|
b45288f…
|
lmata
|
202 |
) |
|
b45288f…
|
lmata
|
203 |
resource_rows = resource_result.result_set or [] |
|
b45288f…
|
lmata
|
204 |
|
|
b45288f…
|
lmata
|
205 |
for row in resource_rows: |
|
b45288f…
|
lmata
|
206 |
res_name, res_file = row[0], row[1] |
|
b45288f…
|
lmata
|
207 |
if not (res_name and res_file): |
|
b45288f…
|
lmata
|
208 |
continue |
|
b45288f…
|
lmata
|
209 |
|
|
b45288f…
|
lmata
|
210 |
# Match resource name against provider prefixes |
|
b45288f…
|
lmata
|
211 |
for prefix, provider in _PROVIDER_PREFIXES.items(): |
|
b45288f…
|
lmata
|
212 |
if res_name.startswith(prefix): |
|
b45288f…
|
lmata
|
213 |
# Find or reference the provider node |
|
b45288f…
|
lmata
|
214 |
provider_result = self.store.query( |
|
b45288f…
|
lmata
|
215 |
"MATCH (p) WHERE p.name = $provider " |
|
b45288f…
|
lmata
|
216 |
"AND p.semantic_type = $prov_type " |
|
b45288f…
|
lmata
|
217 |
"RETURN p.name", |
|
b45288f…
|
lmata
|
218 |
{"provider": provider, "prov_type": "terraform_provider"}, |
|
b45288f…
|
lmata
|
219 |
) |
|
b45288f…
|
lmata
|
220 |
provider_rows = provider_result.result_set or [] |
|
b45288f…
|
lmata
|
221 |
if provider_rows and provider_rows[0][0]: |
|
b45288f…
|
lmata
|
222 |
self._add_semantic_edge( |
|
b45288f…
|
lmata
|
223 |
res_name, |
|
b45288f…
|
lmata
|
224 |
"BELONGS_TO", |
|
b45288f…
|
lmata
|
225 |
provider, |
|
b45288f…
|
lmata
|
226 |
) |
|
b45288f…
|
lmata
|
227 |
edges_added += 1 |
|
b45288f…
|
lmata
|
228 |
break # Only match the first (most specific) prefix |
|
b45288f…
|
lmata
|
229 |
|
|
b45288f…
|
lmata
|
230 |
return edges_added |