| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright (c) 2017 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 |
module: waf_web_acl |
| 9 |
short_description: Create and delete WAF Web ACLs |
| 10 |
version_added: 1.0.0 |
| 11 |
description: |
| 12 |
- Module for WAF classic, for WAF v2 use the I(wafv2_*) modules. |
| 13 |
- Read the AWS documentation for WAF U(https://docs.aws.amazon.com/waf/latest/developerguide/classic-waf-chapter.html). |
| 14 |
- Prior to release 5.0.0 this module was called C(community.aws.aws_waf_web_acl). |
| 15 |
The usage did not change. |
| 16 |
|
| 17 |
author: |
| 18 |
- Mike Mochan (@mmochan) |
| 19 |
- Will Thames (@willthames) |
| 20 |
|
| 21 |
deprecated: |
| 22 |
removed_in: 12.0.0 |
| 23 |
why: Amazon has retired the "WAF Classic" service. |
| 24 |
alternative: Please use the "AWS WAF (WAFv2)" service and modules instead. |
| 25 |
|
| 26 |
options: |
| 27 |
name: |
| 28 |
description: Name of the Web Application Firewall ACL to manage. |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
default_action: |
| 32 |
description: The action that you want AWS WAF to take when a request doesn't |
| 33 |
match the criteria specified in any of the Rule objects that are associated with the WebACL. |
| 34 |
choices: |
| 35 |
- block |
| 36 |
- allow |
| 37 |
- count |
| 38 |
type: str |
| 39 |
state: |
| 40 |
description: Whether the Web ACL should be present or absent. |
| 41 |
choices: |
| 42 |
- present |
| 43 |
- absent |
| 44 |
default: present |
| 45 |
type: str |
| 46 |
metric_name: |
| 47 |
description: |
| 48 |
- A friendly name or description for the metrics for this WebACL. |
| 49 |
- The name can contain only alphanumeric characters (A-Z, a-z, 0-9); the name can't contain whitespace. |
| 50 |
- You can't change I(metric_name) after you create the WebACL. |
| 51 |
- Metric name will default to I(name) with disallowed characters stripped out. |
| 52 |
type: str |
| 53 |
rules: |
| 54 |
description: |
| 55 |
- A list of rules that the Web ACL will enforce. |
| 56 |
type: list |
| 57 |
elements: dict |
| 58 |
suboptions: |
| 59 |
name: |
| 60 |
description: Name of the rule. |
| 61 |
type: str |
| 62 |
required: true |
| 63 |
action: |
| 64 |
description: The action to perform. |
| 65 |
type: str |
| 66 |
required: true |
| 67 |
priority: |
| 68 |
description: The priority of the action. Priorities must be unique. Lower numbered priorities are evaluated first. |
| 69 |
type: int |
| 70 |
required: true |
| 71 |
type: |
| 72 |
description: The type of rule. |
| 73 |
choices: |
| 74 |
- rate_based |
| 75 |
- regular |
| 76 |
type: str |
| 77 |
purge_rules: |
| 78 |
description: |
| 79 |
- Whether to remove rules that aren't passed with I(rules). |
| 80 |
default: False |
| 81 |
type: bool |
| 82 |
waf_regional: |
| 83 |
description: Whether to use C(waf-regional) module. |
| 84 |
default: false |
| 85 |
required: false |
| 86 |
type: bool |
| 87 |
|
| 88 |
extends_documentation_fragment: |
| 89 |
- amazon.aws.common.modules |
| 90 |
- amazon.aws.region.modules |
| 91 |
- amazon.aws.boto3 |
| 92 |
""" |
| 93 |
|
| 94 |
EXAMPLES = r""" |
| 95 |
- name: create web ACL |
| 96 |
community.aws.waf_web_acl: |
| 97 |
name: my_web_acl |
| 98 |
rules: |
| 99 |
- name: my_rule |
| 100 |
priority: 1 |
| 101 |
action: block |
| 102 |
default_action: block |
| 103 |
purge_rules: true |
| 104 |
state: present |
| 105 |
|
| 106 |
- name: delete the web acl |
| 107 |
community.aws.waf_web_acl: |
| 108 |
name: my_web_acl |
| 109 |
state: absent |
| 110 |
""" |
| 111 |
|
| 112 |
RETURN = r""" |
| 113 |
web_acl: |
| 114 |
description: contents of the Web ACL. |
| 115 |
returned: always |
| 116 |
type: complex |
| 117 |
contains: |
| 118 |
default_action: |
| 119 |
description: Default action taken by the Web ACL if no rules match. |
| 120 |
returned: always |
| 121 |
type: dict |
| 122 |
sample: |
| 123 |
type: BLOCK |
| 124 |
metric_name: |
| 125 |
description: Metric name used as an identifier. |
| 126 |
returned: always |
| 127 |
type: str |
| 128 |
sample: mywebacl |
| 129 |
name: |
| 130 |
description: Friendly name of the Web ACL. |
| 131 |
returned: always |
| 132 |
type: str |
| 133 |
sample: my web acl |
| 134 |
rules: |
| 135 |
description: List of rules. |
| 136 |
returned: always |
| 137 |
type: complex |
| 138 |
contains: |
| 139 |
action: |
| 140 |
description: Action taken by the WAF when the rule matches. |
| 141 |
returned: always |
| 142 |
type: complex |
| 143 |
sample: |
| 144 |
type: ALLOW |
| 145 |
priority: |
| 146 |
description: priority number of the rule (lower numbers are run first). |
| 147 |
returned: always |
| 148 |
type: int |
| 149 |
sample: 2 |
| 150 |
rule_id: |
| 151 |
description: Rule ID. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: a6fc7ab5-287b-479f-8004-7fd0399daf75 |
| 155 |
type: |
| 156 |
description: Type of rule (either REGULAR or RATE_BASED). |
| 157 |
returned: always |
| 158 |
type: str |
| 159 |
sample: REGULAR |
| 160 |
web_acl_id: |
| 161 |
description: Unique identifier of Web ACL. |
| 162 |
returned: always |
| 163 |
type: str |
| 164 |
sample: 10fff965-4b6b-46e2-9d78-24f6d2e2d21c |
| 165 |
""" |
| 166 |
|
| 167 |
import re |
| 168 |
|
| 169 |
try: |
| 170 |
import botocore |
| 171 |
except ImportError: |
| 172 |
pass # handled by AnsibleAWSModule |
| 173 |
|
| 174 |
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict |
| 175 |
|
| 176 |
from ansible_collections.amazon.aws.plugins.module_utils.waf import list_regional_rules_with_backoff |
| 177 |
from ansible_collections.amazon.aws.plugins.module_utils.waf import list_regional_web_acls_with_backoff |
| 178 |
from ansible_collections.amazon.aws.plugins.module_utils.waf import list_rules_with_backoff |
| 179 |
from ansible_collections.amazon.aws.plugins.module_utils.waf import list_web_acls_with_backoff |
| 180 |
from ansible_collections.amazon.aws.plugins.module_utils.waf import run_func_with_change_token_backoff |
| 181 |
from ansible_collections.amazon.aws.plugins.module_utils.waiters import get_waiter |
| 182 |
|
| 183 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 184 |
|
| 185 |
|
| 186 |
def get_web_acl_by_name(client, module, name): |
| 187 |
acls = [d["WebACLId"] for d in list_web_acls(client, module) if d["Name"] == name] |
| 188 |
if acls: |
| 189 |
return acls[0] |
| 190 |
else: |
| 191 |
return acls |
| 192 |
|
| 193 |
|
| 194 |
def create_rule_lookup(client, module): |
| 195 |
if client.__class__.__name__ == "WAF": |
| 196 |
try: |
| 197 |
rules = list_rules_with_backoff(client) |
| 198 |
return dict((rule["Name"], rule) for rule in rules) |
| 199 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 200 |
module.fail_json_aws(e, msg="Could not list rules") |
| 201 |
elif client.__class__.__name__ == "WAFRegional": |
| 202 |
try: |
| 203 |
rules = list_regional_rules_with_backoff(client) |
| 204 |
return dict((rule["Name"], rule) for rule in rules) |
| 205 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 206 |
module.fail_json_aws(e, msg="Could not list regional rules") |
| 207 |
|
| 208 |
|
| 209 |
def get_web_acl(client, module, web_acl_id): |
| 210 |
try: |
| 211 |
return client.get_web_acl(WebACLId=web_acl_id)["WebACL"] |
| 212 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 213 |
module.fail_json_aws(e, msg=f"Could not get Web ACL with id {web_acl_id}") |
| 214 |
|
| 215 |
|
| 216 |
def list_web_acls( |
| 217 |
client, |
| 218 |
module, |
| 219 |
): |
| 220 |
if client.__class__.__name__ == "WAF": |
| 221 |
try: |
| 222 |
return list_web_acls_with_backoff(client) |
| 223 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 224 |
module.fail_json_aws(e, msg="Could not get Web ACLs") |
| 225 |
elif client.__class__.__name__ == "WAFRegional": |
| 226 |
try: |
| 227 |
return list_regional_web_acls_with_backoff(client) |
| 228 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 229 |
module.fail_json_aws(e, msg="Could not get Web ACLs") |
| 230 |
|
| 231 |
|
| 232 |
def find_and_update_web_acl(client, module, web_acl_id): |
| 233 |
acl = get_web_acl(client, module, web_acl_id) |
| 234 |
rule_lookup = create_rule_lookup(client, module) |
| 235 |
existing_rules = acl["Rules"] |
| 236 |
desired_rules = [ |
| 237 |
{ |
| 238 |
"RuleId": rule_lookup[rule["name"]]["RuleId"], |
| 239 |
"Priority": rule["priority"], |
| 240 |
"Action": {"Type": rule["action"].upper()}, |
| 241 |
"Type": rule.get("type", "regular").upper(), |
| 242 |
} |
| 243 |
for rule in module.params["rules"] |
| 244 |
] |
| 245 |
missing = [rule for rule in desired_rules if rule not in existing_rules] |
| 246 |
extras = [] |
| 247 |
if module.params["purge_rules"]: |
| 248 |
extras = [rule for rule in existing_rules if rule not in desired_rules] |
| 249 |
|
| 250 |
insertions = [format_for_update(rule, "INSERT") for rule in missing] |
| 251 |
deletions = [format_for_update(rule, "DELETE") for rule in extras] |
| 252 |
changed = bool(insertions + deletions) |
| 253 |
|
| 254 |
# Purge rules before adding new ones in case a deletion shares the same |
| 255 |
# priority as an insertion. |
| 256 |
params = {"WebACLId": acl["WebACLId"], "DefaultAction": acl["DefaultAction"]} |
| 257 |
change_tokens = [] |
| 258 |
if deletions: |
| 259 |
try: |
| 260 |
params["Updates"] = deletions |
| 261 |
result = run_func_with_change_token_backoff(client, module, params, client.update_web_acl) |
| 262 |
change_tokens.append(result["ChangeToken"]) |
| 263 |
get_waiter( |
| 264 |
client, |
| 265 |
"change_token_in_sync", |
| 266 |
).wait(ChangeToken=result["ChangeToken"]) |
| 267 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 268 |
module.fail_json_aws(e, msg="Could not update Web ACL") |
| 269 |
if insertions: |
| 270 |
try: |
| 271 |
params["Updates"] = insertions |
| 272 |
result = run_func_with_change_token_backoff(client, module, params, client.update_web_acl) |
| 273 |
change_tokens.append(result["ChangeToken"]) |
| 274 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 275 |
module.fail_json_aws(e, msg="Could not update Web ACL") |
| 276 |
if change_tokens: |
| 277 |
for token in change_tokens: |
| 278 |
get_waiter( |
| 279 |
client, |
| 280 |
"change_token_in_sync", |
| 281 |
).wait(ChangeToken=token) |
| 282 |
if changed: |
| 283 |
acl = get_web_acl(client, module, web_acl_id) |
| 284 |
return changed, acl |
| 285 |
|
| 286 |
|
| 287 |
def format_for_update(rule, action): |
| 288 |
return dict( |
| 289 |
Action=action, |
| 290 |
ActivatedRule=dict( |
| 291 |
Priority=rule["Priority"], |
| 292 |
RuleId=rule["RuleId"], |
| 293 |
Action=dict(Type=rule["Action"]["Type"]), |
| 294 |
), |
| 295 |
) |
| 296 |
|
| 297 |
|
| 298 |
def remove_rules_from_web_acl(client, module, web_acl_id): |
| 299 |
acl = get_web_acl(client, module, web_acl_id) |
| 300 |
deletions = [format_for_update(rule, "DELETE") for rule in acl["Rules"]] |
| 301 |
try: |
| 302 |
params = {"WebACLId": acl["WebACLId"], "DefaultAction": acl["DefaultAction"], "Updates": deletions} |
| 303 |
run_func_with_change_token_backoff(client, module, params, client.update_web_acl) |
| 304 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 305 |
module.fail_json_aws(e, msg="Could not remove rule") |
| 306 |
|
| 307 |
|
| 308 |
def ensure_web_acl_present(client, module): |
| 309 |
changed = False |
| 310 |
result = None |
| 311 |
name = module.params["name"] |
| 312 |
web_acl_id = get_web_acl_by_name(client, module, name) |
| 313 |
if web_acl_id: |
| 314 |
(changed, result) = find_and_update_web_acl(client, module, web_acl_id) |
| 315 |
else: |
| 316 |
metric_name = module.params["metric_name"] |
| 317 |
if not metric_name: |
| 318 |
metric_name = re.sub(r"[^A-Za-z0-9]", "", module.params["name"]) |
| 319 |
default_action = module.params["default_action"].upper() |
| 320 |
try: |
| 321 |
params = {"Name": name, "MetricName": metric_name, "DefaultAction": {"Type": default_action}} |
| 322 |
new_web_acl = run_func_with_change_token_backoff(client, module, params, client.create_web_acl) |
| 323 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 324 |
module.fail_json_aws(e, msg="Could not create Web ACL") |
| 325 |
(changed, result) = find_and_update_web_acl(client, module, new_web_acl["WebACL"]["WebACLId"]) |
| 326 |
return changed, result |
| 327 |
|
| 328 |
|
| 329 |
def ensure_web_acl_absent(client, module): |
| 330 |
web_acl_id = get_web_acl_by_name(client, module, module.params["name"]) |
| 331 |
if web_acl_id: |
| 332 |
web_acl = get_web_acl(client, module, web_acl_id) |
| 333 |
if web_acl["Rules"]: |
| 334 |
remove_rules_from_web_acl(client, module, web_acl_id) |
| 335 |
try: |
| 336 |
run_func_with_change_token_backoff( |
| 337 |
client, module, {"WebACLId": web_acl_id}, client.delete_web_acl, wait=True |
| 338 |
) |
| 339 |
return True, {} |
| 340 |
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: |
| 341 |
module.fail_json_aws(e, msg="Could not delete Web ACL") |
| 342 |
return False, {} |
| 343 |
|
| 344 |
|
| 345 |
def main(): |
| 346 |
argument_spec = dict( |
| 347 |
name=dict(required=True), |
| 348 |
default_action=dict(choices=["block", "allow", "count"]), |
| 349 |
metric_name=dict(), |
| 350 |
state=dict(default="present", choices=["present", "absent"]), |
| 351 |
rules=dict(type="list", elements="dict"), |
| 352 |
purge_rules=dict(type="bool", default=False), |
| 353 |
waf_regional=dict(type="bool", default=False), |
| 354 |
) |
| 355 |
module = AnsibleAWSModule( |
| 356 |
argument_spec=argument_spec, |
| 357 |
required_if=[["state", "present", ["default_action", "rules"]]], |
| 358 |
) |
| 359 |
state = module.params.get("state") |
| 360 |
|
| 361 |
resource = "waf" if not module.params["waf_regional"] else "waf-regional" |
| 362 |
client = module.client(resource) |
| 363 |
if state == "present": |
| 364 |
(changed, results) = ensure_web_acl_present(client, module) |
| 365 |
else: |
| 366 |
(changed, results) = ensure_web_acl_absent(client, module) |
| 367 |
|
| 368 |
module.exit_json(changed=changed, web_acl=camel_dict_to_snake_dict(results)) |
| 369 |
|
| 370 |
|
| 371 |
if __name__ == "__main__": |
| 372 |
main() |