| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: Contributors to the 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: ec2_vpc_nacl_info |
| 10 |
version_added: 1.0.0 |
| 11 |
version_added_collection: community.aws |
| 12 |
short_description: Gather information about Network ACLs in an AWS VPC |
| 13 |
description: |
| 14 |
- Gather information about Network ACLs in an AWS VPC. |
| 15 |
author: |
| 16 |
- "Brad Davidson (@brandond)" |
| 17 |
options: |
| 18 |
nacl_ids: |
| 19 |
description: |
| 20 |
- A list of Network ACL IDs to retrieve information about. |
| 21 |
required: false |
| 22 |
default: [] |
| 23 |
aliases: [nacl_id] |
| 24 |
type: list |
| 25 |
elements: str |
| 26 |
filters: |
| 27 |
description: |
| 28 |
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. See |
| 29 |
U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeNetworkAcls.html) for possible filters. Filter |
| 30 |
names and values are case sensitive. |
| 31 |
required: false |
| 32 |
default: {} |
| 33 |
type: dict |
| 34 |
notes: |
| 35 |
- By default, the module will return all Network ACLs. |
| 36 |
|
| 37 |
extends_documentation_fragment: |
| 38 |
- amazon.aws.common.modules |
| 39 |
- amazon.aws.region.modules |
| 40 |
- amazon.aws.boto3 |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
# Note: These examples do not set authentication details, see the AWS Guide for details. |
| 45 |
|
| 46 |
# Gather information about all Network ACLs: |
| 47 |
- name: Get All NACLs |
| 48 |
amazon.aws.ec2_vpc_nacl_info: |
| 49 |
region: us-west-2 |
| 50 |
register: all_nacls |
| 51 |
|
| 52 |
# Retrieve default Network ACLs: |
| 53 |
- name: Get Default NACLs |
| 54 |
amazon.aws.ec2_vpc_nacl_info: |
| 55 |
region: us-west-2 |
| 56 |
filters: |
| 57 |
'default': 'true' |
| 58 |
register: default_nacls |
| 59 |
""" |
| 60 |
|
| 61 |
RETURN = r""" |
| 62 |
nacls: |
| 63 |
description: Returns an array of complex objects as described below. |
| 64 |
returned: success |
| 65 |
type: complex |
| 66 |
contains: |
| 67 |
nacl_id: |
| 68 |
description: The ID of the Network Access Control List. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
vpc_id: |
| 72 |
description: The ID of the VPC that the NACL is attached to. |
| 73 |
returned: always |
| 74 |
type: str |
| 75 |
is_default: |
| 76 |
description: True if the NACL is the default for its VPC. |
| 77 |
returned: always |
| 78 |
type: bool |
| 79 |
tags: |
| 80 |
description: A dict of tags associated with the NACL. |
| 81 |
returned: always |
| 82 |
type: dict |
| 83 |
subnets: |
| 84 |
description: A list of subnet IDs that are associated with the NACL. |
| 85 |
returned: always |
| 86 |
type: list |
| 87 |
elements: str |
| 88 |
ingress: |
| 89 |
description: |
| 90 |
- A list of NACL ingress rules. |
| 91 |
- The rule format is C([rule no, protocol, allow/deny, v4 or v6 cidr, icmp_type, icmp_code, port from, port to]). |
| 92 |
returned: always |
| 93 |
type: list |
| 94 |
elements: list |
| 95 |
sample: [[100, 'tcp', 'allow', '0.0.0.0/0', null, null, 22, 22]] |
| 96 |
egress: |
| 97 |
description: |
| 98 |
- A list of NACL egress rules. |
| 99 |
- The rule format is C([rule no, protocol, allow/deny, v4 or v6 cidr, icmp_type, icmp_code, port from, port to]). |
| 100 |
returned: always |
| 101 |
type: list |
| 102 |
elements: list |
| 103 |
sample: [[100, 'all', 'allow', '0.0.0.0/0', null, null, null, null]] |
| 104 |
""" |
| 105 |
|
| 106 |
from typing import Any |
| 107 |
from typing import Dict |
| 108 |
from typing import List |
| 109 |
from typing import Union |
| 110 |
|
| 111 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 112 |
|
| 113 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AnsibleEC2Error |
| 114 |
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import describe_network_acls |
| 115 |
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule |
| 116 |
from ansible_collections.amazon.aws.plugins.module_utils.tagging import boto3_tag_list_to_ansible_dict |
| 117 |
from ansible_collections.amazon.aws.plugins.module_utils.transformation import ansible_dict_to_boto3_filter_list |
| 118 |
|
| 119 |
# VPC-supported IANA protocol numbers |
| 120 |
# http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml |
| 121 |
PROTOCOL_NAMES = {"-1": "all", "1": "icmp", "6": "tcp", "17": "udp"} |
| 122 |
|
| 123 |
|
| 124 |
def format_nacl(nacl: Dict[str, Any]) -> Dict[str, Any]: |
| 125 |
# Turn the boto3 result into ansible friendly snake cases |
| 126 |
nacl = camel_dict_to_snake_dict(nacl) |
| 127 |
|
| 128 |
# convert boto3 tags list into ansible dict |
| 129 |
if "tags" in nacl: |
| 130 |
nacl["tags"] = boto3_tag_list_to_ansible_dict(nacl["tags"], "key", "value") |
| 131 |
|
| 132 |
# Convert NACL entries |
| 133 |
if "entries" in nacl: |
| 134 |
nacl["egress"] = [ |
| 135 |
nacl_entry_to_list(entry) for entry in nacl["entries"] if entry["rule_number"] < 32767 and entry["egress"] |
| 136 |
] |
| 137 |
nacl["ingress"] = [ |
| 138 |
nacl_entry_to_list(entry) |
| 139 |
for entry in nacl["entries"] |
| 140 |
if entry["rule_number"] < 32767 and not entry["egress"] |
| 141 |
] |
| 142 |
del nacl["entries"] |
| 143 |
|
| 144 |
# Read subnets from NACL Associations |
| 145 |
if "associations" in nacl: |
| 146 |
nacl["subnets"] = [a["subnet_id"] for a in nacl["associations"]] |
| 147 |
del nacl["associations"] |
| 148 |
|
| 149 |
# Read Network ACL id |
| 150 |
if "network_acl_id" in nacl: |
| 151 |
nacl["nacl_id"] = nacl["network_acl_id"] |
| 152 |
del nacl["network_acl_id"] |
| 153 |
|
| 154 |
return nacl |
| 155 |
|
| 156 |
|
| 157 |
def list_ec2_vpc_nacls(connection, module: AnsibleAWSModule) -> None: |
| 158 |
nacl_ids = module.params.get("nacl_ids") |
| 159 |
filters = module.params.get("filters") |
| 160 |
|
| 161 |
params = {} |
| 162 |
if filters: |
| 163 |
params["Filters"] = ansible_dict_to_boto3_filter_list(filters) |
| 164 |
if nacl_ids: |
| 165 |
params["NetworkAclIds"] = nacl_ids |
| 166 |
|
| 167 |
try: |
| 168 |
network_acls = describe_network_acls(connection, **params) |
| 169 |
if nacl_ids and not len(nacl_ids) == len(network_acls): |
| 170 |
if len(nacl_ids) == 1: |
| 171 |
module.fail_json(msg="Unable to describe ACL. NetworkAcl does not exist.") |
| 172 |
else: |
| 173 |
module.fail_json(msg="Unable to describe all ACLs. One or more NetworkAcls does not exist.") |
| 174 |
except AnsibleEC2Error as e: |
| 175 |
module.fail_json_aws_error(e) |
| 176 |
|
| 177 |
module.exit_json(nacls=[format_nacl(nacl) for nacl in network_acls]) |
| 178 |
|
| 179 |
|
| 180 |
def nacl_entry_to_list(entry: Dict[str, Any]) -> List[Union[str, int, None]]: |
| 181 |
# entry list format |
| 182 |
# [ rule_num, protocol name or number, allow or deny, ipv4/6 cidr, icmp type, icmp code, port from, port to] |
| 183 |
elist = [] |
| 184 |
|
| 185 |
elist.append(entry["rule_number"]) |
| 186 |
|
| 187 |
if entry.get("protocol") in PROTOCOL_NAMES: |
| 188 |
elist.append(PROTOCOL_NAMES[entry["protocol"]]) |
| 189 |
else: |
| 190 |
elist.append(entry.get("protocol")) |
| 191 |
|
| 192 |
elist.append(entry["rule_action"]) |
| 193 |
|
| 194 |
if entry.get("cidr_block"): |
| 195 |
elist.append(entry["cidr_block"]) |
| 196 |
elif entry.get("ipv6_cidr_block"): |
| 197 |
elist.append(entry["ipv6_cidr_block"]) |
| 198 |
else: |
| 199 |
elist.append(None) |
| 200 |
|
| 201 |
elist = elist + [None, None, None, None] |
| 202 |
|
| 203 |
if entry["protocol"] in ("1", "58"): |
| 204 |
elist[4] = entry.get("icmp_type_code", {}).get("type") |
| 205 |
elist[5] = entry.get("icmp_type_code", {}).get("code") |
| 206 |
|
| 207 |
if entry["protocol"] not in ("1", "6", "17", "58"): |
| 208 |
elist[6] = 0 |
| 209 |
elist[7] = 65535 |
| 210 |
elif "port_range" in entry: |
| 211 |
elist[6] = entry["port_range"]["from"] |
| 212 |
elist[7] = entry["port_range"]["to"] |
| 213 |
|
| 214 |
return elist |
| 215 |
|
| 216 |
|
| 217 |
def main(): |
| 218 |
argument_spec = dict( |
| 219 |
nacl_ids=dict(default=[], type="list", aliases=["nacl_id"], elements="str"), |
| 220 |
filters=dict(default={}, type="dict"), |
| 221 |
) |
| 222 |
|
| 223 |
module = AnsibleAWSModule( |
| 224 |
argument_spec=argument_spec, |
| 225 |
supports_check_mode=True, |
| 226 |
) |
| 227 |
|
| 228 |
connection = module.client("ec2") |
| 229 |
|
| 230 |
list_ec2_vpc_nacls(connection, module) |
| 231 |
|
| 232 |
|
| 233 |
if __name__ == "__main__": |
| 234 |
main() |