3
3
import pytest
4
4
from botocore .exceptions import ClientError
5
5
6
- from localstack .aws .accounts import get_aws_account_id
7
6
from localstack .aws .api .iam import Tag
8
7
from localstack .services .iam .provider import ADDITIONAL_MANAGED_POLICIES
9
8
from localstack .testing .aws .util import create_client_with_keys , wait_for_user
25
24
26
25
27
26
class TestIAMExtensions :
28
- @markers .aws .unknown
27
+ @markers .aws .validated
29
28
def test_get_user_without_username_as_user (self , create_user , aws_client ):
30
29
user_name = f"user-{ short_uid ()} "
31
30
policy_name = f"policy={ short_uid ()} "
@@ -51,7 +50,7 @@ def test_get_user_without_username_as_root(self, aws_client):
51
50
assert user ["UserId" ] == account_id
52
51
assert user ["Arn" ] == f"arn:aws:iam::{ account_id } :root"
53
52
54
- @markers .aws .unknown
53
+ @markers .aws .validated
55
54
def test_get_user_without_username_as_role (self , create_role , wait_and_assume_role , aws_client ):
56
55
role_name = f"role-{ short_uid ()} "
57
56
policy_name = f"policy={ short_uid ()} "
@@ -79,7 +78,7 @@ def test_get_user_without_username_as_role(self, create_role, wait_and_assume_ro
79
78
iam_client_as_role .get_user ()
80
79
e .match ("Must specify userName when calling with non-User credentials" )
81
80
82
- @markers .aws .unknown
81
+ @markers .aws .validated
83
82
def test_create_user_with_permission_boundary (self , create_user , create_policy , aws_client ):
84
83
user_name = f"user-{ short_uid ()} "
85
84
policy_name = f"policy-{ short_uid ()} "
@@ -102,7 +101,7 @@ def test_create_user_with_permission_boundary(self, create_user, create_policy,
102
101
get_user_reply = aws_client .iam .get_user (UserName = user_name )
103
102
assert "PermissionsBoundary" not in get_user_reply ["User" ]
104
103
105
- @markers .aws .unknown
104
+ @markers .aws .validated
106
105
def test_create_user_add_permission_boundary_afterwards (
107
106
self , create_user , create_policy , aws_client
108
107
):
@@ -152,8 +151,10 @@ def test_create_role_with_malformed_assume_role_policy_document(self, aws_client
152
151
153
152
154
153
class TestIAMIntegrations :
155
- @markers .aws .unknown
156
- def test_attach_iam_role_to_new_iam_user (self , aws_client ):
154
+ @markers .aws .validated
155
+ def test_attach_iam_role_to_new_iam_user (
156
+ self , aws_client , account_id , create_user , create_policy
157
+ ):
157
158
test_policy_document = {
158
159
"Version" : "2012-10-17" ,
159
160
"Statement" : {
@@ -162,14 +163,14 @@ def test_attach_iam_role_to_new_iam_user(self, aws_client):
162
163
"Resource" : "arn:aws:s3:::example_bucket" ,
163
164
},
10000
td>164
165
}
165
- test_user_name = "test-user"
166
+ test_user_name = f "test-user- { short_uid () } "
166
167
167
- aws_client . iam . create_user (UserName = test_user_name )
168
- response = aws_client . iam . create_policy (
169
- PolicyName = "test-policy" , PolicyDocument = json .dumps (test_policy_document )
168
+ create_user (UserName = test_user_name )
169
+ response = create_policy (
170
+ PolicyName = f "test-policy- { short_uid () } " , PolicyDocument = json .dumps (test_policy_document )
170
171
)
171
172
test_policy_arn = response ["Policy" ]["Arn" ]
172
- assert get_aws_account_id () in test_policy_arn
173
+ assert account_id in test_policy_arn
173
174
174
175
aws_client .iam .attach_user_policy (UserName = test_user_name , PolicyArn = test_policy_arn )
175
176
attached_user_policies = aws_client .iam .list_attached_user_policies (UserName = test_user_name )
@@ -187,7 +188,7 @@ def test_attach_iam_role_to_new_iam_user(self, aws_client):
187
188
assert ctx .typename == "NoSuchEntityException"
188
189
assert ctx .value .response ["Error" ]["Code" ] == "NoSuchEntity"
189
190
190
- @markers .aws .unknown
191
+ @markers .aws .validated
191
192
def test_delete_non_existent_policy_returns_no_such_entity (self , aws_client ):
192
193
non_existent_policy_arn = "arn:aws:iam::000000000000:policy/non-existent-policy"
193
194
@@ -196,21 +197,22 @@ def test_delete_non_existent_policy_returns_no_such_entity(self, aws_client):
196
197
assert ctx .typename == "NoSuchEntityException"
197
198
assert ctx .value .response ["Error" ]["Code" ] == "NoSuchEntity"
198
199
199
- @markers .aws .unknown
200
- def test_recreate_iam_role (self , aws_client ):
201
- role_name = "role-{}" . format ( short_uid ())
200
+ @markers .aws .validated
201
+ def test_recreate_iam_role (self , aws_client , create_role ):
202
+ role_name = f "role-{ short_uid ()} "
202
203
203
204
assume_policy_document = {
204
205
"Version" : "2012-10-17" ,
205
206
"Statement" : [
206
207
{
207
208
"Action" : "sts:AssumeRole" ,
208
209
"Principal" : {"Service" : "lambda.amazonaws.com" },
210
+ "Effect" : "Allow" ,
209
211
}
210
212
],
211
213
}
212
214
213
- rs = aws_client . iam . create_role (
215
+ rs = create_role (
214
216
RoleName = role_name ,
215
217
AssumeRolePolicyDocument = json .dumps (assume_policy_document ),
216
218
)
@@ -227,43 +229,46 @@ def test_recreate_iam_role(self, aws_client):
227
229
except ClientError as e :
228
230
assert e .response ["Error" ]["Code" ] == "EntityAlreadyExists"
229
231
230
- # clean up
231
- aws_client .iam .delete_role (RoleName = role_name )
232
-
233
- @markers .aws .unknown
234
- def test_instance_profile_tags (self , aws_client ):
232
+ @markers .aws .validated
233
+ def test_instance_profile_tags (self , aws_client , cleanups ):
235
234
def gen_tag ():
236
235
return Tag (Key = f"key-{ long_uid ()} " , Value = f"value-{ short_uid ()} " )
237
236
238
- user_name = "user-role-{}" .format (short_uid ())
237
+ def _sort_key (entry ):
238
+ return entry ["Key" ]
239
+
240
+ user_name = f"user-role-{ short_uid ()} "
239
241
aws_client .iam .create_instance_profile (InstanceProfileName = user_name )
242
+ cleanups .append (
243
+ lambda : aws_client .iam .delete_instance_profile (InstanceProfileName = user_name )
244
+ )
240
245
241
246
tags_v0 = []
242
247
#
243
248
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
244
- assert rs ["Tags" ] == tags_v0
249
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == tags_v0 . sort ( key = _sort_key )
245
250
246
251
tags_v1 = [gen_tag ()]
247
252
#
248
253
rs = aws_client .iam .tag_instance_profile (InstanceProfileName = user_name , Tags = tags_v1 )
249
254
assert rs ["ResponseMetadata" ]["HTTPStatusCode" ] == 200
250
255
#
251
256
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
252
- assert rs ["Tags" ] == tags_v1
257
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == tags_v1 . sort ( key = _sort_key )
253
258
254
259
tags_v2_new = [gen_tag () for _ in range (5 )]
255
260
tags_v2 = tags_v1 + tags_v2_new
256
261
rs = aws_client .iam .tag_instance_profile (InstanceProfileName = user_name , Tags = tags_v2 )
257
262
assert rs ["ResponseMetadata" ]["HTTPStatusCode" ] == 200
258
263
#
259
264
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
260
- assert rs ["Tags" ] == tags_v2
265
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == tags_v2 . sort ( key = _sort_key )
261
266
262
267
rs = aws_client .iam .tag_instance_profile (InstanceProfileName = user_name , Tags = tags_v2 )
263
268
assert rs ["ResponseMetadata" ]["HTTPStatusCode" ] == 200
264
269
#
265
270
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
266
- assert rs ["Tags" ] == tags_v2
271
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == tags_v2 . sort ( key = _sort_key )
267
272
268
273
tags_v3_new = [gen_tag ()]
269
274
tags_v3 = tags_v1 + tags_v3_new
@@ -272,42 +277,40 @@ def gen_tag():
272
277
assert rs ["ResponseMetadata" ]["HTTPStatusCode" ] == 200
273
278
#
274
279
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
275
- assert rs ["Tags" ] == target_tags_v3
280
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == target_tags_v3 . sort ( key = _sort_key )
276
281
277
282
tags_v4 = tags_v1
278
283
target_tags_v4 = target_tags_v3
279
284
rs = aws_client .iam .tag_instance_profile (InstanceProfileName = user_name , Tags = tags_v4 )
280
285
assert rs ["ResponseMetadata" ]["HTTPStatusCode" ] == 200
281
286
#
282
287
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
283
- assert rs ["Tags" ] == target_tags_v4
288
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == target_tags_v4 . sort ( key = _sort_key )
284
289
285
290
tags_u_v1 = [tag ["Key" ] for tag in tags_v1 ]
286
291
target_tags_u_v1 = tags_v2_new + tags_v3_new
287
292
aws_client .iam .untag_instance_profile (InstanceProfileName = user_name , TagKeys = tags_u_v1 )
288
293
#
289
294
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
290
- assert rs ["Tags" ] == target_tags_u_v1
295
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == target_tags_u_v1 . sort ( key = _sort_key )
291
296
292
297
tags_u_v2 = [f"key-{ long_uid ()<
F438
span class=pl-kos>} "]
293
298
target_tags_u_v2 = target_tags_u_v1
294
299
aws_client .iam .untag_instance_profile (InstanceProfileName = user_name , TagKeys = tags_u_v2 )
295
300
#
296
301
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
297
- assert rs ["Tags" ] == target_tags_u_v2
302
+ assert rs ["Tags" ]. sort ( key = _sort_key ) == target_tags_u_v2 . sort ( key = _sort_key )
298
303
299
304
tags_u_v3 = [tag ["Key" ] for tag in target_tags_u_v1 ]
300
305
target_tags_u_v3 = []
301
306
aws_client .iam .untag_instance_profile (InstanceProfileName = user_name , TagKeys = tags_u_v3 )
302
307
#
303
308
rs = aws_client .iam .list_instance_profile_tags (InstanceProfileName = user_name )
304
- assert rs ["Tags" ] == target_tags_u_v3
305
-
306
- aws_client .iam .delete_instance_profile (InstanceProfileName = user_name )
309
+ assert rs ["Tags" ].sort (key = _sort_key ) == target_tags_u_v3 .sort (key = _sort_key )
307
310
308
- @markers .aws .unknown
311
+ @markers .aws .validated
309
312
def test_create_user_with_tags (self , aws_client ):
310
- user_name = "user-role-{}" . format ( short_uid ())
313
+ user_name = f "user-role-{ short_uid ()} "
311
314
312
315
rs = aws_client .iam .create_user (
313
316
UserName = user_name , Tags = [{"Key" : "env" , "Value" : "production" }]
@@ -324,10 +327,10 @@ def test_create_user_with_tags(self, aws_client):
324
327
# clean up
325
328
aws_client .iam .delete_user (UserName = user_name )
326
329
327
- @markers .aws .unknown
330
+ @markers .aws .validated
328
331
def test_attach_detach_role_policy (self , aws_client ):
329
- role_name = "s3-role-{}" . format ( short_uid ())
330
- policy_name = "s3-role-policy-{}" . format ( short_uid ())
332
+ role_name = f "s3-role-{ short_uid ()} "
333
+ policy_name = f "s3-role-policy-{ short_uid ()} "
331
334
332
335
policy_arns = [p ["Arn" ] for p in ADDITIONAL_MANAGED_POLICIES .values ()]
333
336
@@ -337,6 +340,7 @@ def test_attach_detach_role_policy(self, aws_client):
337
340
{
338
341
"Action" : "sts:AssumeRole" ,
339
342
"Principal" : {"Service" : "s3.amazonaws.com" },
343
+ "Effect" : "Allow" ,
340
344
}
341
345
],
342
346
}
@@ -389,8 +393,10 @@ def test_attach_detach_role_policy(self, aws_client):
389
393
390
394
aws_client .iam .delete_policy (PolicyArn = policy_arn )
391
395
392
- @markers .aws .unknown
396
+ @markers .aws .needs_fixing
393
397
def test_simulate_principle_policy (self , aws_client ):
398
+ # FIXME this test should test whether a principal (like user, role) has some permissions, it cannot test
399
+ # the policy itself
394
400
policy_name = "policy-{}" .format (short_uid ())
395
401
policy_document = {
396
402
"Version" : "2012-10-17" ,
@@ -422,8 +428,8 @@ def test_simulate_principle_policy(self, aws_client):
422
428
assert "s3:GetObjectVersion" in actions
423
429
assert actions ["s3:GetObjectVersion" ]["EvalDecision" ] == "allowed"
424
430
425
- @markers .aws .unknown
426
- def test_create_role_with_assume_role_policy (self , aws_client ):
431
+ @markers .aws .validated
432
+ def test_create_role_with_assume_role_policy (self , aws_client , account_id , create_role ):
427
433
role_name_1 = f"role-{ short_uid ()} "
428
434
role_name_2 = f"role-{ short_uid ()} "
429
435
@@ -433,13 +439,13 @@ def test_create_role_with_assume_role_policy(self, aws_client):
433
439
{
434
440
"Action" : "sts:AssumeRole" ,
435
441
"Effect" : "Allow" ,
436
- "Principal" : {"AWS" : [ "arn:aws:iam::123412341234 :root" ] },
442
+ "Principal" : {"AWS" : f "arn:aws:iam::{ account_id } :root" },
437
443
}
438
444
],
439
445
}
440
446
str_assume_role_policy_doc = json .dumps (assume_role_policy_doc )
441
447
442
- aws_client . iam . create_role (
448
+ create_role (
443
449
Path = "/" ,
444
450
RoleName = role_name_1 ,
445
451
AssumeRolePolicyDocument = str_assume_role_policy_doc ,
@@ -450,7 +456,7 @@ def test_create_role_with_assume_role_policy(self, aws_client):
450
456
if role ["RoleName" ] == role_name_1 :
451
457
assert role ["AssumeRolePolicyDocument" ] == assume_role_policy_doc
452
458
453
- aws_client . iam . create_role (
459
+ create_role (
454
460
Path = "/" ,
455
461
RoleName = role_name_2 ,
456
462
AssumeRolePolicyDocument = str_assume_role_policy_doc ,
@@ -463,17 +469,17 @@ def test_create_role_with_assume_role_policy(self, aws_client):
463
469
assert role ["AssumeRolePolicyDocument" ] == assume_role_policy_doc
464
470
aws_client .iam .delete_role (RoleName = role ["RoleName" ])
465
471
466
- aws_client . iam . create_role (
467
- Path = "myPath" ,
472
+ create_role (
473
+ Path = "/ myPath/ " ,
468
474
RoleName = role_name_2 ,
469
475
AssumeRolePolicyDocument = str_assume_role_policy_doc ,
470
476
Description = "string" ,
471
477
)
472
478
473
- roles = aws_client .iam .list_roles (PathPrefix = "my" )
474
- assert roles ["Roles" ][0 ]["Path" ] == "myPath"
475
- assert roles ["Roles" ][0 ]["RoleName" ] == role_name_2
479
+ roles = aws_client .iam .list_roles (PathPrefix = "/my" )
476
480
assert len (roles ["Roles" ]) == 1
481
+ assert roles ["Roles" ][0 ]["Path" ] == "/myPath/"
482
+ assert roles ["Roles" ][0 ]["RoleName" ] == role_name_2
477
483
478
484
@markers .aws .validated
479
485
@pytest .mark .xfail
0 commit comments