| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
module: azure_rm_adserviceprincipal_info |
| 13 |
|
| 14 |
version_added: "0.2.0" |
| 15 |
|
| 16 |
short_description: Get Azure Active Directory service principal info |
| 17 |
|
| 18 |
description: |
| 19 |
- Get Azure Active Directory service principal info. |
| 20 |
|
| 21 |
options: |
| 22 |
app_id: |
| 23 |
description: |
| 24 |
- The application ID. |
| 25 |
type: str |
| 26 |
object_id: |
| 27 |
description: |
| 28 |
- It's service principal's object ID. |
| 29 |
type: str |
| 30 |
|
| 31 |
extends_documentation_fragment: |
| 32 |
- azure.azcollection.azure |
| 33 |
|
| 34 |
author: |
| 35 |
haiyuan_zhang (@haiyuazhang) |
| 36 |
Fred-sun (@Fred-sun) |
| 37 |
''' |
| 38 |
|
| 39 |
EXAMPLES = ''' |
| 40 |
- name: get ad sp info |
| 41 |
azure_rm_adserviceprincipal_info: |
| 42 |
app_id: "{{ app_id }}" |
| 43 |
- name: get all service principals |
| 44 |
azure_rm_adserviceprincipal_info: |
| 45 |
''' |
| 46 |
|
| 47 |
RETURN = ''' |
| 48 |
service_principals: |
| 49 |
description: |
| 50 |
- A list of service principals in the tenant. If app_id or object_id is set, the maximum length |
| 51 |
of this list should be one. |
| 52 |
type: list |
| 53 |
elements: dict |
| 54 |
returned: always |
| 55 |
contains: |
| 56 |
account_enabled: |
| 57 |
description: |
| 58 |
- Whether the service principal is enabled. |
| 59 |
type: bool |
| 60 |
returned: always |
| 61 |
sample: true |
| 62 |
app_display_name: |
| 63 |
description: |
| 64 |
- Object's display name or its prefix. |
| 65 |
type: str |
| 66 |
returned: always |
| 67 |
sample: sp |
| 68 |
app_id: |
| 69 |
description: |
| 70 |
- The application ID. |
| 71 |
returned: always |
| 72 |
type: str |
| 73 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 74 |
app_role_assignment_required: |
| 75 |
description: |
| 76 |
- Whether the Role of the Service Principal is set. |
| 77 |
type: bool |
| 78 |
returned: always |
| 79 |
sample: false |
| 80 |
notes: |
| 81 |
description: |
| 82 |
- The notes of the Service Principal. |
| 83 |
type: str |
| 84 |
returned: always |
| 85 |
object_id: |
| 86 |
description: |
| 87 |
- It's service principal's object ID. |
| 88 |
returned: always |
| 89 |
type: str |
| 90 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 91 |
service_principal_type: |
| 92 |
description: |
| 93 |
- The type of the service principal. Application or ManagedIdentity. |
| 94 |
type: str |
| 95 |
returned: always |
| 96 |
sample: application |
| 97 |
|
| 98 |
|
| 99 |
''' |
| 100 |
|
| 101 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase |
| 102 |
|
| 103 |
try: |
| 104 |
import asyncio |
| 105 |
from msgraph.generated.service_principals.service_principals_request_builder import ServicePrincipalsRequestBuilder |
| 106 |
except ImportError: |
| 107 |
|
| 108 |
pass |
| 109 |
|
| 110 |
|
| 111 |
class AzureRMADServicePrincipalInfo(AzureRMModuleBase): |
| 112 |
def __init__(self): |
| 113 |
|
| 114 |
self.module_arg_spec = dict( |
| 115 |
app_id=dict(type='str'), |
| 116 |
object_id=dict(type='str'), |
| 117 |
) |
| 118 |
|
| 119 |
self.app_id = None |
| 120 |
self.object_id = None |
| 121 |
self.results = dict(changed=False) |
| 122 |
|
| 123 |
super(AzureRMADServicePrincipalInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 124 |
supports_check_mode=True, |
| 125 |
supports_tags=False, |
| 126 |
is_ad_resource=True) |
| 127 |
|
| 128 |
def exec_module(self, **kwargs): |
| 129 |
|
| 130 |
for key in list(self.module_arg_spec.keys()): |
| 131 |
setattr(self, key, kwargs[key]) |
| 132 |
|
| 133 |
self._client = self.get_msgraph_client() |
| 134 |
|
| 135 |
service_principals = [] |
| 136 |
|
| 137 |
try: |
| 138 |
if self.object_id is None: |
| 139 |
service_principals = asyncio.get_event_loop().run_until_complete(self.get_service_principals()) |
| 140 |
else: |
| 141 |
service_principals = [asyncio.get_event_loop().run_until_complete(self.get_service_principal())] |
| 142 |
|
| 143 |
self.results['service_principals'] = [self.to_dict(sp) for sp in service_principals] |
| 144 |
except Exception as ge: |
| 145 |
self.fail("failed to get service principal info {0}".format(str(ge))) |
| 146 |
|
| 147 |
return self.results |
| 148 |
|
| 149 |
def to_dict(self, object): |
| 150 |
return dict( |
| 151 |
app_id=object.app_id, |
| 152 |
object_id=object.id, |
| 153 |
app_display_name=object.display_name, |
| 154 |
app_role_assignment_required=object.app_role_assignment_required, |
| 155 |
notes=object.notes, |
| 156 |
account_enabled=object.account_enabled, |
| 157 |
service_principal_type=object.service_principal_type |
| 158 |
) |
| 159 |
|
| 160 |
async def get_service_principal(self): |
| 161 |
return await self._client.service_principals.by_service_principal_id(self.object_id).get() |
| 162 |
|
| 163 |
async def get_service_principals(self): |
| 164 |
kwargs = {} |
| 165 |
if self.app_id is not None: |
| 166 |
request_configuration = ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetRequestConfiguration( |
| 167 |
query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters( |
| 168 |
filter="servicePrincipalNames/any(c:c eq '{0}')".format(self.app_id)) |
| 169 |
) |
| 170 |
kwargs['request_configuration'] = request_configuration |
| 171 |
service_principals = [] |
| 172 |
response = await self._client.service_principals.get(**kwargs) |
| 173 |
if response: |
| 174 |
service_principals += response.value |
| 175 |
while response is not None and response.odata_next_link is not None: |
| 176 |
response = await self._client.service_principals.with_url(response.odata_next_link).get(**kwargs) |
| 177 |
if response: |
| 178 |
service_principals += response.value |
| 179 |
return service_principals |
| 180 |
|
| 181 |
|
| 182 |
def main(): |
| 183 |
AzureRMADServicePrincipalInfo() |
| 184 |
|
| 185 |
|
| 186 |
if __name__ == '__main__': |
| 187 |
main() |