| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: 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: s3_logging |
| 10 |
version_added: 1.0.0 |
| 11 |
short_description: Manage logging facility of an s3 bucket in AWS |
| 12 |
description: |
| 13 |
- Manage logging facility of an s3 bucket in AWS |
| 14 |
author: |
| 15 |
- Rob White (@wimnat) |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- "Name of the s3 bucket." |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
state: |
| 23 |
description: |
| 24 |
- "Enable or disable logging." |
| 25 |
default: present |
| 26 |
choices: [ 'present', 'absent' ] |
| 27 |
type: str |
| 28 |
target_bucket: |
| 29 |
description: |
| 30 |
- "The bucket to log to. Required when state=present." |
| 31 |
type: str |
| 32 |
target_prefix: |
| 33 |
description: |
| 34 |
- "The prefix that should be prepended to the generated log files written to the target_bucket." |
| 35 |
default: "" |
| 36 |
type: str |
| 37 |
extends_documentation_fragment: |
| 38 |
- amazon.aws.common.modules |
| 39 |
- amazon.aws.region.modules |
| 40 |
- amazon.aws.boto3 |
| 41 |
""" |
| 42 |
|
| 43 |
RETURN = r""" # """ |
| 44 |
|
| 45 |
EXAMPLES = r""" |
| 46 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 47 |
|
| 48 |
- name: Enable logging of s3 bucket mywebsite.com to s3 bucket mylogs |
| 49 |
community.aws.s3_logging: |
| 50 |
name: mywebsite.com |
| 51 |
target_bucket: mylogs |
| 52 |
target_prefix: logs/mywebsite.com |
| 53 |
state: present |
| 54 |
|
| 55 |
- name: Remove logging on an s3 bucket |
| 56 |
community.aws.s3_logging: |
| 57 |
name: mywebsite.com |
| 58 |
state: absent |
| 59 |
""" |
| 60 |
|
| 61 |
try: |
| 62 |
import botocore |
| 63 |
except ImportError: |
| 64 |
pass # Handled by AnsibleAWSModule |
| 65 |
|
| 66 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 67 |
|
| 68 |
from ansible_collections.amazon.aws.plugins.module_utils.botocore import is_boto3_error_code |
| 69 |
from ansible_collections.amazon.aws.plugins.module_utils.retries import AWSRetry |
| 70 |
|
| 71 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 72 |
|
| 73 |
|
| 74 |
def compare_bucket_logging(bucket_logging, target_bucket, target_prefix): |
| 75 |
if not bucket_logging.get("LoggingEnabled", False): |
| 76 |
if target_bucket: |
| 77 |
return True |
| 78 |
return False |
| 79 |
|
| 80 |
logging = bucket_logging["LoggingEnabled"] |
| 81 |
if logging["TargetBucket"] != target_bucket: |
| 82 |
return True |
| 83 |
if logging["TargetPrefix"] != target_prefix: |
| 84 |
return True |
| 85 |
return False |
| 86 |
|
| 87 |
|
| 88 |
def verify_acls(connection, module, target_bucket): |
| 89 |
try: |
| 90 |
current_acl = connection.get_bucket_acl(aws_retry=True, Bucket=target_bucket) |
| 91 |
current_grants = current_acl["Grants"] |
| 92 |
except is_boto3_error_code("NoSuchBucket"): |
| 93 |
module.fail_json(msg=f"Target Bucket '{target_bucket}' not found") |
| 94 |
except ( |
| 95 |
botocore.exceptions.BotoCoreError, |
| 96 |
botocore.exceptions.ClientError, |
| 97 |
) as e: # pylint: disable=duplicate-except |
| 98 |
module.fail_json_aws(e, msg="Failed to fetch target bucket ACL") |
| 99 |
|
| 100 |
required_grant = { |
| 101 |
"Grantee": {"URI": "http://acs.amazonaws.com/groups/s3/LogDelivery", "Type": "Group"}, |
| 102 |
"Permission": "FULL_CONTROL", |
| 103 |
} |
| 104 |
|
| 105 |
for grant in current_grants: |
| 106 |
if grant == required_grant: |
| 107 |
return False |
| 108 |
|
| 109 |
if module.check_mode: |
| 110 |
return True |
| 111 |
|
| 112 |
updated_acl = dict(current_acl) |
| 113 |
updated_grants = list(current_grants) |
| 114 |
updated_grants.append(required_grant) |
| 115 |
updated_acl["Grants"] = updated_grants |
| 116 |
del updated_acl["ResponseMetadata"] |
| 117 |
try: |
| 118 |
connection.put_bucket_acl(aws_retry=True, Bucket=target_bucket, AccessControlPolicy=updated_acl) |
| 119 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 120 |
module.fail_json_aws(e, msg="Failed to update target bucket ACL to allow log delivery") |
| 121 |
|
| 122 |
return True |
| 123 |
|
| 124 |
|
| 125 |
def enable_bucket_logging(connection, module): |
| 126 |
bucket_name = module.params.get("name") |
| 127 |
target_bucket = module.params.get("target_bucket") |
| 128 |
target_prefix = module.params.get("target_prefix") |
| 129 |
changed = False |
| 130 |
|
| 131 |
try: |
| 132 |
bucket_logging = connection.get_bucket_logging(aws_retry=True, Bucket=bucket_name) |
| 133 |
except is_boto3_error_code("NoSuchBucket"): |
| 134 |
module.fail_json(msg=f"Bucket '{bucket_name}' not found") |
| 135 |
except ( |
| 136 |
botocore.exceptions.BotoCoreError, |
| 137 |
botocore.exceptions.ClientError, |
| 138 |
) as e: # pylint: disable=duplicate-except |
| 139 |
module.fail_json_aws(e, msg="Failed to fetch current logging status") |
| 140 |
|
| 141 |
try: |
| 142 |
changed |= verify_acls(connection, module, target_bucket) |
| 143 |
|
| 144 |
if not compare_bucket_logging(bucket_logging, target_bucket, target_prefix): |
| 145 |
bucket_logging = camel_dict_to_snake_dict(bucket_logging) |
| 146 |
module.exit_json(changed=changed, **bucket_logging) |
| 147 |
|
| 148 |
if module.check_mode: |
| 149 |
module.exit_json(changed=True) |
| 150 |
|
| 151 |
result = connection.put_bucket_logging( |
| 152 |
aws_retry=True, |
| 153 |
Bucket=bucket_name, |
| 154 |
BucketLoggingStatus={ |
| 155 |
"LoggingEnabled": { |
| 156 |
"TargetBucket": target_bucket, |
| 157 |
"TargetPrefix": target_prefix, |
| 158 |
} |
| 159 |
}, |
| 160 |
) |
| 161 |
|
| 162 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 163 |
module.fail_json_aws(e, msg="Failed to enable bucket logging") |
| 164 |
|
| 165 |
result = camel_dict_to_snake_dict(result) |
| 166 |
module.exit_json(changed=True, **result) |
| 167 |
|
| 168 |
|
| 169 |
def disable_bucket_logging(connection, module): |
| 170 |
bucket_name = module.params.get("name") |
| 171 |
|
| 172 |
try: |
| 173 |
bucket_logging = connection.get_bucket_logging(aws_retry=True, Bucket=bucket_name) |
| 174 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 175 |
module.fail_json_aws(e, msg="Failed to fetch current logging status") |
| 176 |
|
| 177 |
if not compare_bucket_logging(bucket_logging, None, None): |
| 178 |
module.exit_json(changed=False) |
| 179 |
|
| 180 |
if module.check_mode: |
| 181 |
module.exit_json(changed=True) |
| 182 |
|
| 183 |
try: |
| 184 |
AWSRetry.jittered_backoff(catch_extra_error_codes=["InvalidTargetBucketForLogging"])( |
| 185 |
connection.put_bucket_logging |
| 186 |
)(Bucket=bucket_name, BucketLoggingStatus={}) |
| 187 |
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: |
| 188 |
module.fail_json_aws(e, msg="Failed to disable bucket logging") |
| 189 |
|
| 190 |
module.exit_json(changed=True) |
| 191 |
|
| 192 |
|
| 193 |
def main(): |
| 194 |
argument_spec = dict( |
| 195 |
name=dict(required=True), |
| 196 |
target_bucket=dict(required=False, default=None), |
| 197 |
target_prefix=dict(required=False, default=""), |
| 198 |
state=dict(required=False, default="present", choices=["present", "absent"]), |
| 199 |
) |
| 200 |
|
| 201 |
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) |
| 202 |
|
| 203 |
connection = module.client("s3", retry_decorator=AWSRetry.jittered_backoff()) |
| 204 |
state = module.params.get("state") |
| 205 |
|
| 206 |
if state == "present": |
| 207 |
enable_bucket_logging(connection, module) |
| 208 |
elif state == "absent": |
| 209 |
disable_bucket_logging(connection, module) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == "__main__": |
| 213 |
main() |