| 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_keyvault_info |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Get Azure Key Vault facts |
| 16 |
description: |
| 17 |
- Get facts of Azure Key Vault. |
| 18 |
|
| 19 |
options: |
| 20 |
resource_group: |
| 21 |
description: |
| 22 |
- The name of the resource group to which the key vault belongs. |
| 23 |
type: str |
| 24 |
name: |
| 25 |
description: |
| 26 |
- The name of the key vault. |
| 27 |
type: str |
| 28 |
hsm_name: |
| 29 |
description: |
| 30 |
- The name of the HSM. |
| 31 |
type: str |
| 32 |
tags: |
| 33 |
description: |
| 34 |
- Limit results by providing a list of tags. Format tags as 'key' or 'key:value'. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
|
| 38 |
extends_documentation_fragment: |
| 39 |
- azure.azcollection.azure |
| 40 |
|
| 41 |
author: |
| 42 |
- Yunge Zhu (@yungezz) |
| 43 |
|
| 44 |
''' |
| 45 |
|
| 46 |
EXAMPLES = ''' |
| 47 |
- name: Get Key Vault by name |
| 48 |
azure_rm_keyvault_info: |
| 49 |
resource_group: myResourceGroup |
| 50 |
name: myVault |
| 51 |
|
| 52 |
- name: List Key Vaults in specific resource group |
| 53 |
azure_rm_keyvault_info: |
| 54 |
resource_group: myResourceGroup |
| 55 |
|
| 56 |
- name: List Key Vaults in current subscription |
| 57 |
azure_rm_keyvault_info: |
| 58 |
''' |
| 59 |
|
| 60 |
RETURN = ''' |
| 61 |
keyvaults: |
| 62 |
description: List of Azure Key Vaults. |
| 63 |
returned: always |
| 64 |
type: list |
| 65 |
contains: |
| 66 |
name: |
| 67 |
description: |
| 68 |
- Name of the vault. |
| 69 |
returned: always |
| 70 |
type: str |
| 71 |
sample: myVault |
| 72 |
id: |
| 73 |
description: |
| 74 |
- Resource Id of the vault. |
| 75 |
returned: always |
| 76 |
type: str |
| 77 |
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.KeyVault/vaults/myVault |
| 78 |
vault_uri: |
| 79 |
description: |
| 80 |
- Vault uri. |
| 81 |
returned: always |
| 82 |
type: str |
| 83 |
sample: https://myVault.vault.azure.net/ |
| 84 |
location: |
| 85 |
description: |
| 86 |
- Location of the vault. |
| 87 |
returned: always |
| 88 |
type: str |
| 89 |
sample: eastus |
| 90 |
enabled_for_deployments: |
| 91 |
description: |
| 92 |
- Whether Azure Virtual Machines are permitted to retrieve certificates stored as secrets from the key vault. |
| 93 |
returned: always |
| 94 |
type: bool |
| 95 |
sample: False |
| 96 |
enabled_for_disk_encryption: |
| 97 |
description: |
| 98 |
- Whether Azure Disk Encryption is permitted to retrieve secrets from the vault and unwrap keys. |
| 99 |
returned: always |
| 100 |
type: bool |
| 101 |
sample: False |
| 102 |
enabled_for_template_deployment: |
| 103 |
description: |
| 104 |
- Whether Azure Resource Manager is permitted to retrieve secrets from the key vault. |
| 105 |
returned: always |
| 106 |
type: bool |
| 107 |
sample: False |
| 108 |
enable_rbac_authorization: |
| 109 |
description: |
| 110 |
- Property that controls how data actions are authorized. |
| 111 |
returned: always |
| 112 |
type: bool |
| 113 |
sample: False |
| 114 |
enable_soft_delete: |
| 115 |
description: |
| 116 |
- Property to specify whether Azure Resource Manager is permitted to retrieve secrets from the key vault. |
| 117 |
type: bool |
| 118 |
returned: always |
| 119 |
sample: True |
| 120 |
enable_purge_protection: |
| 121 |
description: |
| 122 |
- Property specifying whether protection against purge is enabled for this vault. |
| 123 |
type: bool |
| 124 |
returned: always |
| 125 |
sample: False |
| 126 |
soft_delete_retention_in_days: |
| 127 |
description: |
| 128 |
- Property specifying the number of days to retain deleted vaults. |
| 129 |
type: int |
| 130 |
returned: always |
| 131 |
sample: 90 |
| 132 |
tags: |
| 133 |
description: |
| 134 |
- List of tags. |
| 135 |
type: list |
| 136 |
sample: |
| 137 |
- foo |
| 138 |
sku: |
| 139 |
description: |
| 140 |
- Sku of the vault. |
| 141 |
returned: always |
| 142 |
type: dict |
| 143 |
contains: |
| 144 |
family: |
| 145 |
description: Sku family name. |
| 146 |
type: str |
| 147 |
returned: always |
| 148 |
sample: A |
| 149 |
name: |
| 150 |
description: Sku name. |
| 151 |
type: str |
| 152 |
returned: always |
| 153 |
sample: standard |
| 154 |
public_network_access: |
| 155 |
description: |
| 156 |
- Property to specify whether the vault will accept traffic from public internet. |
| 157 |
type: str |
| 158 |
returned: always |
| 159 |
sample: Disabled |
| 160 |
network_acls: |
| 161 |
description: |
| 162 |
- A collection of rules governing the accessibility of the vault from specific network locations. |
| 163 |
returned: always |
| 164 |
type: complex |
| 165 |
contains: |
| 166 |
bypass: |
| 167 |
description: |
| 168 |
- Tells what traffic can bypass network rules. |
| 169 |
type: str |
| 170 |
returned: always |
| 171 |
sample: AzureServices |
| 172 |
default_action: |
| 173 |
description: |
| 174 |
- The default action when no rule from ipRules and from virtualNetworkRules match. |
| 175 |
type: str |
| 176 |
returned: always |
| 177 |
sample: Allow |
| 178 |
ip_rules: |
| 179 |
description: |
| 180 |
- The list of IP address rules. |
| 181 |
type: list |
| 182 |
returned: always |
| 183 |
sample: [{'value': '124.56.78.91/32'}] |
| 184 |
virtual_network_rules: |
| 185 |
description: |
| 186 |
- The list of virtual network rules. |
| 187 |
type: list |
| 188 |
returned: always |
| 189 |
sample: [{'id': "/subscriptions/**/resourcegroups/**/providers/microsoft.network/virtualnetworks/**/subnets/subnet01", |
| 190 |
'ignore_missing_vnet_service_endpoint': True}] |
| 191 |
access_policies: |
| 192 |
description: |
| 193 |
- List of policies. |
| 194 |
returned: always |
| 195 |
type: complex |
| 196 |
contains: |
| 197 |
object_id: |
| 198 |
description: The object if of a user, service principal or security group in AAD for the vault. |
| 199 |
type: str |
| 200 |
returned: always |
| 201 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 202 |
tenant_id: |
| 203 |
description: The AAD tenant iD that should be used for authenticating requests to the key vault. |
| 204 |
type: str |
| 205 |
returned: always |
| 206 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 207 |
permissions: |
| 208 |
description: Permissions the identity has for keys, secrets and certificates. |
| 209 |
type: complex |
| 210 |
returned: always |
| 211 |
contains: |
| 212 |
key_permissions: |
| 213 |
description: |
| 214 |
Permissions to keys. |
| 215 |
type: list |
| 216 |
returned: always |
| 217 |
sample: |
| 218 |
- get |
| 219 |
- create |
| 220 |
keys: |
| 221 |
description: |
| 222 |
- Permissions to keys. |
| 223 |
- This return value has been deprecated and will be removed in a |
| 224 |
release after 2027-05-01. Use C(key_permissions) instead. |
| 225 |
type: list |
| 226 |
returned: always |
| 227 |
sample: |
| 228 |
- get |
| 229 |
- create |
| 230 |
secrets: |
| 231 |
description: |
| 232 |
Permissions to secrets. |
| 233 |
type: list |
| 234 |
returned: always |
| 235 |
sample: |
| 236 |
- list |
| 237 |
- set |
| 238 |
certificates: |
| 239 |
description: |
| 240 |
Permissions to secrets. |
| 241 |
type: list |
| 242 |
returned: always |
| 243 |
sample: |
| 244 |
- get |
| 245 |
- import |
| 246 |
''' |
| 247 |
|
| 248 |
|
| 249 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 250 |
|
| 251 |
try: |
| 252 |
from azure.mgmt.keyvault import KeyVaultManagementClient |
| 253 |
from azure.core.exceptions import ResourceNotFoundError |
| 254 |
except ImportError: |
| 255 |
|
| 256 |
pass |
| 257 |
|
| 258 |
|
| 259 |
def keyvault_to_dict(vault): |
| 260 |
return dict( |
| 261 |
id=vault.id, |
| 262 |
name=vault.name, |
| 263 |
location=vault.location, |
| 264 |
tags=vault.tags, |
| 265 |
vault_uri=vault.properties.vault_uri, |
| 266 |
enabled_for_deployment=vault.properties.enabled_for_deployment, |
| 267 |
enabled_for_disk_encryption=vault.properties.enabled_for_disk_encryption, |
| 268 |
enabled_for_template_deployment=vault.properties.enabled_for_template_deployment, |
| 269 |
enable_soft_delete=vault.properties.enable_soft_delete, |
| 270 |
enable_rbac_authorization=vault.properties.enable_rbac_authorization, |
| 271 |
soft_delete_retention_in_days=vault.properties.soft_delete_retention_in_days |
| 272 |
if vault.properties.soft_delete_retention_in_days else 90, |
| 273 |
enable_purge_protection=vault.properties.enable_purge_protection |
| 274 |
if vault.properties.enable_purge_protection else False, |
| 275 |
access_policies=[dict( |
| 276 |
tenant_id=policy.tenant_id, |
| 277 |
object_id=policy.object_id, |
| 278 |
permissions=dict( |
| 279 |
key_permissions=[kp.lower() for kp in policy.permissions.keys] if policy.permissions.keys else None, |
| 280 |
keys=[kp.lower() for kp in policy.permissions.keys] if policy.permissions.keys else None, |
| 281 |
secrets=[sp.lower() for sp in policy.permissions.secrets] if policy.permissions.secrets else None, |
| 282 |
certificates=[cp.lower() for cp in policy.permissions.certificates] if policy.permissions.certificates else None, |
| 283 |
storage=[stp.lower() for stp in policy.permissions.storage] if policy.permissions.storage else None |
| 284 |
) if policy.permissions else None, |
| 285 |
) for policy in vault.properties.access_policies] if vault.properties.access_policies else None, |
| 286 |
sku=dict( |
| 287 |
family=vault.properties.sku.family, |
| 288 |
name=vault.properties.sku.name |
| 289 |
), |
| 290 |
public_network_access=vault.properties.public_network_access, |
| 291 |
network_acls=dict( |
| 292 |
bypass=vault.properties.network_acls.bypass, |
| 293 |
default_action=vault.properties.network_acls.default_action, |
| 294 |
ip_rules=[dict( |
| 295 |
value=item.value |
| 296 |
) for item in vault.properties.network_acls.ip_rules] if vault.properties.network_acls.ip_rules else None, |
| 297 |
virtual_network_rules=[dict( |
| 298 |
id=item.id, |
| 299 |
ignore_missing_vnet_service_endpoint=item.ignore_missing_vnet_service_endpoint |
| 300 |
) for item in vault.properties.network_acls.virtual_network_rules] if vault.properties.network_acls.virtual_network_rules else None |
| 301 |
) if vault.properties.network_acls else None |
| 302 |
) |
| 303 |
|
| 304 |
|
| 305 |
def hsm_to_dict(hsm): |
| 306 |
return dict( |
| 307 |
id=hsm.id, |
| 308 |
name=hsm.name, |
| 309 |
hsm_uri=hsm.properties.hsm_uri, |
| 310 |
location=hsm.location, |
| 311 |
tags=hsm.tags, |
| 312 |
identity=hsm.identity and hsm.identity.as_dict() or None, |
| 313 |
enable_soft_delete=hsm.properties.enable_soft_delete, |
| 314 |
soft_delete_retention_in_days=hsm.properties.soft_delete_retention_in_days |
| 315 |
if hsm.properties.soft_delete_retention_in_days else 90, |
| 316 |
enable_purge_protection=hsm.properties.enable_purge_protection |
| 317 |
if hsm.properties.enable_purge_protection else False, |
| 318 |
sku=dict( |
| 319 |
family=hsm.sku.family, |
| 320 |
name=hsm.sku.name |
| 321 |
) |
| 322 |
) |
| 323 |
|
| 324 |
|
| 325 |
class AzureRMKeyVaultInfo(AzureRMModuleBase): |
| 326 |
|
| 327 |
def __init__(self): |
| 328 |
self.module_arg_spec = dict( |
| 329 |
resource_group=dict(type='str'), |
| 330 |
name=dict(type='str'), |
| 331 |
hsm_name=dict(type='str'), |
| 332 |
tags=dict(type='list', elements='str') |
| 333 |
) |
| 334 |
|
| 335 |
self.resource_group = None |
| 336 |
self.name = None |
| 337 |
self.hsm_name = None |
| 338 |
self.tags = None |
| 339 |
|
| 340 |
self.results = dict(changed=False) |
| 341 |
self._client = None |
| 342 |
|
| 343 |
super(AzureRMKeyVaultInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 344 |
supports_check_mode=True, |
| 345 |
supports_tags=False, |
| 346 |
facts_module=True) |
| 347 |
|
| 348 |
def exec_module(self, **kwargs): |
| 349 |
"""Main module execution method""" |
| 350 |
|
| 351 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 352 |
if hasattr(self, key): |
| 353 |
setattr(self, key, kwargs[key]) |
| 354 |
|
| 355 |
self.module.deprecate( |
| 356 |
"The 'keys' return key in 'permissions' is deprecated. Use 'key_permissions' instead.", |
| 357 |
date="2027-05-01", |
| 358 |
collection_name="azure.azcollection", |
| 359 |
) |
| 360 |
|
| 361 |
self._client = self.get_mgmt_svc_client(KeyVaultManagementClient, |
| 362 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 363 |
api_version="2026-02-01") |
| 364 |
|
| 365 |
if self.name: |
| 366 |
if self.resource_group: |
| 367 |
self.results['keyvaults'] = self.get_by_vault_name() |
| 368 |
else: |
| 369 |
self.fail("resource_group is required when filtering by name") |
| 370 |
elif self.hsm_name: |
| 371 |
if self.resource_group: |
| 372 |
self.results['hsms'] = self.get_by_hsm_name() |
| 373 |
else: |
| 374 |
self.fail("resource_group is required when filtering by hsm_name") |
| 375 |
elif self.resource_group: |
| 376 |
self.results['keyvaults'] = self.list_vault_by_resource_group() |
| 377 |
self.results['hsms'] = self.list_hsm_by_resource_group() |
| 378 |
else: |
| 379 |
self.results['keyvaults'] = self.list_vault() |
| 380 |
self.results['hsms'] = self.list_hsm() |
| 381 |
|
| 382 |
return self.results |
| 383 |
|
| 384 |
def get_by_hsm_name(self): |
| 385 |
''' |
| 386 |
Gets the properties of this specified hsm. |
| 387 |
|
| 388 |
:return: deserialized hsm state dictionary |
| 389 |
''' |
| 390 |
self.log("Get the hsm {0}".format(self.hsm_name)) |
| 391 |
results = [] |
| 392 |
try: |
| 393 |
response = self._client.managed_hsms.get(resource_group_name=self.resource_group, name=self.hsm_name) |
| 394 |
self.log("Response : {0}".format(response)) |
| 395 |
if response and self.has_tags(response.tags, self.tags): |
| 396 |
results.append(hsm_to_dict(response)) |
| 397 |
except ResourceNotFoundError as e: |
| 398 |
self.log("Did not find the hsm {0}: {1}".format(self.hsm_name, str(e))) |
| 399 |
return results |
| 400 |
|
| 401 |
def get_by_vault_name(self): |
| 402 |
''' |
| 403 |
Gets the properties of this specified key vault. |
| 404 |
|
| 405 |
:return: deserialized key vault state dictionary |
| 406 |
''' |
| 407 |
self.log("Get the key vault {0}".format(self.name)) |
| 408 |
results = [] |
| 409 |
try: |
| 410 |
response = self._client.vaults.get(resource_group_name=self.resource_group, vault_name=self.name) |
| 411 |
self.log("Response : {0}".format(response)) |
| 412 |
if response and self.has_tags(response.tags, self.tags): |
| 413 |
results.append(keyvault_to_dict(response)) |
| 414 |
except ResourceNotFoundError as e: |
| 415 |
self.log("Did not find the key vault {0}: {1}".format(self.name, str(e))) |
| 416 |
return results |
| 417 |
|
| 418 |
def list_hsm_by_resource_group(self): |
| 419 |
''' |
| 420 |
Lists the properties of hsms in specific resource group. |
| 421 |
|
| 422 |
:return: deserialized hsm state dictionary |
| 423 |
''' |
| 424 |
self.log("Get the hsms in resource group {0}".format(self.resource_group)) |
| 425 |
|
| 426 |
results = [] |
| 427 |
try: |
| 428 |
response = list(self._client.managed_hsms.list_by_resource_group(resource_group_name=self.resource_group)) |
| 429 |
self.log("Response : {0}".format(response)) |
| 430 |
|
| 431 |
if response: |
| 432 |
for item in response: |
| 433 |
if self.has_tags(item.tags, self.tags): |
| 434 |
results.append(hsm_to_dict(item)) |
| 435 |
except Exception as e: |
| 436 |
self.log("Did not find hsms in resource group {0} : {1}.".format(self.resource_group, str(e))) |
| 437 |
return results |
| 438 |
|
| 439 |
def list_vault_by_resource_group(self): |
| 440 |
''' |
| 441 |
Lists the properties of key vaults in specific resource group. |
| 442 |
|
| 443 |
:return: deserialized key vaults state dictionary |
| 444 |
''' |
| 445 |
self.log("Get the key vaults in resource group {0}".format(self.resource_group)) |
| 446 |
|
| 447 |
results = [] |
| 448 |
try: |
| 449 |
response = list(self._client.vaults.list_by_resource_group(resource_group_name=self.resource_group)) |
| 450 |
self.log("Response : {0}".format(response)) |
| 451 |
|
| 452 |
if response: |
| 453 |
for item in response: |
| 454 |
if self.has_tags(item.tags, self.tags): |
| 455 |
results.append(keyvault_to_dict(item)) |
| 456 |
except Exception as e: |
| 457 |
self.log("Did not find key vaults in resource group {0} : {1}.".format(self.resource_group, str(e))) |
| 458 |
return results |
| 459 |
|
| 460 |
def list_vault(self): |
| 461 |
''' |
| 462 |
Lists the properties of key vaults in specific subscription. |
| 463 |
|
| 464 |
:return: deserialized key vaults state dictionary |
| 465 |
''' |
| 466 |
self.log("Get the key vaults in current subscription") |
| 467 |
|
| 468 |
results = [] |
| 469 |
try: |
| 470 |
response = list(self._client.vaults.list()) |
| 471 |
self.log("Response : {0}".format(response)) |
| 472 |
|
| 473 |
if response: |
| 474 |
for item in response: |
| 475 |
if self.has_tags(item.tags, self.tags): |
| 476 |
source_id = item.id.split('/') |
| 477 |
results.append(keyvault_to_dict(self._client.vaults.get(source_id[4], source_id[8]))) |
| 478 |
except Exception as e: |
| 479 |
self.log("Did not find key vault in current subscription {0}.".format(str(e))) |
| 480 |
return results |
| 481 |
|
| 482 |
def list_hsm(self): |
| 483 |
''' |
| 484 |
Lists the properties of hsms in specific subscription. |
| 485 |
|
| 486 |
:return: deserialized hsms state dictionary |
| 487 |
''' |
| 488 |
self.log("Get the hsms in current subscription") |
| 489 |
|
| 490 |
results = [] |
| 491 |
try: |
| 492 |
response = list(self._client.managed_hsms.list_by_subscription()) |
| 493 |
self.log("Response : {0}".format(response)) |
| 494 |
|
| 495 |
if response: |
| 496 |
for item in response: |
| 497 |
if self.has_tags(item.tags, self.tags): |
| 498 |
source_id = item.id.split('/') |
| 499 |
results.append(hsm_to_dict(self._client.managed_hsms.get(source_id[4], source_id[8]))) |
| 500 |
except Exception as e: |
| 501 |
self.log("Did not find hsm in current subscription {0}.".format(str(e))) |
| 502 |
return results |
| 503 |
|
| 504 |
|
| 505 |
def main(): |
| 506 |
"""Main execution""" |
| 507 |
AzureRMKeyVaultInfo() |
| 508 |
|
| 509 |
|
| 510 |
if __name__ == '__main__': |
| 511 |
main() |