Debian Codegraph @

ansible_collections/azure/azcollection/plugins/modules/azure_rm_adpassword_info.py

raw symbols SCIP
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2020 Haiyuan Zhang, <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8
9 __metaclass__ = type
10
11 DOCUMENTATION = '''
12 module: azure_rm_adpassword_info
13
14 version_added: "0.2.0"
15
16 short_description: Get application password info
17
18 description:
19 - Get application password info.
20
21 options:
22 app_id:
23 description:
24 - The application ID.
25 type: str
26 service_principal_object_id:
27 description:
28 - The service principal object ID.
29 type: str
30 key_id:
31 description:
32 - The password key ID.
33 type: str
34 end_date:
35 description:
36 - Date or datemtime after which credentials expire.
37 - Default value is one year after current time.
38 type: str
39 value:
40 description:
41 - The application password value.
42 - Length greater than 18 characters.
43 type: str
44 app_object_id:
45 description:
46 - The application object ID.
47 type: str
48
49 extends_documentation_fragment:
50 - azure.azcollection.azure
51
52 author:
53 haiyuan_zhang (@haiyuazhang)
54 Fred-sun (@Fred-sun)
55 '''
56
57 EXAMPLES = '''
58 - name: get ad password info
59 azure_rm_adpassword_info:
60 app_id: "{{ app_id }}"
61 key_id: "{{ key_id }}"
62 '''
63
64 RETURN = '''
65 passwords:
66 description:
67 - The password info.
68 returned: success
69 type: dict
70 contains:
71 custom_key_identifier:
72 description:
73 - Custom key identifier.
74 type: str
75 returned: always
76 sample: None
77 end_date:
78 description:
79 - Date or datemtime after which credentials expire.
80 - Default value is one year after current time.
81 type: str
82 returned: always
83 sample: "2021-06-18T06:51:25.508304+00:00"
84 key_id:
85 description:
86 - The password key ID.
87 type: str
88 returned: always
89 sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
90 start_date:
91 description:
92 - Date or datetime at which credentials become valid.
93 - Default value is current time
94 type: str
95 returned: always
96 sample: "2020-06-18T06:51:25.508304+00:00"
97
98 '''
99
100 from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase
101
102 try:
103 import asyncio
104 from msgraph.generated.applications.applications_request_builder import ApplicationsRequestBuilder
105 except ImportError:
106 # This is handled in azure_rm_common
107 pass
108
109
110 class AzureRMADPasswordInfo(AzureRMModuleBase):
111 def __init__(self):
112
113 self.module_arg_spec = dict(
114 app_id=dict(type='str'),
115 app_object_id=dict(type='str'),
116 service_principal_object_id=dict(type='str'),
117 key_id=dict(type='str'),
118 value=dict(type='str'),
119 end_date=dict(type='str'),
120 )
121
122 self.app_id = None
123 self.service_principal_object_id = None
124 self.app_object_id = None
125 self.key_id = None
126 self.value = None
127 self.end_date = None
128 self.results = dict(changed=False)
129
130 self.client = None
131
132 super(AzureRMADPasswordInfo, self).__init__(derived_arg_spec=self.module_arg_spec,
133 supports_tags=False,
134 supports_check_mode=True,
135 is_ad_resource=True)
136
137 def exec_module(self, **kwargs):
138
139 for key in list(self.module_arg_spec.keys()):
140 setattr(self, key, kwargs[key])
141
142 self._client = self.get_msgraph_client()
143 self.resolve_app_obj_id()
144 passwords = self.get_all_passwords()
145
146 if self.key_id:
147 filtered = [pd for pd in passwords if str(pd.key_id) == self.key_id]
148 self.results['passwords'] = [self.to_dict(pd) for pd in filtered]
149 else:
150 self.results['passwords'] = [self.to_dict(pd) for pd in passwords]
151
152 return self.results
153
154 def resolve_app_obj_id(self):
155 try:
156 if self.app_object_id is not None:
157 return
158 elif self.app_id or self.service_principal_object_id:
159 if not self.app_id:
160 sp = asyncio.get_event_loop().run_until_complete(self.get_service_principal())
161 self.app_id = sp.app_id
162 if not self.app_id:
163 self.fail("can't resolve app via service principal object id {0}".format(
164 self.service_principal_object_id))
165
166 apps = asyncio.get_event_loop().run_until_complete(self.get_applications())
167 result = list(apps.value)
168 if result:
169 self.app_object_id = result[0].id
170 else:
171 self.fail("can't resolve app via app id {0}".format(self.app_id))
172 else:
173 self.fail("one of the [app_id, app_object_id, service_principal_object_id] must be set")
174
175 except Exception as ge:
176 self.fail("error in resolve app_object_id {0}".format(str(ge)))
177
178 def get_all_passwords(self):
179
180 try:
181 application = asyncio.get_event_loop().run_until_complete(self.get_application())
182 passwordCredentials = application.password_credentials
183 return passwordCredentials
184 except Exception as ge:
185 self.fail("failed to fetch passwords for app {0}: {1}".format(self.app_object_id, str(ge)))
186
187 def to_dict(self, pd):
188 return dict(
189 end_date=pd.end_date_time,
190 start_date=pd.start_date_time,
191 key_id=str(pd.key_id),
192 custom_key_identifier=str(pd.custom_key_identifier)
193 )
194
195 async def get_service_principal(self):
196 return await self._client.service_principals.by_service_principal_id(self.service_principal_object_id).get()
197
198 async def get_applications(self):
199 request_configuration = ApplicationsRequestBuilder.ApplicationsRequestBuilderGetRequestConfiguration(
200 query_parameters=ApplicationsRequestBuilder.ApplicationsRequestBuilderGetQueryParameters(
201 filter="appId eq '{0}'".format(self.app_id),
202 ),
203 )
204 return await self._client.applications.get(request_configuration=request_configuration)
205
206 async def get_application(self):
207 return await self._client.applications.by_application_id(self.app_object_id).get()
208
209
210 def main():
211 AzureRMADPasswordInfo()
212
213
214 if __name__ == '__main__':
215 main()