| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import (absolute_import, division, print_function) |
| 5 |
__metaclass__ = type |
| 6 |
|
| 7 |
DOCUMENTATION = """ |
| 8 |
--- |
| 9 |
name: azure_service_principal_attribute |
| 10 |
|
| 11 |
requirements: |
| 12 |
- msgraph-sdk |
| 13 |
|
| 14 |
author: |
| 15 |
- Yunge Zhu (@yungezz) |
| 16 |
|
| 17 |
version_added: "1.12.0" |
| 18 |
|
| 19 |
short_description: Look up Azure service principal attributes. |
| 20 |
|
| 21 |
description: |
| 22 |
- Describes object id of your Azure service principal account. |
| 23 |
options: |
| 24 |
client_id: |
| 25 |
aliases: |
| 26 |
- azure_client_id |
| 27 |
secret: |
| 28 |
aliases: |
| 29 |
- azure_secret |
| 30 |
tenant: |
| 31 |
aliases: |
| 32 |
- azure_tenant |
| 33 |
cloud_environment: |
| 34 |
aliases: |
| 35 |
- azure_cloud_environment |
| 36 |
notes: |
| 37 |
- If MSI is not enabled on ansible host, it's required to provide a valid service principal which has access to the key vault. |
| 38 |
- To authenticate via service principal, pass client_id, secret and tenant or set environment variables |
| 39 |
AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID. |
| 40 |
- Authentication via C(az login) is also supported. |
| 41 |
|
| 42 |
extends_documentation_fragment: |
| 43 |
- azure.azcollection.azure_plugin |
| 44 |
""" |
| 45 |
|
| 46 |
EXAMPLES = """ |
| 47 |
set_fact: |
| 48 |
object_id: "{{ lookup('azure_service_principal_attribute', |
| 49 |
client_id=azure_client_id, |
| 50 |
secret=azure_secret, |
| 51 |
tenant=azure_secret) }}" |
| 52 |
""" |
| 53 |
|
| 54 |
RETURN = """ |
| 55 |
_raw: |
| 56 |
description: |
| 57 |
Returns object id of service principal. |
| 58 |
""" |
| 59 |
|
| 60 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMAuth |
| 61 |
from ansible.errors import AnsibleError |
| 62 |
from ansible.plugins.lookup import LookupBase |
| 63 |
from ansible.module_utils._text import to_native |
| 64 |
|
| 65 |
try: |
| 66 |
import asyncio |
| 67 |
from msgraph import GraphServiceClient |
| 68 |
from msgraph.generated.service_principals.service_principals_request_builder import ServicePrincipalsRequestBuilder |
| 69 |
except ImportError: |
| 70 |
pass |
| 71 |
|
| 72 |
|
| 73 |
class LookupModule(LookupBase): |
| 74 |
def run(self, terms, variables, **kwargs): |
| 75 |
|
| 76 |
self.set_options(direct=kwargs) |
| 77 |
|
| 78 |
auth_source = self.get_option('auth_source') |
| 79 |
client_id = self.get_option('client_id') |
| 80 |
secret = self.get_option('secret') |
| 81 |
tenant = self.get_option('tenant') |
| 82 |
|
| 83 |
|
| 84 |
if auth_source == 'auto': |
| 85 |
if any(v is None for v in [client_id, secret, tenant]): |
| 86 |
auth_source = 'cli' |
| 87 |
|
| 88 |
auth_options = dict( |
| 89 |
auth_source=auth_source, |
| 90 |
client_id=client_id, |
| 91 |
secret=secret, |
| 92 |
tenant=tenant, |
| 93 |
cloud_environment=self.get_option('cloud_environment'), |
| 94 |
is_ad_resource=True |
| 95 |
) |
| 96 |
|
| 97 |
azure_auth = AzureRMAuth(**auth_options) |
| 98 |
|
| 99 |
try: |
| 100 |
client = GraphServiceClient(azure_auth.azure_credential_track2) |
| 101 |
|
| 102 |
response = asyncio.get_event_loop().run_until_complete(self.get_service_principals(client, azure_auth.credentials['client_id'])) |
| 103 |
if not response: |
| 104 |
return [] |
| 105 |
return list(response.value)[0].id.split(',') |
| 106 |
except Exception as ex: |
| 107 |
raise AnsibleError("Failed to get service principal object id: %s" % to_native(ex)) |
| 108 |
return False |
| 109 |
|
| 110 |
async def get_service_principals(self, _client, app_id): |
| 111 |
request_configuration = ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetRequestConfiguration( |
| 112 |
query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters( |
| 113 |
filter="servicePrincipalNames/any(c:c eq '{0}')".format(app_id), |
| 114 |
) |
| 115 |
) |
| 116 |
return await _client.service_principals.get(request_configuration=request_configuration) |