| 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_webappaccessrestriction |
| 14 |
version_added: "1.8.0" |
| 15 |
short_description: Manage web app network access restrictions |
| 16 |
description: |
| 17 |
- Add, remove, or update network access restrictions for a web app. |
| 18 |
options: |
| 19 |
name: |
| 20 |
description: |
| 21 |
- Name of the web app. |
| 22 |
required: true |
| 23 |
type: str |
| 24 |
resource_group: |
| 25 |
description: |
| 26 |
- Resource group of the web app. |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
state: |
| 30 |
description: |
| 31 |
- State of the access restrictions. Use C(present) to create or update and C(absent) to delete. |
| 32 |
type: str |
| 33 |
default: present |
| 34 |
choices: |
| 35 |
- absent |
| 36 |
- present |
| 37 |
ip_security_restrictions: |
| 38 |
description: |
| 39 |
- The web app's HTTP access restrictions. |
| 40 |
type: list |
| 41 |
elements: dict |
| 42 |
suboptions: |
| 43 |
name: |
| 44 |
description: |
| 45 |
- Name of the access restriction. |
| 46 |
type: str |
| 47 |
description: |
| 48 |
description: |
| 49 |
- Description of the access restriction. |
| 50 |
type: str |
| 51 |
action: |
| 52 |
description: |
| 53 |
- Traffic action for the access restriction. |
| 54 |
type: str |
| 55 |
default: Allow |
| 56 |
choices: |
| 57 |
- Allow |
| 58 |
- Deny |
| 59 |
priority: |
| 60 |
description: |
| 61 |
- Numerical priority of the access restriction. |
| 62 |
type: int |
| 63 |
required: true |
| 64 |
ip_address: |
| 65 |
description: |
| 66 |
- IPv4 address (with subnet mask) of the access restriction. |
| 67 |
type: str |
| 68 |
subnet_traffic_tag: |
| 69 |
description: |
| 70 |
- (internal) Subnet traffic tags. |
| 71 |
type: int |
| 72 |
vnet_traffic_tag: |
| 73 |
description: |
| 74 |
- (internal) Vnet traffic tag. |
| 75 |
type: int |
| 76 |
tag: |
| 77 |
description: |
| 78 |
- IP restriction rule description. |
| 79 |
type: str |
| 80 |
choices: |
| 81 |
- Default |
| 82 |
- XffProxy |
| 83 |
- ServiceTag |
| 84 |
vnet_subnet_resource_id: |
| 85 |
description: |
| 86 |
- The Virtual network relaste subnet resource id. |
| 87 |
- Only I(ip_adress) or I(vnet_subnet_resource_id) property must be specified. |
| 88 |
- Parameter I(vnet_subnet_resource_id) cannot be used with I(subnet_traffic_tag) or I(vnet_traffic_tag) at the same time. |
| 89 |
type: str |
| 90 |
scm_ip_security_restrictions: |
| 91 |
description: |
| 92 |
- >- |
| 93 |
The web app's SCM access restrictions. If I(scm_ip_security_restrictions_use_main) is set to C(true), |
| 94 |
the SCM restrictions will be configured but not used. |
| 95 |
type: list |
| 96 |
elements: dict |
| 97 |
suboptions: |
| 98 |
name: |
| 99 |
description: |
| 100 |
- Name of the access restriction. |
| 101 |
type: str |
| 102 |
description: |
| 103 |
description: |
| 104 |
- Description of the access restriction. |
| 105 |
type: str |
| 106 |
action: |
| 107 |
description: |
| 108 |
- Traffic action for the access restriction. |
| 109 |
type: str |
| 110 |
default: Allow |
| 111 |
choices: |
| 112 |
- Allow |
| 113 |
- Deny |
| 114 |
priority: |
| 115 |
description: |
| 116 |
- Numerical priority of the access restriction. |
| 117 |
type: int |
| 118 |
required: true |
| 119 |
ip_address: |
| 120 |
description: |
| 121 |
- IPv4 address (with subnet mask) of the access restriction. |
| 122 |
type: str |
| 123 |
subnet_traffic_tag: |
| 124 |
description: |
| 125 |
- (internal) Subnet traffic tags. |
| 126 |
type: int |
| 127 |
vnet_traffic_tag: |
| 128 |
description: |
| 129 |
- (internal) Vnet traffic tag. |
| 130 |
type: int |
| 131 |
tag: |
| 132 |
description: |
| 133 |
- IP restriction rule description. |
| 134 |
type: str |
| 135 |
choices: |
| 136 |
- Default |
| 137 |
- XffProxy |
| 138 |
- ServiceTag |
| 139 |
vnet_subnet_resource_id: |
| 140 |
description: |
| 141 |
- The Virtual network relaste subnet resource id. |
| 142 |
- Only I(ip_adress) or I(vnet_subnet_resource_id) property must be specified. |
| 143 |
- Parameter I(vnet_subnet_resource_id) cannot be used with I(subnet_traffic_tag) or I(vnet_traffic_tag) at the same time. |
| 144 |
type: str |
| 145 |
scm_ip_security_restrictions_use_main: |
| 146 |
description: |
| 147 |
- >- |
| 148 |
Set to C(true) to have the HTTP access restrictions also apply to the SCM site. |
| 149 |
If I(scm_ip_security_restrictions) are also applied, they will configured but not used. |
| 150 |
default: false |
| 151 |
type: bool |
| 152 |
|
| 153 |
extends_documentation_fragment: |
| 154 |
- azure.azcollection.azure |
| 155 |
|
| 156 |
author: |
| 157 |
- Ross Bender (@l3ender) |
| 158 |
''' |
| 159 |
|
| 160 |
EXAMPLES = ''' |
| 161 |
- name: Configure web app access restrictions. |
| 162 |
azure.azcollection.azure_rm_webappaccessrestriction: |
| 163 |
name: "MyWebapp" |
| 164 |
resource_group: "MyResourceGroup" |
| 165 |
ip_security_restrictions: |
| 166 |
- name: "Datacenter 1" |
| 167 |
action: "Allow" |
| 168 |
ip_address: "1.1.1.1/24" |
| 169 |
priority: 1 |
| 170 |
- name: "Datacenter 2" |
| 171 |
action: "Allow" |
| 172 |
ip_address: "2.2.2.2/24" |
| 173 |
priority: 2 |
| 174 |
- name: "Datacenter 3" |
| 175 |
action: Allow |
| 176 |
priority: 3 |
| 177 |
description: "for test 02" |
| 178 |
tag: XffProxy |
| 179 |
vnet_subnet_resource_id: "{{ subnet_output.state.id }}" |
| 180 |
scm_ip_security_restrictions_use_main: true |
| 181 |
|
| 182 |
- name: Delete web app network access restrictions. |
| 183 |
azure.azcollection.azure_rm_webappaccessrestriction: |
| 184 |
name: "MyWebapp" |
| 185 |
resource_group: "MyResourceGroup" |
| 186 |
state: "absent" |
| 187 |
''' |
| 188 |
|
| 189 |
RETURN = ''' |
| 190 |
ip_security_restrictions: |
| 191 |
description: |
| 192 |
- The web app's HTTP access restrictions. |
| 193 |
returned: always |
| 194 |
type: list |
| 195 |
elements: dict |
| 196 |
contains: |
| 197 |
name: |
| 198 |
description: |
| 199 |
- Name of the access restriction. |
| 200 |
returned: always |
| 201 |
type: str |
| 202 |
sample: my-access-restriction |
| 203 |
description: |
| 204 |
description: |
| 205 |
- Description of the access restriction. |
| 206 |
returned: always |
| 207 |
type: str |
| 208 |
sample: my-access-restriction-description |
| 209 |
action: |
| 210 |
description: |
| 211 |
- Traffic action of the access restriction. |
| 212 |
returned: always |
| 213 |
type: str |
| 214 |
sample: Allow |
| 215 |
priority: |
| 216 |
description: |
| 217 |
- Numerical priority of the access restriction. |
| 218 |
returned: always |
| 219 |
type: int |
| 220 |
sample: 1 |
| 221 |
ip_address: |
| 222 |
description: |
| 223 |
- IP address of the access restriction. |
| 224 |
returned: always |
| 225 |
type: str |
| 226 |
sample: 1.1.1.1/32 |
| 227 |
subnet_traffic_tag: |
| 228 |
description: |
| 229 |
- (internal) Subnet traffic tags. |
| 230 |
type: int |
| 231 |
returned: always |
| 232 |
sample: int |
| 233 |
vnet_traffic_tag: |
| 234 |
description: |
| 235 |
- (internal) Vnet traffic tag. |
| 236 |
type: int |
| 237 |
returned: always |
| 238 |
sample: 3 |
| 239 |
tag: |
| 240 |
description: |
| 241 |
- IP restriction rule description. |
| 242 |
type: str |
| 243 |
returned: always |
| 244 |
sample: default |
| 245 |
vnet_subnet_resource_id: |
| 246 |
description: |
| 247 |
- The Virtual network relaste subnet resource id. |
| 248 |
type: str |
| 249 |
returned: always |
| 250 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/vnet01/subnets/subnet01" |
| 251 |
scm_ip_security_restrictions: |
| 252 |
description: |
| 253 |
- The web app's SCM access restrictions. |
| 254 |
returned: always |
| 255 |
type: list |
| 256 |
elements: dict |
| 257 |
contains: |
| 258 |
name: |
| 259 |
description: |
| 260 |
- Name of the access restriction. |
| 261 |
returned: always |
| 262 |
type: str |
| 263 |
sample: my-access-restriction |
| 264 |
description: |
| 265 |
description: |
| 266 |
- Description of the access restriction. |
| 267 |
returned: always |
| 268 |
type: str |
| 269 |
sample: my-access-restriction-description |
| 270 |
action: |
| 271 |
description: |
| 272 |
- Traffic action of the access restriction. |
| 273 |
returned: always |
| 274 |
type: str |
| 275 |
sample: Allow |
| 276 |
priority: |
| 277 |
description: |
| 278 |
- Numerical priority of the access restriction. |
| 279 |
returned: always |
| 280 |
type: int |
| 281 |
sample: 1 |
| 282 |
ip_address: |
| 283 |
description: |
| 284 |
- IP address of the access restriction. |
| 285 |
returned: always |
| 286 |
type: str |
| 287 |
sample: 1.1.1.1/32 |
| 288 |
subnet_traffic_tag: |
| 289 |
description: |
| 290 |
- (internal) Subnet traffic tags. |
| 291 |
type: int |
| 292 |
returned: always |
| 293 |
sample: int |
| 294 |
vnet_traffic_tag: |
| 295 |
description: |
| 296 |
- (internal) Vnet traffic tag. |
| 297 |
type: int |
| 298 |
returned: always |
| 299 |
sample: 3 |
| 300 |
tag: |
| 301 |
description: |
| 302 |
- IP restriction rule description. |
| 303 |
type: str |
| 304 |
returned: always |
| 305 |
sample: default |
| 306 |
vnet_subnet_resource_id: |
| 307 |
description: |
| 308 |
- The Virtual network relaste subnet resource id. |
| 309 |
type: str |
| 310 |
returned: always |
| 311 |
sample: "/subscriptions/xxx-xxx/resourceGroups/testRG/providers/Microsoft.Network/virtualNetworks/vnet01/subnets/subnet01" |
| 312 |
scm_ip_security_restrictions_use_main: |
| 313 |
description: |
| 314 |
- Whether the HTTP access restrictions are used for SCM access. |
| 315 |
returned: always |
| 316 |
type: bool |
| 317 |
sample: false |
| 318 |
''' |
| 319 |
|
| 320 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 321 |
|
| 322 |
try: |
| 323 |
from azure.mgmt.web.models import IpSecurityRestriction |
| 324 |
except Exception: |
| 325 |
|
| 326 |
pass |
| 327 |
|
| 328 |
ip_restriction_spec = dict( |
| 329 |
name=dict(type='str'), |
| 330 |
description=dict(type='str'), |
| 331 |
action=dict(type='str', default='Allow', choices=['Allow', 'Deny']), |
| 332 |
priority=dict(type='int', required=True), |
| 333 |
ip_address=dict(type='str'), |
| 334 |
vnet_subnet_resource_id=dict(type='str'), |
| 335 |
vnet_traffic_tag=dict(type='int'), |
| 336 |
subnet_traffic_tag=dict(type='int'), |
| 337 |
tag=dict(type='str', choices=["Default", "XffProxy", "ServiceTag"]), |
| 338 |
) |
| 339 |
|
| 340 |
|
| 341 |
class AzureRMWebAppAccessRestriction(AzureRMModuleBaseExt): |
| 342 |
|
| 343 |
def __init__(self): |
| 344 |
|
| 345 |
self.module_arg_spec = dict( |
| 346 |
name=dict(type='str', required=True), |
| 347 |
resource_group=dict(type='str', required=True), |
| 348 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 349 |
ip_security_restrictions=dict(type='list', elements='dict', options=ip_restriction_spec), |
| 350 |
scm_ip_security_restrictions=dict(type='list', elements='dict', options=ip_restriction_spec), |
| 351 |
scm_ip_security_restrictions_use_main=dict(type='bool', default=False), |
| 352 |
) |
| 353 |
|
| 354 |
self.results = dict( |
| 355 |
changed=False, |
| 356 |
ip_security_restrictions=[], |
| 357 |
scm_ip_security_restrictions=[], |
| 358 |
scm_ip_security_restrictions_use_main=False, |
| 359 |
) |
| 360 |
|
| 361 |
self.state = None |
| 362 |
self.name = None |
| 363 |
self.resource_group = None |
| 364 |
self.ip_security_restrictions = None |
| 365 |
self.scm_ip_security_restrictions = None |
| 366 |
self.scm_ip_security_restrictions_use_main = False |
| 367 |
|
| 368 |
super(AzureRMWebAppAccessRestriction, self).__init__(self.module_arg_spec, |
| 369 |
supports_check_mode=True, |
| 370 |
supports_tags=False) |
| 371 |
|
| 372 |
def exec_module(self, **kwargs): |
| 373 |
for key in self.module_arg_spec: |
| 374 |
setattr(self, key, kwargs[key]) |
| 375 |
|
| 376 |
changed = False |
| 377 |
site_config = self.get_webapp_config() |
| 378 |
self.results.update(self.set_results(site_config)) |
| 379 |
|
| 380 |
if self.state == 'absent' and self.has_access_restrictions(site_config): |
| 381 |
changed = True |
| 382 |
if not self.check_mode: |
| 383 |
self.log('Removing all access restrictions for webapp {0}'.format(self.name)) |
| 384 |
site_config.ip_security_restrictions = [] |
| 385 |
site_config.scm_ip_security_restrictions = [] |
| 386 |
site_config.scm_ip_security_restrictions_use_main = False |
| 387 |
self.update_webapp_config(site_config) |
| 388 |
self.results['ip_security_restrictions'] = [] |
| 389 |
self.results['scm_ip_security_restrictions'] = [] |
| 390 |
self.results['scm_ip_security_restrictions_use_main'] = False |
| 391 |
elif self.state == 'present': |
| 392 |
if not self.has_access_restrictions(site_config) and (self.ip_security_restrictions or self.scm_ip_security_restrictions): |
| 393 |
self.log('Adding new access restrictions for webapp {0}'.format(self.name)) |
| 394 |
changed = True |
| 395 |
elif self.has_updates(site_config): |
| 396 |
self.log('Detected change in existing access restrictions for webapp {0}'.format(self.name)) |
| 397 |
changed = True |
| 398 |
|
| 399 |
if changed: |
| 400 |
site_config = self.get_updated_config(site_config) |
| 401 |
if not self.check_mode: |
| 402 |
self.log('Updating site config for webapp {0}'.format(self.name)) |
| 403 |
site_config = self.update_webapp_config(site_config) |
| 404 |
|
| 405 |
self.results.update(self.set_results(site_config)) |
| 406 |
|
| 407 |
self.results['changed'] = changed |
| 408 |
|
| 409 |
return self.results |
| 410 |
|
| 411 |
def get_updated_config(self, site_config): |
| 412 |
site_config.ip_security_restrictions = [] if not self.ip_security_restrictions else self.to_restriction_obj_list(self.ip_security_restrictions) |
| 413 |
site_config.scm_ip_security_restrictions = [] if not self.scm_ip_security_restrictions else ( |
| 414 |
self.to_restriction_obj_list(self.scm_ip_security_restrictions)) |
| 415 |
site_config.scm_ip_security_restrictions_use_main = self.scm_ip_security_restrictions_use_main |
| 416 |
return site_config |
| 417 |
|
| 418 |
def has_updates(self, site_config): |
| 419 |
changed = False |
| 420 |
if site_config.scm_ip_security_restrictions_use_main != self.scm_ip_security_restrictions_use_main: |
| 421 |
changed = True |
| 422 |
elif not self.default_compare({}, self.ip_security_restrictions, |
| 423 |
self.to_restriction_dict_list(site_config.ip_security_restrictions), '', dict(compare=[])): |
| 424 |
changed = True |
| 425 |
elif not self.default_compare({}, self.scm_ip_security_restrictions, |
| 426 |
self.to_restriction_dict_list(site_config.scm_ip_security_restrictions), '', dict(compare=[])): |
| 427 |
changed = True |
| 428 |
return changed |
| 429 |
|
| 430 |
def has_access_restrictions(self, site_config): |
| 431 |
return site_config.ip_security_restrictions or site_config.scm_ip_security_restrictions |
| 432 |
|
| 433 |
def get_webapp_config(self): |
| 434 |
try: |
| 435 |
return self.web_client.web_apps.get_configuration(resource_group_name=self.resource_group, name=self.name) |
| 436 |
except Exception as exc: |
| 437 |
self.fail("Error getting webapp config {0} (rg={1}) - {2}".format(self.name, self.resource_group, str(exc))) |
| 438 |
|
| 439 |
def update_webapp_config(self, param): |
| 440 |
try: |
| 441 |
return self.web_client.web_apps.create_or_update_configuration(resource_group_name=self.resource_group, name=self.name, site_config=param) |
| 442 |
except Exception as exc: |
| 443 |
self.fail("Error creating/updating webapp config {0} (rg={1}) - {2}".format(self.name, self.resource_group, str(exc))) |
| 444 |
|
| 445 |
def set_results(self, site_config): |
| 446 |
output = dict() |
| 447 |
if site_config.ip_security_restrictions: |
| 448 |
output['ip_security_restrictions'] = self.to_restriction_dict_list(site_config.ip_security_restrictions) |
| 449 |
if site_config.scm_ip_security_restrictions: |
| 450 |
output['scm_ip_security_restrictions'] = self.to_restriction_dict_list(site_config.scm_ip_security_restrictions) |
| 451 |
output['scm_ip_security_restrictions_use_main'] = site_config.scm_ip_security_restrictions_use_main |
| 452 |
return output |
| 453 |
|
| 454 |
def to_restriction_obj_list(self, restriction_dict_list): |
| 455 |
return [] if not restriction_dict_list else [self.to_restriction_obj(restriction) for restriction in restriction_dict_list] |
| 456 |
|
| 457 |
def to_restriction_obj(self, restriction_dict): |
| 458 |
return IpSecurityRestriction( |
| 459 |
name=restriction_dict['name'], |
| 460 |
description=restriction_dict['description'], |
| 461 |
action=restriction_dict['action'], |
| 462 |
priority=restriction_dict['priority'], |
| 463 |
ip_address=restriction_dict['ip_address'], |
| 464 |
vnet_subnet_resource_id=restriction_dict['vnet_subnet_resource_id'], |
| 465 |
vnet_traffic_tag=restriction_dict['vnet_traffic_tag'], |
| 466 |
subnet_traffic_tag=restriction_dict['subnet_traffic_tag'], |
| 467 |
tag=restriction_dict['tag'], |
| 468 |
) |
| 469 |
|
| 470 |
def to_restriction_dict_list(self, restriction_obj_list): |
| 471 |
restrictions = [] |
| 472 |
if restriction_obj_list: |
| 473 |
for r in restriction_obj_list: |
| 474 |
restriction = self.to_restriction_dict(r) |
| 475 |
if not self.is_azure_default_restriction(restriction): |
| 476 |
restrictions.append(restriction) |
| 477 |
|
| 478 |
return restrictions |
| 479 |
|
| 480 |
def is_azure_default_restriction(self, restriction_obj): |
| 481 |
return (restriction_obj["action"] == "Allow" and restriction_obj["ip_address"] == "Any" and restriction_obj["priority"] == 1) or \ |
| 482 |
(restriction_obj["action"] == "Deny" and restriction_obj["ip_address"] == "Any" and restriction_obj["priority"] == 2147483647) |
| 483 |
|
| 484 |
def to_restriction_dict(self, restriction_obj): |
| 485 |
return dict( |
| 486 |
name=restriction_obj.name, |
| 487 |
description=restriction_obj.description, |
| 488 |
action=restriction_obj.action, |
| 489 |
priority=restriction_obj.priority, |
| 490 |
ip_address=restriction_obj.ip_address, |
| 491 |
vnet_subnet_resource_id=restriction_obj.vnet_subnet_resource_id, |
| 492 |
vnet_traffic_tag=restriction_obj.vnet_traffic_tag, |
| 493 |
subnet_traffic_tag=restriction_obj.subnet_traffic_tag, |
| 494 |
tag=restriction_obj.tag, |
| 495 |
) |
| 496 |
|
| 497 |
|
| 498 |
def main(): |
| 499 |
AzureRMWebAppAccessRestriction() |
| 500 |
|
| 501 |
|
| 502 |
if __name__ == '__main__': |
| 503 |
main() |