| 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_rule_group_info |
| 10 |
version_added: 1.5.0 |
| 11 |
author: |
| 12 |
- "Markus Bergholz (@markuman)" |
| 13 |
short_description: wafv2_web_acl_info |
| 14 |
description: |
| 15 |
- Get informations about existing wafv2 rule groups. |
| 16 |
options: |
| 17 |
name: |
| 18 |
description: |
| 19 |
- The name of the rule group. |
| 20 |
required: true |
| 21 |
type: str |
| 22 |
scope: |
| 23 |
description: |
| 24 |
- Scope of wafv2 rule group. |
| 25 |
required: true |
| 26 |
choices: ["CLOUDFRONT","REGIONAL"] |
| 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: rule group info |
| 37 |
community.aws.wafv2_rule_group_info: |
| 38 |
name: test02 |
| 39 |
scope: REGIONAL |
| 40 |
""" |
| 41 |
|
| 42 |
RETURN = r""" |
| 43 |
arn: |
| 44 |
description: Rule group arn |
| 45 |
sample: arn:aws:wafv2:eu-central-1:11111111:regional/rulegroup/test02/6e90c01a-e4eb-43e5-b6aa-b1604cedf7d7 |
| 46 |
type: str |
| 47 |
returned: Always, as long as the web acl exists |
| 48 |
description: |
| 49 |
description: Description of the rule group |
| 50 |
sample: Some rule group description |
| 51 |
returned: Always, as long as the web acl exists |
| 52 |
type: str |
| 53 |
capacity: |
| 54 |
description: Current capacity of the rule group |
| 55 |
sample: 500 |
| 56 |
returned: Always, as long as the rule group exists |
| 57 |
type: int |
| 58 |
name: |
| 59 |
description: Rule group name |
| 60 |
sample: test02 |
| 61 |
returned: Always, as long as the rule group exists |
| 62 |
type: str |
| 63 |
rules: |
| 64 |
description: Current rules of the rule group |
| 65 |
returned: Always, as long as the rule group exists |
| 66 |
type: list |
| 67 |
sample: |
| 68 |
- action: |
| 69 |
allow: {} |
| 70 |
name: eins |
| 71 |
priority: 1 |
| 72 |
statement: |
| 73 |
ip_set_reference_statement: |
| 74 |
arn: arn:aws:wafv2:eu-central-1:111111111:regional/ipset/test02/b6978915-c67b-4d1c-8832-2b1bb452143a |
| 75 |
visibility_config: |
| 76 |
cloud_watch_metrics_enabled: True |
| 77 |
metric_name: fsd |
| 78 |
sampled_requests_enabled: True |
| 79 |
visibility_config: |
| 80 |
description: Visibility config of the rule group |
| 81 |
returned: Always, as long as the rule group exists |
| 82 |
type: dict |
| 83 |
sample: |
| 84 |
cloud_watch_metrics_enabled: True |
| 85 |
metric_name: blub |
| 86 |
sampled_requests_enabled: False |
| 87 |
""" |
| 88 |
|
| 89 |
try: |
| 90 |
from botocore.exceptions import BotoCoreError |
| 91 |
from botocore.exceptions import ClientError |
| 92 |
except ImportError: |
| 93 |
pass # caught by AnsibleAWSModule |
| 94 |
|
| 95 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 96 |
|
| 97 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 98 |
from ansible_collections.community.aws.plugins.module_utils.wafv2 import describe_wafv2_tags |
| 99 |
from ansible_collections.community.aws.plugins.module_utils.wafv2 import wafv2_list_rule_groups |
| 100 |
|
| 101 |
|
| 102 |
def get_rule_group(wafv2, name, scope, group_id, fail_json_aws): |
| 103 |
try: |
| 104 |
response = wafv2.get_rule_group(Name=name, Scope=scope, Id=group_id) |
| 105 |
except (BotoCoreError, ClientError) as e: |
| 106 |
fail_json_aws(e, msg="Failed to get wafv2 rule group.") |
| 107 |
return response |
| 108 |
|
| 109 |
|
| 110 |
def main(): |
| 111 |
arg_spec = dict( |
| 112 |
name=dict(type="str", required=True), |
| 113 |
scope=dict(type="str", required=True, choices=["CLOUDFRONT", "REGIONAL"]), |
| 114 |
) |
| 115 |
|
| 116 |
module = AnsibleAWSModule( |
| 117 |
argument_spec=arg_spec, |
| 118 |
supports_check_mode=True, |
| 119 |
) |
| 120 |
|
| 121 |
name = module.params.get("name") |
| 122 |
scope = module.params.get("scope") |
| 123 |
|
| 124 |
wafv2 = module.client("wafv2") |
| 125 |
|
| 126 |
# check if rule group exists |
| 127 |
response = wafv2_list_rule_groups(wafv2, scope, module.fail_json_aws) |
| 128 |
group_id = None |
| 129 |
retval = {} |
| 130 |
|
| 131 |
for item in response.get("RuleGroups"): |
| 132 |
if item.get("Name") == name: |
| 133 |
group_id = item.get("Id") |
| 134 |
arn = item.get("ARN") |
| 135 |
|
| 136 |
existing_group = None |
| 137 |
if group_id: |
| 138 |
existing_group = get_rule_group(wafv2, name, scope, group_id, module.fail_json_aws) |
| 139 |
retval = camel_dict_to_snake_dict(existing_group.get("RuleGroup")) |
| 140 |
tags = describe_wafv2_tags(wafv2, arn, module.fail_json_aws) |
| 141 |
retval["tags"] = tags or {} |
| 142 |
|
| 143 |
module.exit_json(**retval) |
| 144 |
|
| 145 |
|
| 146 |
if __name__ == "__main__": |
| 147 |
main() |