-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arm): add variable and parameters edges and rendering (#6787)
* vars and parameters edges * vars and parameters edges * add tests * add tests * add tests * lint * lint * lint * lint * lint * lint * . * . * .
- Loading branch information
1 parent
1da7e33
commit e2fa39d
Showing
13 changed files
with
235 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
|
||
from checkov.arm.graph_builder.graph_components.block_types import BlockType | ||
from checkov.common.graph.graph_builder import Edge | ||
from checkov.common.graph.graph_builder.utils import adjust_value | ||
from checkov.common.graph.graph_builder.variable_rendering.renderer import VariableRenderer | ||
from checkov.common.util.data_structures_utils import pickle_deepcopy | ||
|
||
if TYPE_CHECKING: | ||
from checkov.arm.graph_builder.local_graph import ArmLocalGraph | ||
|
||
|
||
class ArmVariableRenderer(VariableRenderer["ArmLocalGraph"]): | ||
def __init__(self, local_graph: ArmLocalGraph) -> None: | ||
super().__init__(local_graph) | ||
|
||
def _render_variables_from_vertices(self) -> None: | ||
# need to add rendering to function like format, reference etc | ||
pass | ||
|
||
def evaluate_vertex_attribute_from_edge(self, edge_list: list[Edge]) -> None: | ||
origin_vertex_attributes = self.local_graph.vertices[edge_list[0].origin].attributes | ||
val_to_eval = pickle_deepcopy(origin_vertex_attributes.get(edge_list[0].label, "")) | ||
attr_path = None | ||
for edge in edge_list: | ||
attr_path, attr_value = self.extract_dest_attribute_path_and_value(dest_index=edge.dest, | ||
origin_value=val_to_eval) | ||
'''if the arg start with '[parameters'/ '[variables' its mean we need to eval the all attribute | ||
like here - "addressPrefix": "[parameters('subnetAddressPrefix')]" ''' | ||
if len(edge_list) == 1 and val_to_eval.startswith(("[parameters", "[variables")): | ||
val_to_eval = attr_value | ||
continue | ||
''' | ||
if the value i need to eval is part of the full attribute like "[format('{0}/{1}', parameters('vnetName'), variables('subnetName'))]" | ||
or "[resourceId('Microsoft.Network/networkProfiles', variables('networkProfileName'))]". | ||
vertices[edge.dest].id = variables.networkProfileName -> variables('networkProfileName') | ||
''' | ||
val_to_replace = self.local_graph.vertices[edge.dest].id.replace(".", "('") + "')" | ||
val_to_eval = val_to_eval.replace(val_to_replace, attr_value) | ||
|
||
self.local_graph.update_vertex_attribute( | ||
vertex_index=edge_list[0].origin, | ||
attribute_key=edge_list[0].label, | ||
attribute_value=val_to_eval, | ||
change_origin_id=edge_list[0].dest, | ||
attribute_at_dest=attr_path, | ||
) | ||
|
||
def extract_dest_attribute_path_and_value(self, dest_index: int, origin_value: Any) -> tuple[str, Any] | tuple[None, None]: | ||
vertex = self.local_graph.vertices[dest_index] | ||
if vertex.block_type == BlockType.PARAMETER: | ||
new_value = vertex.attributes.get("defaultValue") | ||
if new_value: | ||
new_value = adjust_value(element_name=origin_value, value=new_value) | ||
return "defaultValue", new_value | ||
elif vertex.block_type == BlockType.VARIABLE: | ||
new_value = adjust_value(element_name=origin_value, value=vertex.attributes["value"]) | ||
return "value", new_value | ||
return None, None | ||
|
||
def evaluate_non_rendered_values(self) -> None: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", | ||
"contentVersion": "1.0.0.0", | ||
"metadata": { | ||
"_generator": { | ||
"name": "bicep", | ||
"version": "0.5.6.12127", | ||
"templateHash": "9027093124117826122" | ||
} | ||
}, | ||
"parameters": { | ||
"vnetName": { | ||
"type": "string", | ||
"defaultValue": "aci-vnet", | ||
"metadata": { | ||
"description": "VNet name" | ||
} | ||
}, | ||
"vnetAddressPrefix": { | ||
"type": "string", | ||
"defaultValue": "10.0.0.0/16", | ||
"metadata": { | ||
"description": "Address prefix" | ||
} | ||
} | ||
}, | ||
"variables": { | ||
"networkProfileName": "aci-networkProfile", | ||
"location": "eth0" | ||
|
||
}, | ||
"resources": [ | ||
{ | ||
"type": "Microsoft.Network/virtualNetworks", | ||
"apiVersion": "2020-11-01", | ||
"name": "[format('{0}/{1}', parameters('vnetName'), variables('networkProfileName'))]", | ||
"location": "[variables('location')]", | ||
"id": "[resourceId('Microsoft.Network/networkProfiles', variables('networkProfileName'))]", | ||
"properties": { | ||
"addressSpace": { | ||
"addressPrefixes": [ | ||
"[parameters('vnetAddressPrefix')]" | ||
] | ||
} | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from pathlib import Path | ||
|
||
from checkov.arm.graph_builder.local_graph import ArmLocalGraph | ||
from checkov.arm.utils import get_files_definitions | ||
|
||
EXAMPLES_DIR = Path(__file__).parent | ||
|
||
def test_rander_vars(): | ||
# given | ||
test_file = EXAMPLES_DIR / "test_rendering.json" | ||
definitions, _, _ = get_files_definitions([str(test_file)]) | ||
local_graph = ArmLocalGraph(definitions=definitions) | ||
# when | ||
local_graph.build_graph(render_variables=True) | ||
|
||
# then | ||
assert len(local_graph.vertices) == 5 | ||
assert len(local_graph.edges) == 5 | ||
assert local_graph.vertices[2].attributes['name'] == "[format('{0}/{1}', aci-vnet, aci-networkProfile)]" | ||
assert local_graph.vertices[2].attributes['id'] == "[resourceId('Microsoft.Network/networkProfiles', aci-networkProfile)]" | ||
assert local_graph.vertices[2].attributes['location'] == "eth0" | ||
assert local_graph.vertices[2].attributes['properties.addressSpace.addressPrefixes.0'] == "10.0.0.0/16" | ||
assert local_graph.vertices[2].attributes['properties']['addressSpace']['addressPrefixes'][0] == "10.0.0.0/16" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters