Skip to content

Commit

Permalink
feat(arm): add CKV_AZURE_206 to ensure that Storage Accounts use repl…
Browse files Browse the repository at this point in the history
…ication (#6524)

added a new arm policy for resource: StorageAccountsUseReplication

Co-authored-by: AdamDev <[email protected]>
  • Loading branch information
tehila86127 and AdamDev authored Jul 9, 2024
1 parent 65cd750 commit 4bff87b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
24 changes: 24 additions & 0 deletions checkov/arm/checks/resource/StorageAccountsUseReplication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from checkov.common.models.enums import CheckCategories
from checkov.arm.base_resource_value_check import BaseResourceValueCheck
from typing import Any, List


class StorageAccountsUseReplication(BaseResourceValueCheck):
def __init__(self) -> None:
name = "Ensure that Storage Accounts use replication"
id = "CKV_AZURE_206"
supported_resources = ("Microsoft.Storage/storageAccounts",)
categories = (CheckCategories.BACKUP_AND_RECOVERY,)
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources,)

def get_inspected_key(self) -> str:
return "sku/name"

def get_expected_value(self) -> Any:
return "Standard_GRS"

def get_expected_values(self) -> List[Any]:
return ["Standard_GRS", "Standard_RAGRS", "Standard_GZRS", "Standard_RAGZRS"]


check = StorageAccountsUseReplication()
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.13.1.58284",
"templateHash": "13120038605368246703"
}
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "The storage account location."
}
},
"storageAccountName": {
"type": "string",
"defaultValue": "[format('store{0}', uniqueString(resourceGroup().id))]",
"metadata": {
"description": "The name of the storage account"
}
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "fail",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {}
}
],
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[parameters('storageAccountName')]"
},
"storageAccountId": {
"type": "string",
"value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
}
}
}
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",
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.13.1.58284",
"templateHash": "13120038605368246703"
}
},
"parameters": {
"location": {
"type": "string",
"defaultValue": "[resourceGroup().location]",
"metadata": {
"description": "The storage account location."
}
},
"storageAccountName": {
"type": "string",
"defaultValue": "[format('store{0}', uniqueString(resourceGroup().id))]",
"metadata": {
"description": "The name of the storage account"
}
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "pass",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_GRS"
},
"kind": "StorageV2",
"properties": {}
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2022-09-01",
"name": "pass2",
"location": "[parameters('location')]",
"sku": {
"name": "Standard_RAGRS"
},
"kind": "StorageV2",
"properties": {}
}
],
"outputs": {
"storageAccountName": {
"type": "string",
"value": "[parameters('storageAccountName')]"
},
"storageAccountId": {
"type": "string",
"value": "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
}
}
}
41 changes: 41 additions & 0 deletions tests/arm/checks/resource/test_StorageAccountsUseReplication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import unittest
from pathlib import Path

from checkov.runner_filter import RunnerFilter
from checkov.arm.checks.resource.StorageAccountsUseReplication import check
from checkov.arm.runner import Runner


class TestStorageAccountsUseReplication(unittest.TestCase):
def test(self):
# given
test_files_dir = Path(__file__).parent / "example_StorageAccountsUseReplication"

# when
report = Runner().run(root_folder=str(test_files_dir), runner_filter=RunnerFilter(checks=[check.id]))

# then
summary = report.get_summary()

passing_resources = {
"Microsoft.Storage/storageAccounts.pass",
"Microsoft.Storage/storageAccounts.pass2",
}
failing_resources = {
"Microsoft.Storage/storageAccounts.fail",
}

passed_check_resources = {c.resource for c in report.passed_checks}
failed_check_resources = {c.resource for c in report.failed_checks}

self.assertEqual(summary["passed"], len(passing_resources))
self.assertEqual(summary["failed"], len(failing_resources))
self.assertEqual(summary["skipped"], 0)
self.assertEqual(summary["parsing_errors"], 0)

self.assertEqual(passing_resources, passed_check_resources)
self.assertEqual(failing_resources, failed_check_resources)


if __name__ == "__main__":
unittest.main()

0 comments on commit 4bff87b

Please sign in to comment.