Skip to content

Commit

Permalink
feat(terraform): add check Neptune DB clusters should be configured t…
Browse files Browse the repository at this point in the history
…o copy tags to snapshots (#5552)
  • Loading branch information
omryMen committed Sep 11, 2023
1 parent d9f1391 commit d3e293b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from checkov.common.models.enums import CheckCategories
from checkov.terraform.checks.resource.base_resource_value_check import BaseResourceValueCheck


class NeptuneDBClustersCopyTagsToSnapshots(BaseResourceValueCheck):
def __init__(self):
description = "Neptune DB clusters should be configured to copy tags to snapshots"
id = "CKV_AWS_362"
supported_resources = ['aws_neptune_cluster']
categories = [CheckCategories.BACKUP_AND_RECOVERY]
super().__init__(name=description, id=id, categories=categories, supported_resources=supported_resources)

def get_inspected_key(self):
return "copy_tags_to_snapshot"


check = NeptuneDBClustersCopyTagsToSnapshots()
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## SHOULD PASS: has copy_tags_to_snapshot = true
resource "aws_neptune_cluster" "ckv_unittest_pass" {
copy_tags_to_snapshot = true
}

## SHOULD FAIL: doens't have copy_tags_to_snapshot
resource "aws_neptune_cluster" "ckv_unittest_fail" {
copy_tags_to_snapshot = false
}

## SHOULD FAIL: have copy_tags_to_snapshot = false
resource "aws_neptune_cluster" "ckv_unittest2_fail" {
## Your test here
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import unittest

from checkov.runner_filter import RunnerFilter
from checkov.terraform.runner import Runner
from checkov.terraform.checks.resource.aws.NeptuneDBClustersCopyTagsToSnapshots import check


class TestNeptuneDBClustersCopyTagsToSnapshots(unittest.TestCase):

def test(self):
runner = Runner()
current_dir = os.path.dirname(os.path.realpath(__file__))

test_files_dir = os.path.join(current_dir, "example_NeptuneDBClustersCopyTagsToSnapshots")
report = runner.run(root_folder=test_files_dir,
runner_filter=RunnerFilter(checks=[check.id]))
summary = report.get_summary()

passing_resources = {
'aws_neptune_cluster.ckv_unittest_pass'
}
failing_resources = {
'aws_neptune_cluster.ckv_unittest_fail',
'aws_neptune_cluster.ckv_unittest2_fail',
}
skipped_resources = {}

passed_check_resources = set([c.resource for c in report.passed_checks])
failed_check_resources = set([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'], len(skipped_resources))
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 d3e293b

Please sign in to comment.