Skip to content

Commit b9fe96f

Browse files
authored
common sentinel policy
1 parent b12e6a8 commit b9fe96f

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

sentinel-samples

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,70 @@ main = rule {
5757
}
5858

5959

60-
##############
60+
#####################################
61+
Policy to allow certain regions
62+
#####################################
63+
64+
# This policy restricts the AWS region based on the region set for
65+
# instances of the AWS provider in the root module of the workspace.
66+
# It does not check providers in nested modules.
67+
68+
import "tfconfig"
69+
import "tfplan"
70+
import "strings"
71+
72+
# Initialize array of regions found in AWS providers
73+
region_values = []
74+
75+
# Allowed Regions
76+
allowed_regions = [
77+
"us-east-1",
78+
"ap-south-1",
79+
"us-west-1",
80+
"us-west-2",
81+
]
82+
83+
84+
# Iterate through all AWS providers in root module
85+
if ((length(tfconfig.providers) else 0) > 0) {
86+
providers = tfconfig.providers
87+
if "aws" in keys(providers) {
88+
aws = tfconfig.providers.aws
89+
aliases = aws["alias"]
90+
for aliases as alias, data {
91+
print ( "alias is: ", alias )
92+
region = data["config"]["region"]
93+
if region matches "\\$\\{var\\.(.*)\\}" {
94+
# AWS provider was configured with variable
95+
print ( "region is a variable" )
96+
region_variable = strings.trim_suffix(strings.trim_prefix(region, "${var."), "}")
97+
print ( "region variable is: ", region_variable )
98+
print ( "Value of region is: ", tfplan.variables[region_variable] )
99+
region_value = tfplan.variables[region_variable]
100+
region_values += [region_value]
101+
} else {
102+
print ( "region is a hard-coded value" )
103+
print ( "Value of region is: ", region )
104+
region_value = region
105+
region_values += [region_value]
106+
}
107+
}
108+
}
109+
}
110+
111+
# Print all regions found in AWS providers
112+
print ( "region_values is: ", region_values )
113+
114+
aws_region_valid = rule {
115+
all region_values as rv {
116+
rv in allowed_regions
117+
}
118+
}
119+
120+
main = rule {
121+
(aws_region_valid) else true
122+
}
123+
124+
125+
##################################
61126

0 commit comments

Comments
 (0)