| 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 |
module: networkfirewall_policy |
| 9 |
short_description: manage AWS Network Firewall policies |
| 10 |
version_added: 4.0.0 |
| 11 |
description: |
| 12 |
- A module for creating, updating and deleting AWS Network Firewall policies. |
| 13 |
options: |
| 14 |
arn: |
| 15 |
description: |
| 16 |
- The ARN of the Network Firewall policy. |
| 17 |
- Exactly one of I(arn) or I(name) must be provided. |
| 18 |
required: false |
| 19 |
type: str |
| 20 |
name: |
| 21 |
description: |
| 22 |
- The name of the Network Firewall policy. |
| 23 |
- Cannot be updated after creation. |
| 24 |
- Exactly one of I(arn) or I(name) must be provided. |
| 25 |
required: false |
| 26 |
type: str |
| 27 |
state: |
| 28 |
description: |
| 29 |
- Create or remove the Network Firewall policy. |
| 30 |
required: false |
| 31 |
choices: ['present', 'absent'] |
| 32 |
default: 'present' |
| 33 |
type: str |
| 34 |
description: |
| 35 |
description: |
| 36 |
- A description for the Network Firewall policy. |
| 37 |
required: false |
| 38 |
type: str |
| 39 |
stateful_rule_groups: |
| 40 |
description: |
| 41 |
- A list of names or ARNs of stateful firewall rule groups. |
| 42 |
required: false |
| 43 |
type: list |
| 44 |
elements: str |
| 45 |
aliases: ['stateful_groups'] |
| 46 |
stateless_rule_groups: |
| 47 |
description: |
| 48 |
- A list of names or ARNs of stateless firewall rule groups. |
| 49 |
required: false |
| 50 |
type: list |
| 51 |
elements: str |
| 52 |
aliases: ['stateless_groups'] |
| 53 |
stateless_default_actions: |
| 54 |
description: |
| 55 |
- Actions to take on a packet if it doesn't match any of the stateless |
| 56 |
rules in the policy. |
| 57 |
- Common actions are C(aws:pass), C(aws:drop) and C(aws:forward_to_sfe). |
| 58 |
- When creating a new policy defaults to C(aws:forward_to_sfe). |
| 59 |
required: false |
| 60 |
type: list |
| 61 |
elements: str |
| 62 |
stateless_fragment_default_actions: |
| 63 |
description: |
| 64 |
- Actions to take on a fragmented UDP packet if it doesn't match any |
| 65 |
of the stateless rules in the policy. |
| 66 |
- Common actions are C(aws:pass), C(aws:drop) and C(aws:forward_to_sfe). |
| 67 |
- When creating a new policy defaults to C(aws:forward_to_sfe). |
| 68 |
required: false |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
stateful_default_actions: |
| 72 |
description: |
| 73 |
- Actions to take on a packet if it doesn't match any of the stateful |
| 74 |
rules in the policy. |
| 75 |
- Common actions are C(aws:drop_strict), C(aws:drop_established), |
| 76 |
C(aws:alert_strict) and C(aws:alert_established). |
| 77 |
- Only valid for policies where I(strict_rule_order=true). |
| 78 |
- When creating a new policy defaults to C(aws:drop_strict). |
| 79 |
required: false |
| 80 |
type: list |
| 81 |
elements: str |
| 82 |
stateful_rule_order: |
| 83 |
description: |
| 84 |
- Indicates how to manage the order of stateful rule evaluation for the policy. |
| 85 |
- When I(strict_rule_order='strict') rules and rule groups are evaluated in |
| 86 |
the order that they're defined. |
| 87 |
- Cannot be updated after creation. |
| 88 |
required: false |
| 89 |
type: str |
| 90 |
choices: ['default', 'strict'] |
| 91 |
aliases: ['rule_order'] |
| 92 |
stateless_custom_actions: |
| 93 |
description: |
| 94 |
- A list of dictionaries defining custom actions which can be used in |
| 95 |
I(stateless_default_actions) and I(stateless_fragment_default_actions). |
| 96 |
required: false |
| 97 |
type: list |
| 98 |
elements: dict |
| 99 |
aliases: ['custom_stateless_actions'] |
| 100 |
suboptions: |
| 101 |
name: |
| 102 |
description: |
| 103 |
- The name of the custom action. |
| 104 |
required: true |
| 105 |
type: str |
| 106 |
publish_metric_dimension_value: |
| 107 |
description: |
| 108 |
- When the custom action is used, metrics will have a dimension of |
| 109 |
C(CustomAction) the value of which is set to |
| 110 |
I(publish_metric_dimension_value). |
| 111 |
required: false |
| 112 |
type: str |
| 113 |
aliases: ['publish_metric_dimension_values'] |
| 114 |
purge_stateless_custom_actions: |
| 115 |
description: |
| 116 |
- If I(purge_stateless_custom_actions=true), existing custom actions will |
| 117 |
be purged from the resource to match exactly what is defined by |
| 118 |
the I(stateless_custom_actions) parameter. |
| 119 |
type: bool |
| 120 |
required: false |
| 121 |
default: True |
| 122 |
aliases: ['purge_custom_stateless_actions'] |
| 123 |
wait: |
| 124 |
description: |
| 125 |
- Whether to wait for the firewall policy to reach the |
| 126 |
C(ACTIVE) or C(DELETED) state before the module returns. |
| 127 |
type: bool |
| 128 |
required: false |
| 129 |
default: true |
| 130 |
wait_timeout: |
| 131 |
description: |
| 132 |
- Maximum time, in seconds, to wait for the firewall policy |
| 133 |
to reach the expected state. |
| 134 |
- Defaults to 600 seconds. |
| 135 |
type: int |
| 136 |
required: false |
| 137 |
|
| 138 |
author: |
| 139 |
- Mark Chappell (@tremble) |
| 140 |
extends_documentation_fragment: |
| 141 |
- amazon.aws.common.modules |
| 142 |
- amazon.aws.region.modules |
| 143 |
- amazon.aws.boto3 |
| 144 |
- amazon.aws.tags |
| 145 |
""" |
| 146 |
|
| 147 |
EXAMPLES = r""" |
| 148 |
# Create an AWS Network Firewall Policy with default rule order |
| 149 |
- community.aws.networkfirewall_policy: |
| 150 |
stateful_rule_order: 'default' |
| 151 |
state: present |
| 152 |
name: 'ExamplePolicy' |
| 153 |
|
| 154 |
# Create an AWS Network Firewall Policy with strict rule order |
| 155 |
- community.aws.networkfirewall_policy: |
| 156 |
stateful_rule_order: 'strict' |
| 157 |
state: present |
| 158 |
name: 'ExampleStrictPolicy' |
| 159 |
|
| 160 |
|
| 161 |
# Create an AWS Network Firewall Policy that defaults to dropping all packets |
| 162 |
- community.aws.networkfirewall_policy: |
| 163 |
stateful_rule_order: 'strict' |
| 164 |
state: present |
| 165 |
name: 'ExampleDropPolicy' |
| 166 |
stateful_default_actions: |
| 167 |
- 'aws:drop_strict' |
| 168 |
stateful_rule_groups: |
| 169 |
- 'ExampleStrictRuleGroup' |
| 170 |
- 'arn:aws:network-firewall:us-east-1:aws-managed:stateful-rulegroup/BotNetCommandAndControlDomainsStrictOrder' |
| 171 |
|
| 172 |
# Delete an AWS Network Firewall Policy |
| 173 |
- community.aws.networkfirewall_policy: |
| 174 |
state: absent |
| 175 |
name: 'ExampleDropPolicy' |
| 176 |
""" |
| 177 |
|
| 178 |
RETURN = r""" |
| 179 |
policy: |
| 180 |
description: The details of the policy |
| 181 |
type: dict |
| 182 |
returned: success |
| 183 |
contains: |
| 184 |
policy: |
| 185 |
description: The details of the policy |
| 186 |
type: dict |
| 187 |
returned: success |
| 188 |
contains: |
| 189 |
stateful_engine_options: |
| 190 |
description: |
| 191 |
- Extra options describing how the stateful rules should be handled. |
| 192 |
type: dict |
| 193 |
returned: success |
| 194 |
contains: |
| 195 |
rule_order: |
| 196 |
description: |
| 197 |
- How rule group evaluation will be ordered. |
| 198 |
- For more information on rule evaluation ordering see the AWS documentation |
| 199 |
U(https://docs.aws.amazon.com/network-firewall/latest/developerguide/suricata-rule-evaluation-order.html). |
| 200 |
type: str |
| 201 |
returned: success |
| 202 |
example: 'DEFAULT_ACTION_ORDER' |
| 203 |
stateful_rule_group_references: |
| 204 |
description: Information about the stateful rule groups attached to the policy. |
| 205 |
type: list |
| 206 |
elements: dict |
| 207 |
returned: success |
| 208 |
contains: |
| 209 |
resource_arn: |
| 210 |
description: The ARN of the rule group. |
| 211 |
type: str |
| 212 |
returned: success |
| 213 |
example: 'arn:aws:network-firewall:us-east-1:aws-managed:stateful-rulegroup/AbusedLegitMalwareDomainsActionOrder' |
| 214 |
priority: |
| 215 |
description: |
| 216 |
- An integer that indicates the order in which to run the stateful rule groups in a single policy. |
| 217 |
- This only applies to policies that specify the STRICT_ORDER rule order in the stateful engine options settings. |
| 218 |
type: int |
| 219 |
returned: success |
| 220 |
example: 1234 |
| 221 |
stateless_custom_actions: |
| 222 |
description: |
| 223 |
- A description of additional custom actions available for use as |
| 224 |
default rules to apply to stateless packets. |
| 225 |
type: list |
| 226 |
elements: dict |
| 227 |
returned: success |
| 228 |
contains: |
| 229 |
action_name: |
| 230 |
description: A name for the action. |
| 231 |
type: str |
| 232 |
returned: success |
| 233 |
example: 'ExampleAction' |
| 234 |
action_definition: |
| 235 |
description: The action to perform. |
| 236 |
type: dict |
| 237 |
returned: success |
| 238 |
contains: |
| 239 |
publish_metric_action: |
| 240 |
description: |
| 241 |
- Definition of a custom metric to be published to CloudWatch. |
| 242 |
- U(https://docs.aws.amazon.com/network-firewall/latest/developerguide/monitoring-cloudwatch.html) |
| 243 |
type: dict |
| 244 |
returned: success |
| 245 |
contains: |
| 246 |
dimensions: |
| 247 |
description: |
| 248 |
- The values of the CustomAction dimension to set on the metrics. |
| 249 |
- The dimensions of a metric are used to identify unique |
| 250 |
streams of data. |
| 251 |
type: list |
| 252 |
elements: dict |
| 253 |
returned: success |
| 254 |
contains: |
| 255 |
value: |
| 256 |
description: A value of the CustomAction dimension to set on the metrics. |
| 257 |
type: str |
| 258 |
returned: success |
| 259 |
example: 'ExampleRule' |
| 260 |
stateless_default_actions: |
| 261 |
description: The default actions to take on a packet that doesn't match any stateful rules. |
| 262 |
type: list |
| 263 |
elements: str |
| 264 |
returned: success |
| 265 |
example: ['aws:alert_strict'] |
| 266 |
stateless_fragment_default_actions: |
| 267 |
description: The actions to take on a packet if it doesn't match any of the stateless rules in the policy. |
| 268 |
type: list |
| 269 |
elements: str |
| 270 |
returned: success |
| 271 |
example: ['aws:pass'] |
| 272 |
stateless_rule_group_references: |
| 273 |
description: Information about the stateful rule groups attached to the policy. |
| 274 |
type: list |
| 275 |
elements: dict |
| 276 |
returned: success |
| 277 |
contains: |
| 278 |
resource_arn: |
| 279 |
description: The ARN of the rule group. |
| 280 |
type: str |
| 281 |
returned: success |
| 282 |
example: 'arn:aws:network-firewall:us-east-1:123456789012:stateless-rulegroup/ExampleGroup' |
| 283 |
priority: |
| 284 |
description: |
| 285 |
- An integer that indicates the order in which to run the stateless rule groups in a single policy. |
| 286 |
type: str |
| 287 |
returned: success |
| 288 |
example: 12345 |
| 289 |
policy_metadata: |
| 290 |
description: Metadata about the policy |
| 291 |
type: dict |
| 292 |
returned: success |
| 293 |
contains: |
| 294 |
consumed_stateful_rule_capacity: |
| 295 |
description: The total number of capacity units used by the stateful rule groups. |
| 296 |
type: int |
| 297 |
returned: success |
| 298 |
example: 165 |
| 299 |
consumed_stateless_rule_capacity: |
| 300 |
description: The total number of capacity units used by the stateless rule groups. |
| 301 |
type: int |
| 302 |
returned: success |
| 303 |
example: 2010 |
| 304 |
firewall_policy_arn: |
| 305 |
description: The ARN of the policy. |
| 306 |
type: str |
| 307 |
returned: success |
| 308 |
example: arn:aws:network-firewall:us-east-1:123456789012:firewall-policy/ExamplePolicy |
| 309 |
firewall_policy_id: |
| 310 |
description: The unique ID of the policy. |
| 311 |
type: str |
| 312 |
returned: success |
| 313 |
example: 12345678-abcd-1234-5678-123456789abc |
| 314 |
firewall_policy_name: |
| 315 |
description: The name of the policy. |
| 316 |
type: str |
| 317 |
returned: success |
| 318 |
example: ExamplePolicy |
| 319 |
firewall_policy_status: |
| 320 |
description: The current status of the policy. |
| 321 |
type: str |
| 322 |
returned: success |
| 323 |
example: ACTIVE |
| 324 |
number_of_associations: |
| 325 |
description: The number of firewalls the policy is associated to. |
| 326 |
type: int |
| 327 |
returned: success |
| 328 |
example: 1 |
| 329 |
tags: |
| 330 |
description: A dictionary representing the tags associated with the policy. |
| 331 |
type: dict |
| 332 |
returned: success |
| 333 |
example: {'tagName': 'Some Value'} |
| 334 |
""" |
| 335 |
|
| 336 |
from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule |
| 337 |
from ansible_collections.community.aws.plugins.module_utils.networkfirewall import NetworkFirewallPolicyManager |
| 338 |
|
| 339 |
|
| 340 |
def main(): |
| 341 |
custom_action_options = dict( |
| 342 |
name=dict(type="str", required=True), |
| 343 |
# Poorly documented, but "publishMetricAction.dimensions ... must have length less than or equal to 1" |
| 344 |
publish_metric_dimension_value=dict(type="str", required=False, aliases=["publish_metric_dimension_values"]), |
| 345 |
# NetworkFirewallPolicyManager can cope with a list for future-proofing |
| 346 |
# publish_metric_dimension_values=dict(type='list', elements='str', required=False, aliases=['publish_metric_dimension_value']), |
| 347 |
) |
| 348 |
|
| 349 |
argument_spec = dict( |
| 350 |
name=dict(type="str", required=False), |
| 351 |
arn=dict(type="str", required=False), |
| 352 |
state=dict(type="str", required=False, default="present", choices=["present", "absent"]), |
| 353 |
description=dict(type="str", required=False), |
| 354 |
tags=dict(type="dict", required=False, aliases=["resource_tags"]), |
| 355 |
purge_tags=dict(type="bool", required=False, default=True), |
| 356 |
stateful_rule_groups=dict(type="list", elements="str", required=False, aliases=["stateful_groups"]), |
| 357 |
stateless_rule_groups=dict(type="list", elements="str", required=False, aliases=["stateless_groups"]), |
| 358 |
stateful_default_actions=dict(type="list", elements="str", required=False), |
| 359 |
stateless_default_actions=dict(type="list", elements="str", required=False), |
| 360 |
stateless_fragment_default_actions=dict(type="list", elements="str", required=False), |
| 361 |
stateful_rule_order=dict(type="str", required=False, choices=["strict", "default"], aliases=["rule_order"]), |
| 362 |
stateless_custom_actions=dict( |
| 363 |
type="list", |
| 364 |
elements="dict", |
| 365 |
required=False, |
| 366 |
options=custom_action_options, |
| 367 |
aliases=["custom_stateless_actions"], |
| 368 |
), |
| 369 |
purge_stateless_custom_actions=dict( |
| 370 |
type="bool", required=False, default=True, aliases=["purge_custom_stateless_actions"] |
| 371 |
), |
| 372 |
wait=dict(type="bool", required=False, default=True), |
| 373 |
wait_timeout=dict(type="int", required=False), |
| 374 |
) |
| 375 |
|
| 376 |
mutually_exclusive = [ |
| 377 |
["arn", "name"], |
| 378 |
] |
| 379 |
required_one_of = [ |
| 380 |
["arn", "name"], |
| 381 |
] |
| 382 |
|
| 383 |
module = AnsibleAWSModule( |
| 384 |
argument_spec=argument_spec, |
| 385 |
supports_check_mode=True, |
| 386 |
mutually_exclusive=mutually_exclusive, |
| 387 |
required_one_of=required_one_of, |
| 388 |
) |
| 389 |
|
| 390 |
arn = module.params.get("arn") |
| 391 |
name = module.params.get("name") |
| 392 |
state = module.params.get("state") |
| 393 |
|
| 394 |
manager = NetworkFirewallPolicyManager(module, name=name, arn=arn) |
| 395 |
manager.set_wait(module.params.get("wait", None)) |
| 396 |
manager.set_wait_timeout(module.params.get("wait_timeout", None)) |
| 397 |
|
| 398 |
if state == "absent": |
| 399 |
manager.delete() |
| 400 |
else: |
| 401 |
manager.set_description(module.params.get("description", None)) |
| 402 |
manager.set_tags(module.params.get("tags", None), module.params.get("purge_tags", None)) |
| 403 |
# Actions need to be defined before potentially consuming them |
| 404 |
manager.set_custom_stateless_actions( |
| 405 |
module.params.get("stateless_custom_actions", None), |
| 406 |
module.params.get("purge_stateless_custom_actions", True), |
| 407 |
) |
| 408 |
manager.set_stateful_rule_order(module.params.get("stateful_rule_order", None)) |
| 409 |
manager.set_stateful_rule_groups(module.params.get("stateful_rule_groups", None)) |
| 410 |
manager.set_stateless_rule_groups(module.params.get("stateless_rule_groups", None)) |
| 411 |
manager.set_stateful_default_actions(module.params.get("stateful_default_actions", None)) |
| 412 |
manager.set_stateless_default_actions(module.params.get("stateless_default_actions", None)) |
| 413 |
manager.set_stateless_fragment_default_actions(module.params.get("stateless_fragment_default_actions", None)) |
| 414 |
|
| 415 |
manager.flush_changes() |
| 416 |
|
| 417 |
results = dict( |
| 418 |
changed=manager.changed, |
| 419 |
policy=manager.updated_resource, |
| 420 |
) |
| 421 |
if manager.changed: |
| 422 |
diff = dict( |
| 423 |
before=manager.original_resource, |
| 424 |
after=manager.updated_resource, |
| 425 |
) |
| 426 |
results["diff"] = diff |
| 427 |
module.exit_json(**results) |
| 428 |
|
| 429 |
|
| 430 |
if __name__ == "__main__": |
| 431 |
main() |