Navegador

Source Blame History 203 lines
b45288f… lmata 1 """
b45288f… lmata 2 Chef framework enricher.
b45288f… lmata 3
b45288f… lmata 4 Promotes generic graph nodes created by the Ruby parser to Chef-specific
b45288f… lmata 5 semantic types:
b45288f… lmata 6 - chef_recipe — files under recipes/
b45288f… lmata 7 - chef_cookbook — metadata.rb files under cookbooks/
b45288f… lmata 8 - chef_resource — functions/methods in recipes/ or libraries/ matching
b45288f… lmata 9 Chef resource names (package, template, service, etc.)
b45288f… lmata 10 - include_recipe — DEPENDS_ON edges for cross-recipe includes
b45288f… lmata 11 """
b45288f… lmata 12
b45288f… lmata 13 from navegador.enrichment.base import EnrichmentResult, FrameworkEnricher
b45288f… lmata 14 from navegador.graph.store import GraphStore
b45288f… lmata 15
b45288f… lmata 16 # Built-in Chef resource types that appear as method calls in recipes
b45288f… lmata 17 _CHEF_RESOURCES = frozenset(
b45288f… lmata 18 {
b45288f… lmata 19 "package",
b45288f… lmata 20 "template",
b45288f… lmata 21 "service",
b45288f… lmata 22 "execute",
b45288f… lmata 23 "file",
b45288f… lmata 24 "directory",
b45288f… lmata 25 "cookbook_file",
b45288f… lmata 26 "remote_file",
b45288f… lmata 27 "cron",
b45288f… lmata 28 "user",
b45288f… lmata 29 "group",
b45288f… lmata 30 "mount",
b45288f… lmata 31 "link",
b45288f… lmata 32 "bash",
b45288f… lmata 33 "ruby_block",
b45288f… lmata 34 "apt_package",
b45288f… lmata 35 "yum_package",
b45288f… lmata 36 "powershell_script",
b45288f… lmata 37 "windows_service",
b45288f… lmata 38 "chef_gem",
b45288f… lmata 39 "log",
b45288f… lmata 40 "http_request",
b45288f… lmata 41 "remote_directory",
b45288f… lmata 42 }
b45288f… lmata 43 )
b45288f… lmata 44
b45288f… lmata 45
b45288f… lmata 46 class ChefEnricher(FrameworkEnricher):
b45288f… lmata 47 """Enriches a navegador graph with Chef-specific semantic types."""
b45288f… lmata 48
b45288f… lmata 49 def __init__(self, store: GraphStore) -> None:
b45288f… lmata 50 super().__init__(store)
b45288f… lmata 51
b45288f… lmata 52 # ── Identity ──────────────────────────────────────────────────────────────
b45288f… lmata 53
b45288f… lmata 54 @property
b45288f… lmata 55 def framework_name(self) -> str:
b45288f… lmata 56 return "chef"
b45288f… lmata 57
b45288f… lmata 58 @property
b45288f… lmata 59 def detection_patterns(self) -> list[str]:
b45288f… lmata 60 return ["chef"]
b45288f… lmata 61
b45288f… lmata 62 @property
b45288f… lmata 63 def detection_files(self) -> list[str]:
b45288f… lmata 64 return ["metadata.rb", "Berksfile"]
b45288f… lmata 65
b45288f… lmata 66 # ── Enrichment ────────────────────────────────────────────────────────────
b45288f… lmata 67
b45288f… lmata 68 def enrich(self) -> EnrichmentResult:
b45288f… lmata 69 result = EnrichmentResult()
b45288f… lmata 70
b45288f… lmata 71 recipes = self._enrich_recipes()
b45288f… lmata 72 result.promoted += recipes
b45288f… lmata 73 result.patterns_found["recipes"] = recipes
b45288f… lmata 74
b45288f… lmata 75 cookbooks = self._enrich_cookbooks()
b45288f… lmata 76 result.promoted += cookbooks
b45288f… lmata 77 result.patterns_found["cookbooks"] = cookbooks
b45288f… lmata 78
b45288f… lmata 79 resources = self._enrich_resources()
b45288f… lmata 80 result.promoted += resources
b45288f… lmata 81 result.patterns_found["resources"] = resources
b45288f… lmata 82
b45288f… lmata 83 includes = self._enrich_include_recipe()
b45288f… lmata 84 result.edges_added += includes
b45288f… lmata 85 result.patterns_found["include_recipe"] = includes
b45288f… lmata 86
b45288f… lmata 87 return result
b45288f… lmata 88
b45288f… lmata 89 # ── Pattern helpers ───────────────────────────────────────────────────────
b45288f… lmata 90
b45288f… lmata 91 def _enrich_recipes(self) -> int:
b45288f… lmata 92 """Promote File nodes under /recipes/ to chef_recipe."""
b45288f… lmata 93 promoted = 0
b45288f… lmata 94 query_result = self.store.query(
b45288f… lmata 95 "MATCH (n:File) WHERE n.file_path CONTAINS $pattern RETURN n.name, n.file_path",
b45288f… lmata 96 {"pattern": "/recipes/"},
b45288f… lmata 97 )
b45288f… lmata 98 rows = query_result.result_set or []
b45288f… lmata 99 for row in rows:
b45288f… lmata 100 name, file_path = row[0], row[1]
b45288f… lmata 101 if name and file_path:
b45288f… lmata 102 self._promote_node(name, file_path, "chef_recipe")
b45288f… lmata 103 promoted += 1
b45288f… lmata 104 return promoted
b45288f… lmata 105
b45288f… lmata 106 def _enrich_cookbooks(self) -> int:
b45288f… lmata 107 """Promote metadata.rb File nodes under /cookbooks/ to chef_cookbook."""
b45288f… lmata 108 promoted = 0
b45288f… lmata 109 query_result = self.store.query(
b45288f… lmata 110 "MATCH (n:File) WHERE n.file_path CONTAINS $cookbooks "
b45288f… lmata 111 "AND n.name = $name "
b45288f… lmata 112 "RETURN n.name, n.file_path",
b45288f… lmata 113 {"cookbooks": "/cookbooks/", "name": "metadata.rb"},
b45288f… lmata 114 )
b45288f… lmata 115 rows = query_result.result_set or []
b45288f… lmata 116 for row in rows:
b45288f… lmata 117 name, file_path = row[0], row[1]
b45288f… lmata 118 if name and file_path:
b45288f… lmata 119 self._promote_node(name, file_path, "chef_cookbook")
b45288f… lmata 120 promoted += 1
b45288f… lmata 121 return promoted
b45288f… lmata 122
b45288f… lmata 123 def _enrich_resources(self) -> int:
b45288f… lmata 124 """Promote Function/Method nodes in recipes/ or libraries/ whose names
b45288f… lmata 125 match Chef built-in resource types."""
b45288f… lmata 126 promoted = 0
b45288f… lmata 127 for path_fragment in ("/recipes/", "/libraries/"):
b45288f… lmata 128 query_result = self.store.query(
b45288f… lmata 129 "MATCH (n) WHERE (n:Function OR n:Method) "
b45288f… lmata 130 "AND n.file_path CONTAINS $pattern "
b45288f… lmata 131 "RETURN n.name, n.file_path",
b45288f… lmata 132 {"pattern": path_fragment},
b45288f… lmata 133 )
b45288f… lmata 134 rows = query_result.result_set or []
b45288f… lmata 135 for row in rows:
b45288f… lmata 136 name, file_path = row[0], row[1]
b45288f… lmata 137 if name and file_path and name in _CHEF_RESOURCES:
b45288f… lmata 138 self._promote_node(name, file_path, "chef_resource")
b45288f… lmata 139 promoted += 1
b45288f… lmata 140 return promoted
b45288f… lmata 141
b45288f… lmata 142 def _enrich_include_recipe(self) -> int:
b45288f… lmata 143 """Link include_recipe calls to the referenced recipe File nodes.
b45288f… lmata 144
b45288f… lmata 145 Looks for Function nodes named ``include_recipe`` and follows CALLS
b45288f… lmata 146 edges or checks node properties to find the recipe name argument,
b45288f… lmata 147 then creates a DEPENDS_ON edge to the matching recipe File node.
b45288f… lmata 148 """
b45288f… lmata 149 edges_added = 0
b45288f… lmata 150
b45288f… lmata 151 # Strategy 1: follow CALLS edges from include_recipe nodes
b45288f… lmata 152 query_result = self.store.query(
b45288f… lmata 153 "MATCH (n:Function)-[:CALLS]->(target) "
b45288f… lmata 154 "WHERE n.name = $name "
b45288f… lmata 155 "RETURN n.file_path, target.name",
b45288f… lmata 156 {"name": "include_recipe"},
b45288f… lmata 157 )
b45288f… lmata 158 rows = query_result.result_set or []
b45288f… lmata 159 for row in rows:
b45288f… lmata 160 caller_path, recipe_ref = row[0], row[1]
b45288f… lmata 161 if caller_path and recipe_ref:
b45288f… lmata 162 # recipe_ref may be "cookbook::recipe" — extract recipe name
b45288f… lmata 163 recipe_name = recipe_ref.split("::")[-1] if "::" in recipe_ref else recipe_ref
b45288f… lmata 164 # Find the recipe File node
b45288f… lmata 165 match_result = self.store.query(
b45288f… lmata 166 "MATCH (f:File) WHERE f.file_path CONTAINS $recipes "
b45288f… lmata 167 "AND f.name CONTAINS $recipe "
b45288f… lmata 168 "RETURN f.name",
b45288f… lmata 169 {"recipes": "/recipes/", "recipe": recipe_name},
b45288f… lmata 170 )
b45288f… lmata 171 match_rows = match_result.result_set or []
b45288f… lmata 172 if match_rows and match_rows[0][0]:
b45288f… lmata 173 # Create DEPENDS_ON from the caller's file to the recipe file
b45288f… lmata 174 caller_file_result = self.store.query(
b45288f… lmata 175 "MATCH (f:File) WHERE f.file_path = $path RETURN f.name",
b45288f… lmata 176 {"path": caller_path},
b45288f… lmata 177 )
b45288f… lmata 178 caller_rows = caller_file_result.result_set or []
b45288f… lmata 179 if caller_rows and caller_rows[0][0]:
b45288f… lmata 180 self._add_semantic_edge(
b45288f… lmata 181 caller_rows[0][0],
b45288f… lmata 182 "DEPENDS_ON",
b45288f… lmata 183 match_rows[0][0],
b45288f… lmata 184 )
b45288f… lmata 185 edges_added += 1
b45288f… lmata 186
b45288f… lmata 187 # Strategy 2: check signature/docstring for include_recipe calls
b45288f… lmata 188 for prop in ("signature", "docstring"):
b45288f… lmata 189 query_result = self.store.query(
b45288f… lmata 190 f"MATCH (n) WHERE (n:Function OR n:Method) "
b45288f… lmata 191 f"AND n.{prop} IS NOT NULL "
b45288f… lmata 192 f"AND n.{prop} CONTAINS $pattern "
b45288f… lmata 193 "RETURN n.name, n.file_path",
b45288f… lmata 194 {"pattern": "include_recipe"},
b45288f… lmata 195 )
b45288f… lmata 196 rows = query_result.result_set or []
b45288f… lmata 197 for row in rows:
b45288f… lmata 198 name, file_path = row[0], row[1]
b45288f… lmata 199 if name and file_path and name == "include_recipe":
b45288f… lmata 200 # Already handled in strategy 1 via CALLS edges
b45288f… lmata 201 continue
b45288f… lmata 202
b45288f… lmata 203 return edges_added

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button