@@ -26,9 +26,10 @@ forbidden_from_port = 3389
26
26
# Get all Security Group Ingress Rules
27
27
aws_security_group_rules = filter tfplan.resource_changes as address, rc {
28
28
rc.type is "aws_security_group_rule" and
29
- rc.mode is "managed" and rc.change.after.type is "ingress" and
29
+ rc.mode is "managed" and
30
30
(rc.change.actions contains "create" or rc.change.actions contains "update" or
31
- rc.change.actions contains "read" or rc.change.actions contains "no-op")
31
+ rc.change.actions contains "read" or rc.change.actions contains "no-op") and
32
+ rc.change.after.type is "ingress"
32
33
}
33
34
34
35
# Validate Security Group Rules
@@ -42,11 +43,11 @@ for aws_security_group_rules as address, sgr {
42
43
if sgr.change.after.cidr_blocks else null is not null and
43
44
types.type_of(sgr.change.after.cidr_blocks) is "list" and
44
45
sgr.change.after.cidr_blocks contains "0.0.0.0/0" and
45
- sgr.change.after.from_port else null is null and
46
- sgr.change.after.to_port else null is not null and
46
+ sgr.change.after.from_port else null is null and
47
+ sgr.change.after.to_port else null is not null and
47
48
sgr.change.after.to_port is forbidden_to_port{
48
49
violatingSGRulesCount += 1
49
- print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
50
+ print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
50
51
"(RDP) open to", forbidden_cidrs, "that is not allowed")
51
52
print(" Ingress Rule has from_port that is null or undefined")
52
53
print(" and Ingress Rule has to_port with value", sgr.change.after.to_port)
@@ -57,10 +58,10 @@ for aws_security_group_rules as address, sgr {
57
58
types.type_of(sgr.change.after.cidr_blocks) is "list" and
58
59
sgr.change.after.cidr_blocks contains "0.0.0.0/0" and
59
60
sgr.change.after.from_port else null is not null and
60
- sgr.change.after.from_port is forbidden_from_port and
61
+ sgr.change.after.from_port is forbidden_from_port and
61
62
sgr.change.after.to_port else null is null{
62
63
violatingSGRulesCount += 1
63
- print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
64
+ print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
64
65
"(RDP) open to", forbidden_cidrs, "that is not allowed")
65
66
print(" Ingress Rule has from_port with value", sgr.change.after.from_port)
66
67
print(" and Ingress Rule has to_port that is null or undefined")
@@ -71,15 +72,15 @@ for aws_security_group_rules as address, sgr {
71
72
types.type_of(sgr.change.after.cidr_blocks) is "list" and
72
73
sgr.change.after.cidr_blocks contains "0.0.0.0/0" and
73
74
sgr.change.after.from_port else null is not null and
74
- sgr.change.after.from_port <= forbidden_from_port and
75
+ sgr.change.after.from_port <= forbidden_from_port and
75
76
sgr.change.after.to_port else null is not null and
76
77
sgr.change.after.to_port >= forbidden_to_port{
77
78
violatingSGRulesCount += 1
78
- print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
79
+ print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
79
80
"(RDP) open to", forbidden_cidrs, "that is not allowed")
80
- print(" Ingress Rule has from_port with value", sgr.change.after.from_port,
81
+ print(" Ingress Rule has from_port with value", sgr.change.after.from_port,
81
82
"that is less than or equal to", forbidden_from_port)
82
- print(" and Ingress Rule has to_port with value", sgr.change.after.to_port,
83
+ print(" and Ingress Rule has to_port with value", sgr.change.after.to_port,
83
84
"that is greater than or equal to", forbidden_to_port)
84
85
print(" The to_port and from_port both need to be set to an integer",
85
86
"range or of equal")
@@ -90,7 +91,7 @@ for aws_security_group_rules as address, sgr {
90
91
sgr.change.after.to_port else null is not null and
91
92
sgr.change.after.to_port is forbidden_port{
92
93
violatingSGRulesCount += 1
93
- print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
94
+ print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
94
95
"(RDP) open to", forbidden_cidrs, "that is not allowed")
95
96
print(" Ingress Rule has to_port with value", sgr.change.after.to_port)
96
97
print(" The to_port and from_port both need to be set to an integer",
@@ -108,27 +109,27 @@ for allSGs as address, sg {
108
109
109
110
# Find the ingress rules of the current SG
110
111
ingressRules = plan.find_blocks(sg, "ingress")
111
-
112
+
112
113
# Filter to violating CIDR blocks
113
114
# Warnings will not be printed for violations since the last parameter is false
114
115
violatingCidr = plan.filter_attribute_contains_items_from_list(ingressRules,
115
116
"cidr_blocks", forbidden_cidrs, false)
116
-
117
+
117
118
# Filter to violating Service port
118
119
# Warnings will not be printed for violations since the last parameter is false
119
120
violatingFromPortLess = plan.filter_attribute_less_than_equal_to_value(ingressRules,
120
121
"from_port", forbidden_from_port, false)
121
-
122
+
122
123
# Filter to violating Service port
123
124
# Warnings will not be printed for violations since the last parameter is false
124
125
violatingToPortGreater = plan.filter_attribute_greater_than_equal_to_value(ingressRules,
125
126
"to_port", forbidden_to_port, false)
126
-
127
+
127
128
# Print violation messages
128
- if length(violatingCidr["messages"]) > 0 and length(violatingFromPortLess["messages"]) > 0 and
129
+ if length(violatingCidr["messages"]) > 0 and length(violatingFromPortLess["messages"]) > 0 and
129
130
length(violatingToPortGreater["messages"]) > 0{
130
131
violatingSGsCount += 1
131
- print("SG Ingress Violation:", address, "has port", forbidden_port,
132
+ print("SG Ingress Violation:", address, "has port", forbidden_port,
132
133
"(RDP) open to", forbidden_cidrs, "that is not allowed")
133
134
###Uncomment below if you want to show the CIDRs as a separate message as well
134
135
# plan.print_violations(violatingCidr["messages"], " Ingress Rule")
@@ -143,4 +144,4 @@ for allSGs as address, sg {
143
144
validated = violatingSGsCount is 0 and violatingSGRulesCount is 0
144
145
main = rule {
145
146
validated is true
146
- }
147
+ }
0 commit comments