| 1 |
--- |
| 2 |
# =========================================== |
| 3 |
# Test Managed Policy management |
| 4 |
# |
| 5 |
# Use a couple of benign policies for testing: |
| 6 |
# - AWSDenyAll |
| 7 |
# - ServiceQuotasReadOnlyAccess |
| 8 |
# |
| 9 |
- name: Attach managed policy to user (check mode) |
| 10 |
amazon.aws.iam_user: |
| 11 |
name: "{{ test_user }}" |
| 12 |
state: present |
| 13 |
managed_policy: |
| 14 |
- arn:aws:iam::aws:policy/AWSDenyAll |
| 15 |
register: iam_user |
| 16 |
check_mode: true |
| 17 |
- name: Assert that the user is changed |
| 18 |
ansible.builtin.assert: |
| 19 |
that: |
| 20 |
- iam_user is changed |
| 21 |
|
| 22 |
- name: Attach managed policy to user |
| 23 |
amazon.aws.iam_user: |
| 24 |
name: "{{ test_user }}" |
| 25 |
state: present |
| 26 |
managed_policy: |
| 27 |
- arn:aws:iam::aws:policy/AWSDenyAll |
| 28 |
register: iam_user |
| 29 |
- name: Assert that the user is changed |
| 30 |
ansible.builtin.assert: |
| 31 |
that: |
| 32 |
- iam_user is changed |
| 33 |
- '"user" in iam_user' |
| 34 |
- '"attached_policies" in iam_user.user' |
| 35 |
- iam_user.user.attached_policies | length == 1 |
| 36 |
- '"AWSDenyAll" in attached_policy_names' |
| 37 |
- '"arn:aws:iam::aws:policy/AWSDenyAll" in attached_policy_arns' |
| 38 |
vars: |
| 39 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 40 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 41 |
|
| 42 |
- name: Ensure managed policy is attached to user (no change - check mode) |
| 43 |
amazon.aws.iam_user: |
| 44 |
name: "{{ test_user }}" |
| 45 |
state: present |
| 46 |
managed_policy: |
| 47 |
- arn:aws:iam::aws:policy/AWSDenyAll |
| 48 |
register: iam_user |
| 49 |
check_mode: true |
| 50 |
- name: Assert that the user hasn't changed |
| 51 |
ansible.builtin.assert: |
| 52 |
that: |
| 53 |
- iam_user is not changed |
| 54 |
|
| 55 |
- name: Ensure managed policy is attached to user (no change) |
| 56 |
amazon.aws.iam_user: |
| 57 |
name: "{{ test_user }}" |
| 58 |
state: present |
| 59 |
managed_policy: |
| 60 |
- arn:aws:iam::aws:policy/AWSDenyAll |
| 61 |
register: iam_user |
| 62 |
- name: Assert that the user hasn't changed |
| 63 |
ansible.builtin.assert: |
| 64 |
that: |
| 65 |
- iam_user is not changed |
| 66 |
- '"user" in iam_user' |
| 67 |
- '"attached_policies" in iam_user.user' |
| 68 |
- iam_user.user.attached_policies | length == 1 |
| 69 |
- '"AWSDenyAll" in attached_policy_names' |
| 70 |
- '"arn:aws:iam::aws:policy/AWSDenyAll" in attached_policy_arns' |
| 71 |
vars: |
| 72 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 73 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 74 |
|
| 75 |
# ------------------------------------------------------------------------------------------ |
| 76 |
|
| 77 |
- name: Attach different managed policy to user (check mode) |
| 78 |
check_mode: true |
| 79 |
amazon.aws.iam_user: |
| 80 |
name: "{{ test_user }}" |
| 81 |
state: present |
| 82 |
managed_policy: |
| 83 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 84 |
purge_policy: false |
| 85 |
register: iam_user |
| 86 |
- name: Assert that the user changed |
| 87 |
ansible.builtin.assert: |
| 88 |
that: |
| 89 |
- iam_user is changed |
| 90 |
|
| 91 |
- name: Attach different managed policy to user |
| 92 |
amazon.aws.iam_user: |
| 93 |
name: "{{ test_user }}" |
| 94 |
state: present |
| 95 |
managed_policy: |
| 96 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 97 |
purge_policy: false |
| 98 |
register: iam_user |
| 99 |
- name: Assert that the user changed |
| 100 |
ansible.builtin.assert: |
| 101 |
that: |
| 102 |
- iam_user is changed |
| 103 |
- '"user" in iam_user' |
| 104 |
- '"attached_policies" in iam_user.user' |
| 105 |
- iam_user.user.attached_policies | length == 2 |
| 106 |
- '"AWSDenyAll" in attached_policy_names' |
| 107 |
- '"ServiceQuotasReadOnlyAccess" in attached_policy_names' |
| 108 |
- '"arn:aws:iam::aws:policy/AWSDenyAll" in attached_policy_arns' |
| 109 |
- '"arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess" in attached_policy_arns' |
| 110 |
vars: |
| 111 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 112 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 113 |
|
| 114 |
- name: Attach different managed policy to user (no change - check mode) |
| 115 |
amazon.aws.iam_user: |
| 116 |
name: "{{ test_user }}" |
| 117 |
state: present |
| 118 |
managed_policy: |
| 119 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 120 |
purge_policy: false |
| 121 |
register: iam_user |
| 122 |
check_mode: true |
| 123 |
- name: Assert that the user hasn't changed |
| 124 |
ansible.builtin.assert: |
| 125 |
that: |
| 126 |
- iam_user is not changed |
| 127 |
|
| 128 |
- name: Check first policy wasn't purged |
| 129 |
amazon.aws.iam_user: |
| 130 |
name: "{{ test_user }}" |
| 131 |
state: present |
| 132 |
managed_policy: |
| 133 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 134 |
- arn:aws:iam::aws:policy/AWSDenyAll |
| 135 |
purge_policy: false |
| 136 |
register: iam_user |
| 137 |
- name: Assert that the user hasn't changed |
| 138 |
ansible.builtin.assert: |
| 139 |
that: |
| 140 |
- iam_user is not changed |
| 141 |
- '"user" in iam_user' |
| 142 |
- '"attached_policies" in iam_user.user' |
| 143 |
- iam_user.user.attached_policies | length == 2 |
| 144 |
- '"AWSDenyAll" in attached_policy_names' |
| 145 |
- '"ServiceQuotasReadOnlyAccess" in attached_policy_names' |
| 146 |
- '"arn:aws:iam::aws:policy/AWSDenyAll" in attached_policy_arns' |
| 147 |
- '"arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess" in attached_policy_arns' |
| 148 |
vars: |
| 149 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 150 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 151 |
|
| 152 |
- name: Check that managed policy order doesn't matter |
| 153 |
amazon.aws.iam_user: |
| 154 |
name: "{{ test_user }}" |
| 155 |
state: present |
| 156 |
managed_policy: |
| 157 |
- arn:aws:iam::aws:policy/AWSDenyAll |
| 158 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 159 |
purge_policy: false |
| 160 |
register: iam_user |
| 161 |
- name: Assert that the user hasn't changed |
| 162 |
ansible.builtin.assert: |
| 163 |
that: |
| 164 |
- iam_user is not changed |
| 165 |
- '"user" in iam_user' |
| 166 |
- '"attached_policies" in iam_user.user' |
| 167 |
- iam_user.user.attached_policies | length == 2 |
| 168 |
- '"AWSDenyAll" in attached_policy_names' |
| 169 |
- '"ServiceQuotasReadOnlyAccess" in attached_policy_names' |
| 170 |
- '"arn:aws:iam::aws:policy/AWSDenyAll" in attached_policy_arns' |
| 171 |
- '"arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess" in attached_policy_arns' |
| 172 |
vars: |
| 173 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 174 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 175 |
|
| 176 |
- name: Check that policy doesn't require full ARN path |
| 177 |
amazon.aws.iam_user: |
| 178 |
name: "{{ test_user }}" |
| 179 |
state: present |
| 180 |
managed_policy: |
| 181 |
- AWSDenyAll |
| 182 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 183 |
purge_policy: false |
| 184 |
register: iam_user |
| 185 |
- name: Assert that the user hasn't changed |
| 186 |
ansible.builtin.assert: |
| 187 |
that: |
| 188 |
- iam_user is not changed |
| 189 |
- '"user" in iam_user' |
| 190 |
- '"attached_policies" in iam_user.user' |
| 191 |
- iam_user.user.attached_policies | length == 2 |
| 192 |
- '"AWSDenyAll" in attached_policy_names' |
| 193 |
- '"ServiceQuotasReadOnlyAccess" in attached_policy_names' |
| 194 |
- '"arn:aws:iam::aws:policy/AWSDenyAll" in attached_policy_arns' |
| 195 |
- '"arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess" in attached_policy_arns' |
| 196 |
vars: |
| 197 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 198 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 199 |
|
| 200 |
# ------------------------------------------------------------------------------------------ |
| 201 |
|
| 202 |
- name: Remove one of the managed policies - with purge (check mode) |
| 203 |
check_mode: true |
| 204 |
amazon.aws.iam_user: |
| 205 |
name: "{{ test_user }}" |
| 206 |
state: present |
| 207 |
managed_policy: |
| 208 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 209 |
purge_policy: true |
| 210 |
register: iam_user |
| 211 |
- name: Assert that the user changed |
| 212 |
ansible.builtin.assert: |
| 213 |
that: |
| 214 |
- iam_user is changed |
| 215 |
|
| 216 |
- name: Remove one of the managed policies - with purge |
| 217 |
amazon.aws.iam_user: |
| 218 |
name: "{{ test_user }}" |
| 219 |
state: present |
| 220 |
managed_policy: |
| 221 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 222 |
purge_policy: true |
| 223 |
register: iam_user |
| 224 |
- name: Assert that the user changed |
| 225 |
ansible.builtin.assert: |
| 226 |
that: |
| 227 |
- iam_user is changed |
| 228 |
- '"user" in iam_user' |
| 229 |
- '"attached_policies" in iam_user.user' |
| 230 |
- iam_user.user.attached_policies | length == 1 |
| 231 |
- '"ServiceQuotasReadOnlyAccess" in attached_policy_names' |
| 232 |
- '"arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess" in attached_policy_arns' |
| 233 |
vars: |
| 234 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 235 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |
| 236 |
|
| 237 |
- name: Remove one of the managed policies - with purge (no change - check mode) |
| 238 |
amazon.aws.iam_user: |
| 239 |
name: "{{ test_user }}" |
| 240 |
state: present |
| 241 |
managed_policy: |
| 242 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 243 |
purge_policy: true |
| 244 |
register: iam_user |
| 245 |
check_mode: true |
| 246 |
- name: Assert that the user hasn't changed |
| 247 |
ansible.builtin.assert: |
| 248 |
that: |
| 249 |
- iam_user is not changed |
| 250 |
|
| 251 |
- name: Remove one of the managed policies - with purge (no change) |
| 252 |
amazon.aws.iam_user: |
| 253 |
name: "{{ test_user }}" |
| 254 |
state: present |
| 255 |
managed_policy: |
| 256 |
- arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess |
| 257 |
purge_policy: true |
| 258 |
register: iam_user |
| 259 |
- name: Assert that the user hasn't changed |
| 260 |
ansible.builtin.assert: |
| 261 |
that: |
| 262 |
- iam_user is not changed |
| 263 |
- '"user" in iam_user' |
| 264 |
- '"attached_policies" in iam_user.user' |
| 265 |
- iam_user.user.attached_policies | length == 1 |
| 266 |
- '"ServiceQuotasReadOnlyAccess" in attached_policy_names' |
| 267 |
- '"arn:aws:iam::aws:policy/ServiceQuotasReadOnlyAccess" in attached_policy_arns' |
| 268 |
vars: |
| 269 |
attached_policy_arns: "{{ iam_user.user.attached_policies | map(attribute='policy_arn') }}" |
| 270 |
attached_policy_names: "{{ iam_user.user.attached_policies | map(attribute='policy_name') }}" |