|
| 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 | +} |
0 commit comments