You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's a pseudo-code explanation of what this code does:
113
+
114
+
Initialize a set called resource_types with the names of resource types that you want to manage, such as "aws_autoscaling_group," "aws_instance," "aws_iam," and "aws_launch_configuration."
115
+
116
+
Initialize an empty dictionary called resources.
117
+
118
+
Iterate over each resource_type in the resource_types set:
119
+
120
+
a. For each resource_type, create a sub-set of resources called all.
121
+
122
+
b. In this sub-set, filter out resource names (name) from a source called tfplan.resource_changes.
123
+
124
+
c. Only include name if it has a type matching the current resource_type.
125
+
126
+
d. Store this filtered list of resource names as the value for the current resource_type in the resources dictionary.
127
+
```
128
+
129
+
## Sample 1
130
+
### Code
131
+
```
132
+
# Define the rule to find the type of bucket in the Terraform plan
133
+
find_bucket_type[resource_type] {
134
+
# Iterate over each change in the Terraform plan
135
+
change := input.resource_changes[_]
136
+
137
+
# Check if the change is related to an AWS S3 bucket
138
+
change.type == "aws_s3_bucket"
139
+
140
+
# Extract the resource type (in this case, it's "aws_s3_bucket")
141
+
resource_type := change.type
142
+
}
143
+
```
144
+
### Meaing
145
+
```In this Rego policy:
146
+
147
+
The package is named find_bucket_type, and it contains a rule named find_bucket_type.
148
+
149
+
The rule iterates through each change in the Terraform plan using _ as the iterator variable.
150
+
151
+
It checks if the change.type is equal to "aws_s3_bucket". This condition identifies changes related to AWS S3 buckets.
152
+
153
+
If the condition is met, it sets resource_type to "aws_s3_bucket".
154
+
155
+
Now, you can use this Rego policy to find AWS S3 bucket changes in a Terraform JSON plan by passing the plan as input to the Rego policy evaluator. The find_bucket_type rule will return the resource type when it encounters an AWS S3 bucket resource in the plan.
156
+
157
+
Please note that this Rego policy assumes that the Terraform JSON plan follows the typical structure where each resource change has a type field indicating the resource type. You may need to adapt the policy to your specific Terraform plan format if it differs.```
158
+
159
+
## Sample 2
160
+
### Code
161
+
```
162
+
package find_public_s3_buckets
163
+
164
+
# Define the rule to find AWS S3 buckets with PublicAccess
165
+
public_s3_bucket[resource_name] {
166
+
# Iterate through each change in the Terraform plan
167
+
change := input.resource_changes[_]
168
+
169
+
# Check if the change is related to an AWS S3 bucket
170
+
change.type == "aws_s3_bucket"
171
+
172
+
# Extract the resource name (the AWS S3 bucket name)
173
+
resource_name := change.name
174
+
175
+
# Check if the access_type is PublicAccess
176
+
change.change.after.access_type == "PublicAccess"
177
+
}
178
+
```
179
+
180
+
### Explain
181
+
1. The package is named find_public_s3_buckets, and it contains a rule named public_s3_bucket.
182
+
183
+
2. The rule iterates through each change in the Terraform plan using _ as the iterator variable.
184
+
185
+
3. It checks if the change.type is equal to "aws_s3_bucket". This condition identifies changes related to AWS S3 buckets.
186
+
187
+
4. If the condition is met, it extracts the AWS S3 bucket's name as resource_name.
188
+
189
+
5. It further checks if the access_type in the change.change.after field is equal to "PublicAccess". This condition identifies AWS S3 buckets with public access.
190
+
191
+
Now, you can use this modified Rego policy to find AWS S3 buckets with public access in a Terraform JSON plan by passing the plan as input to the Rego policy evaluator. The public_s3_bucket rule will return the names of the AWS S3 buckets with public access when it encounters such resources in the plan.
192
+
193
+
Please make sure that the structure of your Terraform JSON plan includes the access_type field as indicated in the policy for this to work correctly.
0 commit comments