| 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_accesstoken_info |
| 14 |
|
| 15 |
version_added: "1.19.0" |
| 16 |
|
| 17 |
short_description: Get Azure API access token |
| 18 |
|
| 19 |
description: |
| 20 |
- Get an access token for Azure APIs. |
| 21 |
|
| 22 |
options: |
| 23 |
scopes: |
| 24 |
description: |
| 25 |
- The scopes to request. |
| 26 |
type: list |
| 27 |
elements: str |
| 28 |
required: True |
| 29 |
claims: |
| 30 |
description: |
| 31 |
- Additional claims required in the token. |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
token_tenant_id: |
| 35 |
description: |
| 36 |
- Tenant to include in the token request. |
| 37 |
type: str |
| 38 |
enable_cae: |
| 39 |
description: |
| 40 |
- Whether to enable Continuous Access Evaluation (CAE) for the requested token. |
| 41 |
default: false |
| 42 |
type: bool |
| 43 |
|
| 44 |
extends_documentation_fragment: |
| 45 |
- azure.azcollection.azure |
| 46 |
|
| 47 |
author: |
| 48 |
- Patrick Uiterwijk (@puiterwijk) |
| 49 |
''' |
| 50 |
|
| 51 |
EXAMPLES = ''' |
| 52 |
- name: Get access token for Microsoft Graph |
| 53 |
azure.azcollection.azure_rm_accesstoken_info: |
| 54 |
scopes: |
| 55 |
- https://graph.microsoft.com/.default |
| 56 |
''' |
| 57 |
|
| 58 |
RETURN = ''' |
| 59 |
access_token: |
| 60 |
description: |
| 61 |
- API access token. |
| 62 |
returned: success |
| 63 |
type: str |
| 64 |
sample: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiaWF0IjoxNTE2MjM5MDIyfQ.L8i6g3PfcHlioHCCPURC9pmXT7gdJpx3kOoyAfNUwCc |
| 65 |
expires_on: |
| 66 |
description: |
| 67 |
- Timestamp the token expires on. |
| 68 |
returned: success |
| 69 |
type: int |
| 70 |
sample: 1699337824 |
| 71 |
''' |
| 72 |
|
| 73 |
|
| 74 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 75 |
|
| 76 |
|
| 77 |
class AzureRMAccessToken(AzureRMModuleBase): |
| 78 |
|
| 79 |
def __init__(self): |
| 80 |
|
| 81 |
self.module_arg_spec = dict( |
| 82 |
scopes=dict(type='list', elements='str', required=True), |
| 83 |
claims=dict(type='list', elements='str'), |
| 84 |
token_tenant_id=dict(type='str'), |
| 85 |
enable_cae=dict(type='bool', default=False), |
| 86 |
) |
| 87 |
|
| 88 |
self.scopes = None |
| 89 |
self.claims = None |
| 90 |
self.token_tenant_id = None |
| 91 |
self.enable_cae = False |
| 92 |
|
| 93 |
self.results = dict(changed=False) |
| 94 |
|
| 95 |
super(AzureRMAccessToken, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 96 |
supports_check_mode=True, |
| 97 |
supports_tags=False, |
| 98 |
is_ad_resource=True) |
| 99 |
|
| 100 |
def exec_module(self, **kwargs): |
| 101 |
for key in list(self.module_arg_spec.keys()): |
| 102 |
setattr(self, key, kwargs[key]) |
| 103 |
|
| 104 |
claims = None |
| 105 |
if self.claims is not None: |
| 106 |
claims = ' '.join(self.claims) |
| 107 |
|
| 108 |
cred = self.azure_auth.azure_credential_track2 |
| 109 |
token = cred.get_token( |
| 110 |
*self.scopes, |
| 111 |
claims=claims, |
| 112 |
tenant_id=self.token_tenant_id, |
| 113 |
enable_cae=self.enable_cae, |
| 114 |
) |
| 115 |
|
| 116 |
self.results['access_token'] = token.token |
| 117 |
self.results['expires_on'] = token.expires_on |
| 118 |
return self.results |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
AzureRMAccessToken() |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == '__main__': |
| 126 |
main() |