| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = ''' |
| 13 |
--- |
| 14 |
module: azure_rm_adpassword |
| 15 |
|
| 16 |
version_added: "0.2.0" |
| 17 |
|
| 18 |
short_description: Manage application password |
| 19 |
|
| 20 |
description: |
| 21 |
- Manage application password. |
| 22 |
|
| 23 |
options: |
| 24 |
app_id: |
| 25 |
description: |
| 26 |
- The application ID. |
| 27 |
type: str |
| 28 |
service_principal_object_id: |
| 29 |
description: |
| 30 |
- The service principal object ID. |
| 31 |
type: str |
| 32 |
key_id: |
| 33 |
description: |
| 34 |
- The password key ID. |
| 35 |
- It isn't supported anymore in the create operation. See the Azure documentation for more information |
| 36 |
U(https://learn.microsoft.com/en-us/graph/api/application-addpassword?view=graph-rest-1.0&tabs=http#request-body). |
| 37 |
type: str |
| 38 |
end_date: |
| 39 |
description: |
| 40 |
- Date or datemtime after which credentials expire. |
| 41 |
- Default value is one year after current time. |
| 42 |
type: str |
| 43 |
display_name: |
| 44 |
description: |
| 45 |
- The friendly name of the application password. |
| 46 |
type: str |
| 47 |
app_object_id: |
| 48 |
description: |
| 49 |
- The application object ID. |
| 50 |
type: str |
| 51 |
state: |
| 52 |
description: |
| 53 |
- Assert the state of Active Dirctory Password. |
| 54 |
- Use C(present) to create or update a Password and use C(absent) to delete. |
| 55 |
- Update is not supported, if I(state=absent) and I(key_id=None), then all passwords of the application will be deleted. |
| 56 |
default: present |
| 57 |
choices: |
| 58 |
- absent |
| 59 |
- present |
| 60 |
type: str |
| 61 |
|
| 62 |
extends_documentation_fragment: |
| 63 |
- azure.azcollection.azure |
| 64 |
|
| 65 |
author: |
| 66 |
haiyuan_zhang (@haiyuazhang) |
| 67 |
Fred-sun (@Fred-sun) |
| 68 |
|
| 69 |
''' |
| 70 |
|
| 71 |
EXAMPLES = ''' |
| 72 |
- name: create ad password |
| 73 |
azure_rm_adpassword: |
| 74 |
app_id: "{{ app_id }}" |
| 75 |
state: present |
| 76 |
display_name: "Password friendly name" |
| 77 |
''' |
| 78 |
|
| 79 |
RETURN = ''' |
| 80 |
end_date: |
| 81 |
description: |
| 82 |
- Date or datemtime after which credentials expire. |
| 83 |
- Default value is one year after current time. |
| 84 |
type: str |
| 85 |
returned: always |
| 86 |
sample: "2021-06-28T06:00:32.637070+00:00" |
| 87 |
key_id: |
| 88 |
description: |
| 89 |
- The password key ID |
| 90 |
type: str |
| 91 |
returned: always |
| 92 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 93 |
start_date: |
| 94 |
description: |
| 95 |
- Date or datetime at which credentials become valid. |
| 96 |
- Default value is current time. |
| 97 |
type: str |
| 98 |
returned: always |
| 99 |
sample: "2020-06-28T06:00:32.637070+00:00" |
| 100 |
secret_text: |
| 101 |
description: |
| 102 |
- The application password value. |
| 103 |
- API only returns the application password value at creation. |
| 104 |
type: str |
| 105 |
returned: created |
| 106 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 107 |
''' |
| 108 |
|
| 109 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 110 |
import datetime |
| 111 |
|
| 112 |
try: |
| 113 |
import asyncio |
| 114 |
from msgraph.generated.models.password_credential import PasswordCredential |
| 115 |
from msgraph.generated.applications.item.add_password.add_password_post_request_body import \ |
| 116 |
AddPasswordPostRequestBody |
| 117 |
from msgraph.generated.applications.item.remove_password.remove_password_post_request_body import \ |
| 118 |
RemovePasswordPostRequestBody |
| 119 |
from msgraph.generated.applications.applications_request_builder import ApplicationsRequestBuilder |
| 120 |
from dateutil.relativedelta import relativedelta |
| 121 |
except ImportError: |
| 122 |
|
| 123 |
pass |
| 124 |
|
| 125 |
|
| 126 |
class AzureRMADPassword(AzureRMModuleBase): |
| 127 |
def __init__(self): |
| 128 |
|
| 129 |
self.module_arg_spec = dict( |
| 130 |
app_id=dict(type='str'), |
| 131 |
service_principal_object_id=dict(type='str'), |
| 132 |
app_object_id=dict(type='str'), |
| 133 |
key_id=dict(type='str'), |
| 134 |
display_name=dict(type='str'), |
| 135 |
end_date=dict(type='str'), |
| 136 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 137 |
) |
| 138 |
|
| 139 |
self.state = None |
| 140 |
self.app_id = None |
| 141 |
self.service_principal_object_id = None |
| 142 |
self.app_object_id = None |
| 143 |
self.key_id = None |
| 144 |
self.display_name = None |
| 145 |
self.end_date = None |
| 146 |
self.results = dict(changed=False) |
| 147 |
|
| 148 |
self.client = None |
| 149 |
|
| 150 |
super(AzureRMADPassword, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 151 |
supports_check_mode=False, |
| 152 |
supports_tags=False, |
| 153 |
is_ad_resource=True) |
| 154 |
|
| 155 |
def exec_module(self, **kwargs): |
| 156 |
for key in list(self.module_arg_spec.keys()): |
| 157 |
setattr(self, key, kwargs[key]) |
| 158 |
|
| 159 |
self._client = self.get_msgraph_client() |
| 160 |
self.resolve_app_obj_id() |
| 161 |
passwords = self.get_all_passwords() |
| 162 |
|
| 163 |
if self.state == 'present': |
| 164 |
if self.key_id and self.key_exists(passwords): |
| 165 |
self.update_password() |
| 166 |
else: |
| 167 |
self.create_password() |
| 168 |
else: |
| 169 |
if self.key_id is None: |
| 170 |
self.delete_all_passwords(passwords) |
| 171 |
else: |
| 172 |
self.delete_password(passwords) |
| 173 |
|
| 174 |
return self.results |
| 175 |
|
| 176 |
def key_exists(self, old_passwords): |
| 177 |
for pd in old_passwords: |
| 178 |
if str(pd.key_id) == self.key_id: |
| 179 |
return True |
| 180 |
return False |
| 181 |
|
| 182 |
def resolve_app_obj_id(self): |
| 183 |
try: |
| 184 |
if self.app_object_id is not None: |
| 185 |
return |
| 186 |
elif self.app_id or self.service_principal_object_id: |
| 187 |
if not self.app_id: |
| 188 |
sp = asyncio.get_event_loop().run_until_complete(self.get_service_principal()) |
| 189 |
self.app_id = sp.app_id |
| 190 |
if not self.app_id: |
| 191 |
self.fail("can't resolve app via service principal object id {0}".format( |
| 192 |
self.service_principal_object_id)) |
| 193 |
|
| 194 |
apps = asyncio.get_event_loop().run_until_complete(self.get_applications()) |
| 195 |
result = list(apps.value) |
| 196 |
if result: |
| 197 |
self.app_object_id = result[0].id |
| 198 |
else: |
| 199 |
self.fail("can't resolve app via app id {0}".format(self.app_id)) |
| 200 |
else: |
| 201 |
self.fail("one of the [app_id, app_object_id, service_principal_id] must be set") |
| 202 |
|
| 203 |
except Exception as ge: |
| 204 |
self.fail("error in resolve app_object_id {0}".format(str(ge))) |
| 205 |
|
| 206 |
def get_all_passwords(self): |
| 207 |
|
| 208 |
try: |
| 209 |
application = asyncio.get_event_loop().run_until_complete(self.get_application()) |
| 210 |
passwordCredentials = application.password_credentials |
| 211 |
return passwordCredentials |
| 212 |
except Exception as ge: |
| 213 |
self.fail("failed to fetch passwords for app {0}: {1}".format(self.app_object_id, str(ge))) |
| 214 |
|
| 215 |
def delete_all_passwords(self, old_passwords): |
| 216 |
|
| 217 |
if len(old_passwords) == 0: |
| 218 |
self.results['changed'] = False |
| 219 |
return |
| 220 |
try: |
| 221 |
for pd in old_passwords: |
| 222 |
asyncio.get_event_loop().run_until_complete(self.remove_password(pd.key_id)) |
| 223 |
self.results['changed'] = True |
| 224 |
except Exception as ge: |
| 225 |
self.fail("fail to purge all passwords for app: {0} - {1}".format(self.app_object_id, str(ge))) |
| 226 |
|
| 227 |
def delete_password(self, old_passwords): |
| 228 |
if not self.key_exists(old_passwords): |
| 229 |
self.results['changed'] = False |
| 230 |
return |
| 231 |
|
| 232 |
num_of_passwords_before_delete = len(old_passwords) |
| 233 |
|
| 234 |
for pd in old_passwords: |
| 235 |
if str(pd.key_id) == self.key_id: |
| 236 |
try: |
| 237 |
asyncio.get_event_loop().run_until_complete(self.remove_password(pd.key_id)) |
| 238 |
|
| 239 |
num_of_passwords_after_delete = len(self.get_all_passwords()) |
| 240 |
if num_of_passwords_after_delete != num_of_passwords_before_delete: |
| 241 |
self.results['changed'] = True |
| 242 |
except Exception as ge: |
| 243 |
self.fail("failed to delete password with key id {0} - {1}".format(self.app_id, str(ge))) |
| 244 |
break |
| 245 |
|
| 246 |
def create_password(self): |
| 247 |
start_date = datetime.datetime.now(datetime.timezone.utc) |
| 248 |
end_date = self.end_date or start_date + relativedelta(years=1) |
| 249 |
display_name = self.display_name |
| 250 |
|
| 251 |
try: |
| 252 |
request_body = AddPasswordPostRequestBody( |
| 253 |
password_credential=PasswordCredential( |
| 254 |
start_date_time=start_date, |
| 255 |
end_date_time=end_date, |
| 256 |
display_name=display_name |
| 257 |
), |
| 258 |
) |
| 259 |
pd = asyncio.get_event_loop().run_until_complete(self.add_password(request_body)) |
| 260 |
|
| 261 |
self.results['changed'] = True |
| 262 |
self.results.update(self.to_dict(pd)) |
| 263 |
|
| 264 |
except Exception as ge: |
| 265 |
self.fail("failed to create new password: {0}".format(str(ge))) |
| 266 |
|
| 267 |
def update_password(self): |
| 268 |
self.fail("update existing password is not supported") |
| 269 |
|
| 270 |
def to_dict(self, pd): |
| 271 |
return dict( |
| 272 |
end_date=str(pd.end_date_time), |
| 273 |
start_date=str(pd.start_date_time), |
| 274 |
key_id=str(pd.key_id), |
| 275 |
secret_text=str(pd.secret_text) |
| 276 |
) |
| 277 |
|
| 278 |
async def get_service_principal(self): |
| 279 |
return await self._client.service_principals.by_service_principal_id(self.service_principal_object_id).get() |
| 280 |
|
| 281 |
async def get_applications(self): |
| 282 |
request_configuration = ApplicationsRequestBuilder.ApplicationsRequestBuilderGetRequestConfiguration( |
| 283 |
query_parameters=ApplicationsRequestBuilder.ApplicationsRequestBuilderGetQueryParameters( |
| 284 |
filter="appId eq '{0}'".format(self.app_id), |
| 285 |
), |
| 286 |
) |
| 287 |
return await self._client.applications.get(request_configuration=request_configuration) |
| 288 |
|
| 289 |
async def get_application(self): |
| 290 |
return await self._client.applications.by_application_id(self.app_object_id).get() |
| 291 |
|
| 292 |
async def remove_password(self, key_id): |
| 293 |
request_body = RemovePasswordPostRequestBody( |
| 294 |
key_id=key_id, |
| 295 |
) |
| 296 |
return await self._client.applications.by_application_id(self.app_object_id).remove_password.post( |
| 297 |
body=request_body) |
| 298 |
|
| 299 |
async def add_password(self, request_body): |
| 300 |
return await self._client.applications.by_application_id(self.app_object_id).add_password.post( |
| 301 |
body=request_body) |
| 302 |
|
| 303 |
|
| 304 |
def main(): |
| 305 |
AzureRMADPassword() |
| 306 |
|
| 307 |
|
| 308 |
if __name__ == '__main__': |
| 309 |
main() |