| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_firewallpolicy |
| 13 |
|
| 14 |
version_added: "1.13.0" |
| 15 |
|
| 16 |
short_description: Create, delete or update specified firewall policy. |
| 17 |
|
| 18 |
description: |
| 19 |
- Creates, deletes, or updates given firewall policy in specified resource group. |
| 20 |
|
| 21 |
options: |
| 22 |
resource_group: |
| 23 |
description: |
| 24 |
- Name of the resource group. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the firewall policy. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
location: |
| 33 |
description: |
| 34 |
- Location for firewall policy. Defaults to location of resource group if not specified. |
| 35 |
type: str |
| 36 |
base_policy: |
| 37 |
description: |
| 38 |
- The name of the parent firewall policy from which rules are inherited. |
| 39 |
type: str |
| 40 |
threat_intel_mode: |
| 41 |
description: |
| 42 |
- The operation mode for Threat Intel. |
| 43 |
default: alert |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- alert |
| 47 |
- deny |
| 48 |
- 'off' |
| 49 |
threat_intel_whitelist: |
| 50 |
description: |
| 51 |
- ThreatIntel Whitelist for Firewall Policy. |
| 52 |
type: dict |
| 53 |
suboptions: |
| 54 |
ip_addresses: |
| 55 |
description: |
| 56 |
- List of IP addresses for the ThreatIntel Whitelist. |
| 57 |
type: list |
| 58 |
elements: str |
| 59 |
append_ip_addresses: |
| 60 |
description: |
| 61 |
- Flag to indicate if the ip_addresses to be appended or not. |
| 62 |
type: bool |
| 63 |
default: true |
| 64 |
fqdns: |
| 65 |
description: |
| 66 |
- List of FQDNs for the ThreatIntel Whitelist |
| 67 |
type: list |
| 68 |
elements: str |
| 69 |
append_fqdns: |
| 70 |
description: |
| 71 |
- Flag to indicate if the fqdns to be appended or not. |
| 72 |
type: bool |
| 73 |
default: true |
| 74 |
state: |
| 75 |
description: |
| 76 |
- Assert the state of the firewall policy. Use C(present) to create or update and C(absent) to delete. |
| 77 |
default: present |
| 78 |
type: str |
| 79 |
choices: |
| 80 |
- absent |
| 81 |
- present |
| 82 |
|
| 83 |
extends_documentation_fragment: |
| 84 |
- azure.azcollection.azure |
| 85 |
- azure.azcollection.azure_tags |
| 86 |
|
| 87 |
author: |
| 88 |
- Aparna Patil (@aparna-patil) |
| 89 |
''' |
| 90 |
|
| 91 |
EXAMPLES = ''' |
| 92 |
- name: Create a Firewall Policy |
| 93 |
azure_rm_firewallpolicy: |
| 94 |
resource_group: myAzureResourceGroup |
| 95 |
name: myfirewallpolicy |
| 96 |
base_policy: firewallparentpolicy |
| 97 |
threat_intel_mode: alert |
| 98 |
threat_intel_whitelist: |
| 99 |
ip_addresses: |
| 100 |
- 10.0.0.1 |
| 101 |
- 10.0.0.2 |
| 102 |
fqdns: |
| 103 |
- "*.microsoft.com" |
| 104 |
- "*.azure.com" |
| 105 |
state: present |
| 106 |
|
| 107 |
- name: Update Firewall Policy |
| 108 |
azure_rm_firewallpolicy: |
| 109 |
resource_group: myAzureResourceGroup |
| 110 |
name: myfirewallpolicy |
| 111 |
base_policy: firewallparentpolicy |
| 112 |
threat_intel_mode: deny |
| 113 |
threat_intel_whitelist: |
| 114 |
ip_addresses: |
| 115 |
- 10.0.0.1 |
| 116 |
fqdns: |
| 117 |
- "*.microsoft.com" |
| 118 |
state: present |
| 119 |
tags: |
| 120 |
key1: "value1" |
| 121 |
|
| 122 |
- name: Delete Firewall Policy |
| 123 |
azure_rm_firewallpolicy: |
| 124 |
resource_group: myAzureResourceGroup |
| 125 |
name: myfirewallpolicy |
| 126 |
state: absent |
| 127 |
''' |
| 128 |
|
| 129 |
RETURN = ''' |
| 130 |
state: |
| 131 |
description: |
| 132 |
- Current state of the Firewall Policy. |
| 133 |
returned: always |
| 134 |
type: complex |
| 135 |
contains: |
| 136 |
id: |
| 137 |
description: |
| 138 |
- The firewall policy ID. |
| 139 |
returned: always |
| 140 |
type: str |
| 141 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyAzureResourceGroup/providers/ |
| 142 |
Microsoft.Network/firewallPolicies/myfirewallpolicy" |
| 143 |
name: |
| 144 |
description: |
| 145 |
- The firewall policy name. |
| 146 |
returned: always |
| 147 |
type: str |
| 148 |
sample: 'myfirewallpolicy' |
| 149 |
location: |
| 150 |
description: |
| 151 |
- The Azure Region where the resource lives. |
| 152 |
returned: always |
| 153 |
type: str |
| 154 |
sample: eastus |
| 155 |
base_policy: |
| 156 |
description: |
| 157 |
- The parent firewall policy from which rules are inherited. |
| 158 |
returned: always |
| 159 |
type: dict |
| 160 |
sample: { |
| 161 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyAzureResourceGroup/providers/ |
| 162 |
Microsoft.Network/firewallPolicies/firewallparentpolicy" |
| 163 |
} |
| 164 |
child_policies: |
| 165 |
description: |
| 166 |
- List of references to Child Firewall Policies. |
| 167 |
returned: always |
| 168 |
type: list |
| 169 |
elements: dict |
| 170 |
sample: [ |
| 171 |
{ |
| 172 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyAzureResourceGroup/providers/ |
| 173 |
Microsoft.Network/firewallPolicies/childpolicy1" |
| 174 |
} |
| 175 |
] |
| 176 |
provisioning_state: |
| 177 |
description: |
| 178 |
- The provisioning state of the resource. |
| 179 |
returned: always |
| 180 |
type: str |
| 181 |
sample: Succeeded |
| 182 |
firewalls: |
| 183 |
description: |
| 184 |
- List of references to Azure Firewalls that this Firewall Policy is associated with. |
| 185 |
returned: always |
| 186 |
type: list |
| 187 |
elements: dict |
| 188 |
sample: [ |
| 189 |
{ |
| 190 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myAzureResourceGroup/providers/ |
| 191 |
Microsoft.Network/azureFirewalls/azurefirewall" |
| 192 |
} |
| 193 |
] |
| 194 |
rule_collection_groups: |
| 195 |
description: |
| 196 |
- List of references to FirewallPolicyRuleCollectionGroups. |
| 197 |
returned: always |
| 198 |
type: list |
| 199 |
elements: dict |
| 200 |
sample: [ |
| 201 |
{ |
| 202 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/MyAzureResourceGroup/providers/ |
| 203 |
Microsoft.Network/firewallPolicies/myfirewallpolicy/ |
| 204 |
ruleCollectionGroups/DefaultNetworkRuleCollectionGroup" |
| 205 |
} |
| 206 |
] |
| 207 |
threat_intel_mode: |
| 208 |
description: |
| 209 |
- The operation mode for Threat Intelligence. |
| 210 |
returned: always |
| 211 |
type: str |
| 212 |
sample: Alert |
| 213 |
threat_intel_whitelist: |
| 214 |
description: |
| 215 |
- ThreatIntel Whitelist for Firewall Policy. |
| 216 |
returned: always |
| 217 |
type: dict |
| 218 |
sample: { |
| 219 |
"fqdns": [ |
| 220 |
"*.microsoft.com", |
| 221 |
"*.azure.com" |
| 222 |
], |
| 223 |
"ip_addresses": [ |
| 224 |
"10.0.0.1", |
| 225 |
"10.0.0.2" |
| 226 |
] |
| 227 |
} |
| 228 |
tags: |
| 229 |
description: |
| 230 |
- Resource tags. |
| 231 |
returned: always |
| 232 |
type: list |
| 233 |
sample: [{"key1": "value1"}] |
| 234 |
type: |
| 235 |
description: |
| 236 |
- The type of resource. |
| 237 |
returned: always |
| 238 |
type: str |
| 239 |
sample: Microsoft.Network/FirewallPolicies |
| 240 |
etag: |
| 241 |
description: |
| 242 |
- The etag of the firewall policy. |
| 243 |
returned: always |
| 244 |
type: str |
| 245 |
sample: 7cb2538e-0e52-4435-8979-4f417e7269d1 |
| 246 |
''' |
| 247 |
|
| 248 |
from ansible.module_utils.basic import _load_params |
| 249 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, \ |
| 250 |
format_resource_id, normalize_location_name |
| 251 |
import copy |
| 252 |
|
| 253 |
try: |
| 254 |
from azure.core.exceptions import ResourceNotFoundError |
| 255 |
from azure.core.polling import LROPoller |
| 256 |
except ImportError: |
| 257 |
|
| 258 |
pass |
| 259 |
|
| 260 |
threat_intel_whitelist_spec = dict( |
| 261 |
ip_addresses=dict(type='list', elements='str'), |
| 262 |
append_ip_addresses=dict(type='bool', default=True), |
| 263 |
fqdns=dict(type='list', elements='str'), |
| 264 |
append_fqdns=dict(type='bool', default=True) |
| 265 |
) |
| 266 |
|
| 267 |
|
| 268 |
class AzureRMFirewallPolicy(AzureRMModuleBase): |
| 269 |
|
| 270 |
def __init__(self): |
| 271 |
|
| 272 |
_load_params() |
| 273 |
|
| 274 |
self.module_arg_spec = dict( |
| 275 |
resource_group=dict(type='str', required=True), |
| 276 |
name=dict(type='str', required=True), |
| 277 |
location=dict(type='str'), |
| 278 |
base_policy=dict(type='str'), |
| 279 |
threat_intel_mode=dict(choices=['alert', 'deny', 'off'], default='alert', type='str'), |
| 280 |
threat_intel_whitelist=dict(type='dict', options=threat_intel_whitelist_spec), |
| 281 |
state=dict(choices=['present', 'absent'], default='present', type='str'), |
| 282 |
) |
| 283 |
|
| 284 |
self.results = dict( |
| 285 |
changed=False, |
| 286 |
state=dict() |
| 287 |
) |
| 288 |
|
| 289 |
self.resource_group = None |
| 290 |
self.name = None |
| 291 |
self.state = None |
| 292 |
self.location = None |
| 293 |
self.base_policy = None |
| 294 |
self.threat_intel_mode = None |
| 295 |
self.threat_intel_whitelist = None |
| 296 |
self.tags = None |
| 297 |
|
| 298 |
super(AzureRMFirewallPolicy, self).__init__(self.module_arg_spec, |
| 299 |
supports_tags=True, |
| 300 |
supports_check_mode=True) |
| 301 |
|
| 302 |
def exec_module(self, **kwargs): |
| 303 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 304 |
setattr(self, key, kwargs[key]) |
| 305 |
|
| 306 |
changed = False |
| 307 |
results = dict() |
| 308 |
firewall_policy_old = None |
| 309 |
firewall_policy_new = None |
| 310 |
update_ip_address = False |
| 311 |
update_fqdns = False |
| 312 |
|
| 313 |
|
| 314 |
resource_group = self.get_resource_group(self.resource_group) |
| 315 |
if not self.location: |
| 316 |
|
| 317 |
self.location = resource_group.location |
| 318 |
|
| 319 |
self.location = normalize_location_name(self.location) |
| 320 |
|
| 321 |
if self.base_policy: |
| 322 |
base_policy = self.parse_resource_to_dict(self.base_policy) |
| 323 |
self.base_policy = format_resource_id(val=base_policy['name'], |
| 324 |
subscription_id=base_policy['subscription_id'], |
| 325 |
namespace='Microsoft.Network', |
| 326 |
types='firewallPolicies', |
| 327 |
resource_group=base_policy['resource_group']) |
| 328 |
|
| 329 |
try: |
| 330 |
self.log('Fetching Firewall policy {0}'.format(self.name)) |
| 331 |
firewall_policy_old = self.network_client.firewall_policies.get(self.resource_group, self.name) |
| 332 |
|
| 333 |
results = self.firewallpolicy_to_dict(firewall_policy_old) |
| 334 |
if self.state == 'present': |
| 335 |
changed = False |
| 336 |
update_tags, results['tags'] = self.update_tags(results['tags']) |
| 337 |
if update_tags: |
| 338 |
changed = True |
| 339 |
self.tags = results['tags'] |
| 340 |
if self.base_policy is not None: |
| 341 |
if ('base_policy' not in results and self.base_policy != "") or \ |
| 342 |
('base_policy' in results and self.base_policy != results['base_policy']['id']): |
| 343 |
changed = True |
| 344 |
results['base_policy'] = self.base_policy |
| 345 |
if self.threat_intel_mode is not None and \ |
| 346 |
self.threat_intel_mode.lower() != results['threat_intel_mode'].lower(): |
| 347 |
changed = True |
| 348 |
results['threat_intel_mode'] = self.threat_intel_mode |
| 349 |
if self.threat_intel_whitelist is not None: |
| 350 |
if 'threat_intel_whitelist' not in results: |
| 351 |
changed = True |
| 352 |
results['threat_intel_whitelist'] = self.threat_intel_whitelist |
| 353 |
else: |
| 354 |
update_ip_addresses, results['threat_intel_whitelist']['ip_addresses'] = \ |
| 355 |
self.update_values(results['threat_intel_whitelist']['ip_addresses'] |
| 356 |
if 'ip_addresses' in results['threat_intel_whitelist'] else [], |
| 357 |
self.threat_intel_whitelist['ip_addresses'] |
| 358 |
if self.threat_intel_whitelist['ip_addresses'] is not None else [], |
| 359 |
self.threat_intel_whitelist['append_ip_addresses']) |
| 360 |
update_fqdns, results['threat_intel_whitelist']['fqdns'] = \ |
| 361 |
self.update_values(results['threat_intel_whitelist']['fqdns'] |
| 362 |
if 'fqdns' in results['threat_intel_whitelist'] else [], |
| 363 |
self.threat_intel_whitelist['fqdns'] |
| 364 |
if self.threat_intel_whitelist['fqdns'] is not None else [], |
| 365 |
self.threat_intel_whitelist['append_fqdns']) |
| 366 |
if update_ip_addresses: |
| 367 |
changed = True |
| 368 |
self.threat_intel_whitelist['ip_addresses'] = results['threat_intel_whitelist']['ip_addresses'] |
| 369 |
if update_fqdns: |
| 370 |
changed = True |
| 371 |
self.threat_intel_whitelist['fqdns'] = results['threat_intel_whitelist']['fqdns'] |
| 372 |
elif self.state == 'absent': |
| 373 |
changed = True |
| 374 |
|
| 375 |
except ResourceNotFoundError: |
| 376 |
if self.state == 'present': |
| 377 |
changed = True |
| 378 |
else: |
| 379 |
changed = False |
| 380 |
|
| 381 |
self.results['changed'] = changed |
| 382 |
self.results['state'] = results |
| 383 |
|
| 384 |
if self.check_mode: |
| 385 |
return self.results |
| 386 |
|
| 387 |
if changed: |
| 388 |
if self.state == 'present': |
| 389 |
|
| 390 |
firewall_policy_new = \ |
| 391 |
self.network_models.FirewallPolicy(location=self.location, |
| 392 |
threat_intel_mode=self.threat_intel_mode) |
| 393 |
if self.base_policy: |
| 394 |
firewall_policy_new.base_policy = \ |
| 395 |
self.network_models.FirewallPolicy(id=self.base_policy) |
| 396 |
if self.threat_intel_whitelist: |
| 397 |
firewall_policy_new.threat_intel_whitelist = self.network_models.FirewallPolicyThreatIntelWhitelist( |
| 398 |
ip_addresses=self.threat_intel_whitelist['ip_addresses'], |
| 399 |
fqdns=self.threat_intel_whitelist['fqdns'] |
| 400 |
) |
| 401 |
if self.tags: |
| 402 |
firewall_policy_new.tags = self.tags |
| 403 |
self.results['state'] = self.create_or_update_firewallpolicy(firewall_policy_new) |
| 404 |
|
| 405 |
elif self.state == 'absent': |
| 406 |
|
| 407 |
self.delete_firewallpolicy() |
| 408 |
self.results['state'] = 'Deleted' |
| 409 |
|
| 410 |
return self.results |
| 411 |
|
| 412 |
def create_or_update_firewallpolicy(self, firewall_policy): |
| 413 |
try: |
| 414 |
|
| 415 |
response = self.network_client.firewall_policies.begin_create_or_update(resource_group_name=self.resource_group, |
| 416 |
firewall_policy_name=self.name, |
| 417 |
parameters=firewall_policy) |
| 418 |
if isinstance(response, LROPoller): |
| 419 |
response = self.get_poller_result(response) |
| 420 |
except Exception as exc: |
| 421 |
self.fail("Error creating or updating Firewall policy {0} - {1}".format(self.name, str(exc))) |
| 422 |
return self.firewallpolicy_to_dict(response) |
| 423 |
|
| 424 |
def delete_firewallpolicy(self): |
| 425 |
try: |
| 426 |
|
| 427 |
response = self.network_client.firewall_policies.begin_delete(resource_group_name=self.resource_group, |
| 428 |
firewall_policy_name=self.name) |
| 429 |
if isinstance(response, LROPoller): |
| 430 |
response = self.get_poller_result(response) |
| 431 |
except Exception as exc: |
| 432 |
self.fail("Error deleting Firewall policy {0} - {1}".format(self.name, str(exc))) |
| 433 |
return response |
| 434 |
|
| 435 |
def update_values(self, existing_values, param_values, append): |
| 436 |
|
| 437 |
|
| 438 |
new_values = copy.copy(existing_values) |
| 439 |
changed = False |
| 440 |
|
| 441 |
|
| 442 |
for item in param_values: |
| 443 |
if item not in new_values: |
| 444 |
changed = True |
| 445 |
new_values.append(item) |
| 446 |
|
| 447 |
if not append: |
| 448 |
for item in existing_values: |
| 449 |
if item not in param_values: |
| 450 |
new_values.remove(item) |
| 451 |
changed = True |
| 452 |
return changed, new_values |
| 453 |
|
| 454 |
def firewallpolicy_to_dict(self, firewallpolicy): |
| 455 |
result = firewallpolicy.as_dict() |
| 456 |
result['tags'] = firewallpolicy.tags |
| 457 |
return result |
| 458 |
|
| 459 |
|
| 460 |
def main(): |
| 461 |
AzureRMFirewallPolicy() |
| 462 |
|
| 463 |
|
| 464 |
if __name__ == '__main__': |
| 465 |
main() |