1
1
# This policy uses the Sentinel tfplan/v2 import to validate that no security group
2
- # rules have the have SSH open to CIDR "0.0.0.0/0" for ingress rules. It covers both the
2
+ # rules have the have RDP open to CIDR "0.0.0.0/0" for ingress rules. It covers both the
3
3
# aws_security_group and the aws_security_group_rule resources which can both
4
4
# define rules.
5
5
@@ -40,19 +40,50 @@ for aws_security_group_rules as address, sgr {
40
40
# We check that it is present and really a list
41
41
# before checking whether it contains "0.0.0.0/0"
42
42
if sgr.change.after.cidr_blocks else null is not null and
43
+ types.type_of(sgr.change.after.cidr_blocks) is "list" and
44
+ 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
47
+ sgr.change.after.to_port is forbidden_to_port{
48
+ violatingSGRulesCount += 1
49
+ print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
50
+ "(RDP) open to", forbidden_cidrs, "that is not allowed")
51
+ print(" Ingress Rule has from_port that is null or undefined")
52
+ print(" and Ingress Rule has to_port with value", sgr.change.after.to_port)
53
+ print(" The to_port and from_port both need to be set to an integer",
54
+ "range or of equal")
55
+ print(" value to each other that is either less than or greater than", forbidden_port)
56
+ } else if sgr.change.after.cidr_blocks else null is not null and
43
57
types.type_of(sgr.change.after.cidr_blocks) is "list" and
44
58
sgr.change.after.cidr_blocks contains "0.0.0.0/0" and
45
59
sgr.change.after.from_port else null is not null and
46
- sgr.change.after.from_port <= forbidden_from_port and
60
+ sgr.change.after.from_port is forbidden_from_port and
61
+ sgr.change.after.to_port else null is null{
62
+ violatingSGRulesCount += 1
63
+ print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
64
+ "(RDP) open to", forbidden_cidrs, "that is not allowed")
65
+ print(" Ingress Rule has from_port with value", sgr.change.after.from_port)
66
+ print(" and Ingress Rule has to_port that is null or undefined")
67
+ print(" The to_port and from_port both need to be set to an integer",
68
+ "range or of equal")
69
+ print(" value to each other that is either less than or greater than", forbidden_port)
70
+ } else if sgr.change.after.cidr_blocks else null is not null and
71
+ types.type_of(sgr.change.after.cidr_blocks) is "list" and
72
+ sgr.change.after.cidr_blocks contains "0.0.0.0/0" and
73
+ sgr.change.after.from_port else null is not null and
74
+ sgr.change.after.from_port <= forbidden_from_port and
47
75
sgr.change.after.to_port else null is not null and
48
76
sgr.change.after.to_port >= forbidden_to_port{
49
77
violatingSGRulesCount += 1
50
78
print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
51
79
"(RDP) open to", forbidden_cidrs, "that is not allowed")
52
- print(" Ingress Rule", " has from_port with value", sgr.change.after.from_port,
80
+ print(" Ingress Rule has from_port with value", sgr.change.after.from_port,
53
81
"that is less than or equal to", forbidden_from_port)
54
- print(" and Ingress Rule has to_port with value", sgr.change.after.to_port,
82
+ print(" and Ingress Rule has to_port with value", sgr.change.after.to_port,
55
83
"that is greater than or equal to", forbidden_to_port)
84
+ print(" The to_port and from_port both need to be set to an integer",
85
+ "range or of equal")
86
+ print(" value to each other that is either less than or greater than", forbidden_port)
56
87
} else if sgr.change.after.cidr_blocks else null is not null and
57
88
types.type_of(sgr.change.after.cidr_blocks) is "list" and
58
89
sgr.change.after.cidr_blocks contains "0.0.0.0/0" and
@@ -61,7 +92,10 @@ for aws_security_group_rules as address, sgr {
61
92
violatingSGRulesCount += 1
62
93
print("SG Rule Ingress Violation:", address, "has port", forbidden_port,
63
94
"(RDP) open to", forbidden_cidrs, "that is not allowed")
64
- print(" Ingress Rule", "has to_port with value", sgr.change.after.to_port)
95
+ print(" Ingress Rule has to_port with value", sgr.change.after.to_port)
96
+ print(" The to_port and from_port both need to be set to an integer",
97
+ "range or of equal")
98
+ print(" value to each other that is either less than or greater than", forbidden_port)
65
99
}
66
100
} // end of SG Rules
67
101
@@ -80,11 +114,6 @@ for allSGs as address, sg {
80
114
violatingCidr = plan.filter_attribute_contains_items_from_list(ingressRules,
81
115
"cidr_blocks", forbidden_cidrs, false)
82
116
83
- # Filter to violating Service port
84
- # Warnings will not be printed for violations since the last parameter is false
85
- violatingToPort = plan.filter_attribute_is_value(ingressRules,
86
- "to_port", forbidden_to_port, false)
87
-
88
117
# Filter to violating Service port
89
118
# Warnings will not be printed for violations since the last parameter is false
90
119
violatingFromPortLess = plan.filter_attribute_less_than_equal_to_value(ingressRules,
@@ -104,14 +133,10 @@ for allSGs as address, sg {
104
133
###Uncomment below if you want to show the CIDRs as a separate message as well
105
134
# plan.print_violations(violatingCidr["messages"], " Ingress Rule")
106
135
plan.print_violations(violatingFromPortLess["messages"], " Ingress Rule")
107
- plan.print_violations(violatingToPortGreater["messages"], " and Ingress Rule")
108
- } else if length(violatingCidr["messages"]) > 0 and length(violatingToPort["messages"]) > 0{
109
- violatingSGsCount += 1
110
- print("SG Ingress Violation:", address, "has port", forbidden_port,
111
- "(RDP) open to", forbidden_cidrs, "that is not allowed")
112
- ###Uncomment below if you want to show the CIDRs as a separate message as well
113
- # plan.print_violations(violatingCidr["messages"], " Ingress Rule")
114
- plan.print_violations(violatingToPort["messages"], " Ingress Rule")
136
+ plan.print_violations(violatingToPortGreater["messages"], " and Ingress Rule")
137
+ print(" The to_port and from_port both need to be set to an integer",
138
+ "range or of equal")
139
+ print(" value to each other that is either less than or greater than", forbidden_port)
115
140
}
116
141
} // end for SGs
117
142
0 commit comments