| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017, Ben Tomasik <[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: ses_rule_set |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manages SES inbound receipt rule sets |
| 12 |
description: |
| 13 |
- This module allows you to create, delete, and manage SES receipt rule sets |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_ses_rule_set). |
| 15 |
The usage did not change. |
| 16 |
author: |
| 17 |
- "Ben Tomasik (@tomislacker)" |
| 18 |
- "Ed Costello (@orthanc)" |
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name of the receipt rule set. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
state: |
| 26 |
description: |
| 27 |
- Whether to create (or update) or destroy the receipt rule set. |
| 28 |
required: False |
| 29 |
default: present |
| 30 |
choices: ["absent", "present"] |
| 31 |
type: str |
| 32 |
active: |
| 33 |
description: |
| 34 |
- Whether or not this rule set should be the active rule set. Only has an impact if I(state) is C(present). |
| 35 |
- If omitted, the active rule set will not be changed. |
| 36 |
- If C(True) then this rule set will be made active and all others inactive. |
| 37 |
- if C(False) then this rule set will be deactivated. Be careful with this as you can end up with no active rule set. |
| 38 |
type: bool |
| 39 |
required: False |
| 40 |
force: |
| 41 |
description: |
| 42 |
- When deleting a rule set, deactivate it first (AWS prevents deletion of the active rule set). |
| 43 |
type: bool |
| 44 |
required: False |
| 45 |
default: False |
| 46 |
extends_documentation_fragment: |
| 47 |
- amazon.aws.common.modules |
| 48 |
- amazon.aws.region.modules |
| 49 |
- amazon.aws.boto3 |
| 50 |
""" |
| 51 |
|
| 52 |
EXAMPLES = r""" |
| 53 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 54 |
|
| 55 |
- name: Create default rule set and activate it if not already |
| 56 |
community.aws.ses_rule_set: |
| 57 |
name: default-rule-set |
| 58 |
state: present |
| 59 |
active: true |
| 60 |
|
| 61 |
- name: Create some arbitrary rule set but do not activate it |
| 62 |
community.aws.ses_rule_set: |
| 63 |
name: arbitrary-rule-set |
| 64 |
state: present |
| 65 |
|
| 66 |
- name: Explicitly deactivate the default rule set leaving no active rule set |
| 67 |
community.aws.ses_rule_set: |
| 68 |
name: default-rule-set |
| 69 |
state: present |
| 70 |
active: false |
| 71 |
|
| 72 |
- name: Remove an arbitrary inactive rule set |
| 73 |
community.aws.ses_rule_set: |
| 74 |
name: arbitrary-rule-set |
| 75 |
state: absent |
| 76 |
|
| 77 |
- name: Remove an ruleset even if we have to first deactivate it to remove it |
| 78 |
community.aws.ses_rule_set: |
| 79 |
name: default-rule-set |
| 80 |
state: absent |
| 81 |
force: true |
| 82 |
""" |
| 83 |
|
| 84 |
RETURN = r""" |
| 85 |
active: |
| 86 |
description: if the SES rule set is active |
| 87 |
returned: success if I(state) is C(present) |
| 88 |
type: bool |
| 89 |
sample: true |
| 90 |
rule_sets: |
| 91 |
description: The list of SES receipt rule sets that exist after any changes. |
| 92 |
returned: success |
| 93 |
type: list |
| 94 |
sample: [{ |
| 95 |
"created_timestamp": "2018-02-25T01:20:32.690000+00:00", |
| 96 |
"name": "default-rule-set" |
| 97 |
}] |
| 98 |
""" |
| 99 |
|
| 100 |
try: |
| 101 |
from botocore.exceptions import BotoCoreError |
| 102 |
from botocore.exceptions import ClientError |
| 103 |
except ImportError: |
| 104 |
pass # handled by AnsibleAWSModule |
| 105 |
|
| 106 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 107 |
|
| 108 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 109 |
|
| 110 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 111 |
|
| 112 |
|
| 113 |
def list_rule_sets(client, module): |
| 114 |
try: |
| 115 |
response = client.list_receipt_rule_sets(aws_retry=True) |
| 116 |
except (BotoCoreError, ClientError) as e: |
| 117 |
module.fail_json_aws(e, msg="Couldn't list rule sets.") |
| 118 |
return response["RuleSets"] |
| 119 |
|
| 120 |
|
| 121 |
def rule_set_in(name, rule_sets): |
| 122 |
return any(s for s in rule_sets if s["Name"] == name) |
| 123 |
|
| 124 |
|
| 125 |
def ruleset_active(client, module, name): |
| 126 |
try: |
| 127 |
active_rule_set = client.describe_active_receipt_rule_set(aws_retry=True) |
| 128 |
except (BotoCoreError, ClientError) as e: |
| 129 |
module.fail_json_aws(e, msg="Couldn't get the active rule set.") |
| 130 |
if active_rule_set is not None and "Metadata" in active_rule_set: |
| 131 |
return name == active_rule_set["Metadata"]["Name"] |
| 132 |
else: |
| 133 |
# Metadata was not set meaning there is no active rule set |
| 134 |
return False |
| 135 |
|
| 136 |
|
| 137 |
def deactivate_rule_set(client, module): |
| 138 |
try: |
| 139 |
# No ruleset name deactivates all rulesets |
| 140 |
client.set_active_receipt_rule_set(aws_retry=True) |
| 141 |
except (BotoCoreError, ClientError) as e: |
| 142 |
module.fail_json_aws(e, msg="Couldn't set active rule set to None.") |
| 143 |
|
| 144 |
|
| 145 |
def update_active_rule_set(client, module, name, desired_active): |
| 146 |
check_mode = module.check_mode |
| 147 |
|
| 148 |
active = ruleset_active(client, module, name) |
| 149 |
|
| 150 |
changed = False |
| 151 |
if desired_active is not None: |
| 152 |
if desired_active and not active: |
| 153 |
if not check_mode: |
| 154 |
try: |
| 155 |
client.set_active_receipt_rule_set(RuleSetName=name, aws_retry=True) |
| 156 |
except (BotoCoreError, ClientError) as e: |
| 157 |
module.fail_json_aws(e, msg=f"Couldn't set active rule set to {name}.") |
| 158 |
changed = True |
| 159 |
active = True |
| 160 |
elif not desired_active and active: |
| 161 |
if not check_mode: |
| 162 |
deactivate_rule_set(client, module) |
| 163 |
changed = True |
| 164 |
active = False |
| 165 |
return changed, active |
| 166 |
|
| 167 |
|
| 168 |
def create_or_update_rule_set(client, module): |
| 169 |
name = module.params.get("name") |
| 170 |
check_mode = module.check_mode |
| 171 |
changed = False |
| 172 |
|
| 173 |
rule_sets = list_rule_sets(client, module) |
| 174 |
if not rule_set_in(name, rule_sets): |
| 175 |
if not check_mode: |
| 176 |
try: |
| 177 |
client.create_receipt_rule_set(RuleSetName=name, aws_retry=True) |
| 178 |
except (BotoCoreError, ClientError) as e: |
| 179 |
module.fail_json_aws(e, msg=f"Couldn't create rule set {name}.") |
| 180 |
changed = True |
| 181 |
rule_sets = list(rule_sets) |
| 182 |
rule_sets.append( |
| 183 |
{ |
| 184 |
"Name": name, |
| 185 |
} |
| 186 |
) |
| 187 |
|
| 188 |
(active_changed, active) = update_active_rule_set(client, module, name, module.params.get("active")) |
| 189 |
changed |= active_changed |
| 190 |
|
| 191 |
module.exit_json( |
| 192 |
changed=changed, |
| 193 |
active=active, |
| 194 |
rule_sets=[camel_dict_to_snake_dict(x) for x in rule_sets], |
| 195 |
) |
| 196 |
|
| 197 |
|
| 198 |
def remove_rule_set(client, module): |
| 199 |
name = module.params.get("name") |
| 200 |
check_mode = module.check_mode |
| 201 |
changed = False |
| 202 |
|
| 203 |
rule_sets = list_rule_sets(client, module) |
| 204 |
if rule_set_in(name, rule_sets): |
| 205 |
active = ruleset_active(client, module, name) |
| 206 |
if active and not module.params.get("force"): |
| 207 |
module.fail_json( |
| 208 |
msg=( |
| 209 |
f"Couldn't delete rule set {name} because it is currently active. Set force=true to delete an" |
| 210 |
" active ruleset." |
| 211 |
), |
| 212 |
error={ |
| 213 |
"code": "CannotDelete", |
| 214 |
"message": f"Cannot delete active rule set: {name}", |
| 215 |
}, |
| 216 |
) |
| 217 |
if not check_mode: |
| 218 |
if active and module.params.get("force"): |
| 219 |
deactivate_rule_set(client, module) |
| 220 |
try: |
| 221 |
client.delete_receipt_rule_set(RuleSetName=name, aws_retry=True) |
| 222 |
except (BotoCoreError, ClientError) as e: |
| 223 |
module.fail_json_aws(e, msg=f"Couldn't delete rule set {name}.") |
| 224 |
changed = True |
| 225 |
rule_sets = [x for x in rule_sets if x["Name"] != name] |
| 226 |
|
| 227 |
module.exit_json( |
| 228 |
changed=changed, |
| 229 |
rule_sets=[camel_dict_to_snake_dict(x) for x in rule_sets], |
| 230 |
) |
| 231 |
|
| 232 |
|
| 233 |
def main(): |
| 234 |
argument_spec = dict( |
| 235 |
name=dict(type="str", required=True), |
| 236 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 237 |
active=dict(type="bool"), |
| 238 |
force=dict(type="bool", default=False), |
| 239 |
) |
| 240 |
|
| 241 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 242 |
|
| 243 |
state = module.params.get("state") |
| 244 |
|
| 245 |
# SES APIs seem to have a much lower throttling threshold than most of the rest of the AWS APIs. |
| 246 |
# Docs say 1 call per second. This shouldn't actually be a big problem for normal usage, but |
| 247 |
# the ansible build runs multiple instances of the test in parallel that's caused throttling |
| 248 |
# failures so apply a jittered backoff to call SES calls. |
| 249 |
client = module.client("ses", retry_decorator=AWSRetry.jittered_backoff()) |
| 250 |
|
| 251 |
if state == "absent": |
| 252 |
remove_rule_set(client, module) |
| 253 |
else: |
| 254 |
create_or_update_rule_set(client, module) |
| 255 |
|
| 256 |
|
| 257 |
if __name__ == "__main__": |
| 258 |
main() |