| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Aaron Smith <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
--- |
| 9 |
module: iam_password_policy |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Update an IAM Password Policy |
| 13 |
description: |
| 14 |
- Module updates an IAM Password Policy on a given AWS account. |
| 15 |
author: |
| 16 |
- "Aaron Smith (@slapula)" |
| 17 |
options: |
| 18 |
state: |
| 19 |
description: |
| 20 |
- Specifies the overall state of the password policy. |
| 21 |
required: true |
| 22 |
choices: ['present', 'absent'] |
| 23 |
type: str |
| 24 |
min_pw_length: |
| 25 |
description: |
| 26 |
- Minimum password length. |
| 27 |
default: 6 |
| 28 |
aliases: [minimum_password_length] |
| 29 |
type: int |
| 30 |
require_symbols: |
| 31 |
description: |
| 32 |
- Require symbols in password. |
| 33 |
default: false |
| 34 |
type: bool |
| 35 |
require_numbers: |
| 36 |
description: |
| 37 |
- Require numbers in password. |
| 38 |
default: false |
| 39 |
type: bool |
| 40 |
require_uppercase: |
| 41 |
description: |
| 42 |
- Require uppercase letters in password. |
| 43 |
default: false |
| 44 |
type: bool |
| 45 |
require_lowercase: |
| 46 |
description: |
| 47 |
- Require lowercase letters in password. |
| 48 |
default: false |
| 49 |
type: bool |
| 50 |
allow_pw_change: |
| 51 |
description: |
| 52 |
- Allow users to change their password. |
| 53 |
default: false |
| 54 |
type: bool |
| 55 |
aliases: [allow_password_change] |
| 56 |
pw_max_age: |
| 57 |
description: |
| 58 |
- Maximum age for a password in days. When this option is 0 then passwords |
| 59 |
do not expire automatically. |
| 60 |
default: 0 |
| 61 |
aliases: [password_max_age] |
| 62 |
type: int |
| 63 |
pw_reuse_prevent: |
| 64 |
description: |
| 65 |
- Prevent re-use of passwords. |
| 66 |
default: 0 |
| 67 |
aliases: [password_reuse_prevent, prevent_reuse] |
| 68 |
type: int |
| 69 |
pw_expire: |
| 70 |
description: |
| 71 |
- Prevents users from change an expired password. |
| 72 |
default: false |
| 73 |
type: bool |
| 74 |
aliases: [password_expire, expire] |
| 75 |
extends_documentation_fragment: |
| 76 |
- amazon.aws.common.modules |
| 77 |
- amazon.aws.region.modules |
| 78 |
- amazon.aws.boto3 |
| 79 |
""" |
| 80 |
|
| 81 |
EXAMPLES = r""" |
| 82 |
- name: Password policy for AWS account |
| 83 |
amazon.aws.iam_password_policy: |
| 84 |
state: present |
| 85 |
min_pw_length: 8 |
| 86 |
require_symbols: false |
| 87 |
require_numbers: true |
| 88 |
require_uppercase: true |
| 89 |
require_lowercase: true |
| 90 |
allow_pw_change: true |
| 91 |
pw_max_age: 60 |
| 92 |
pw_reuse_prevent: 5 |
| 93 |
pw_expire: false |
| 94 |
""" |
| 95 |
|
| 96 |
RETURN = r""" # """ |
| 97 |
|
| 98 |
try: |
| 99 |
import botocore |
| 100 |
except ImportError: |
| 101 |
pass # caught by AnsibleAWSModule |
| 102 |
|
| 103 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 104 |
|
| 105 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 106 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 107 |
|
| 108 |
|
| 109 |
class IAMConnection(object): |
| 110 |
def __init__(self, module): |
| 111 |
try: |
| 112 |
self.connection = module.resource("iam") |
| 113 |
self.module = module |
| 114 |
except Exception as e: |
| 115 |
module.fail_json(msg=f"Failed to connect to AWS: {str(e)}") |
| 116 |
|
| 117 |
def policy_to_dict(self, policy): |
| 118 |
policy_attributes = [ |
| 119 |
"allow_users_to_change_password", |
| 120 |
"expire_passwords", |
| 121 |
"hard_expiry", |
| 122 |
"max_password_age", |
| 123 |
"minimum_password_length", |
| 124 |
"password_reuse_prevention", |
| 125 |
"require_lowercase_characters", |
| 126 |
"require_numbers", |
| 127 |
"require_symbols", |
| 128 |
"require_uppercase_characters", |
| 129 |
] |
| 130 |
ret = {} |
| 131 |
for attr in policy_attributes: |
| 132 |
ret[attr] = getattr(policy, attr) |
| 133 |
return ret |
| 134 |
|
| 135 |
def update_password_policy(self, module, policy): |
| 136 |
min_pw_length = module.params.get("min_pw_length") |
| 137 |
require_symbols = module.params.get("require_symbols") |
| 138 |
require_numbers = module.params.get("require_numbers") |
| 139 |
require_uppercase = module.params.get("require_uppercase") |
| 140 |
require_lowercase = module.params.get("require_lowercase") |
| 141 |
allow_pw_change = module.params.get("allow_pw_change") |
| 142 |
pw_max_age = module.params.get("pw_max_age") |
| 143 |
pw_reuse_prevent = module.params.get("pw_reuse_prevent") |
| 144 |
pw_expire = module.params.get("pw_expire") |
| 145 |
|
| 146 |
update_parameters = dict( |
| 147 |
MinimumPasswordLength=min_pw_length, |
| 148 |
RequireSymbols=require_symbols, |
| 149 |
RequireNumbers=require_numbers, |
| 150 |
RequireUppercaseCharacters=require_uppercase, |
| 151 |
RequireLowercaseCharacters=require_lowercase, |
| 152 |
AllowUsersToChangePassword=allow_pw_change, |
| 153 |
HardExpiry=pw_expire, |
| 154 |
) |
| 155 |
if pw_reuse_prevent: |
| 156 |
update_parameters.update(PasswordReusePrevention=pw_reuse_prevent) |
| 157 |
if pw_max_age: |
| 158 |
update_parameters.update(MaxPasswordAge=pw_max_age) |
| 159 |
|
| 160 |
try: |
| 161 |
original_policy = self.policy_to_dict(policy) |
| 162 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError): |
| 163 |
original_policy = {} |
| 164 |
|
| 165 |
try: |
| 166 |
results = policy.update(**update_parameters) |
| 167 |
policy.reload() |
| 168 |
updated_policy = self.policy_to_dict(policy) |
| 169 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 170 |
self.module.fail_json_aws(e, msg="Couldn't update IAM Password Policy") |
| 171 |
|
| 172 |
changed = original_policy != updated_policy |
| 173 |
return (changed, updated_policy, camel_dict_to_snake_dict(results)) |
| 174 |
|
| 175 |
def delete_password_policy(self, policy): |
| 176 |
try: |
| 177 |
results = policy.delete() |
| 178 |
except is_boto3_error_code("NoSuchEntity"): |
| 179 |
self.module.exit_json(changed=False, task_status={"IAM": "Couldn't find IAM Password Policy"}) |
| 180 |
except ( |
| 181 |
botocore.exceptions.ClientError, |
| 182 |
botocore.exceptions.BotoCoreError, |
| 183 |
) as e: # pylint: disable=duplicate-except |
| 184 |
self.module.fail_json_aws(e, msg="Couldn't delete IAM Password Policy") |
| 185 |
return camel_dict_to_snake_dict(results) |
| 186 |
|
| 187 |
|
| 188 |
def main(): |
| 189 |
module = AnsibleAWSModule( |
| 190 |
argument_spec={ |
| 191 |
"state": dict(choices=["present", "absent"], required=True), |
| 192 |
"min_pw_length": dict(type="int", aliases=["minimum_password_length"], default=6), |
| 193 |
"require_symbols": dict(type="bool", default=False), |
| 194 |
"require_numbers": dict(type="bool", default=False), |
| 195 |
"require_uppercase": dict(type="bool", default=False), |
| 196 |
"require_lowercase": dict(type="bool", default=False), |
| 197 |
"allow_pw_change": dict(type="bool", aliases=["allow_password_change"], default=False), |
| 198 |
"pw_max_age": dict(type="int", aliases=["password_max_age"], default=0), |
| 199 |
"pw_reuse_prevent": dict(type="int", aliases=["password_reuse_prevent", "prevent_reuse"], default=0), |
| 200 |
"pw_expire": dict(type="bool", aliases=["password_expire", "expire"], default=False), |
| 201 |
}, |
| 202 |
supports_check_mode=True, |
| 203 |
) |
| 204 |
|
| 205 |
resource = IAMConnection(module) |
| 206 |
policy = resource.connection.AccountPasswordPolicy() |
| 207 |
|
| 208 |
state = module.params.get("state") |
| 209 |
|
| 210 |
if state == "present": |
| 211 |
(changed, new_policy, update_result) = resource.update_password_policy(module, policy) |
| 212 |
module.exit_json(changed=changed, task_status={"IAM": update_result}, policy=new_policy) |
| 213 |
|
| 214 |
if state == "absent": |
| 215 |
delete_result = resource.delete_password_policy(policy) |
| 216 |
module.exit_json(changed=True, task_status={"IAM": delete_result}) |
| 217 |
|
| 218 |
|
| 219 |
if __name__ == "__main__": |
| 220 |
main() |