|
| 1 | +package main |
| 2 | + |
| 3 | +import rego.v1 |
| 4 | + |
| 5 | +test_iam_policy_document_denied if { |
| 6 | + result := deny_iam_policy_document with input as {"data": {"aws_iam_policy_document": {"my_policy": [{}]}}} |
| 7 | + count(result) == 1 |
| 8 | +} |
| 9 | + |
| 10 | +test_no_iam_policy_document_allowed if { |
| 11 | + result := deny_iam_policy_document with input as {"data": {}} |
| 12 | + count(result) == 0 |
| 13 | +} |
| 14 | + |
| 15 | +test_other_data_source_allowed if { |
| 16 | + result := deny_iam_policy_document with input as {"data": {"aws_caller_identity": {"current": [{}]}}} |
| 17 | + count(result) == 0 |
| 18 | +} |
| 19 | + |
| 20 | +test_heredoc_iam_policy_denied if { |
| 21 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_policy": {"bad": [{"name": "test", "policy": "{\n \"Version\": \"2012-10-17\"\n}"}]}}} |
| 22 | + count(result) == 1 |
| 23 | +} |
| 24 | + |
| 25 | +test_heredoc_iam_role_policy_denied if { |
| 26 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_role_policy": {"bad": [{"name": "test", "role": "r", "policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 27 | + count(result) == 1 |
| 28 | +} |
| 29 | + |
| 30 | +test_heredoc_assume_role_policy_denied if { |
| 31 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_role": {"bad": [{"name": "test", "assume_role_policy": "{\n \"Version\": \"2012-10-17\"\n}"}]}}} |
| 32 | + count(result) == 1 |
| 33 | +} |
| 34 | + |
| 35 | +test_heredoc_s3_bucket_policy_denied if { |
| 36 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_s3_bucket_policy": {"bad": [{"bucket": "b", "policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 37 | + count(result) == 1 |
| 38 | +} |
| 39 | + |
| 40 | +test_heredoc_sns_topic_policy_denied if { |
| 41 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_sns_topic_policy": {"bad": [{"arn": "a", "policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 42 | + count(result) == 1 |
| 43 | +} |
| 44 | + |
| 45 | +test_heredoc_sqs_queue_policy_denied if { |
| 46 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_sqs_queue_policy": {"bad": [{"queue_url": "q", "policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 47 | + count(result) == 1 |
| 48 | +} |
| 49 | + |
| 50 | +test_heredoc_kms_key_denied if { |
| 51 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_kms_key": {"bad": [{"policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 52 | + count(result) == 1 |
| 53 | +} |
| 54 | + |
| 55 | +test_heredoc_ecr_repository_policy_denied if { |
| 56 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_ecr_repository_policy": {"bad": [{"repository": "r", "policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 57 | + count(result) == 1 |
| 58 | +} |
| 59 | + |
| 60 | +test_heredoc_opensearch_domain_denied if { |
| 61 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_opensearch_domain": {"bad": [{"domain_name": "d", "access_policies": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 62 | + count(result) == 1 |
| 63 | +} |
| 64 | + |
| 65 | +test_heredoc_cloudwatch_log_resource_policy_denied if { |
| 66 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_cloudwatch_log_resource_policy": {"bad": [{"policy_name": "p", "policy_document": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 67 | + count(result) == 1 |
| 68 | +} |
| 69 | + |
| 70 | +test_heredoc_glacier_vault_denied if { |
| 71 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_glacier_vault": {"bad": [{"name": "v", "access_policy": "{ \"Version\": \"2012-10-17\" }"}]}}} |
| 72 | + count(result) == 1 |
| 73 | +} |
| 74 | + |
| 75 | +test_jsonencode_iam_policy_allowed if { |
| 76 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_policy": {"good": [{"name": "test", "policy": "${jsonencode({\n Version = \"2012-10-17\"\n })}"}]}}} |
| 77 | + count(result) == 0 |
| 78 | +} |
| 79 | + |
| 80 | +test_jsonencode_assume_role_allowed if { |
| 81 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_role": {"good": [{"name": "test", "assume_role_policy": "${jsonencode({\n Version = \"2012-10-17\"\n })}"}]}}} |
| 82 | + count(result) == 0 |
| 83 | +} |
| 84 | + |
| 85 | +test_jsonencode_s3_bucket_policy_allowed if { |
| 86 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_s3_bucket_policy": {"good": [{"bucket": "b", "policy": "${jsonencode({\n Version = \"2012-10-17\"\n })}"}]}}} |
| 87 | + count(result) == 0 |
| 88 | +} |
| 89 | + |
| 90 | +test_jsonencode_kms_key_allowed if { |
| 91 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_kms_key": {"good": [{"policy": "${jsonencode({\n Version = \"2012-10-17\"\n })}"}]}}} |
| 92 | + count(result) == 0 |
| 93 | +} |
| 94 | + |
| 95 | +test_unrelated_resource_ignored if { |
| 96 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_instance": {"test": [{"ami": "ami-123"}]}}} |
| 97 | + count(result) == 0 |
| 98 | +} |
| 99 | + |
| 100 | +test_missing_field_ignored if { |
| 101 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_policy": {"test": [{"name": "test"}]}}} |
| 102 | + count(result) == 0 |
| 103 | +} |
| 104 | + |
| 105 | +test_non_string_value_ignored if { |
| 106 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_policy": {"test": [{"name": "test", "policy": 42}]}}} |
| 107 | + count(result) == 0 |
| 108 | +} |
| 109 | + |
| 110 | +test_empty_resource_block_ignored if { |
| 111 | + result := deny_heredoc_json_policy with input as {"resource": {"aws_iam_policy": {"test": [{}]}}} |
| 112 | + count(result) == 0 |
| 113 | +} |
| 114 | + |
| 115 | +test_multiple_violations if { |
| 116 | + result := deny_heredoc_json_policy with input as {"resource": { |
| 117 | + "aws_iam_policy": {"a": [{"policy": "{ \"Version\": \"2012-10-17\" }"}]}, |
| 118 | + "aws_s3_bucket_policy": {"b": [{"policy": "{ \"Version\": \"2012-10-17\" }"}]}, |
| 119 | + }} |
| 120 | + count(result) == 2 |
| 121 | +} |
0 commit comments