Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arm): implement CKV_AZURE_100 for arm #5490

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions checkov/arm/checks/resource/CosmosDBHaveCMK.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from checkov.common.models.consts import ANY_VALUE
from checkov.common.models.enums import CheckCategories
from checkov.arm.base_resource_value_check import BaseResourceValueCheck


class CosmosDBHaveCMK(BaseResourceValueCheck):
def __init__(self):
name = "Ensure that Cosmos DB accounts have customer-managed keys to encrypt data at rest"
id = "CKV_AZURE_100"
supported_resources = ['Microsoft.DocumentDb/databaseAccounts']
categories = [CheckCategories.NETWORKING]
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources)

def get_inspected_key(self):
return 'properties/keyVaultKeyUri'

def get_expected_value(self):
return ANY_VALUE


check = CosmosDBHaveCMK()
65 changes: 65 additions & 0 deletions tests/arm/checks/resource/example_CosmosDBHaveCMK/fail.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"locationName": {
"type": "string"
},
"defaultExperience": {
"type": "string"
},
"isZoneRedundant": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"apiVersion": "2023-03-15-preview",
"kind": "GlobalDocumentDB",
"type": "Microsoft.DocumentDb/databaseAccounts",
"name": "fail",
"location": "[parameters('location')]",
"properties": {
"databaseAccountOfferType": "Standard",
"locations": [
{
"id": "[concat(parameters('name'), '-', parameters('location'))]",
"failoverPriority": 0,
"locationName": "[parameters('locationName')]"
}
],
"backupPolicy": {
"type": "Periodic",
"periodicModeProperties": {
"backupIntervalInMinutes": 240,
"backupRetentionIntervalInHours": 8,
"backupStorageRedundancy": "Geo"
}
},
"isVirtualNetworkFilterEnabled": false,
"virtualNetworkRules": [],
"ipRules": [],
"dependsOn": [],
"minimalTlsVersion": "Tls12",
"enableMultipleWriteLocations": false,
"capabilities": [],
"enableFreeTier": true,
"capacity": {
"totalThroughputLimit": 1000
}
},
"tags": {
"defaultExperience": "[parameters('defaultExperience')]",
"hidden-cosmos-mmspecial": ""
}
}
],
"outputs": {}
}
66 changes: 66 additions & 0 deletions tests/arm/checks/resource/example_CosmosDBHaveCMK/pass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"name": {
"type": "string"
},
"location": {
"type": "string"
},
"locationName": {
"type": "string"
},
"defaultExperience": {
"type": "string"
},
"isZoneRedundant": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"apiVersion": "2023-03-15-preview",
"kind": "GlobalDocumentDB",
"type": "Microsoft.DocumentDb/databaseAccounts",
"name": "pass",
"location": "[parameters('location')]",
"properties": {
"databaseAccountOfferType": "Standard",
"locations": [
{
"id": "[concat(parameters('name'), '-', parameters('location'))]",
"failoverPriority": 0,
"locationName": "[parameters('locationName')]"
}
],
"backupPolicy": {
"type": "Periodic",
"periodicModeProperties": {
"backupIntervalInMinutes": 240,
"backupRetentionIntervalInHours": 8,
"backupStorageRedundancy": "Geo"
}
},
"isVirtualNetworkFilterEnabled": false,
"virtualNetworkRules": [],
"ipRules": [],
"dependsOn": [],
"minimalTlsVersion": "Tls12",
"enableMultipleWriteLocations": false,
"capabilities": [],
"enableFreeTier": true,
"capacity": {
"totalThroughputLimit": 1000
},
"keyVaultKeyUri": "https://examplekeyvaultjgw.vault.azure.net/keys/temp"
},
"tags": {
"defaultExperience": "[parameters('defaultExperience')]",
"hidden-cosmos-mmspecial": ""
}
}
],
"outputs": {}
}
40 changes: 40 additions & 0 deletions tests/arm/checks/resource/test_CosmosDBHaveCMK.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from pathlib import Path

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


class TestCosmosDBHaveCMK(unittest.TestCase):
def test_summary(self):
# given
test_files_dir = Path(__file__).parent / "example_CosmosDBHaveCMK"

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

# then
summary = report.get_summary()

passing_resources = {
"Microsoft.DocumentDb/databaseAccounts.pass",
}
failing_resources = {
"Microsoft.DocumentDb/databaseAccounts.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()
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

resource "azurerm_cosmosdb_account" "fail" {
name = "tfex-cosmos-db-${random_integer.ri.result}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
offer_type = "Standard"
kind = "GlobalDocumentDB"

enable_automatic_failover = true

capabilities {
name = "EnableAggregationPipeline"
}

capabilities {
name = "mongoEnableDocLevelTTL"
}

capabilities {
name = "MongoDBv3.4"
}

consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 10
max_staleness_prefix = 200
}

geo_location {
location = var.failover_location
failover_priority = 1
}

geo_location {
location = azurerm_resource_group.rg.location
failover_priority = 0
}
}

resource "azurerm_cosmosdb_account" "pass" {
name = "tfex-cosmos-db-${random_integer.ri.result}"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
offer_type = "Standard"
kind = "GlobalDocumentDB"

enable_automatic_failover = true

capabilities {
name = "EnableAggregationPipeline"
}

capabilities {
name = "mongoEnableDocLevelTTL"
}

capabilities {
name = "MongoDBv3.4"
}

consistency_policy {
consistency_level = "BoundedStaleness"
max_interval_in_seconds = 10
max_staleness_prefix = 200
}

geo_location {
location = var.failover_location
failover_priority = 1
}

geo_location {
location = azurerm_resource_group.rg.location
failover_priority = 0
}

key_vault_key_id = "A versionless Key Vault Key ID for CMK encryption"
}
Loading