mongodb_user: properly guard user adding with try...except (#2582)

The user adding part is not properly guarded by a try...except
block, so pymongo exceptions can escape from it.
Also there's a double-guarding where roles are given.

Fixes: #2575
reviewable/pr18780/r1
gyurco 8 years ago committed by René Moser
parent 38d364103d
commit 960fcaf67a

@ -198,11 +198,7 @@ def user_add(module, client, db_name, user, password, roles):
if roles is None: if roles is None:
db.add_user(user, password, False) db.add_user(user, password, False)
else: else:
try:
db.add_user(user, password, None, roles=roles) db.add_user(user, password, None, roles=roles)
except OperationFailure, e:
err_msg = str(e)
module.fail_json(msg=err_msg)
def user_remove(module, client, db_name, user): def user_remove(module, client, db_name, user):
exists = user_find(client, user, db_name) exists = user_find(client, user, db_name)
@ -346,6 +342,7 @@ def main():
if password is None and update_password == 'always': if password is None and update_password == 'always':
module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create') module.fail_json(msg='password parameter required when adding a user unless update_password is set to on_create')
try:
uinfo = user_find(client, user, db_name) uinfo = user_find(client, user, db_name)
if update_password != 'always' and uinfo: if update_password != 'always' and uinfo:
password = None password = None
@ -355,7 +352,6 @@ def main():
if module.check_mode: if module.check_mode:
module.exit_json(changed=True, user=user) module.exit_json(changed=True, user=user)
try:
user_add(module, client, db_name, user, password, roles) user_add(module, client, db_name, user, password, roles)
except OperationFailure, e: except OperationFailure, e:
module.fail_json(msg='Unable to add or update user: %s' % str(e)) module.fail_json(msg='Unable to add or update user: %s' % str(e))

Loading…
Cancel
Save