@@ -3,6 +3,7 @@ package aws
3
3
import (
4
4
"context"
5
5
"encoding/json"
6
+ "errors"
6
7
"fmt"
7
8
"regexp"
8
9
"slices"
@@ -17,11 +18,16 @@ func DataAwsCrossaccountPolicy() common.Resource {
17
18
PassRole []string `json:"pass_roles,omitempty"`
18
19
JSON string `json:"json" tf:"computed"`
19
20
AwsAccountId string `json:"aws_account_id,omitempty"`
21
+ AwsPartition string `json:"aws_partition,omitempty" tf:"default:aws"`
20
22
VpcId string `json:"vpc_id,omitempty"`
21
23
Region string `json:"region,omitempty"`
22
24
SecurityGroupId string `json:"security_group_id,omitempty"`
23
25
}
24
26
return common .NoClientData (func (ctx context.Context , data * AwsCrossAccountPolicy ) error {
27
+ if ! slices .Contains (AwsPartitions , data .AwsPartition ) {
28
+ return errors .New (AwsPartitionsValidationError )
29
+ }
30
+
25
31
if ! slices .Contains ([]string {"managed" , "customer" , "restricted" }, data .PolicyType ) {
26
32
return fmt .Errorf ("policy_type must be either 'managed', 'customer' or 'restricted'" )
27
33
}
@@ -145,7 +151,7 @@ func DataAwsCrossaccountPolicy() common.Resource {
145
151
"iam:CreateServiceLinkedRole" ,
146
152
"iam:PutRolePolicy" ,
147
153
},
148
- Resources : "arn:aws :iam::*:role/aws-service-role/spot.amazonaws.com/AWSServiceRoleForEC2Spot" ,
154
+ Resources : fmt . Sprintf ( "arn:%s :iam::*:role/aws-service-role/spot.amazonaws.com/AWSServiceRoleForEC2Spot" , data . AwsPartition ) ,
149
155
Condition : map [string ]map [string ]string {
150
156
"StringLike" : {
151
157
"iam:AWSServiceName" : "spot.amazonaws.com" ,
@@ -168,6 +174,7 @@ func DataAwsCrossaccountPolicy() common.Resource {
168
174
if data .PolicyType == "restricted" {
169
175
region := data .Region
170
176
aws_account_id := data .AwsAccountId
177
+ awsPartition := data .AwsPartition
171
178
vpc_id := data .VpcId
172
179
security_group_id := data .SecurityGroupId
173
180
policy .Statements = append (policy .Statements ,
@@ -179,7 +186,7 @@ func DataAwsCrossaccountPolicy() common.Resource {
179
186
"ec2:DisassociateIamInstanceProfile" ,
180
187
"ec2:ReplaceIamInstanceProfileAssociation" ,
181
188
},
182
- Resources : fmt .Sprintf ("arn:aws :ec2:%s:%s:instance/*" , region , aws_account_id ),
189
+ Resources : fmt .Sprintf ("arn:%s :ec2:%s:%s:instance/*" , awsPartition , region , aws_account_id ),
183
190
Condition : map [string ]map [string ]string {
184
191
"StringEquals" : {
185
192
"ec2:ResourceTag/Vendor" : "Databricks" ,
@@ -191,8 +198,8 @@ func DataAwsCrossaccountPolicy() common.Resource {
191
198
Effect : "Allow" ,
192
199
Actions : "ec2:RunInstances" ,
193
200
Resources : []string {
194
- fmt .Sprintf ("arn:aws :ec2:%s:%s:volume/*" , region , aws_account_id ),
195
- fmt .Sprintf ("arn:aws :ec2:%s:%s:instance/*" , region , aws_account_id ),
201
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:volume/*" , awsPartition , region , aws_account_id ),
202
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:instance/*" , awsPartition , region , aws_account_id ),
196
203
},
197
204
Condition : map [string ]map [string ]string {
198
205
"StringEquals" : {
@@ -204,7 +211,7 @@ func DataAwsCrossaccountPolicy() common.Resource {
204
211
Sid : "AllowEc2RunInstanceImagePerTag" ,
205
212
Effect : "Allow" ,
206
213
Actions : "ec2:RunInstances" ,
207
- Resources : fmt .Sprintf ("arn:aws :ec2:%s:%s:image/*" , region , aws_account_id ),
214
+ Resources : fmt .Sprintf ("arn:%s :ec2:%s:%s:image/*" , awsPartition , region , aws_account_id ),
208
215
Condition : map [string ]map [string ]string {
209
216
"StringEquals" : {
210
217
"aws:ResourceTag/Vendor" : "Databricks" ,
@@ -216,13 +223,13 @@ func DataAwsCrossaccountPolicy() common.Resource {
216
223
Effect : "Allow" ,
217
224
Actions : "ec2:RunInstances" ,
218
225
Resources : []string {
219
- fmt .Sprintf ("arn:aws :ec2:%s:%s:network-interface/*" , region , aws_account_id ),
220
- fmt .Sprintf ("arn:aws :ec2:%s:%s:subnet/*" , region , aws_account_id ),
221
- fmt .Sprintf ("arn:aws :ec2:%s:%s:security-group/*" , region , aws_account_id ),
226
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:network-interface/*" , awsPartition , region , aws_account_id ),
227
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:subnet/*" , awsPartition , region , aws_account_id ),
228
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:security-group/*" , awsPartition , region , aws_account_id ),
222
229
},
223
230
Condition : map [string ]map [string ]string {
224
231
"StringEquals" : {
225
- "ec2:vpc" : fmt .Sprintf ("arn:aws :ec2:%s:%s:vpc/%s" , region , aws_account_id , vpc_id ),
232
+ "ec2:vpc" : fmt .Sprintf ("arn:%s :ec2:%s:%s:vpc/%s" , awsPartition , region , aws_account_id , vpc_id ),
226
233
},
227
234
},
228
235
},
@@ -231,19 +238,19 @@ func DataAwsCrossaccountPolicy() common.Resource {
231
238
Effect : "Allow" ,
232
239
Actions : "ec2:RunInstances" ,
233
240
NotResources : []string {
234
- fmt .Sprintf ("arn:aws :ec2:%s:%s:image/*" , region , aws_account_id ),
235
- fmt .Sprintf ("arn:aws :ec2:%s:%s:network-interface/*" , region , aws_account_id ),
236
- fmt .Sprintf ("arn:aws :ec2:%s:%s:subnet/*" , region , aws_account_id ),
237
- fmt .Sprintf ("arn:aws :ec2:%s:%s:security-group/*" , region , aws_account_id ),
238
- fmt .Sprintf ("arn:aws :ec2:%s:%s:volume/*" , region , aws_account_id ),
239
- fmt .Sprintf ("arn:aws :ec2:%s:%s:instance/*" , region , aws_account_id ),
241
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:image/*" , awsPartition , region , aws_account_id ),
242
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:network-interface/*" , awsPartition , region , aws_account_id ),
243
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:subnet/*" , awsPartition , region , aws_account_id ),
244
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:security-group/*" , awsPartition , region , aws_account_id ),
245
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:volume/*" , awsPartition , region , aws_account_id ),
246
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:instance/*" , awsPartition , region , aws_account_id ),
240
247
},
241
248
},
242
249
& awsIamPolicyStatement {
243
250
Sid : "EC2TerminateInstancesTag" ,
244
251
Effect : "Allow" ,
245
252
Actions : "ec2:TerminateInstances" ,
246
- Resources : fmt .Sprintf ("arn:aws :ec2:%s:%s:instance/*" , region , aws_account_id ),
253
+ Resources : fmt .Sprintf ("arn:%s :ec2:%s:%s:instance/*" , awsPartition , region , aws_account_id ),
247
254
Condition : map [string ]map [string ]string {
248
255
"StringEquals" : {
249
256
"ec2:ResourceTag/Vendor" : "Databricks" ,
@@ -258,8 +265,8 @@ func DataAwsCrossaccountPolicy() common.Resource {
258
265
"ec2:DetachVolume" ,
259
266
},
260
267
Resources : []string {
261
- fmt .Sprintf ("arn:aws :ec2:%s:%s:instance/*" , region , aws_account_id ),
262
- fmt .Sprintf ("arn:aws :ec2:%s:%s:volume/*" , region , aws_account_id ),
268
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:instance/*" , awsPartition , region , aws_account_id ),
269
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:volume/*" , awsPartition , region , aws_account_id ),
263
270
},
264
271
Condition : map [string ]map [string ]string {
265
272
"StringEquals" : {
@@ -271,7 +278,7 @@ func DataAwsCrossaccountPolicy() common.Resource {
271
278
Sid : "EC2CreateVolumeByTag" ,
272
279
Effect : "Allow" ,
273
280
Actions : "ec2:CreateVolume" ,
274
- Resources : fmt .Sprintf ("arn:aws :ec2:%s:%s:volume/*" , region , aws_account_id ),
281
+ Resources : fmt .Sprintf ("arn:%s :ec2:%s:%s:volume/*" , awsPartition , region , aws_account_id ),
275
282
Condition : map [string ]map [string ]string {
276
283
"StringEquals" : {
277
284
"aws:RequestTag/Vendor" : "Databricks" ,
@@ -283,7 +290,7 @@ func DataAwsCrossaccountPolicy() common.Resource {
283
290
Effect : "Allow" ,
284
291
Actions : "ec2:DeleteVolume" ,
285
292
Resources : []string {
286
- fmt .Sprintf ("arn:aws :ec2:%s:%s:volume/*" , region , aws_account_id ),
293
+ fmt .Sprintf ("arn:%s :ec2:%s:%s:volume/*" , awsPartition , region , aws_account_id ),
287
294
},
288
295
Condition : map [string ]map [string ]string {
289
296
"StringEquals" : {
@@ -300,10 +307,10 @@ func DataAwsCrossaccountPolicy() common.Resource {
300
307
"ec2:RevokeSecurityGroupEgress" ,
301
308
"ec2:RevokeSecurityGroupIngress" ,
302
309
},
303
- Resources : fmt .Sprintf ("arn:aws :ec2:%s:%s:security-group/%s" , region , aws_account_id , security_group_id ),
310
+ Resources : fmt .Sprintf ("arn:%s :ec2:%s:%s:security-group/%s" , awsPartition , region , aws_account_id , security_group_id ),
304
311
Condition : map [string ]map [string ]string {
305
312
"StringEquals" : {
306
- "ec2:vpc" : fmt .Sprintf ("arn:aws :ec2:%s:%s:vpc/%s" , region , aws_account_id , vpc_id ),
313
+ "ec2:vpc" : fmt .Sprintf ("arn:%s :ec2:%s:%s:vpc/%s" , awsPartition , region , aws_account_id , vpc_id ),
307
314
},
308
315
},
309
316
},
0 commit comments