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

AZURE TF Check - CKV_AZURE_136 #2331

Merged
merged 6 commits into from
Feb 4, 2022
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from checkov.common.models.enums import CheckResult, CheckCategories
from checkov.terraform.checks.resource.base_resource_value_check import BaseResourceValueCheck


class PostgreSQLFlexiServerGeoBackupEnabled(BaseResourceValueCheck):
def __init__(self):
name = "Ensure that PostgreSQL Flexible server enables geo-redundant backups"
id = "CKV_AZURE_136"
supported_resources = ['azurerm_postgresql_flexible_server']
categories = [CheckCategories.BACKUP_AND_RECOVERY]
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources)

def get_inspected_key(self):
return 'geo_redundant_backup_enabled'


check = PostgreSQLFlexiServerGeoBackupEnabled()
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# pass

resource "azurerm_postgresql_flexible_server" "pass" {
name = "example-psqlflexibleserver"
resource_group_name = "azurerm_resource_group.example.name"
location = "azurerm_resource_group.example.location"
version = "12"
delegated_subnet_id = "azurerm_subnet.example.id"
private_dns_zone_id = "azurerm_private_dns_zone.example.id"
administrator_login = "psqladmin"
administrator_password = "H@Sh1CoR3!"
zone = "1"

storage_mb = 32768
geo_redundant_backup_enabled = true

sku_name = "GP_Standard_D4s_v3"
depends_on = ["azurerm_private_dns_zone_virtual_network_link.example"]

}

# fail

resource "azurerm_postgresql_flexible_server" "fail1" {
name = "example-psqlflexibleserver"
resource_group_name = "azurerm_resource_group.example.name"
location = "azurerm_resource_group.example.location"
version = "12"
delegated_subnet_id = "azurerm_subnet.example.id"
private_dns_zone_id = "azurerm_private_dns_zone.example.id"
administrator_login = "psqladmin"
administrator_password = "H@Sh1CoR3!"
zone = "1"

storage_mb = 32768
geo_redundant_backup_enabled = false

sku_name = "GP_Standard_D4s_v3"
depends_on = ["azurerm_private_dns_zone_virtual_network_link.example"]

}

resource "azurerm_postgresql_flexible_server" "fail2" {
name = "example-psqlflexibleserver"
resource_group_name = "azurerm_resource_group.example.name"
location = "azurerm_resource_group.example.location"
version = "12"
delegated_subnet_id = "azurerm_subnet.example.id"
private_dns_zone_id = "azurerm_private_dns_zone.example.id"
administrator_login = "psqladmin"
administrator_password = "H@Sh1CoR3!"
zone = "1"

storage_mb = 32768

sku_name = "GP_Standard_D4s_v3"
depends_on = ["azurerm_private_dns_zone_virtual_network_link.example"]

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unittest
from pathlib import Path

from checkov.runner_filter import RunnerFilter
from checkov.terraform.checks.resource.azure.PostgreSQLFlexiServerGeoBackupEnabled import check
from checkov.terraform.runner import Runner


class TestPostgreSQLFlexiServerGeoBackupEnabled(unittest.TestCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please adjust the tests, so they scan real terraform code, like here #2304


def test(self):
# given
test_files_dir = Path(__file__).parent / "example_PostgreSQLFlexiServerGeoBackupEnabled"

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

# then
summary = report.get_summary()

passing_resources = {
"azurerm_postgresql_flexible_server.pass",
}
failing_resources = {
"azurerm_postgresql_flexible_server.fail1",
"azurerm_postgresql_flexible_server.fail2",

}

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"], 1)
self.assertEqual(summary["failed"], 2)
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()