| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_securitygroup |
| 15 |
version_added: "0.1.0" |
| 16 |
short_description: Manage Azure network security groups |
| 17 |
description: |
| 18 |
- Create, update or delete a network security group. |
| 19 |
- A security group contains Access Control List (ACL) rules that allow or deny network traffic to subnets or individual network interfaces. |
| 20 |
- A security group is created with a set of default security rules and an empty set of security rules. |
| 21 |
- Shape traffic flow by adding rules to the empty set of security rules. |
| 22 |
|
| 23 |
options: |
| 24 |
default_rules: |
| 25 |
description: |
| 26 |
- The set of default rules automatically added to a security group at creation. |
| 27 |
- In general default rules will not be modified. Modify rules to shape the flow of traffic to or from a subnet or NIC. |
| 28 |
- See rules below for the makeup of a rule dict. |
| 29 |
type: list |
| 30 |
elements: dict |
| 31 |
suboptions: |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Unique name for the rule. |
| 35 |
type: str |
| 36 |
required: true |
| 37 |
description: |
| 38 |
description: |
| 39 |
- Short description of the rule's purpose. |
| 40 |
type: str |
| 41 |
protocol: |
| 42 |
description: |
| 43 |
- Accepted traffic protocol. |
| 44 |
type: str |
| 45 |
choices: |
| 46 |
- Udp |
| 47 |
- UDP |
| 48 |
- Tcp |
| 49 |
- TCP |
| 50 |
- Icmp |
| 51 |
- ICMP |
| 52 |
- "*" |
| 53 |
default: "*" |
| 54 |
source_port_range: |
| 55 |
description: |
| 56 |
- Port or range of ports from which traffic originates. |
| 57 |
- It can accept string type or a list of string type. |
| 58 |
default: "*" |
| 59 |
type: raw |
| 60 |
destination_port_range: |
| 61 |
description: |
| 62 |
- Port or range of ports to which traffic is headed. |
| 63 |
- It can accept string type or a list of string type. |
| 64 |
default: "*" |
| 65 |
type: raw |
| 66 |
source_address_prefix: |
| 67 |
description: |
| 68 |
- The CIDR or source IP range. |
| 69 |
- Asterisk C(*) can also be used to match all source IPs. |
| 70 |
- Default tags such as C(VirtualNetwork), C(AzureLoadBalancer) and C(Internet) can also be used. |
| 71 |
- If this is an ingress rule, specifies where network traffic originates from. |
| 72 |
- It can accept string type or a list of string type. |
| 73 |
- Asterisk C(*) and default tags can only be specified as single string type, not as a list of string. |
| 74 |
type: raw |
| 75 |
default: "*" |
| 76 |
destination_address_prefix: |
| 77 |
description: |
| 78 |
- The destination address prefix. |
| 79 |
- CIDR or destination IP range. |
| 80 |
- Asterisk C(*) can also be used to match all source IPs. |
| 81 |
- Default tags such as C(VirtualNetwork), C(AzureLoadBalancer) and C(Internet) can also be used. |
| 82 |
- It can accept string type or a list of string type. |
| 83 |
- Asterisk C(*) and default tags can only be specified as single string type, not as a list of string. |
| 84 |
default: "*" |
| 85 |
type: raw |
| 86 |
source_application_security_groups: |
| 87 |
description: |
| 88 |
- List of the source application security groups. |
| 89 |
- It could be list of resource id. |
| 90 |
- It could be list of names in same resource group. |
| 91 |
- It could be list of dict containing resource_group and name. |
| 92 |
- It is mutually exclusive with C(source_address_prefix) and C(source_address_prefixes). |
| 93 |
type: list |
| 94 |
elements: raw |
| 95 |
destination_application_security_groups: |
| 96 |
description: |
| 97 |
- List of the destination application security groups. |
| 98 |
- It could be list of resource id. |
| 99 |
- It could be list of names in same resource group. |
| 100 |
- It could be list of dict containing I(resource_group) and I(name). |
| 101 |
- It is mutually exclusive with C(destination_address_prefix) and C(destination_address_prefixes). |
| 102 |
type: list |
| 103 |
elements: raw |
| 104 |
access: |
| 105 |
description: |
| 106 |
- Whether or not to allow the traffic flow. |
| 107 |
type: str |
| 108 |
choices: |
| 109 |
- Allow |
| 110 |
- Deny |
| 111 |
default: Allow |
| 112 |
priority: |
| 113 |
description: |
| 114 |
- Order in which to apply the rule. Must a unique integer between 100 and 4096 inclusive. |
| 115 |
type: int |
| 116 |
required: true |
| 117 |
direction: |
| 118 |
description: |
| 119 |
- Indicates the direction of the traffic flow. |
| 120 |
type: str |
| 121 |
choices: |
| 122 |
- Inbound |
| 123 |
- Outbound |
| 124 |
default: Inbound |
| 125 |
location: |
| 126 |
description: |
| 127 |
- Valid azure location. Defaults to location of the resource group. |
| 128 |
type: str |
| 129 |
name: |
| 130 |
description: |
| 131 |
- Name of the security group to operate on. |
| 132 |
type: str |
| 133 |
required: true |
| 134 |
purge_default_rules: |
| 135 |
description: |
| 136 |
- Remove any existing rules not matching those defined in the default_rules parameter. |
| 137 |
type: bool |
| 138 |
default: false |
| 139 |
purge_rules: |
| 140 |
description: |
| 141 |
- Remove any existing rules not matching those defined in the rules parameters. |
| 142 |
type: bool |
| 143 |
default: false |
| 144 |
resource_group: |
| 145 |
description: |
| 146 |
- Name of the resource group the security group belongs to. |
| 147 |
required: true |
| 148 |
type: str |
| 149 |
rules: |
| 150 |
description: |
| 151 |
- Set of rules shaping traffic flow to or from a subnet or NIC. Each rule is a dictionary. |
| 152 |
type: list |
| 153 |
elements: dict |
| 154 |
suboptions: |
| 155 |
name: |
| 156 |
description: |
| 157 |
- Unique name for the rule. |
| 158 |
type: str |
| 159 |
required: true |
| 160 |
description: |
| 161 |
description: |
| 162 |
- Short description of the rule's purpose. |
| 163 |
type: str |
| 164 |
protocol: |
| 165 |
description: |
| 166 |
- Accepted traffic protocol. |
| 167 |
type: str |
| 168 |
choices: |
| 169 |
- Udp |
| 170 |
- UDP |
| 171 |
- Tcp |
| 172 |
- TCP |
| 173 |
- Icmp |
| 174 |
- ICMP |
| 175 |
- "*" |
| 176 |
default: "*" |
| 177 |
source_port_range: |
| 178 |
description: |
| 179 |
- Port or range of ports from which traffic originates. |
| 180 |
- It can accept string type or a list of string type. |
| 181 |
default: "*" |
| 182 |
type: raw |
| 183 |
destination_port_range: |
| 184 |
description: |
| 185 |
- Port or range of ports to which traffic is headed. |
| 186 |
- It can accept string type or a list of string type. |
| 187 |
default: "*" |
| 188 |
type: raw |
| 189 |
source_address_prefix: |
| 190 |
description: |
| 191 |
- The CIDR or source IP range. |
| 192 |
- Asterisk C(*) can also be used to match all source IPs. |
| 193 |
- Default tags such as C(VirtualNetwork), C(AzureLoadBalancer) and C(Internet) can also be used. |
| 194 |
- If this is an ingress rule, specifies where network traffic originates from. |
| 195 |
- It can accept string type or a list of string type. |
| 196 |
- Asterisk C(*) and default tags can only be specified as single string type, not as a list of string. |
| 197 |
type: raw |
| 198 |
default: "*" |
| 199 |
destination_address_prefix: |
| 200 |
description: |
| 201 |
- The destination address prefix. |
| 202 |
- CIDR or destination IP range. |
| 203 |
- Asterisk C(*) can also be used to match all source IPs. |
| 204 |
- Default tags such as C(VirtualNetwork), C(AzureLoadBalancer) and C(Internet) can also be used. |
| 205 |
- It can accept string type or a list of string type. |
| 206 |
- Asterisk C(*) and default tags can only be specified as single string type, not as a list of string. |
| 207 |
default: "*" |
| 208 |
type: raw |
| 209 |
source_application_security_groups: |
| 210 |
description: |
| 211 |
- List of the source application security groups. |
| 212 |
- It could be list of resource id. |
| 213 |
- It could be list of names in same resource group. |
| 214 |
- It could be list of dict containing resource_group and name. |
| 215 |
- It is mutually exclusive with C(source_address_prefix) and C(source_address_prefixes). |
| 216 |
type: list |
| 217 |
elements: raw |
| 218 |
destination_application_security_groups: |
| 219 |
description: |
| 220 |
- List of the destination application security groups. |
| 221 |
- It could be list of resource id. |
| 222 |
- It could be list of names in same resource group. |
| 223 |
- It could be list of dict containing I(resource_group) and I(name). |
| 224 |
- It is mutually exclusive with C(destination_address_prefix) and C(destination_address_prefixes). |
| 225 |
type: list |
| 226 |
elements: raw |
| 227 |
access: |
| 228 |
description: |
| 229 |
- Whether or not to allow the traffic flow. |
| 230 |
type: str |
| 231 |
choices: |
| 232 |
- Allow |
| 233 |
- Deny |
| 234 |
default: Allow |
| 235 |
priority: |
| 236 |
description: |
| 237 |
- Order in which to apply the rule. Must a unique integer between 100 and 4096 inclusive. |
| 238 |
type: int |
| 239 |
required: true |
| 240 |
direction: |
| 241 |
description: |
| 242 |
- Indicates the direction of the traffic flow. |
| 243 |
type: str |
| 244 |
choices: |
| 245 |
- Inbound |
| 246 |
- Outbound |
| 247 |
default: Inbound |
| 248 |
state: |
| 249 |
description: |
| 250 |
- Assert the state of the security group. Set to C(present) to create or update a security group. Set to C(absent) to remove a security group. |
| 251 |
default: present |
| 252 |
type: str |
| 253 |
choices: |
| 254 |
- absent |
| 255 |
- present |
| 256 |
|
| 257 |
extends_documentation_fragment: |
| 258 |
- azure.azcollection.azure |
| 259 |
- azure.azcollection.azure_tags |
| 260 |
|
| 261 |
author: |
| 262 |
- Chris Houseknecht (@chouseknecht) |
| 263 |
- Matt Davis (@nitzmahone) |
| 264 |
|
| 265 |
''' |
| 266 |
|
| 267 |
EXAMPLES = ''' |
| 268 |
- name: Create a security group |
| 269 |
azure_rm_securitygroup: |
| 270 |
resource_group: myResourceGroup |
| 271 |
name: mysecgroup |
| 272 |
purge_rules: true |
| 273 |
rules: |
| 274 |
- name: DenySSH |
| 275 |
protocol: Tcp |
| 276 |
destination_port_range: 22 |
| 277 |
access: Deny |
| 278 |
priority: 100 |
| 279 |
direction: Inbound |
| 280 |
- name: 'AllowSSH' |
| 281 |
protocol: Tcp |
| 282 |
source_address_prefix: |
| 283 |
- '174.109.158.0/24' |
| 284 |
- '174.109.159.0/24' |
| 285 |
destination_port_range: 22 |
| 286 |
access: Allow |
| 287 |
priority: 101 |
| 288 |
direction: Inbound |
| 289 |
- name: 'AllowMultiplePorts' |
| 290 |
protocol: Tcp |
| 291 |
source_address_prefix: |
| 292 |
- '174.109.158.0/24' |
| 293 |
- '174.109.159.0/24' |
| 294 |
destination_port_range: |
| 295 |
- 80 |
| 296 |
- 443 |
| 297 |
access: Allow |
| 298 |
priority: 102 |
| 299 |
|
| 300 |
- name: Update rules on existing security group |
| 301 |
azure_rm_securitygroup: |
| 302 |
resource_group: myResourceGroup |
| 303 |
name: mysecgroup |
| 304 |
rules: |
| 305 |
- name: DenySSH |
| 306 |
protocol: Tcp |
| 307 |
destination_port_range: 22-23 |
| 308 |
access: Deny |
| 309 |
priority: 100 |
| 310 |
direction: Inbound |
| 311 |
- name: AllowSSHFromHome |
| 312 |
protocol: Tcp |
| 313 |
source_address_prefix: '174.109.158.0/24' |
| 314 |
destination_port_range: 22-23 |
| 315 |
access: Allow |
| 316 |
priority: 102 |
| 317 |
direction: Inbound |
| 318 |
tags: |
| 319 |
testing: testing |
| 320 |
delete: on-exit |
| 321 |
|
| 322 |
- name: Create a securiy group with I(protocol=Icmp) |
| 323 |
azure_rm_securitygroup: |
| 324 |
name: mysecgroup |
| 325 |
resource_group: myResourceGroup |
| 326 |
rules: |
| 327 |
- name: SSH |
| 328 |
protocol: Tcp |
| 329 |
destination_port_range: 22 |
| 330 |
access: Allow |
| 331 |
priority: 105 |
| 332 |
direction: Inbound |
| 333 |
- name: ICMP |
| 334 |
protocol: Icmp |
| 335 |
priority: 106 |
| 336 |
|
| 337 |
- name: Delete security group |
| 338 |
azure_rm_securitygroup: |
| 339 |
resource_group: myResourceGroup |
| 340 |
name: mysecgroup |
| 341 |
state: absent |
| 342 |
''' |
| 343 |
|
| 344 |
RETURN = ''' |
| 345 |
state: |
| 346 |
description: |
| 347 |
- Current state of the security group. |
| 348 |
returned: always |
| 349 |
type: complex |
| 350 |
contains: |
| 351 |
default_rules: |
| 352 |
description: |
| 353 |
- The default security rules of network security group. |
| 354 |
returned: always |
| 355 |
type: list |
| 356 |
sample: [ |
| 357 |
{ |
| 358 |
"access": "Allow", |
| 359 |
"description": "Allow inbound traffic from all VMs in VNET", |
| 360 |
"destination_address_prefix": "VirtualNetwork", |
| 361 |
"destination_port_range": "*", |
| 362 |
"direction": "Inbound", |
| 363 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 364 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetInBound", |
| 365 |
"name": "AllowVnetInBound", |
| 366 |
"priority": 65000, |
| 367 |
"protocol": "*", |
| 368 |
"provisioning_state": "Succeeded", |
| 369 |
"source_address_prefix": "VirtualNetwork", |
| 370 |
"source_port_range": "*" |
| 371 |
}, |
| 372 |
{ |
| 373 |
"access": "Allow", |
| 374 |
"description": "Allow inbound traffic from azure load balancer", |
| 375 |
"destination_address_prefix": "*", |
| 376 |
"destination_port_range": "*", |
| 377 |
"direction": "Inbound", |
| 378 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 379 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowAzureLoadBalancerInBound", |
| 380 |
"name": "AllowAzureLoadBalancerInBound", |
| 381 |
"priority": 65001, |
| 382 |
"protocol": "*", |
| 383 |
"provisioning_state": "Succeeded", |
| 384 |
"source_address_prefix": "AzureLoadBalancer", |
| 385 |
"source_port_range": "*" |
| 386 |
}, |
| 387 |
{ |
| 388 |
"access": "Deny", |
| 389 |
"description": "Deny all inbound traffic", |
| 390 |
"destination_address_prefix": "*", |
| 391 |
"destination_port_range": "*", |
| 392 |
"direction": "Inbound", |
| 393 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 394 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllInBound", |
| 395 |
"name": "DenyAllInBound", |
| 396 |
"priority": 65500, |
| 397 |
"protocol": "*", |
| 398 |
"provisioning_state": "Succeeded", |
| 399 |
"source_address_prefix": "*", |
| 400 |
"source_port_range": "*" |
| 401 |
}, |
| 402 |
{ |
| 403 |
"access": "Allow", |
| 404 |
"description": "Allow outbound traffic from all VMs to all VMs in VNET", |
| 405 |
"destination_address_prefix": "VirtualNetwork", |
| 406 |
"destination_port_range": "*", |
| 407 |
"direction": "Outbound", |
| 408 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 409 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetOutBound", |
| 410 |
"name": "AllowVnetOutBound", |
| 411 |
"priority": 65000, |
| 412 |
"protocol": "*", |
| 413 |
"provisioning_state": "Succeeded", |
| 414 |
"source_address_prefix": "VirtualNetwork", |
| 415 |
"source_port_range": "*" |
| 416 |
}, |
| 417 |
{ |
| 418 |
"access": "Allow", |
| 419 |
"description": "Allow outbound traffic from all VMs to Internet", |
| 420 |
"destination_address_prefix": "Internet", |
| 421 |
"destination_port_range": "*", |
| 422 |
"direction": "Outbound", |
| 423 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 424 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowInternetOutBound", |
| 425 |
"name": "AllowInternetOutBound", |
| 426 |
"priority": 65001, |
| 427 |
"protocol": "*", |
| 428 |
"provisioning_state": "Succeeded", |
| 429 |
"source_address_prefix": "*", |
| 430 |
"source_port_range": "*" |
| 431 |
}, |
| 432 |
{ |
| 433 |
"access": "Deny", |
| 434 |
"description": "Deny all outbound traffic", |
| 435 |
"destination_address_prefix": "*", |
| 436 |
"destination_port_range": "*", |
| 437 |
"direction": "Outbound", |
| 438 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 439 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllOutBound", |
| 440 |
"name": "DenyAllOutBound", |
| 441 |
"priority": 65500, |
| 442 |
"protocol": "*", |
| 443 |
"provisioning_state": "Succeeded", |
| 444 |
"source_address_prefix": "*", |
| 445 |
"source_port_range": "*" |
| 446 |
} |
| 447 |
] |
| 448 |
id: |
| 449 |
description: |
| 450 |
- The resource ID. |
| 451 |
returned: always |
| 452 |
type: str |
| 453 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup" |
| 454 |
location: |
| 455 |
description: |
| 456 |
- The resource location. |
| 457 |
returned: always |
| 458 |
type: str |
| 459 |
sample: "westus" |
| 460 |
name: |
| 461 |
description: |
| 462 |
- Name of the secrurity group. |
| 463 |
returned: always |
| 464 |
type: str |
| 465 |
sample: "mysecgroup" |
| 466 |
network_interfaces: |
| 467 |
description: |
| 468 |
- A collection of references to network interfaces. |
| 469 |
returned: always |
| 470 |
type: list |
| 471 |
sample: [] |
| 472 |
rules: |
| 473 |
description: |
| 474 |
- A collection of security rules of the network security group. |
| 475 |
returned: always |
| 476 |
type: list |
| 477 |
sample: [ |
| 478 |
{ |
| 479 |
"access": "Deny", |
| 480 |
"description": null, |
| 481 |
"destination_address_prefix": "*", |
| 482 |
"destination_port_range": "22", |
| 483 |
"direction": "Inbound", |
| 484 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 485 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/DenySSH", |
| 486 |
"name": "DenySSH", |
| 487 |
"priority": 100, |
| 488 |
"protocol": "Tcp", |
| 489 |
"provisioning_state": "Succeeded", |
| 490 |
"source_address_prefix": "*", |
| 491 |
"source_port_range": "*" |
| 492 |
}, |
| 493 |
{ |
| 494 |
"access": "Allow", |
| 495 |
"description": null, |
| 496 |
"destination_address_prefix": "*", |
| 497 |
"destination_port_range": "22", |
| 498 |
"direction": "Inbound", |
| 499 |
"etag": 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', |
| 500 |
"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/AllowSSH", |
| 501 |
"name": "AllowSSH", |
| 502 |
"priority": 101, |
| 503 |
"protocol": "Tcp", |
| 504 |
"provisioning_state": "Succeeded", |
| 505 |
"source_address_prefix": "174.109.158.0/24", |
| 506 |
"source_port_range": "*" |
| 507 |
} |
| 508 |
] |
| 509 |
subnets: |
| 510 |
description: |
| 511 |
- A collection of references to subnets. |
| 512 |
returned: always |
| 513 |
type: list |
| 514 |
sample: [] |
| 515 |
tags: |
| 516 |
description: |
| 517 |
- Tags to assign to the security group. |
| 518 |
returned: always |
| 519 |
type: dict |
| 520 |
sample: { |
| 521 |
"delete": "on-exit", |
| 522 |
"foo": "bar", |
| 523 |
"testing": "testing" |
| 524 |
} |
| 525 |
type: |
| 526 |
description: |
| 527 |
- The resource type. |
| 528 |
returned: always |
| 529 |
type: str |
| 530 |
sample: "Microsoft.Network/networkSecurityGroups" |
| 531 |
''' |
| 532 |
|
| 533 |
try: |
| 534 |
from azure.core.exceptions import ResourceNotFoundError |
| 535 |
from azure.mgmt.core.tools import is_valid_resource_id |
| 536 |
except ImportError: |
| 537 |
|
| 538 |
pass |
| 539 |
|
| 540 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 541 |
from ansible.module_utils._text import to_native |
| 542 |
|
| 543 |
|
| 544 |
def validate_rule(self, rule, rule_type=None): |
| 545 |
''' |
| 546 |
Apply defaults to a rule dictionary and check that all values are valid. |
| 547 |
|
| 548 |
:param rule: rule dict |
| 549 |
:param rule_type: Set to 'default' if the rule is part of the default set of rules. |
| 550 |
:return: None |
| 551 |
''' |
| 552 |
priority = rule.get('priority', 0) |
| 553 |
if rule_type != 'default' and (priority < 100 or priority > 4096): |
| 554 |
raise Exception("Rule priority must be between 100 and 4096") |
| 555 |
|
| 556 |
def check_plural(src, dest): |
| 557 |
if isinstance(rule.get(src), list): |
| 558 |
rule[dest] = rule[src] |
| 559 |
rule[src] = None |
| 560 |
|
| 561 |
check_plural('destination_address_prefix', 'destination_address_prefixes') |
| 562 |
check_plural('source_address_prefix', 'source_address_prefixes') |
| 563 |
check_plural('source_port_range', 'source_port_ranges') |
| 564 |
check_plural('destination_port_range', 'destination_port_ranges') |
| 565 |
|
| 566 |
|
| 567 |
if rule.get('source_application_security_groups') and rule.get('source_address_prefix') == '*': |
| 568 |
rule['source_address_prefix'] = None |
| 569 |
if rule.get('destination_application_security_groups') and rule.get('destination_address_prefix') == '*': |
| 570 |
rule['destination_address_prefix'] = None |
| 571 |
|
| 572 |
|
| 573 |
def compare_rules_change(old_list, new_list, purge_list): |
| 574 |
old_list = old_list or [] |
| 575 |
new_list = new_list or [] |
| 576 |
changed = False |
| 577 |
|
| 578 |
for old_rule in old_list: |
| 579 |
matched = next((x for x in new_list if x['name'].lower() == old_rule['name'].lower()), []) |
| 580 |
if matched: |
| 581 |
changed = changed or compare_rules(old_rule, matched) |
| 582 |
elif not purge_list: |
| 583 |
new_list.append(old_rule) |
| 584 |
else: |
| 585 |
changed = True |
| 586 |
|
| 587 |
if not changed: |
| 588 |
new_names = [to_native(x['name'].lower()) for x in new_list] |
| 589 |
old_names = [to_native(x['name'].lower()) for x in old_list] |
| 590 |
changed = (set(new_names) != set(old_names)) |
| 591 |
return changed, new_list |
| 592 |
|
| 593 |
|
| 594 |
def compare_rules(old_rule, rule): |
| 595 |
def compare_list_rule(old_rule, rule, key): |
| 596 |
return set(map(lambda x: str(x).lower(), rule.get(key) or [])) != set(map(lambda x: str(x).lower(), old_rule.get(key) or [])) |
| 597 |
changed = False |
| 598 |
if old_rule['name'].lower() != rule['name'].lower(): |
| 599 |
changed = True |
| 600 |
if rule.get('description', None) != old_rule['description']: |
| 601 |
changed = True |
| 602 |
if rule['protocol'].lower() != old_rule['protocol'].lower(): |
| 603 |
changed = True |
| 604 |
if str(rule['source_port_range']) != str(old_rule['source_port_range']): |
| 605 |
changed = True |
| 606 |
if str(rule['destination_port_range']) != str(old_rule['destination_port_range']): |
| 607 |
changed = True |
| 608 |
if rule['access'] != old_rule['access']: |
| 609 |
changed = True |
| 610 |
if rule['priority'] != old_rule['priority']: |
| 611 |
changed = True |
| 612 |
if rule['direction'] != old_rule['direction']: |
| 613 |
changed = True |
| 614 |
if str(rule['source_address_prefix']) != str(old_rule['source_address_prefix']): |
| 615 |
changed = True |
| 616 |
if str(rule['destination_address_prefix']) != str(old_rule['destination_address_prefix']): |
| 617 |
changed = True |
| 618 |
if compare_list_rule(old_rule, rule, 'source_address_prefixes'): |
| 619 |
changed = True |
| 620 |
if compare_list_rule(old_rule, rule, 'destination_address_prefixes'): |
| 621 |
changed = True |
| 622 |
if compare_list_rule(old_rule, rule, 'source_port_ranges'): |
| 623 |
changed = True |
| 624 |
if compare_list_rule(old_rule, rule, 'destination_port_ranges'): |
| 625 |
changed = True |
| 626 |
if compare_list_rule(old_rule, rule, 'source_application_security_groups'): |
| 627 |
changed = True |
| 628 |
if compare_list_rule(old_rule, rule, 'destination_application_security_groups'): |
| 629 |
changed = True |
| 630 |
return changed |
| 631 |
|
| 632 |
|
| 633 |
def create_rule_instance(self, rule): |
| 634 |
''' |
| 635 |
Create an instance of SecurityRule from a dict. |
| 636 |
|
| 637 |
:param rule: dict |
| 638 |
:return: SecurityRule |
| 639 |
''' |
| 640 |
return self.nsg_models.SecurityRule( |
| 641 |
description=rule.get('description', None), |
| 642 |
protocol=rule.get('protocol', None), |
| 643 |
source_port_range=rule.get('source_port_range', None), |
| 644 |
destination_port_range=rule.get('destination_port_range', None), |
| 645 |
source_address_prefix=rule.get('source_address_prefix', None), |
| 646 |
source_address_prefixes=rule.get('source_address_prefixes', None), |
| 647 |
destination_address_prefix=rule.get('destination_address_prefix', None), |
| 648 |
destination_address_prefixes=rule.get('destination_address_prefixes', None), |
| 649 |
source_port_ranges=rule.get('source_port_ranges', None), |
| 650 |
destination_port_ranges=rule.get('destination_port_ranges', None), |
| 651 |
source_application_security_groups=[ |
| 652 |
self.nsg_models.ApplicationSecurityGroup(id=p) |
| 653 |
for p in rule.get('source_application_security_groups')] if rule.get('source_application_security_groups') else None, |
| 654 |
destination_application_security_groups=[ |
| 655 |
self.nsg_models.ApplicationSecurityGroup(id=p) |
| 656 |
for p in rule.get('destination_application_security_groups')] if rule.get('destination_application_security_groups') else None, |
| 657 |
access=rule.get('access', None), |
| 658 |
priority=rule.get('priority', None), |
| 659 |
direction=rule.get('direction', None), |
| 660 |
provisioning_state=rule.get('provisioning_state', None), |
| 661 |
name=rule.get('name', None), |
| 662 |
etag=rule.get('etag', None) |
| 663 |
) |
| 664 |
|
| 665 |
|
| 666 |
def create_rule_dict_from_obj(rule): |
| 667 |
''' |
| 668 |
Create a dict from an instance of a SecurityRule. |
| 669 |
|
| 670 |
:param rule: SecurityRule |
| 671 |
:return: dict |
| 672 |
''' |
| 673 |
return dict( |
| 674 |
id=rule.id, |
| 675 |
name=rule.name, |
| 676 |
description=rule.description, |
| 677 |
protocol=rule.protocol, |
| 678 |
source_port_range=rule.source_port_range, |
| 679 |
destination_port_range=rule.destination_port_range, |
| 680 |
source_address_prefix=rule.source_address_prefix, |
| 681 |
destination_address_prefix=rule.destination_address_prefix, |
| 682 |
source_port_ranges=rule.source_port_ranges, |
| 683 |
destination_port_ranges=rule.destination_port_ranges, |
| 684 |
source_address_prefixes=rule.source_address_prefixes, |
| 685 |
destination_address_prefixes=rule.destination_address_prefixes, |
| 686 |
source_application_security_groups=[p.id for p in rule.source_application_security_groups] if rule.source_application_security_groups else None, |
| 687 |
destination_application_security_groups=[ |
| 688 |
p.id for p in rule.destination_application_security_groups] if rule.destination_application_security_groups else None, |
| 689 |
access=rule.access, |
| 690 |
priority=rule.priority, |
| 691 |
direction=rule.direction, |
| 692 |
provisioning_state=rule.provisioning_state, |
| 693 |
etag=rule.etag |
| 694 |
) |
| 695 |
|
| 696 |
|
| 697 |
def create_network_security_group_dict(nsg): |
| 698 |
results = dict( |
| 699 |
id=nsg.id, |
| 700 |
name=nsg.name, |
| 701 |
type=nsg.type, |
| 702 |
location=nsg.location, |
| 703 |
tags=nsg.tags, |
| 704 |
) |
| 705 |
results['rules'] = [] |
| 706 |
if nsg.security_rules: |
| 707 |
for rule in nsg.security_rules: |
| 708 |
results['rules'].append(create_rule_dict_from_obj(rule)) |
| 709 |
|
| 710 |
results['default_rules'] = [] |
| 711 |
if nsg.default_security_rules: |
| 712 |
for rule in nsg.default_security_rules: |
| 713 |
results['default_rules'].append(create_rule_dict_from_obj(rule)) |
| 714 |
|
| 715 |
results['network_interfaces'] = [] |
| 716 |
if nsg.network_interfaces: |
| 717 |
for interface in nsg.network_interfaces: |
| 718 |
results['network_interfaces'].append(interface.id) |
| 719 |
|
| 720 |
results['subnets'] = [] |
| 721 |
if nsg.subnets: |
| 722 |
for subnet in nsg.subnets: |
| 723 |
results['subnets'].append(subnet.id) |
| 724 |
|
| 725 |
return results |
| 726 |
|
| 727 |
|
| 728 |
rule_spec = dict( |
| 729 |
name=dict(type='str', required=True), |
| 730 |
description=dict(type='str'), |
| 731 |
protocol=dict(type='str', choices=['Udp', 'UDP', 'Tcp', 'TCP', 'Icmp', 'ICMP', '*'], default='*'), |
| 732 |
source_port_range=dict(type='raw', default='*'), |
| 733 |
destination_port_range=dict(type='raw', default='*'), |
| 734 |
source_address_prefix=dict(type='raw', default='*'), |
| 735 |
destination_address_prefix=dict(type='raw', default='*'), |
| 736 |
source_application_security_groups=dict(type='list', elements='raw'), |
| 737 |
destination_application_security_groups=dict(type='list', elements='raw'), |
| 738 |
access=dict(type='str', choices=['Allow', 'Deny'], default='Allow'), |
| 739 |
priority=dict(type='int', required=True), |
| 740 |
direction=dict(type='str', choices=['Inbound', 'Outbound'], default='Inbound') |
| 741 |
) |
| 742 |
|
| 743 |
|
| 744 |
class AzureRMSecurityGroup(AzureRMModuleBase): |
| 745 |
|
| 746 |
def __init__(self): |
| 747 |
|
| 748 |
self.module_arg_spec = dict( |
| 749 |
default_rules=dict( |
| 750 |
type='list', |
| 751 |
elements='dict', |
| 752 |
options=rule_spec, |
| 753 |
mutually_exclusive=[("source_application_security_groups", "source_address_prefix"), |
| 754 |
("destination_application_security_groups", "destination_address_prefix")] |
| 755 |
), |
| 756 |
location=dict(type='str'), |
| 757 |
name=dict(type='str', required=True), |
| 758 |
purge_default_rules=dict(type='bool', default=False), |
| 759 |
purge_rules=dict(type='bool', default=False), |
| 760 |
resource_group=dict(required=True, type='str'), |
| 761 |
rules=dict( |
| 762 |
type='list', |
| 763 |
elements='dict', |
| 764 |
options=rule_spec, |
| 765 |
mutually_exclusive=[("source_application_security_groups", "source_address_prefix"), |
| 766 |
("destination_application_security_groups", "destination_address_prefix")] |
| 767 |
), |
| 768 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 769 |
) |
| 770 |
|
| 771 |
self.default_rules = None |
| 772 |
self.location = None |
| 773 |
self.name = None |
| 774 |
self.purge_default_rules = None |
| 775 |
self.purge_rules = None |
| 776 |
self.resource_group = None |
| 777 |
self.rules = None |
| 778 |
self.state = None |
| 779 |
self.tags = None |
| 780 |
self.nsg_models = None |
| 781 |
|
| 782 |
self.results = dict( |
| 783 |
changed=False, |
| 784 |
state=dict() |
| 785 |
) |
| 786 |
|
| 787 |
super(AzureRMSecurityGroup, self).__init__(self.module_arg_spec, |
| 788 |
supports_check_mode=True) |
| 789 |
|
| 790 |
def exec_module(self, **kwargs): |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
self.nsg_models = self.network_client.network_security_groups.models |
| 795 |
|
| 796 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 797 |
setattr(self, key, kwargs[key]) |
| 798 |
|
| 799 |
changed = False |
| 800 |
results = dict() |
| 801 |
|
| 802 |
resource_group = self.get_resource_group(self.resource_group) |
| 803 |
if not self.location: |
| 804 |
|
| 805 |
self.location = resource_group.location |
| 806 |
|
| 807 |
if self.rules: |
| 808 |
for rule in self.rules: |
| 809 |
try: |
| 810 |
validate_rule(self, rule) |
| 811 |
except Exception as exc: |
| 812 |
self.fail("Error validating rule {0} - {1}".format(rule, str(exc))) |
| 813 |
self.convert_asg_to_id(rule) |
| 814 |
|
| 815 |
if self.default_rules: |
| 816 |
for rule in self.default_rules: |
| 817 |
try: |
| 818 |
validate_rule(self, rule, 'default') |
| 819 |
except Exception as exc: |
| 820 |
self.fail("Error validating default rule {0} - {1}".format(rule, str(exc))) |
| 821 |
self.convert_asg_to_id(rule) |
| 822 |
|
| 823 |
try: |
| 824 |
nsg = self.network_client.network_security_groups.get(self.resource_group, self.name) |
| 825 |
results = create_network_security_group_dict(nsg) |
| 826 |
self.log("Found security group:") |
| 827 |
self.log(results, pretty_print=True) |
| 828 |
self.check_provisioning_state(nsg, self.state) |
| 829 |
if self.state == 'present': |
| 830 |
pass |
| 831 |
elif self.state == 'absent': |
| 832 |
self.log("CHANGED: security group found but state is 'absent'") |
| 833 |
changed = True |
| 834 |
except ResourceNotFoundError: |
| 835 |
if self.state == 'present': |
| 836 |
self.log("CHANGED: security group not found and state is 'present'") |
| 837 |
changed = True |
| 838 |
|
| 839 |
if self.state == 'present' and not changed: |
| 840 |
|
| 841 |
self.log("Update security group {0}".format(self.name)) |
| 842 |
|
| 843 |
update_tags, results['tags'] = self.update_tags(results['tags']) |
| 844 |
if update_tags: |
| 845 |
changed = True |
| 846 |
|
| 847 |
rule_changed, new_rule = compare_rules_change(results['rules'], self.rules, self.purge_rules) |
| 848 |
if rule_changed: |
| 849 |
changed = True |
| 850 |
results['rules'] = new_rule |
| 851 |
rule_changed, new_rule = compare_rules_change(results['default_rules'], self.default_rules, self.purge_default_rules) |
| 852 |
if rule_changed: |
| 853 |
changed = True |
| 854 |
results['default_rules'] = new_rule |
| 855 |
|
| 856 |
self.results['changed'] = changed |
| 857 |
self.results['state'] = results |
| 858 |
if not self.check_mode and changed: |
| 859 |
self.results['state'] = self.create_or_update(results) |
| 860 |
|
| 861 |
elif self.state == 'present' and changed: |
| 862 |
|
| 863 |
self.log("Create security group {0}".format(self.name)) |
| 864 |
|
| 865 |
if not self.location: |
| 866 |
self.fail("Parameter error: location required when creating a security group.") |
| 867 |
|
| 868 |
results['name'] = self.name |
| 869 |
results['location'] = self.location |
| 870 |
results['rules'] = [] |
| 871 |
results['default_rules'] = [] |
| 872 |
results['tags'] = {} |
| 873 |
|
| 874 |
if self.rules: |
| 875 |
results['rules'] = self.rules |
| 876 |
if self.default_rules: |
| 877 |
results['default_rules'] = self.default_rules |
| 878 |
if self.tags: |
| 879 |
results['tags'] = self.tags |
| 880 |
|
| 881 |
self.results['changed'] = changed |
| 882 |
self.results['state'] = results |
| 883 |
if not self.check_mode: |
| 884 |
self.results['state'] = self.create_or_update(results) |
| 885 |
|
| 886 |
elif self.state == 'absent' and changed: |
| 887 |
self.log("Delete security group {0}".format(self.name)) |
| 888 |
self.results['changed'] = changed |
| 889 |
self.results['state'] = dict() |
| 890 |
if not self.check_mode: |
| 891 |
self.delete() |
| 892 |
|
| 893 |
|
| 894 |
self.results['state']['status'] = 'Deleted' |
| 895 |
|
| 896 |
return self.results |
| 897 |
|
| 898 |
def create_or_update(self, results): |
| 899 |
parameters = self.nsg_models.NetworkSecurityGroup() |
| 900 |
if results.get('rules'): |
| 901 |
parameters.security_rules = [] |
| 902 |
for rule in results.get('rules'): |
| 903 |
parameters.security_rules.append(create_rule_instance(self, rule)) |
| 904 |
if results.get('default_rules'): |
| 905 |
parameters.default_security_rules = [] |
| 906 |
for rule in results.get('default_rules'): |
| 907 |
parameters.default_security_rules.append(create_rule_instance(self, rule)) |
| 908 |
parameters.tags = results.get('tags') |
| 909 |
parameters.location = results.get('location') |
| 910 |
|
| 911 |
try: |
| 912 |
poller = self.network_client.network_security_groups.begin_create_or_update(resource_group_name=self.resource_group, |
| 913 |
network_security_group_name=self.name, |
| 914 |
parameters=parameters) |
| 915 |
result = self.get_poller_result(poller) |
| 916 |
except Exception as exc: |
| 917 |
self.fail("Error creating/updating security group {0} - {1}".format(self.name, str(exc))) |
| 918 |
return create_network_security_group_dict(result) |
| 919 |
|
| 920 |
def delete(self): |
| 921 |
try: |
| 922 |
poller = self.network_client.network_security_groups.begin_delete(resource_group_name=self.resource_group, network_security_group_name=self.name) |
| 923 |
result = self.get_poller_result(poller) |
| 924 |
except Exception as exc: |
| 925 |
self.fail("Error deleting security group {0} - {1}".format(self.name, str(exc))) |
| 926 |
|
| 927 |
return result |
| 928 |
|
| 929 |
def convert_asg_to_id(self, rule): |
| 930 |
def convert_to_id(rule, key): |
| 931 |
if rule.get(key): |
| 932 |
ids = [] |
| 933 |
for p in rule.get(key): |
| 934 |
if isinstance(p, dict): |
| 935 |
ids.append("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/applicationSecurityGroups/{2}".format( |
| 936 |
self.subscription_id, p.get('resource_group'), p.get('name'))) |
| 937 |
elif isinstance(p, str): |
| 938 |
if is_valid_resource_id(p): |
| 939 |
ids.append(p) |
| 940 |
else: |
| 941 |
ids.append("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/applicationSecurityGroups/{2}".format( |
| 942 |
self.subscription_id, self.resource_group, p)) |
| 943 |
rule[key] = ids |
| 944 |
convert_to_id(rule, 'source_application_security_groups') |
| 945 |
convert_to_id(rule, 'destination_application_security_groups') |
| 946 |
|
| 947 |
|
| 948 |
def main(): |
| 949 |
AzureRMSecurityGroup() |
| 950 |
|
| 951 |
|
| 952 |
if __name__ == '__main__': |
| 953 |
main() |