Debian Codegraph @

ansible_collections/community/aws/plugins/modules/wafv2_resources.py

raw
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_resources
10 version_added: 1.5.0
11 author:
12 - "Markus Bergholz (@markuman)"
13 short_description: wafv2_web_acl
14 description:
15 - Apply or remove wafv2 to other aws resources.
16 options:
17 state:
18 description:
19 - Whether the rule is present or absent.
20 choices: ["present", "absent"]
21 required: true
22 type: str
23 name:
24 description:
25 - The name of the web acl.
26 type: str
27 scope:
28 description:
29 - Scope of waf
30 choices: ["CLOUDFRONT","REGIONAL"]
31 type: str
32 arn:
33 description:
34 - AWS resources (ALB, API Gateway or AppSync GraphQL API) ARN
35 type: str
36 required: true
37
38 extends_documentation_fragment:
39 - amazon.aws.common.modules
40 - amazon.aws.region.modules
41 - amazon.aws.boto3
42 """
43
44 EXAMPLES = r"""
45 - name: add test alb to waf string03
46 community.aws.wafv2_resources:
47 name: string03
48 scope: REGIONAL
49 state: present
50 arn: "arn:aws:elasticloadbalancing:eu-central-1:111111111:loadbalancer/app/test03/dd83ea041ba6f933"
51 """
52
53 RETURN = r"""
54 resource_arns:
55 description: Current resources where the wafv2 is applied on
56 sample:
57 - "arn:aws:elasticloadbalancing:eu-central-1:111111111:loadbalancer/app/test03/dd83ea041ba6f933"
58 returned: Always, as long as the wafv2 exists
59 type: list
60 """
61
62 try:
63 from botocore.exceptions import BotoCoreError
64 from botocore.exceptions import ClientError
65 except ImportError:
66 pass # caught by AnsibleAWSModule
67
68 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
69
70 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
71 from ansible_collections.community.aws.plugins.module_utils.wafv2 import wafv2_list_web_acls
72
73
74 def get_web_acl(wafv2, name, scope, acl_id, fail_json_aws):
75 try:
76 response = wafv2.get_web_acl(Name=name, Scope=scope, Id=acl_id)
77 except (BotoCoreError, ClientError) as e:
78 fail_json_aws(e, msg="Failed to get wafv2 web acl.")
79 return response
80
81
82 def list_wafv2_resources(wafv2, arn, fail_json_aws):
83 try:
84 response = wafv2.list_resources_for_web_acl(WebACLArn=arn)
85 except (BotoCoreError, ClientError) as e:
86 fail_json_aws(e, msg="Failed to list wafv2 web acl.")
87 return response
88
89
90 def add_wafv2_resources(wafv2, waf_arn, arn, fail_json_aws):
91 try:
92 response = wafv2.associate_web_acl(WebACLArn=waf_arn, ResourceArn=arn)
93 except (BotoCoreError, ClientError) as e:
94 fail_json_aws(e, msg="Failed to add wafv2 web acl.")
95 return response
96
97
98 def remove_resources(wafv2, arn, fail_json_aws):
99 try:
100 response = wafv2.disassociate_web_acl(ResourceArn=arn)
101 except (BotoCoreError, ClientError) as e:
102 fail_json_aws(e, msg="Failed to remove wafv2 web acl.")
103 return response
104
105
106 def main():
107 arg_spec = dict(
108 state=dict(type="str", required=True, choices=["present", "absent"]),
109 name=dict(type="str"),
110 scope=dict(type="str", choices=["CLOUDFRONT", "REGIONAL"]),
111 arn=dict(type="str", required=True),
112 )
113
114 module = AnsibleAWSModule(
115 argument_spec=arg_spec,
116 supports_check_mode=True,
117 required_if=[["state", "present", ["name", "scope"]]],
118 )
119
120 state = module.params.get("state")
121 name = module.params.get("name")
122 scope = module.params.get("scope")
123 arn = module.params.get("arn")
124 check_mode = module.check_mode
125
126 wafv2 = module.client("wafv2")
127
128 # check if web acl exists
129
130 response = wafv2_list_web_acls(wafv2, scope, module.fail_json_aws)
131
132 acl_id = None
133 retval = {}
134 change = False
135
136 for item in response.get("WebACLs"):
137 if item.get("Name") == name:
138 acl_id = item.get("Id")
139
140 if acl_id:
141 existing_acl = get_web_acl(wafv2, name, scope, acl_id, module.fail_json_aws)
142 waf_arn = existing_acl.get("WebACL").get("ARN")
143
144 retval = list_wafv2_resources(wafv2, waf_arn, module.fail_json_aws)
145
146 if state == "present":
147 if retval:
148 if arn not in retval.get("ResourceArns"):
149 change = True
150 if not check_mode:
151 retval = add_wafv2_resources(wafv2, waf_arn, arn, module.fail_json_aws)
152
153 elif state == "absent":
154 if retval:
155 if arn in retval.get("ResourceArns"):
156 change = True
157 if not check_mode:
158 retval = remove_resources(wafv2, arn, module.fail_json_aws)
159
160 module.exit_json(changed=change, **camel_dict_to_snake_dict(retval))
161
162
163 if __name__ == "__main__":
164 main()