| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright (c) 2018 Red Hat, Inc. |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import copy |
| 9 |
import typing |
| 10 |
|
| 11 |
if typing.TYPE_CHECKING: |
| 12 |
from typing import List |
| 13 |
from typing import Optional |
| 14 |
|
| 15 |
from ansible.module_utils.basic import to_text |
| 16 |
|
| 17 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import boto3_resource_to_ansible_dict |
| 18 |
|
| 19 |
|
| 20 |
def normalize_s3_bucket_versioning(versioning_status: Optional[dict]) -> Optional[dict]: |
| 21 |
""" |
| 22 |
Normalize S3 bucket versioning status to Ansible dictionary format. |
| 23 |
|
| 24 |
Parameters: |
| 25 |
versioning_status (dict): The versioning status returned from boto3. |
| 26 |
|
| 27 |
Returns: |
| 28 |
Normalized versioning status dictionary with both legacy and current formats. |
| 29 |
""" |
| 30 |
if not versioning_status: |
| 31 |
return versioning_status |
| 32 |
versioning_result = typing.cast(dict, boto3_resource_to_ansible_dict(versioning_status)) |
| 33 |
# Original s3_bucket format, no longer advertised but not officially deprecated |
| 34 |
versioning_result["Versioning"] = versioning_status.get("Status", "Disabled") |
| 35 |
versioning_result["MfaDelete"] = versioning_status.get("MFADelete", "Disabled") |
| 36 |
# Original s3_bucket_info format, no longer advertised but not officially deprecated |
| 37 |
versioning_result["Status"] = versioning_status.get("Status", "Disabled") |
| 38 |
versioning_result["MFADelete"] = versioning_status.get("MFADelete", "Disabled") |
| 39 |
return versioning_result |
| 40 |
|
| 41 |
|
| 42 |
def normalize_s3_bucket_public_access(public_access_status: Optional[dict]) -> Optional[dict]: |
| 43 |
""" |
| 44 |
Normalize S3 bucket public access block configuration to Ansible dictionary format. |
| 45 |
|
| 46 |
Parameters: |
| 47 |
public_access_status (dict): The public access block configuration from boto3. |
| 48 |
|
| 49 |
Returns: |
| 50 |
Normalized public access configuration dictionary. |
| 51 |
""" |
| 52 |
if not public_access_status: |
| 53 |
return public_access_status |
| 54 |
public_access_result = typing.cast(dict, boto3_resource_to_ansible_dict(public_access_status)) |
| 55 |
public_access_result["PublicAccessBlockConfiguration"] = copy.deepcopy(public_access_status) |
| 56 |
public_access_result.update(public_access_status) |
| 57 |
return public_access_result |
| 58 |
|
| 59 |
|
| 60 |
def normalize_s3_bucket_acls(acls: Optional[dict]) -> Optional[dict]: |
| 61 |
""" |
| 62 |
Normalize S3 bucket ACLs to Ansible dictionary format. |
| 63 |
|
| 64 |
Parameters: |
| 65 |
acls (dict): The ACL configuration returned from boto3. |
| 66 |
|
| 67 |
Returns: |
| 68 |
Normalized ACL grants dictionary. |
| 69 |
""" |
| 70 |
if not acls: |
| 71 |
return acls |
| 72 |
acls_result = typing.cast(dict, boto3_resource_to_ansible_dict(acls)) |
| 73 |
return typing.cast(dict, acls_result["grants"]) |
| 74 |
|
| 75 |
|
| 76 |
def normalize_s3_bucket_logging(logging_config: Optional[dict]) -> Optional[dict]: |
| 77 |
""" |
| 78 |
Normalize S3 bucket logging configuration to Ansible dictionary format. |
| 79 |
|
| 80 |
Parameters: |
| 81 |
logging_config (dict): The logging configuration returned from boto3. |
| 82 |
|
| 83 |
Returns: |
| 84 |
Normalized logging configuration dictionary or None if logging is disabled. |
| 85 |
""" |
| 86 |
if not logging_config or "LoggingEnabled" not in logging_config: |
| 87 |
return None |
| 88 |
|
| 89 |
result = boto3_resource_to_ansible_dict(logging_config.get("LoggingEnabled"), force_tags=False) |
| 90 |
# Ensure target_prefix is always present, even if not in source |
| 91 |
if "target_prefix" not in result: |
| 92 |
result["target_prefix"] = "" |
| 93 |
return result |
| 94 |
|
| 95 |
|
| 96 |
def _grantee_is_owner(grant: dict, owner_id: str) -> bool: |
| 97 |
return grant.get("Grantee", {}).get("ID") == owner_id |
| 98 |
|
| 99 |
|
| 100 |
def _grantee_is_public(grant: dict) -> bool: |
| 101 |
return grant.get("Grantee", {}).get("URI") == "http://acs.amazonaws.com/groups/global/AllUsers" |
| 102 |
|
| 103 |
|
| 104 |
def _grantee_is_authenticated(grant: dict) -> bool: |
| 105 |
return grant.get("Grantee", {}).get("URI") == "http://acs.amazonaws.com/groups/global/AuthenticatedUsers" |
| 106 |
|
| 107 |
|
| 108 |
def _acl_permissions(grants: Optional[List[dict]]) -> List[str]: |
| 109 |
if not grants: |
| 110 |
return [] |
| 111 |
return [grant.get("Permission") for grant in grants if grant] |
| 112 |
|
| 113 |
|
| 114 |
def s3_acl_to_name(acl: Optional[dict]) -> Optional[str]: |
| 115 |
""" |
| 116 |
Convert S3 ACL grant structure to a named canned ACL template. |
| 117 |
|
| 118 |
Attempts to match the ACL grants to standard AWS canned ACLs by analyzing |
| 119 |
the owner, authenticated user, and public grants. |
| 120 |
|
| 121 |
Parameters: |
| 122 |
acl (dict): The ACL structure with 'Grants' and 'Owner' keys. |
| 123 |
|
| 124 |
Returns: |
| 125 |
The name of the matching canned ACL ('private', 'public-read', |
| 126 |
'public-read-write', 'authenticated-read'), or None if no match is found. |
| 127 |
""" |
| 128 |
if not acl: |
| 129 |
return None |
| 130 |
|
| 131 |
try: |
| 132 |
grants = acl["Grants"] |
| 133 |
owner_id = acl["Owner"]["ID"] |
| 134 |
owner_acl = [grant for grant in grants if _grantee_is_owner(grant, owner_id)] |
| 135 |
auth_acl = [grant for grant in grants if _grantee_is_authenticated(grant)] |
| 136 |
public_acl = [grant for grant in grants if _grantee_is_public(grant)] |
| 137 |
|
| 138 |
if len(grants) > (len(owner_acl) + len(auth_acl) + len(public_acl)): |
| 139 |
raise ValueError("Unrecognised Grantee") |
| 140 |
if public_acl and auth_acl: |
| 141 |
raise ValueError("Public ACLs and Authenticated User ACLs are only used alone in templated ACL") |
| 142 |
|
| 143 |
if ["FULL_CONTROL"] != _acl_permissions(owner_acl): |
| 144 |
raise ValueError("Owner doesn't have full control") |
| 145 |
if len(grants) == 1: |
| 146 |
return "private" |
| 147 |
|
| 148 |
if auth_acl: |
| 149 |
if ["READ"] == _acl_permissions(auth_acl): |
| 150 |
return "authenticated-read" |
| 151 |
raise ValueError("Authenticated User ACLs don't match templated ACL") |
| 152 |
|
| 153 |
permissions = sorted(_acl_permissions(public_acl)) |
| 154 |
if permissions == ["READ"]: |
| 155 |
return "public-read" |
| 156 |
if permissions == ["READ", "WRITE"]: |
| 157 |
return "public-read-write" |
| 158 |
|
| 159 |
raise ValueError("Public ACLs don't match templated ACL") |
| 160 |
|
| 161 |
except (KeyError, IndexError, ValueError): |
| 162 |
return None |
| 163 |
|
| 164 |
|
| 165 |
def merge_tags(current_tags: dict, new_tags: dict, purge_tags: bool = True) -> dict: |
| 166 |
""" |
| 167 |
Compare and merge two dicts of tags. |
| 168 |
|
| 169 |
:param current_tags: The current tags |
| 170 |
:param new_tags: The tags passed as a parameter |
| 171 |
:param purge_tags: Whether to remove current tags that aren't in new_tags |
| 172 |
:return: updated_tags: The updated dictionary of tags |
| 173 |
""" |
| 174 |
|
| 175 |
if new_tags is None: |
| 176 |
return current_tags |
| 177 |
|
| 178 |
updated_tags = copy.deepcopy(current_tags) if not purge_tags else {} |
| 179 |
# Tags are always returned as text |
| 180 |
new_tags = dict((to_text(k), to_text(v)) for k, v in new_tags.items()) |
| 181 |
updated_tags.update(new_tags) |
| 182 |
|
| 183 |
return updated_tags |