| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_azurefirewall_info |
| 14 |
version_added: '0.1.2' |
| 15 |
short_description: Get AzureFirewall info |
| 16 |
description: |
| 17 |
- Get info of AzureFirewall. |
| 18 |
options: |
| 19 |
resource_group: |
| 20 |
description: |
| 21 |
- The name of the resource group. |
| 22 |
type: str |
| 23 |
name: |
| 24 |
description: |
| 25 |
- Resource name. |
| 26 |
type: str |
| 27 |
extends_documentation_fragment: |
| 28 |
- azure.azcollection.azure |
| 29 |
author: |
| 30 |
- Liu Qingyi (@smile37773) |
| 31 |
|
| 32 |
''' |
| 33 |
|
| 34 |
EXAMPLES = ''' |
| 35 |
- name: List all Azure Firewalls for a given subscription |
| 36 |
azure_rm_azurefirewall_info: |
| 37 |
- name: List all Azure Firewalls for a given resource group |
| 38 |
azure_rm_azurefirewall_info: |
| 39 |
resource_group: myResourceGroup |
| 40 |
- name: Get Azure Firewall |
| 41 |
azure_rm_azurefirewall_info: |
| 42 |
resource_group: myResourceGroup |
| 43 |
name: myAzureFirewall |
| 44 |
''' |
| 45 |
|
| 46 |
RETURN = ''' |
| 47 |
firewalls: |
| 48 |
description: |
| 49 |
- A list of dict results where the key is the name of the AzureFirewall and the values are the facts for that AzureFirewall. |
| 50 |
returned: always |
| 51 |
type: complex |
| 52 |
contains: |
| 53 |
id: |
| 54 |
description: |
| 55 |
- Resource ID. |
| 56 |
returned: always |
| 57 |
type: str |
| 58 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/ |
| 59 |
myResourceGroup/providers/Microsoft.Network/azureFirewalls/myAzureFirewall" |
| 60 |
name: |
| 61 |
description: |
| 62 |
- Resource name. |
| 63 |
returned: always |
| 64 |
type: str |
| 65 |
sample: "myAzureFirewall" |
| 66 |
location: |
| 67 |
description: |
| 68 |
- Resource location. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: "eastus" |
| 72 |
tags: |
| 73 |
description: |
| 74 |
- Resource tags. |
| 75 |
returned: always |
| 76 |
type: dict |
| 77 |
sample: { "tag": "value" } |
| 78 |
etag: |
| 79 |
description: |
| 80 |
- Gets a unique read-only string that changes whenever the resource is updated. |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 84 |
application_rule_collections: |
| 85 |
description: |
| 86 |
- Collection of application rule collections used by Azure Firewall. |
| 87 |
type: list |
| 88 |
returned: always |
| 89 |
sample: [ |
| 90 |
{ |
| 91 |
"etag": "25b7fca4-c909-4913-8799-6cdb7da2e298", |
| 92 |
"id": "/subscriptions/xxx-xxx1/resourceGroups/myResourceGroup/providers/ |
| 93 |
Microsoft.Network/azureFirewalls/myFirewall/applicationRuleCollections/apprulecoll", |
| 94 |
"name": "apprulecoll", |
| 95 |
"properties": { |
| 96 |
"action": { |
| 97 |
"type": "Deny" |
| 98 |
}, |
| 99 |
"priority": 110, |
| 100 |
"provisioningState": "Failed", |
| 101 |
"rules": [ |
| 102 |
{ |
| 103 |
"actions": [], |
| 104 |
"description": "Deny inbound rule", |
| 105 |
"direction": "Inbound", |
| 106 |
"fqdnTags": [], |
| 107 |
"name": "rule1", |
| 108 |
"priority": 0, |
| 109 |
"protocols": [ |
| 110 |
{ |
| 111 |
"port": 443, |
| 112 |
"protocolType": "Https" |
| 113 |
} |
| 114 |
], |
| 115 |
"sourceAddresses": [ |
| 116 |
"216.58.216.164", |
| 117 |
"10.0.0.0/25" |
| 118 |
], |
| 119 |
"sourceIpGroups": [], |
| 120 |
"targetFqdns": [ |
| 121 |
"www.test.com" |
| 122 |
] |
| 123 |
} |
| 124 |
] |
| 125 |
}, |
| 126 |
"type": "Microsoft.Network/azureFirewalls/applicationRuleCollections" |
| 127 |
} |
| 128 |
] |
| 129 |
nat_rule_collections: |
| 130 |
description: |
| 131 |
- Collection of NAT rule collections used by Azure Firewall. |
| 132 |
type: list |
| 133 |
returned: always |
| 134 |
sample: [ |
| 135 |
{ |
| 136 |
"etag": '3759e8b1-cdf7-44f4-a643-3a75e2b67877', |
| 137 |
"id": "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/ |
| 138 |
azureFirewalls/myFirewall/natRuleCollections/natrulecoll", |
| 139 |
"name": "natrulecoll", |
| 140 |
"properties": { |
| 141 |
"action": { |
| 142 |
"type": "Dnat" |
| 143 |
}, |
| 144 |
"priority": 112, |
| 145 |
"provisioningState": "Failed", |
| 146 |
"rules": [ |
| 147 |
{ |
| 148 |
"description": "D-NAT all outbound web traffic for inspection", |
| 149 |
"destinationAddresses": [ |
| 150 |
"20.169.156.124" |
| 151 |
], |
| 152 |
"destinationPorts": [ |
| 153 |
"443" |
| 154 |
], |
| 155 |
"name": "DNAT-HTTPS-traffic", |
| 156 |
"protocols": [ |
| 157 |
"TCP" |
| 158 |
], |
| 159 |
"sourceAddresses": [ |
| 160 |
"*" |
| 161 |
], |
| 162 |
"translatedAddress": "1.2.3.5", |
| 163 |
"translatedPort": "8443" |
| 164 |
} |
| 165 |
] |
| 166 |
}, |
| 167 |
"type": "Microsoft.Network/azureFirewalls/natRuleCollections" |
| 168 |
} |
| 169 |
] |
| 170 |
network_rule_collections: |
| 171 |
description: |
| 172 |
- Collection of network rule collections used by Azure Firewall. |
| 173 |
type: list |
| 174 |
returned: always |
| 175 |
sample: [ |
| 176 |
{ |
| 177 |
"etag": "3759e8b1-cdf7-44f4-a643-3a75e2b67877", |
| 178 |
"id": "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/ |
| 179 |
azureFirewalls/myFirewall/networkRuleCollections/netrulecoll", |
| 180 |
"name": "netrulecoll", |
| 181 |
"properties": { |
| 182 |
"action": { |
| 183 |
"type": "Deny" |
| 184 |
}, |
| 185 |
"priority": 112, |
| 186 |
"provisioningState": "Failed", |
| 187 |
"rules": [ |
| 188 |
{ |
| 189 |
"description": "Block traffic based on source IPs and ports", |
| 190 |
"destinationAddresses": [ |
| 191 |
"*" |
| 192 |
], |
| 193 |
"destinationPorts": [ |
| 194 |
"443-444", |
| 195 |
"8443" |
| 196 |
], |
| 197 |
"name": "L4-traffic", |
| 198 |
"protocols": [ |
| 199 |
"TCP" |
| 200 |
], |
| 201 |
"sourceAddresses": [ |
| 202 |
"192.168.1.1-192.168.1.12", |
| 203 |
"10.1.4.12-10.1.4.255" |
| 204 |
] |
| 205 |
}, |
| 206 |
{ |
| 207 |
"description": "Block traffic based on source IPs and ports to amazon", |
| 208 |
"destinationAddresses": [], |
| 209 |
"destinationPorts": [ |
| 210 |
"443-444", |
| 211 |
"8443" |
| 212 |
], |
| 213 |
"name": "L4-traffic-with_FQDN", |
| 214 |
"protocols": [ |
| 215 |
"TCP" |
| 216 |
], |
| 217 |
"sourceAddresses": [ |
| 218 |
"10.2.4.12-10.2.4.255" |
| 219 |
] |
| 220 |
} |
| 221 |
] |
| 222 |
}, |
| 223 |
"type": "Microsoft.Network/azureFirewalls/networkRuleCollections" |
| 224 |
} |
| 225 |
] |
| 226 |
ip_configurations: |
| 227 |
description: |
| 228 |
- IP configuration of the Azure Firewall resource. |
| 229 |
type: list |
| 230 |
returned: always |
| 231 |
sample: [ |
| 232 |
{ |
| 233 |
"etag": "3759e8b1-cdf7-44f4-a643-3a75e2b67877", |
| 234 |
"id": "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/azureFirewalls/myFirewall/i |
| 235 |
azureFirewallIpConfigurations/azureFirewallIpConfiguration", |
| 236 |
"name": "azureFirewallIpConfiguration", |
| 237 |
"properties": { |
| 238 |
"privateIPAllocationMethod": "Dynamic", |
| 239 |
"provisioningState": "Succeeded", |
| 240 |
"publicIPAddress": { |
| 241 |
"id": "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/publicIPAddresses/myPublicIpAddress" |
| 242 |
}, |
| 243 |
"subnet": { |
| 244 |
"id": "/subscriptions/xxx-xxx/resourceGroups/myResourceGroup/providers/Microsoft.Network/ |
| 245 |
virtualNetworks/myVirtualNetwork/subnets/AzureFirewallSubnet" |
| 246 |
} |
| 247 |
}, |
| 248 |
"type": "Microsoft.Network/azureFirewalls/azureFirewallIpConfigurations" |
| 249 |
} |
| 250 |
] |
| 251 |
provisioning_state: |
| 252 |
description: |
| 253 |
- The current state of the gallery. |
| 254 |
type: str |
| 255 |
sample: "Succeeded" |
| 256 |
|
| 257 |
''' |
| 258 |
|
| 259 |
import json |
| 260 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 261 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_rest import GenericRestClient |
| 262 |
|
| 263 |
|
| 264 |
class AzureRMAzureFirewallsInfo(AzureRMModuleBase): |
| 265 |
def __init__(self): |
| 266 |
self.module_arg_spec = dict( |
| 267 |
resource_group=dict( |
| 268 |
type='str' |
| 269 |
), |
| 270 |
name=dict( |
| 271 |
type='str' |
| 272 |
) |
| 273 |
) |
| 274 |
|
| 275 |
self.resource_group = None |
| 276 |
self.name = None |
| 277 |
|
| 278 |
self.results = dict(changed=False) |
| 279 |
self.mgmt_client = None |
| 280 |
self.state = None |
| 281 |
self.url = None |
| 282 |
self.status_code = [200] |
| 283 |
|
| 284 |
self.query_parameters = {} |
| 285 |
self.query_parameters['api-version'] = '2024-01-01' |
| 286 |
self.header_parameters = {} |
| 287 |
self.header_parameters['Content-Type'] = 'application/json; charset=utf-8' |
| 288 |
|
| 289 |
self.mgmt_client = None |
| 290 |
super(AzureRMAzureFirewallsInfo, self).__init__(self.module_arg_spec, supports_check_mode=True, supports_tags=False) |
| 291 |
|
| 292 |
def exec_module(self, **kwargs): |
| 293 |
|
| 294 |
for key in self.module_arg_spec: |
| 295 |
setattr(self, key, kwargs[key]) |
| 296 |
|
| 297 |
self.mgmt_client = self.get_mgmt_svc_client(GenericRestClient, |
| 298 |
base_url=self._cloud_environment.endpoints.resource_manager) |
| 299 |
|
| 300 |
if (self.resource_group is not None and self.name is not None): |
| 301 |
self.results['firewalls'] = self.get() |
| 302 |
elif (self.resource_group is not None): |
| 303 |
self.results['firewalls'] = self.list() |
| 304 |
else: |
| 305 |
self.results['firewalls'] = self.listall() |
| 306 |
return self.results |
| 307 |
|
| 308 |
def get(self): |
| 309 |
response = None |
| 310 |
results = {} |
| 311 |
|
| 312 |
self.url = ('/subscriptions' + |
| 313 |
'/{{ subscription_id }}' + |
| 314 |
'/resourceGroups' + |
| 315 |
'/{{ resource_group }}' + |
| 316 |
'/providers' + |
| 317 |
'/Microsoft.Network' + |
| 318 |
'/azureFirewalls' + |
| 319 |
'/{{ azure_firewall_name }}') |
| 320 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 321 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 322 |
self.url = self.url.replace('{{ azure_firewall_name }}', self.name) |
| 323 |
|
| 324 |
try: |
| 325 |
response = self.mgmt_client.query(self.url, |
| 326 |
'GET', |
| 327 |
self.query_parameters, |
| 328 |
self.header_parameters, |
| 329 |
None, |
| 330 |
self.status_code, |
| 331 |
600, |
| 332 |
30) |
| 333 |
results = json.loads(response.body()) |
| 334 |
|
| 335 |
except Exception as e: |
| 336 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 337 |
|
| 338 |
return self.format_item(results) |
| 339 |
|
| 340 |
def list(self): |
| 341 |
response = None |
| 342 |
results = {} |
| 343 |
|
| 344 |
self.url = ('/subscriptions' + |
| 345 |
'/{{ subscription_id }}' + |
| 346 |
'/resourceGroups' + |
| 347 |
'/{{ resource_group }}' + |
| 348 |
'/providers' + |
| 349 |
'/Microsoft.Network' + |
| 350 |
'/azureFirewalls') |
| 351 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 352 |
self.url = self.url.replace('{{ resource_group }}', self.resource_group) |
| 353 |
|
| 354 |
try: |
| 355 |
response = self.mgmt_client.query(self.url, |
| 356 |
'GET', |
| 357 |
self.query_parameters, |
| 358 |
self.header_parameters, |
| 359 |
None, |
| 360 |
self.status_code, |
| 361 |
600, |
| 362 |
30) |
| 363 |
results = json.loads(response.body()) |
| 364 |
|
| 365 |
except Exception as e: |
| 366 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 367 |
|
| 368 |
return [self.format_item(x) for x in results['value']] if results['value'] else [] |
| 369 |
|
| 370 |
def listall(self): |
| 371 |
response = None |
| 372 |
results = {} |
| 373 |
|
| 374 |
self.url = ('/subscriptions' + |
| 375 |
'/{{ subscription_id }}' + |
| 376 |
'/providers' + |
| 377 |
'/Microsoft.Network' + |
| 378 |
'/azureFirewalls') |
| 379 |
self.url = self.url.replace('{{ subscription_id }}', self.subscription_id) |
| 380 |
|
| 381 |
try: |
| 382 |
response = self.mgmt_client.query(self.url, |
| 383 |
'GET', |
| 384 |
self.query_parameters, |
| 385 |
self.header_parameters, |
| 386 |
None, |
| 387 |
self.status_code, |
| 388 |
600, |
| 389 |
30) |
| 390 |
results = json.loads(response.body()) |
| 391 |
|
| 392 |
except Exception as e: |
| 393 |
self.log('Could not get info for @(Model.ModuleOperationNameUpper).') |
| 394 |
|
| 395 |
return [self.format_item(x) for x in results['value']] if results['value'] else [] |
| 396 |
|
| 397 |
def format_item(self, item): |
| 398 |
if item is None or item == {}: |
| 399 |
return {} |
| 400 |
d = { |
| 401 |
'id': item.get('id'), |
| 402 |
'name': item.get('name'), |
| 403 |
'location': item.get('location'), |
| 404 |
'etag': item.get('etag'), |
| 405 |
'tags': item.get('tags'), |
| 406 |
'nat_rule_collections': dict(), |
| 407 |
'network_rule_collections': dict(), |
| 408 |
'ip_configurations': dict(), |
| 409 |
} |
| 410 |
if isinstance(item.get('properties'), dict): |
| 411 |
d['nat_rule_collections'] = item.get('properties').get('natRuleCollections') |
| 412 |
d['network_rule_collections'] = item.get('properties').get('networkRuleCollections') |
| 413 |
d['application_rule_collections'] = item.get('properties').get('applicationRuleCollections') |
| 414 |
d['ip_configurations'] = item.get('properties').get('ipConfigurations') |
| 415 |
d['provisioning_state'] = item.get('properties').get('provisioningState') |
| 416 |
return d |
| 417 |
|
| 418 |
|
| 419 |
def main(): |
| 420 |
AzureRMAzureFirewallsInfo() |
| 421 |
|
| 422 |
|
| 423 |
if __name__ == '__main__': |
| 424 |
main() |