Skip to content

Commit 0ecbeb0

Browse files
markumanGomathiselviS
authored andcommitted
add missing password_reset_required parameter (ansible-collections#860)
add missing password_reset_required parameter SUMMARY password_reset_required parameter is missing in iam_user module. ISSUE TYPE Feature Pull Request COMPONENT NAME iam_user ADDITIONAL INFORMATION Sadly, LoginProfile is only returned on create_login_profile and not on update_login_profile. Therefor the functionality can only be verified when the user is created, not when the user is udpated. https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.create_login_profile https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iam.html#IAM.Client.update_login_profile retval of update_login_profile is just 0.0s create_or_update_user: {'ResponseMetadata': {'HTTPHeaders': {'content-length': '216', 'content-type': 'text/xml', 'date': 'Wed, 12 Jan 2022 20:18:08 GMT', 'x-amzn-requestid': '11b6fde3-9f28-4265-8fac-88e3f5a238d3'}, 'HTTPStatusCode': 200, 'RequestId': '11b6fde3-9f28-4265-8fac-88e3f5a238d3', 'RetryAttempts': 0}} Reviewed-by: Mark Woolley <[email protected]> Reviewed-by: Alina Buzachis <None> Reviewed-by: Markus Bergholz <[email protected]> This commit was initially merged in https://github.com/ansible-collections/community.aws See: ansible-collections/community.aws@c0b3e1b
1 parent fe6af9c commit 0ecbeb0

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

plugins/modules/iam_user.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
required: false
2828
type: str
2929
version_added: 2.2.0
30+
password_reset_required:
31+
description:
32+
- Defines if the user is required to set a new password after login.
33+
required: false
34+
type: bool
35+
default: false
36+
version_added: 3.1.0
3037
update_password:
3138
default: always
3239
choices: ['always', 'on_create']
@@ -250,18 +257,20 @@ def create_or_update_login_profile(connection, module):
250257
user_params = dict()
251258
user_params['UserName'] = module.params.get('name')
252259
user_params['Password'] = module.params.get('password')
260+
user_params['PasswordResetRequired'] = module.params.get('password_reset_required')
261+
retval = {}
253262

254263
try:
255-
connection.update_login_profile(**user_params)
264+
retval = connection.update_login_profile(**user_params)
256265
except is_boto3_error_code('NoSuchEntity'):
257266
try:
258-
connection.create_login_profile(**user_params)
267+
retval = connection.create_login_profile(**user_params)
259268
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
260269
module.fail_json_aws(e, msg="Unable to create user login profile")
261270
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
262271
module.fail_json_aws(e, msg="Unable to update user login profile")
263272

264-
return True
273+
return True, retval
265274

266275

267276
def delete_login_profile(connection, module):
@@ -296,6 +305,7 @@ def create_or_update_user(connection, module):
296305
user = get_user(connection, module, params['UserName'])
297306

298307
# If user is None, create it
308+
new_login_profile = False
299309
if user is None:
300310
# Check mode means we would create the user
301311
if module.check_mode:
@@ -312,13 +322,20 @@ def create_or_update_user(connection, module):
312322
wait_iam_exists(connection, module)
313323

314324
if module.params.get('password') is not None:
315-
create_or_update_login_profile(connection, module)
325+
login_profile_result, login_profile_data = create_or_update_login_profile(connection, module)
326+
327+
if login_profile_data.get('LoginProfile', {}).get('PasswordResetRequired', False):
328+
new_login_profile = True
316329
else:
317330
login_profile_result = None
318331
update_result = update_user_tags(connection, module, params, user)
319332

320333
if module.params['update_password'] == "always" and module.params.get('password') is not None:
321-
login_profile_result = create_or_update_login_profile(connection, module)
334+
login_profile_result, login_profile_data = create_or_update_login_profile(connection, module)
335+
336+
if login_profile_data.get('LoginProfile', {}).get('PasswordResetRequired', False):
337+
new_login_profile = True
338+
322339
elif module.params.get('remove_password'):
323340
login_profile_result = delete_login_profile(connection, module)
324341

@@ -361,6 +378,9 @@ def create_or_update_user(connection, module):
361378

362379
# Get the user again
363380
user = get_user(connection, module, params['UserName'])
381+
if changed and new_login_profile:
382+
# `LoginProfile` is only returned on `create_login_profile` method
383+
user['user']['password_reset_required'] = login_profile_data.get('LoginProfile', {}).get('PasswordResetRequired', False)
364384

365385
module.exit_json(changed=changed, iam_user=user)
366386

@@ -505,8 +525,9 @@ def main():
505525
argument_spec = dict(
506526
name=dict(required=True, type='str'),
507527
password=dict(type='str', no_log=True),
528+
password_reset_required=dict(type='bool', default=False, no_log=False),
508529
update_password=dict(default='always', choices=['always', 'on_create'], no_log=False),
509-
remove_password=dict(type='bool'),
530+
remove_password=dict(type='bool', no_log=False),
510531
managed_policies=dict(default=[], type='list', aliases=['managed_policy'], elements='str'),
511532
state=dict(choices=['present', 'absent'], required=True),
512533
purge_policies=dict(default=False, type='bool', aliases=['purge_policy', 'purge_managed_policies']),
@@ -519,7 +540,7 @@ def main():
519540
module = AnsibleAWSModule(
520541
argument_spec=argument_spec,
521542
supports_check_mode=True,
522-
mutually_exclusive=[['password', 'remove_password']]
543+
mutually_exclusive=[['password', 'remove_password']],
523544
)
524545

525546
connection = module.client('iam')

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,15 @@
111111
iam_user:
112112
name: "{{ test_user3 }}"
113113
password: "{{ test_password }}"
114+
password_reset_required: yes
114115
state: present
115116
register: iam_user
116117

117118
- name: assert that the second user is created
118119
assert:
119120
that:
120121
- iam_user is changed
122+
- iam_user.iam_user.user.password_reset_required
121123

122124
- name: get info on IAM user(s) on path
123125
iam_user_info:
@@ -275,12 +277,17 @@
275277
that:
276278
- iam_user_update is not changed
277279

280+
# flakey, there is no waiter for login profiles
281+
# Login Profile for User ansible-user-c cannot be modified while login profile is being created.
278282
- name: update IAM password
279283
iam_user:
280284
name: "{{ test_user3 }}"
281285
password: "{{ test_new_password }}"
282286
state: present
283287
register: iam_user_update
288+
until: iam_user_update.failed == false
289+
delay: 3
290+
retries: 5
284291

285292
- assert:
286293
that:

0 commit comments

Comments
 (0)