Skip to content

Commit

Permalink
implicit dependencies edges and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omriyoffe-panw committed Oct 22, 2024
1 parent ca6eda4 commit 030c333
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 22 deletions.
18 changes: 8 additions & 10 deletions checkov/arm/graph_builder/local_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,27 +131,25 @@ def _create_edge(self, element_name: str, origin_vertex_index: int, label: str)
@staticmethod
def _extract_resource_name_from_resource_id_func(resource_id: str) -> str:
# Extract name from resourceId function
return resource_id.split(',')[-1].split(')')[0]
return ArmLocalGraph._clean_string(resource_id.split(',')[-1].split(')')[0])

@staticmethod
def _extract_resource_name_from_reference_func(reference: str) -> str:
resource_name = "".join(reference.split('reference(', 1)[1].split(')')[:-1])
resource_name = ''.join(reference.split('reference(', 1)[1].split(')')[:-1])
if 'resourceId' in resource_name:
return "".join(resource_name.split('resourceId(', 1)[1].split(')')[:-1]).split(',')[-1]
return ArmLocalGraph._clean_string(''.join(resource_name.split('resourceId(', 1)[1].split(')')[0]).split(',')[-1])
else:
return resource_name.split(',')[0]
return ArmLocalGraph._clean_string(resource_name.split(',')[0].split('/')[-1])

@staticmethod
def _clean_string(input: str) -> str:
return input.replace("'", '').replace(" ", "")

def _create_implicit_edges(self, origin_vertex_index: int, resource_name: str, d: dict[str, Any]) -> None:
for key, value in d.items():
if isinstance(value, str):
if 'reference' in value:
self._create_implicit_edge(origin_vertex_index, resource_name, value)
elif isinstance(value, list):
for item in value:
if isinstance(item, str) and 'reference' in item:
self._create_implicit_edge(origin_vertex_index, resource_name, item)
elif isinstance(value, dict):
self._create_implicit_edges(origin_vertex_index, resource_name, value)

def _create_implicit_edge(self, origin_vertex_index: int, resource_name: str, reference_string: str) -> None:
dep_name = ArmLocalGraph._extract_resource_name_from_reference_func(reference_string)
Expand Down
37 changes: 37 additions & 0 deletions tests/arm/examples/ImplicitDepsResources/interface.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/publicIPAddresses",
"apiVersion": "2020-06-01",
"name": "PublicIP1",
"location": "westus",
"properties": {
"publicIPAllocationMethod": "Dynamic"
}
},
{
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2020-06-01",
"name": "NIC1",
"location": "westus",
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[reference(resourceId('Microsoft.Network/virtualNetworks', 'myVNet')).subnets[0].id]"
},
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[reference(resourceId('Microsoft.Network/publicIPAddresses', 'PublicIP1')).id]"
}
}
}
]
}
}
]
}
61 changes: 61 additions & 0 deletions tests/arm/examples/ImplicitDepsResources/storage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.KeyVault/vaults",
"apiVersion": "2019-09-01",
"name": "myKeyVault",
"location": "westus",
"properties": {
"sku": {
"family": "A",
"name": "standard"
},
"tenantId": "tenantId",
"accessPolicies": []
},
"resources": [
{
"type": "secrets",
"name": "MySecret",
"apiVersion": "2019-09-01",
"properties": {
"value": "MySecretValue123"
}
}
]
},
{
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2019-07-01",
"name": "myVM",
"location": "westus",
"properties": {
"hardwareProfile": {
"vmSize": "Standard_DS1_v2"
},
"osProfile": {
"computerName": "myVM",
"adminUsername": "adminuser",
"customData": "[reference('Microsoft.KeyVault/vaults/myKeyVault', '2019-09-01').secrets['MySecret'].value]"
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', 'myNIC')]"
}
]
},
"storageProfile": {
"osDisk": {
"createOption": "FromImage",
"managedDisk": {
"storageAccountType": "Premium_LRS"
}
}
}
}
}
]
}
34 changes: 34 additions & 0 deletions tests/arm/examples/ImplicitDepsResources/subnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-06-01",
"name": "myNSG",
"location": "westus"
},
{
"type": "Microsoft.Network/virtualNetworks",
"apiVersion": "2020-06-01",
"name": "myVNet",
"location": "westus",
"properties": {
"addressSpace": {
"addressPrefixes": ["10.0.0.0/16"]
},
"subnets": [
{
"name": "mySubnet",
"properties": {
"addressPrefix": "10.0.1.0/24",
"networkSecurityGroup": {
"id": "[reference('myNSG', '2020-06-01').id]"
}
}
}
]
}
}
]
}
35 changes: 23 additions & 12 deletions tests/arm/graph_builder/test_local_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,34 @@

EXAMPLES_DIR = Path(__file__).parent.parent / "examples"
EXPLICIT_DEPS_DIR = EXAMPLES_DIR / "ExplicitDepsResources"
IMPLICIT_DEPS_DIR = EXAMPLES_DIR / "ImplicitDepsResources"

def test_build_graph():
# given
test_files = [EXPLICIT_DEPS_DIR / "interface.json",
EXPLICIT_DEPS_DIR / "storage.json",
EXPLICIT_DEPS_DIR / "subnet.json"]

def test_graph_explicit_deps():
test_files = [str(EXPLICIT_DEPS_DIR / "subnet.json"),
str(EXPLICIT_DEPS_DIR / "storage.json"),
str(EXPLICIT_DEPS_DIR / "interface.json")]
definitions, _, _ = get_files_definitions(test_files)
test_graph = ArmLocalGraph(definitions)
test_graph.build_graph()

local_graph = ArmLocalGraph(definitions=definitions)
assert len(test_graph.vertices) == 6
assert len(test_graph.edges) == 5

assert len(test_graph.vertices_by_block_type[BlockType.RESOURCE]) == 6


def test_graph_implicit_deps():
test_files = [str(IMPLICIT_DEPS_DIR / "subnet.json"),
str(IMPLICIT_DEPS_DIR / "storage.json"),
str(IMPLICIT_DEPS_DIR / "interface.json")]
definitions, _, _ = get_files_definitions(test_files)
test_graph = ArmLocalGraph(definitions)
test_graph.build_graph()

# when
local_graph.build_graph(render_variables=False)
assert len(test_graph.vertices) == 6
assert len(test_graph.edges) == 4

# then
assert len(local_graph.vertices) == 6
assert len(local_graph.edges) == 5
assert len(test_graph.vertices_by_block_type[BlockType.RESOURCE]) == 6

assert len(local_graph.vertices_by_block_type[BlockType.RESOURCE]) == 6
# TODO: add tests with parameters and variables vertices and rendering

0 comments on commit 030c333

Please sign in to comment.