| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
module: azure_rm_adapplication_info |
| 14 |
|
| 15 |
version_added: "1.6.0" |
| 16 |
|
| 17 |
short_description: Get Azure Active Directory application info |
| 18 |
|
| 19 |
description: |
| 20 |
- Get Azure Active Directory application info. |
| 21 |
|
| 22 |
options: |
| 23 |
app_id: |
| 24 |
description: |
| 25 |
- The application ID. |
| 26 |
type: str |
| 27 |
object_id: |
| 28 |
description: |
| 29 |
- The application's object ID. |
| 30 |
type: str |
| 31 |
identifier_uri: |
| 32 |
description: |
| 33 |
- The identifier_uri's object ID. |
| 34 |
type: str |
| 35 |
app_display_name: |
| 36 |
description: |
| 37 |
- The application Name. |
| 38 |
type: str |
| 39 |
app_diff: |
| 40 |
description: |
| 41 |
- A list of applications |
| 42 |
- The application name or application ID is mandatory |
| 43 |
- All fields of the applications can also be provided (parsing from a JSON or YAML). |
| 44 |
- With this option, you can compare your self-defined applications with the current state using an application list.\ |
| 45 |
The applications that are present only in current state but not in your list will be returned as a list with all fields\ |
| 46 |
and will receive an additional status of ABSENT.\ |
| 47 |
This allows you to first add applications with azure_rm_adapplication and then use azure_rm_adapplication_info (option diff)\ |
| 48 |
to identify the applications that should not be in the current state and subsequently delete them with azure_rm_adapplication |
| 49 |
type: list |
| 50 |
elements: dict |
| 51 |
|
| 52 |
extends_documentation_fragment: |
| 53 |
- azure.azcollection.azure |
| 54 |
|
| 55 |
author: |
| 56 |
haiyuan_zhang (@haiyuazhang) |
| 57 |
Fred-sun (@Fred-sun) |
| 58 |
guopeng_lin (@guopenglin) |
| 59 |
Xu Zhang (@xuzhang) |
| 60 |
''' |
| 61 |
|
| 62 |
EXAMPLES = ''' |
| 63 |
- name: get ad app info by App ID |
| 64 |
azure_rm_adapplication_info: |
| 65 |
app_id: "{{ app_id }}" |
| 66 |
|
| 67 |
- name: get ad app info ---- by object ID |
| 68 |
azure_rm_adapplication_info: |
| 69 |
object_id: "{{ object_id }}" |
| 70 |
|
| 71 |
- name: get ad app info ---- by identifier uri |
| 72 |
azure_rm_adapplication_info: |
| 73 |
identifier_uri: "{{ identifier_uri }}" |
| 74 |
|
| 75 |
- name: get ad app info ---- by display name |
| 76 |
azure_rm_adapplication_info: |
| 77 |
app_display_name: "{{ display_name }}" |
| 78 |
|
| 79 |
- name: get ad app diff ---- by display name |
| 80 |
azure_rm_adapplication_info: |
| 81 |
app_diff: |
| 82 |
- app_display_name: "{{ display_name }}" |
| 83 |
- app_id: "{{ app_id }}" |
| 84 |
''' |
| 85 |
|
| 86 |
RETURN = ''' |
| 87 |
applications: |
| 88 |
description: |
| 89 |
- The info of the ad application. |
| 90 |
type: complex |
| 91 |
returned: aways |
| 92 |
contains: |
| 93 |
app_display_name: |
| 94 |
description: |
| 95 |
- Object's display name or its prefix. |
| 96 |
type: str |
| 97 |
returned: always |
| 98 |
sample: app |
| 99 |
app_id: |
| 100 |
description: |
| 101 |
- The application ID. |
| 102 |
returned: always |
| 103 |
type: str |
| 104 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 105 |
identifier_uris: |
| 106 |
description: |
| 107 |
- The identifiers_uri list of app. |
| 108 |
type: list |
| 109 |
returned: always |
| 110 |
sample: ["http://ansible-atodorov"] |
| 111 |
object_id: |
| 112 |
description: |
| 113 |
- It's application's object ID. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 117 |
sign_in_audience: |
| 118 |
description: |
| 119 |
- The application can be used from any Azure AD tenants |
| 120 |
type: str |
| 121 |
returned: always |
| 122 |
sample: AzureADandPersonalMicrosoftAccount |
| 123 |
public_client_reply_urls: |
| 124 |
description: |
| 125 |
- The public client redirect urls. |
| 126 |
- Space-separated URIs to which Azure AD will redirect in response to an OAuth 2.0 request. |
| 127 |
returned: always |
| 128 |
type: list |
| 129 |
sample: [] |
| 130 |
web_reply_urls: |
| 131 |
description: |
| 132 |
- The web redirect urls. |
| 133 |
- Space-separated URIs to which Azure AD will redirect in response to an OAuth 2.0 request. |
| 134 |
returned: always |
| 135 |
type: list |
| 136 |
sample: [] |
| 137 |
spa_reply_urls: |
| 138 |
description: |
| 139 |
- The spa redirect urls. |
| 140 |
- Space-separated URIs to which Azure AD will redirect in response to an OAuth 2.0 request. |
| 141 |
returned: always |
| 142 |
type: list |
| 143 |
sample: [] |
| 144 |
optional_claims: |
| 145 |
description: |
| 146 |
- Declare the optional claims for the application. |
| 147 |
type: complex |
| 148 |
returned: always |
| 149 |
contains: |
| 150 |
access_token_claims : |
| 151 |
description: |
| 152 |
- The optional claims returned in the JWT access token |
| 153 |
type: list |
| 154 |
returned: always |
| 155 |
sample: ['name': 'aud', 'source': null, 'essential': false, 'additional_properties': []] |
| 156 |
id_token_claims: |
| 157 |
description: |
| 158 |
- The optional claims returned in the JWT ID token |
| 159 |
type: list |
| 160 |
returned: always |
| 161 |
sample: ['name': 'acct', 'source': null, 'essential': false, 'additional_properties': []] |
| 162 |
saml2_token_claims: |
| 163 |
description: |
| 164 |
- The optional claims returned in the SAML token |
| 165 |
type: list |
| 166 |
returned: always |
| 167 |
sample: ['name': 'acct', 'source': null, 'essential': false, 'additional_properties': []] |
| 168 |
app_diff: |
| 169 |
description: |
| 170 |
- The info of the ad application. |
| 171 |
type: complex |
| 172 |
returned: aways |
| 173 |
contains: |
| 174 |
app_display_name: |
| 175 |
description: |
| 176 |
- Object's display name or its prefix. |
| 177 |
type: str |
| 178 |
returned: always |
| 179 |
sample: app |
| 180 |
app_id: |
| 181 |
description: |
| 182 |
- The application ID. |
| 183 |
returned: always |
| 184 |
type: str |
| 185 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 186 |
identifier_uris: |
| 187 |
description: |
| 188 |
- The identifiers_uri list of app. |
| 189 |
type: list |
| 190 |
returned: always |
| 191 |
sample: ["http://ansible-atodorov"] |
| 192 |
object_id: |
| 193 |
description: |
| 194 |
- It's application's object ID. |
| 195 |
returned: always |
| 196 |
type: str |
| 197 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 198 |
sign_in_audience: |
| 199 |
description: |
| 200 |
- The application can be used from any Azure AD tenants |
| 201 |
type: str |
| 202 |
returned: always |
| 203 |
sample: AzureADandPersonalMicrosoftAccount |
| 204 |
public_client_reply_urls: |
| 205 |
description: |
| 206 |
- The public client redirect urls. |
| 207 |
- Space-separated URIs to which Azure AD will redirect in response to an OAuth 2.0 request. |
| 208 |
returned: always |
| 209 |
type: list |
| 210 |
sample: [] |
| 211 |
web_reply_urls: |
| 212 |
description: |
| 213 |
- The web redirect urls. |
| 214 |
- Space-separated URIs to which Azure AD will redirect in response to an OAuth 2.0 request. |
| 215 |
returned: always |
| 216 |
type: list |
| 217 |
sample: [] |
| 218 |
spa_reply_urls: |
| 219 |
description: |
| 220 |
- The spa redirect urls. |
| 221 |
- Space-separated URIs to which Azure AD will redirect in response to an OAuth 2.0 request. |
| 222 |
returned: always |
| 223 |
type: list |
| 224 |
sample: [] |
| 225 |
state: |
| 226 |
description: |
| 227 |
- absent --> The app isn't in the app_diff. |
| 228 |
returned: always |
| 229 |
type: str |
| 230 |
sample: absent |
| 231 |
notes: |
| 232 |
description: |
| 233 |
- Notes relevant for the management of the application. |
| 234 |
type: str |
| 235 |
returned: always |
| 236 |
sample: "Test value" |
| 237 |
optional_claims: |
| 238 |
description: |
| 239 |
- Declare the optional claims for the application. |
| 240 |
type: complex |
| 241 |
returned: always |
| 242 |
contains: |
| 243 |
access_token_claims : |
| 244 |
description: |
| 245 |
- The optional claims returned in the JWT access token |
| 246 |
type: list |
| 247 |
returned: always |
| 248 |
sample: ['name': 'aud', 'source': null, 'essential': false, 'additional_properties': []] |
| 249 |
id_token_claims: |
| 250 |
description: |
| 251 |
- The optional claims returned in the JWT ID token |
| 252 |
type: list |
| 253 |
returned: always |
| 254 |
sample: ['name': 'acct', 'source': null, 'essential': false, 'additional_properties': []] |
| 255 |
saml2_token_claims: |
| 256 |
description: |
| 257 |
- The optional claims returned in the SAML token |
| 258 |
type: list |
| 259 |
returned: always |
| 260 |
sample: ['name': 'acct', 'source': null, 'essential': false, 'additional_properties': []] |
| 261 |
''' |
| 262 |
|
| 263 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBase |
| 264 |
|
| 265 |
try: |
| 266 |
import asyncio |
| 267 |
from msgraph.generated.applications.applications_request_builder import ApplicationsRequestBuilder |
| 268 |
from kiota_abstractions.api_error import APIError |
| 269 |
except ImportError: |
| 270 |
|
| 271 |
pass |
| 272 |
|
| 273 |
|
| 274 |
class AzureRMADApplicationInfo(AzureRMModuleBase): |
| 275 |
|
| 276 |
def __init__(self): |
| 277 |
self.module_arg_spec = dict( |
| 278 |
app_id=dict(type='str'), |
| 279 |
object_id=dict(type='str'), |
| 280 |
identifier_uri=dict(type='str'), |
| 281 |
app_display_name=dict(type='str'), |
| 282 |
app_diff=dict(type='list', elements='dict') |
| 283 |
) |
| 284 |
self.app_id = None |
| 285 |
self.app_display_name = None |
| 286 |
self.object_id = None |
| 287 |
self.identifier_uri = None |
| 288 |
self.results = dict(changed=False) |
| 289 |
self._client = None |
| 290 |
super(AzureRMADApplicationInfo, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 291 |
supports_check_mode=True, |
| 292 |
supports_tags=False, |
| 293 |
is_ad_resource=True) |
| 294 |
|
| 295 |
def exec_module(self, **kwargs): |
| 296 |
for key in list(self.module_arg_spec.keys()): |
| 297 |
setattr(self, key, kwargs[key]) |
| 298 |
|
| 299 |
applications = [] |
| 300 |
try: |
| 301 |
self._client = self.get_msgraph_client() |
| 302 |
if self.object_id: |
| 303 |
applications = [asyncio.get_event_loop().run_until_complete(self.get_application(self.object_id))] |
| 304 |
else: |
| 305 |
sub_filters = [] |
| 306 |
if self.identifier_uri: |
| 307 |
sub_filters.append("identifierUris/any(s:s eq '{0}')".format(self.identifier_uri)) |
| 308 |
if self.app_id: |
| 309 |
sub_filters.append("appId eq '{0}'".format(self.app_id)) |
| 310 |
if self.app_display_name: |
| 311 |
sub_filters.append("displayName eq '{0}'".format(self.app_display_name)) |
| 312 |
apps = asyncio.get_event_loop().run_until_complete(self.get_applications(sub_filters)) |
| 313 |
applications = list(apps) |
| 314 |
self.results['applications'] = [self.to_dict(app) for app in applications] |
| 315 |
current_app = [self.to_dict(app) for app in applications] |
| 316 |
except APIError as e: |
| 317 |
if e.response_status_code != 404: |
| 318 |
self.fail("failed to get application info {0}".format(str(e))) |
| 319 |
except Exception as e: |
| 320 |
if "Name or service not known" in str(e): |
| 321 |
self.fail("DNS resolution error occurred.") |
| 322 |
else: |
| 323 |
self.fail("An unexpected error occurred: {0}".format(str(e))) |
| 324 |
if self.app_diff: |
| 325 |
temp_app = [] |
| 326 |
for app in current_app: |
| 327 |
found = False |
| 328 |
for diff in self.app_diff: |
| 329 |
if app.get("app_id") == diff.get("app_id") or app.get("app_display_name") == diff.get("app_display_name"): |
| 330 |
found = True |
| 331 |
break |
| 332 |
if not found: |
| 333 |
app['state'] = 'absent' |
| 334 |
temp_app.append(app) |
| 335 |
self.results['app_diff'] = temp_app |
| 336 |
return self.results |
| 337 |
|
| 338 |
def serialize_claims(self, claims): |
| 339 |
if claims is None: |
| 340 |
return None |
| 341 |
return [{ |
| 342 |
"additional_properties": claim.additional_properties, |
| 343 |
"essential": claim.essential, |
| 344 |
"name": claim.name, |
| 345 |
"source": claim.source} for claim in claims] |
| 346 |
|
| 347 |
def to_dict(self, object): |
| 348 |
response = dict( |
| 349 |
app_id=object.app_id, |
| 350 |
object_id=object.id, |
| 351 |
app_display_name=object.display_name, |
| 352 |
identifier_uris=object.identifier_uris, |
| 353 |
notes=object.notes, |
| 354 |
sign_in_audience=object.sign_in_audience, |
| 355 |
web_reply_urls=object.web.redirect_uris, |
| 356 |
spa_reply_urls=object.spa.redirect_uris, |
| 357 |
public_client_reply_urls=object.public_client.redirect_uris, |
| 358 |
optional_claims=dict(access_token=[], id_token=[], saml2_token=[]) |
| 359 |
) |
| 360 |
|
| 361 |
if object.optional_claims is not None: |
| 362 |
response['optional_claims']['id_token'] = self.serialize_claims(object.optional_claims.id_token) |
| 363 |
response['optional_claims']['saml2_token'] = self.serialize_claims(object.optional_claims.saml2_token) |
| 364 |
response['optional_claims']['access_token'] = self.serialize_claims(object.optional_claims.access_token) |
| 365 |
return response |
| 366 |
|
| 367 |
async def get_application(self, obj_id): |
| 368 |
return await self._client.applications.by_application_id(obj_id).get() |
| 369 |
|
| 370 |
async def get_applications(self, sub_filters): |
| 371 |
if sub_filters: |
| 372 |
request_configuration = ApplicationsRequestBuilder.ApplicationsRequestBuilderGetRequestConfiguration( |
| 373 |
query_parameters=ApplicationsRequestBuilder.ApplicationsRequestBuilderGetQueryParameters( |
| 374 |
filter=(' and '.join(sub_filters)), |
| 375 |
), |
| 376 |
) |
| 377 |
applications = await self._client.applications.get(request_configuration=request_configuration) |
| 378 |
return applications.value |
| 379 |
else: |
| 380 |
applications_list = [] |
| 381 |
applications = await self._client.applications.get() |
| 382 |
for app in applications.value: |
| 383 |
applications_list.append(app) |
| 384 |
|
| 385 |
if applications.odata_next_link: |
| 386 |
next_link = applications.odata_next_link |
| 387 |
else: |
| 388 |
next_link = None |
| 389 |
|
| 390 |
while next_link: |
| 391 |
applications = await self._client.applications.with_url(next_link).get() |
| 392 |
next_link = applications.odata_next_link |
| 393 |
for app in applications.value: |
| 394 |
applications_list.append(app) |
| 395 |
return applications_list |
| 396 |
|
| 397 |
|
| 398 |
def main(): |
| 399 |
AzureRMADApplicationInfo() |
| 400 |
|
| 401 |
|
| 402 |
if __name__ == '__main__': |
| 403 |
main() |