Skip to content

Commit 1f84cbc

Browse files
committed
resolved comment
1 parent e98b861 commit 1f84cbc

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import "tfconfig/v2" as tfconfig
16+
import "tfplan/v2" as tfplan
17+
import "tfresources" as tf
18+
import "report" as report
19+
import "collection" as collection
20+
import "collection/maps" as maps
21+
22+
# Constants
23+
24+
const = {
25+
"resource_efs_file_system": "aws_efs_file_system",
26+
"policy_name": "efs-encryption-at-rest-enabled",
27+
"kms_key_id": "kms_key_id",
28+
"constant_value": "constant_value",
29+
"encrypted": "encrypted",
30+
"encrypted_attr_violation_msg": "Attribute 'encrypted' should be true for 'aws_efs_file_system' resources. Refer to https://docs.aws.amazon.com/securityhub/latest/userguide/efs-controls.html#efs-1 for more details.",
31+
"kms_key_id_attr_violation_msg": "Attribute 'kms_key_id' should be non empty for 'aws_efs_file_system' resources. Refer to https://docs.aws.amazon.com/securityhub/latest/userguide/efs-controls.html#efs-1 for more details.",
32+
}
33+
34+
# Functions
35+
36+
build_violation_object = func(res, message) {
37+
return {
38+
"address": res.address,
39+
"module_address": res.module_address,
40+
"message": message,
41+
}
42+
}
43+
44+
# Variables
45+
46+
efs_file_systems_from_plan = tf.plan(tfplan.planned_values.resources).type(const.resource_efs_file_system).resources
47+
48+
# Filter out aws_efs_file_systems that have invalid 'encrypted' attribute
49+
non_encrypted_file_systems = collection.reject(efs_file_systems_from_plan, func(res) {
50+
encrypted_val = maps.get(res, "values.encrypted", false)
51+
return encrypted_val is true
52+
})
53+
54+
non_encrypted_file_systems_violations = map non_encrypted_file_systems as _, res {
55+
build_violation_object(res, const.encrypted_attr_violation_msg)
56+
}
57+
58+
efs_file_systems_from_configs = tf.config(tfconfig.resources).type(const.resource_efs_file_system).resources
59+
60+
# Filter out aws_efs_file_systems that have empty 'kms_key_id' attribute
61+
efs_resources_with_empty_kms_key_ids = collection.reject(efs_file_systems_from_configs, func(res) {
62+
key_path = "config.kms_key_id"
63+
return maps.get(res, key_path, false) is not false and
64+
maps.get(res, key_path + "." + const.constant_value, false) is not ""
65+
})
66+
67+
efs_resources_with_empty_kms_key_ids_violations = map efs_resources_with_empty_kms_key_ids as _, res {
68+
build_violation_object(res, const.kms_key_id_attr_violation_msg)
69+
}
70+
71+
summary = {
72+
"policy_name": const.policy_name,
73+
"violations": non_encrypted_file_systems_violations + efs_resources_with_empty_kms_key_ids_violations,
74+
}
75+
76+
# Outputs
77+
78+
print(report.generate_policy_report(summary))
79+
80+
# Rules
81+
82+
verify_non_encrypted_file_systems = rule {
83+
non_encrypted_file_systems_violations is empty
84+
}
85+
86+
verify_kms_key_referencing_file_systems = rule {
87+
efs_resources_with_empty_kms_key_ids_violations is empty
88+
}
89+
90+
main = rule {
91+
verify_non_encrypted_file_systems and verify_kms_key_referencing_file_systems
92+
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import "tfconfig/v2" as tfconfig
2+
import "tfplan/v2" as tfplan
3+
import "tfresources" as tf
4+
import "report" as report
5+
import "collection" as collection
6+
import "collection/maps" as maps
7+
8+
# Constants
9+
10+
const = {
11+
"resource_efs_file_system": "aws_efs_file_system",
12+
"policy_name": "efs-encryption-at-rest-enabled",
13+
"kms_key_id": "kms_key_id",
14+
"constant_value": "constant_value",
15+
"encrypted": "encrypted",
16+
"encrypted_attr_violation_msg": "Attribute 'encrypted' should be true for 'aws_efs_file_system' resources. Refer to https://docs.aws.amazon.com/securityhub/latest/userguide/efs-controls.html#efs-1 for more details.",
17+
"kms_key_id_attr_violation_msg": "Attribute 'kms_key_id' should be non empty for 'aws_efs_file_system' resources. Refer to https://docs.aws.amazon.com/securityhub/latest/userguide/efs-controls.html#efs-1 for more details.",
18+
}
19+
20+
# Functions
21+
22+
build_violation_object = func(res, message) {
23+
return {
24+
"address": res.address,
25+
"module_address": res.module_address,
26+
"message": message,
27+
}
28+
}
29+
30+
# Variables
31+
32+
efs_file_systems_from_plan = tf.plan(tfplan.planned_values.resources).type(const.resource_efs_file_system).resources
33+
34+
# Filter out aws_efs_file_systems that have invalid 'encrypted' attribute
35+
non_encrypted_file_systems = collection.reject(efs_file_systems_from_plan, func(res) {
36+
encrypted_val = maps.get(res, "values.encrypted", false)
37+
return encrypted_val is true
38+
})
39+
40+
non_encrypted_file_systems_violations = map non_encrypted_file_systems as _, res {
41+
build_violation_object(res, const.encrypted_attr_violation_msg)
42+
}
43+
44+
efs_file_systems_from_configs = tf.config(tfconfig.resources).type(const.resource_efs_file_system).resources
45+
46+
# Filter out aws_efs_file_systems that have empty 'kms_key_id' attribute
47+
efs_resources_with_empty_kms_key_ids = collection.reject(efs_file_systems_from_configs, func(res) {
48+
key_path = "config.kms_key_id"
49+
return maps.get(res, key_path, false) is not false and
50+
maps.get(res, key_path + "." + const.constant_value, false) is not ""
51+
})
52+
53+
efs_resources_with_empty_kms_key_ids_violations = map efs_resources_with_empty_kms_key_ids as _, res {
54+
build_violation_object(res, const.kms_key_id_attr_violation_msg)
55+
}
56+
57+
summary = {
58+
"policy_name": const.policy_name,
59+
"violations": non_encrypted_file_systems_violations + efs_resources_with_empty_kms_key_ids_violations,
60+
}
61+
62+
# Outputs
63+
64+
print(report.generate_policy_report(summary))
65+
66+
# Rules
67+
68+
verify_non_encrypted_file_systems = rule {
69+
non_encrypted_file_systems_violations is empty
70+
}
71+
72+
verify_kms_key_referencing_file_systems = rule {
73+
efs_resources_with_empty_kms_key_ids_violations is empty
74+
}
75+
76+
main = rule {
77+
verify_non_encrypted_file_systems and verify_kms_key_referencing_file_systems
78+
}

0 commit comments

Comments
 (0)