| 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: wafv2_ip_set_info |
| 10 |
version_added: 1.5.0 |
| 11 |
author: |
| 12 |
- "Markus Bergholz (@markuman)" |
| 13 |
short_description: Get information about wafv2 ip sets |
| 14 |
description: |
| 15 |
- Get information about existing wafv2 ip sets. |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- The name of the IP set. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
scope: |
| 23 |
description: |
| 24 |
- Specifies whether this is for an AWS CloudFront distribution or for a regional application. |
| 25 |
choices: ["CLOUDFRONT","REGIONAL"] |
| 26 |
required: true |
| 27 |
type: str |
| 28 |
|
| 29 |
extends_documentation_fragment: |
| 30 |
- amazon.aws.common.modules |
| 31 |
- amazon.aws.region.modules |
| 32 |
- amazon.aws.boto3 |
| 33 |
""" |
| 34 |
|
| 35 |
EXAMPLES = r""" |
| 36 |
- name: test ip set |
| 37 |
wafv2_ip_set_info: |
| 38 |
name: test02 |
| 39 |
scope: REGIONAL |
| 40 |
""" |
| 41 |
|
| 42 |
RETURN = r""" |
| 43 |
addresses: |
| 44 |
description: Current addresses of the ip set |
| 45 |
sample: |
| 46 |
- 8.8.8.8/32 |
| 47 |
- 8.8.4.4/32 |
| 48 |
returned: Always, as long as the ip set exists |
| 49 |
type: list |
| 50 |
arn: |
| 51 |
description: IP set arn |
| 52 |
sample: "arn:aws:wafv2:eu-central-1:11111111:regional/ipset/test02/4b007330-2934-4dc5-af24-82dcb3aeb127" |
| 53 |
type: str |
| 54 |
returned: Always, as long as the ip set exists |
| 55 |
description: |
| 56 |
description: Description of the ip set |
| 57 |
sample: Some IP set description |
| 58 |
returned: Always, as long as the ip set exists |
| 59 |
type: str |
| 60 |
ip_address_version: |
| 61 |
description: IP version of the ip set |
| 62 |
sample: IPV4 |
| 63 |
type: str |
| 64 |
returned: Always, as long as the ip set exists |
| 65 |
name: |
| 66 |
description: IP set name |
| 67 |
sample: test02 |
| 68 |
returned: Always, as long as the ip set exists |
| 69 |
type: str |
| 70 |
""" |
| 71 |
|
| 72 |
try: |
| 73 |
from botocore.exceptions import BotoCoreError |
| 74 |
from botocore.exceptions import ClientError |
| 75 |
except ImportError: |
| 76 |
pass # caught by AnsibleAWSModule |
| 77 |
|
| 78 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 79 |
|
| 80 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 81 |
from ansible_collections.community.aws.plugins.module_utils.wafv2 import describe_wafv2_tags |
| 82 |
|
| 83 |
|
| 84 |
def list_ip_sets(wafv2, scope, fail_json_aws, Nextmarker=None): |
| 85 |
# there is currently no paginator for wafv2 |
| 86 |
req_obj = {"Scope": scope, "Limit": 100} |
| 87 |
if Nextmarker: |
| 88 |
req_obj["NextMarker"] = Nextmarker |
| 89 |
|
| 90 |
try: |
| 91 |
response = wafv2.list_ip_sets(**req_obj) |
| 92 |
if response.get("NextMarker"): |
| 93 |
response["IPSets"] += list_ip_sets(wafv2, scope, fail_json_aws, Nextmarker=response.get("NextMarker")).get( |
| 94 |
"IPSets" |
| 95 |
) |
| 96 |
except (BotoCoreError, ClientError) as e: |
| 97 |
fail_json_aws(e, msg="Failed to list wafv2 ip set") |
| 98 |
return response |
| 99 |
|
| 100 |
|
| 101 |
def get_ip_set(wafv2, name, scope, set_id, fail_json_aws): |
| 102 |
try: |
| 103 |
response = wafv2.get_ip_set(Name=name, Scope=scope, Id=set_id) |
| 104 |
except (BotoCoreError, ClientError) as e: |
| 105 |
fail_json_aws(e, msg="Failed to get wafv2 ip set") |
| 106 |
return response |
| 107 |
|
| 108 |
|
| 109 |
def main(): |
| 110 |
arg_spec = dict( |
| 111 |
name=dict(type="str", required=True), scope=dict(type="str", required=True, choices=["CLOUDFRONT", "REGIONAL"]) |
| 112 |
) |
| 113 |
|
| 114 |
module = AnsibleAWSModule( |
| 115 |
argument_spec=arg_spec, |
| 116 |
supports_check_mode=True, |
| 117 |
) |
| 118 |
|
| 119 |
name = module.params.get("name") |
| 120 |
scope = module.params.get("scope") |
| 121 |
|
| 122 |
wafv2 = module.client("wafv2") |
| 123 |
|
| 124 |
# check if ip set exist |
| 125 |
response = list_ip_sets(wafv2, scope, module.fail_json_aws) |
| 126 |
|
| 127 |
set_id = None |
| 128 |
|
| 129 |
for item in response.get("IPSets"): |
| 130 |
if item.get("Name") == name: |
| 131 |
set_id = item.get("Id") |
| 132 |
arn = item.get("ARN") |
| 133 |
|
| 134 |
retval = {} |
| 135 |
existing_set = None |
| 136 |
if set_id: |
| 137 |
existing_set = get_ip_set(wafv2, name, scope, set_id, module.fail_json_aws) |
| 138 |
retval = camel_dict_to_snake_dict(existing_set.get("IPSet")) |
| 139 |
retval["tags"] = describe_wafv2_tags(wafv2, arn, module.fail_json_aws) or {} |
| 140 |
module.exit_json(**retval) |
| 141 |
|
| 142 |
|
| 143 |
if __name__ == "__main__": |
| 144 |
main() |