Skip to content

Commit f80fb6c

Browse files
Merge branch 'main' into test_ruff
2 parents 2a5bb88 + 970c303 commit f80fb6c

File tree

5 files changed

+79
-2
lines changed

5 files changed

+79
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
bugfixes:
3+
- iam_managed_policy - fixes bug that causes ``ParamValidationError`` when attempting to delete a policy that's attached to a role or a user (https://github.com/ansible-collections/amazon.aws/issues/2067).

plugins/module_utils/iam.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ def detach_iam_group_policy(client, arn, group):
4949
@IAMErrorHandler.deletion_error_handler("detach role policy")
5050
@AWSRetry.jittered_backoff()
5151
def detach_iam_role_policy(client, arn, role):
52-
client.detach_group_policy(PolicyArn=arn, RoleName=role)
52+
client.detach_role_policy(PolicyArn=arn, RoleName=role)
5353
return True
5454

5555

5656
@IAMErrorHandler.deletion_error_handler("detach user policy")
5757
@AWSRetry.jittered_backoff()
5858
def detach_iam_user_policy(client, arn, user):
59-
client.detach_group_policy(PolicyArn=arn, UserName=user)
59+
client.detach_user_policy(PolicyArn=arn, UserName=user)
6060
return True
6161

6262

tests/integration/targets/iam_managed_policy/defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
policy_name: "{{ resource_prefix }}-policy"
33
policy_path: "/ansible-test-{{ tiny_prefix }}/"
44
policy_description: "An example Managed Policy description"
5+
test_role: "{{ resource_prefix }}-mp-role"
6+
test_user: "{{ resource_prefix }}-mp-user"
7+
test_group: "{{ resource_prefix }}-mp-group"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Action": "sts:AssumeRole",
6+
"Principal": { "Service": "ec2.amazonaws.com" },
7+
"Effect": "Deny"
8+
}
9+
]
10+
}

tests/integration/targets/iam_managed_policy/tasks/main.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@
99
collections:
1010
- amazon.aws
1111
block:
12+
- name: Create IAM group
13+
amazon.aws.iam_group:
14+
name: "{{ test_group }}"
15+
state: present
16+
- name: Create IAM user
17+
amazon.aws.iam_user:
18+
name: "{{ test_user }}"
19+
state: present
20+
- name: Create IAM role
21+
amazon.aws.iam_role:
22+
name: "{{ test_role }}"
23+
assume_role_policy_document: '{{ lookup("file", "deny-assume.json") }}'
24+
create_instance_profile: false
25+
state: present
26+
1227
## Test policy creation
1328
- name: Create IAM managed policy - check mode
1429
amazon.aws.iam_managed_policy:
@@ -448,14 +463,60 @@
448463
- result.policy.tags["Tag C"] == "Value C"
449464
- result.policy.tags["tag d"] == "value d"
450465

466+
- name: Attach managed policy to group
467+
amazon.aws.iam_group:
468+
name: "{{ test_group }}"
469+
state: present
470+
managed_policies:
471+
- "{{ policy_name }}"
472+
- name: Attach managed policy to user
473+
amazon.aws.iam_user:
474+
name: "{{ test_user }}"
475+
state: present
476+
managed_policies:
477+
- "{{ policy_name }}"
478+
- name: Attach managed policy to role
479+
amazon.aws.iam_role:
480+
name: "{{ test_role }}"
481+
state: present
482+
assume_role_policy_document: '{{ lookup("file", "deny-assume.json") }}'
483+
managed_policies:
484+
- "{{ policy_name }}"
485+
451486
- name: Delete IAM managed policy
452487
amazon.aws.iam_managed_policy:
453488
policy_name: "{{ policy_name }}"
454489
state: absent
455490

491+
- name: Delete IAM group
492+
amazon.aws.iam_group:
493+
name: "{{ test_group }}"
494+
state: absent
495+
- name: Delete IAM user
496+
amazon.aws.iam_user:
497+
name: "{{ test_user }}"
498+
state: absent
499+
- name: Delete IAM role
500+
amazon.aws.iam_role:
501+
name: "{{ test_role }}"
502+
state: absent
503+
456504
always:
457505
- name: Delete IAM managed policy
458506
amazon.aws.iam_managed_policy:
459507
policy_name: "{{ policy_name }}"
460508
state: absent
461509
ignore_errors: true # noqa: ignore-errors
510+
511+
- name: Delete IAM group
512+
amazon.aws.iam_group:
513+
name: "{{ test_group }}"
514+
state: absent
515+
- name: Delete IAM user
516+
amazon.aws.iam_user:
517+
name: "{{ test_user }}"
518+
state: absent
519+
- name: Delete IAM role
520+
amazon.aws.iam_role:
521+
name: "{{ test_role }}"
522+
state: absent

0 commit comments

Comments
 (0)