| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 Ansible Project |
| 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_identity_policy |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manages SES sending authorization policies |
| 12 |
description: |
| 13 |
- This module allows the user to manage sending authorization policies associated with an SES |
| 14 |
identity (email or domain). |
| 15 |
- SES authorization sending policies can be used to control what actors are able to send email |
| 16 |
on behalf of the validated identity and what conditions must be met by the sent emails. |
| 17 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_ses_identity_policy). |
| 18 |
The usage did not change. |
| 19 |
author: |
| 20 |
- Ed Costello (@orthanc) |
| 21 |
|
| 22 |
options: |
| 23 |
identity: |
| 24 |
description: | |
| 25 |
The SES identity to attach or remove a policy from. This can be either the full ARN or just |
| 26 |
the verified email or domain. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
policy_name: |
| 30 |
description: The name used to identify the policy within the scope of the identity it's attached to. |
| 31 |
required: true |
| 32 |
type: str |
| 33 |
policy: |
| 34 |
description: A properly formatted JSON sending authorization policy. Required when I(state=present). |
| 35 |
type: json |
| 36 |
state: |
| 37 |
description: Whether to create(or update) or delete the authorization policy on the identity. |
| 38 |
default: present |
| 39 |
choices: [ 'present', 'absent' ] |
| 40 |
type: str |
| 41 |
extends_documentation_fragment: |
| 42 |
- amazon.aws.common.modules |
| 43 |
- amazon.aws.region.modules |
| 44 |
- amazon.aws.boto3 |
| 45 |
""" |
| 46 |
|
| 47 |
EXAMPLES = r""" |
| 48 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 49 |
|
| 50 |
- name: add sending authorization policy to domain identity |
| 51 |
community.aws.ses_identity_policy: |
| 52 |
identity: example.com |
| 53 |
policy_name: ExamplePolicy |
| 54 |
policy: "{{ lookup('template', 'policy.json.j2') }}" |
| 55 |
state: present |
| 56 |
|
| 57 |
- name: add sending authorization policy to email identity |
| 58 |
community.aws.ses_identity_policy: |
| 59 |
identity: [email protected] |
| 60 |
policy_name: ExamplePolicy |
| 61 |
policy: "{{ lookup('template', 'policy.json.j2') }}" |
| 62 |
state: present |
| 63 |
|
| 64 |
- name: add sending authorization policy to identity using ARN |
| 65 |
community.aws.ses_identity_policy: |
| 66 |
identity: "arn:aws:ses:us-east-1:12345678:identity/example.com" |
| 67 |
policy_name: ExamplePolicy |
| 68 |
policy: "{{ lookup('template', 'policy.json.j2') }}" |
| 69 |
state: present |
| 70 |
|
| 71 |
- name: remove sending authorization policy |
| 72 |
community.aws.ses_identity_policy: |
| 73 |
identity: example.com |
| 74 |
policy_name: ExamplePolicy |
| 75 |
state: absent |
| 76 |
""" |
| 77 |
|
| 78 |
RETURN = r""" |
| 79 |
policies: |
| 80 |
description: A list of all policies present on the identity after the operation. |
| 81 |
returned: success |
| 82 |
type: list |
| 83 |
sample: [ExamplePolicy] |
| 84 |
""" |
| 85 |
|
| 86 |
import json |
| 87 |
|
| 88 |
try: |
| 89 |
from botocore.exceptions import BotoCoreError |
| 90 |
from botocore.exceptions import ClientError |
| 91 |
except ImportError: |
| 92 |
pass # caught by AnsibleAWSModule |
| 93 |
|
| 94 |
from ansible_collections.amazon.aws.plugins.module_utils.policy import compare_policies |
| 95 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 96 |
|
| 97 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 98 |
|
| 99 |
|
| 100 |
def get_identity_policy(connection, module, identity, policy_name): |
| 101 |
try: |
| 102 |
response = connection.get_identity_policies(Identity=identity, PolicyNames=[policy_name], aws_retry=True) |
| 103 |
except (BotoCoreError, ClientError) as e: |
| 104 |
module.fail_json_aws(e, msg=f"Failed to retrieve identity policy {policy_name}") |
| 105 |
policies = response["Policies"] |
| 106 |
if policy_name in policies: |
| 107 |
return policies[policy_name] |
| 108 |
return None |
| 109 |
|
| 110 |
|
| 111 |
def create_or_update_identity_policy(connection, module): |
| 112 |
identity = module.params.get("identity") |
| 113 |
policy_name = module.params.get("policy_name") |
| 114 |
required_policy = module.params.get("policy") |
| 115 |
required_policy_dict = json.loads(required_policy) |
| 116 |
|
| 117 |
changed = False |
| 118 |
policy = get_identity_policy(connection, module, identity, policy_name) |
| 119 |
policy_dict = json.loads(policy) if policy else None |
| 120 |
if compare_policies(policy_dict, required_policy_dict): |
| 121 |
changed = True |
| 122 |
try: |
| 123 |
if not module.check_mode: |
| 124 |
connection.put_identity_policy( |
| 125 |
Identity=identity, PolicyName=policy_name, Policy=required_policy, aws_retry=True |
| 126 |
) |
| 127 |
except (BotoCoreError, ClientError) as e: |
| 128 |
module.fail_json_aws(e, msg=f"Failed to put identity policy {policy_name}") |
| 129 |
|
| 130 |
# Load the list of applied policies to include in the response. |
| 131 |
# In principle we should be able to just return the response, but given |
| 132 |
# eventual consistency behaviours in AWS it's plausible that we could |
| 133 |
# end up with a list that doesn't contain the policy we just added. |
| 134 |
# So out of paranoia check for this case and if we're missing the policy |
| 135 |
# just make sure it's present. |
| 136 |
# |
| 137 |
# As a nice side benefit this also means the return is correct in check mode |
| 138 |
try: |
| 139 |
policies_present = connection.list_identity_policies(Identity=identity, aws_retry=True)["PolicyNames"] |
| 140 |
except (BotoCoreError, ClientError) as e: |
| 141 |
module.fail_json_aws(e, msg="Failed to list identity policies") |
| 142 |
if policy_name is not None and policy_name not in policies_present: |
| 143 |
policies_present = list(policies_present) |
| 144 |
policies_present.append(policy_name) |
| 145 |
module.exit_json( |
| 146 |
changed=changed, |
| 147 |
policies=policies_present, |
| 148 |
) |
| 149 |
|
| 150 |
|
| 151 |
def delete_identity_policy(connection, module): |
| 152 |
identity = module.params.get("identity") |
| 153 |
policy_name = module.params.get("policy_name") |
| 154 |
|
| 155 |
changed = False |
| 156 |
try: |
| 157 |
policies_present = connection.list_identity_policies(Identity=identity, aws_retry=True)["PolicyNames"] |
| 158 |
except (BotoCoreError, ClientError) as e: |
| 159 |
module.fail_json_aws(e, msg="Failed to list identity policies") |
| 160 |
if policy_name in policies_present: |
| 161 |
try: |
| 162 |
if not module.check_mode: |
| 163 |
connection.delete_identity_policy(Identity=identity, PolicyName=policy_name, aws_retry=True) |
| 164 |
except (BotoCoreError, ClientError) as e: |
| 165 |
module.fail_json_aws(e, msg=f"Failed to delete identity policy {policy_name}") |
| 166 |
changed = True |
| 167 |
policies_present = list(policies_present) |
| 168 |
policies_present.remove(policy_name) |
| 169 |
|
| 170 |
module.exit_json( |
| 171 |
changed=changed, |
| 172 |
policies=policies_present, |
| 173 |
) |
| 174 |
|
| 175 |
|
| 176 |
def main(): |
| 177 |
module = AnsibleAWSModule( |
| 178 |
argument_spec={ |
| 179 |
"identity": dict(required=True, type="str"), |
| 180 |
"state": dict(default="present", choices=["present", "absent"]), |
| 181 |
"policy_name": dict(required=True, type="str"), |
| 182 |
"policy": dict(type="json", default=None), |
| 183 |
}, |
| 184 |
required_if=[["state", "present", ["policy"]]], |
| 185 |
supports_check_mode=True, |
| 186 |
) |
| 187 |
|
| 188 |
# SES APIs seem to have a much lower throttling threshold than most of the rest of the AWS APIs. |
| 189 |
# Docs say 1 call per second. This shouldn't actually be a big problem for normal usage, but |
| 190 |
# the ansible build runs multiple instances of the test in parallel that's caused throttling |
| 191 |
# failures so apply a jittered backoff to call SES calls. |
| 192 |
connection = module.client("ses", retry_decorator=AWSRetry.jittered_backoff()) |
| 193 |
|
| 194 |
state = module.params.get("state") |
| 195 |
|
| 196 |
if state == "present": |
| 197 |
create_or_update_identity_policy(connection, module) |
| 198 |
else: |
| 199 |
delete_identity_policy(connection, module) |
| 200 |
|
| 201 |
|
| 202 |
if __name__ == "__main__": |
| 203 |
main() |