| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = ''' |
| 12 |
--- |
| 13 |
module: azure_rm_adserviceprincipal |
| 14 |
|
| 15 |
version_added: "0.2.0" |
| 16 |
|
| 17 |
short_description: Manage Azure Active Directory service principal |
| 18 |
|
| 19 |
description: |
| 20 |
- Manage Azure Active Directory service principal. |
| 21 |
|
| 22 |
options: |
| 23 |
account_enabled: |
| 24 |
description: |
| 25 |
- Whether the service principal is enabled. |
| 26 |
type: bool |
| 27 |
default: True |
| 28 |
app_id: |
| 29 |
description: |
| 30 |
- The application ID. |
| 31 |
type: str |
| 32 |
required: True |
| 33 |
app_role_assignment_required: |
| 34 |
description: |
| 35 |
- Whether the Role of the Service Principal is set. |
| 36 |
type: bool |
| 37 |
notes: |
| 38 |
description: |
| 39 |
- Use this field to add information that is relevant for the management of the application |
| 40 |
type: str |
| 41 |
service_principal_type: |
| 42 |
description: |
| 43 |
- The type of the service principal. |
| 44 |
choices: |
| 45 |
- application |
| 46 |
- managedIdentity |
| 47 |
type: str |
| 48 |
default: application |
| 49 |
state: |
| 50 |
description: |
| 51 |
- Assert the state of Active Dirctory service principal. |
| 52 |
- Use C(present) to create or update a Password and use C(absent) to delete. |
| 53 |
default: present |
| 54 |
choices: |
| 55 |
- absent |
| 56 |
- present |
| 57 |
type: str |
| 58 |
|
| 59 |
extends_documentation_fragment: |
| 60 |
- azure.azcollection.azure |
| 61 |
|
| 62 |
author: |
| 63 |
haiyuan_zhang (@haiyuazhang) |
| 64 |
Fred-sun (@Fred-sun) |
| 65 |
''' |
| 66 |
|
| 67 |
EXAMPLES = ''' |
| 68 |
- name: create ad sp |
| 69 |
azure_rm_adserviceprincipal: |
| 70 |
app_id: "{{ app_id }}" |
| 71 |
state: present |
| 72 |
''' |
| 73 |
|
| 74 |
RETURN = ''' |
| 75 |
app_display_name: |
| 76 |
description: |
| 77 |
- Object's display name or its prefix. |
| 78 |
type: str |
| 79 |
returned: always |
| 80 |
sample: fredAKSCluster |
| 81 |
app_id: |
| 82 |
description: |
| 83 |
- The application ID. |
| 84 |
returned: always |
| 85 |
type: str |
| 86 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 87 |
app_role_assignment_required: |
| 88 |
description: |
| 89 |
- Whether the Role of the Service Principal is set. |
| 90 |
returned: always |
| 91 |
type: bool |
| 92 |
sample: false |
| 93 |
object_id: |
| 94 |
description: |
| 95 |
- Object ID of the associated service principal. |
| 96 |
returned: always |
| 97 |
type: str |
| 98 |
sample: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 99 |
|
| 100 |
''' |
| 101 |
|
| 102 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common_ext import AzureRMModuleBaseExt |
| 103 |
|
| 104 |
try: |
| 105 |
from msgraph.generated.models.service_principal import ServicePrincipal |
| 106 |
except Exception: |
| 107 |
pass |
| 108 |
|
| 109 |
try: |
| 110 |
import asyncio |
| 111 |
from msgraph.generated.service_principals.service_principals_request_builder import ServicePrincipalsRequestBuilder |
| 112 |
except ImportError: |
| 113 |
|
| 114 |
pass |
| 115 |
|
| 116 |
|
| 117 |
class AzureRMADServicePrincipal(AzureRMModuleBaseExt): |
| 118 |
def __init__(self): |
| 119 |
|
| 120 |
self.module_arg_spec = dict( |
| 121 |
account_enabled=dict(type='bool', default=True), |
| 122 |
app_id=dict(type='str', required=True), |
| 123 |
notes=dict(type='str'), |
| 124 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 125 |
app_role_assignment_required=dict(type='bool'), |
| 126 |
service_principal_type=dict(type='str', default='application', choices=['application', 'managedIdentity']) |
| 127 |
) |
| 128 |
|
| 129 |
self.account_enabled = None |
| 130 |
self.state = None |
| 131 |
self.app_id = None |
| 132 |
self.app_role_assignment_required = None |
| 133 |
self.notes = None |
| 134 |
self.object_id = None |
| 135 |
self.service_principal_type = None |
| 136 |
self.results = dict(changed=False) |
| 137 |
|
| 138 |
super(AzureRMADServicePrincipal, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 139 |
supports_check_mode=False, |
| 140 |
supports_tags=False, |
| 141 |
is_ad_resource=True) |
| 142 |
|
| 143 |
mutually_exclusive = {} |
| 144 |
|
| 145 |
def exec_module(self, **kwargs): |
| 146 |
|
| 147 |
for key in list(self.module_arg_spec.keys()): |
| 148 |
setattr(self, key, kwargs[key]) |
| 149 |
|
| 150 |
self._client = self.get_msgraph_client() |
| 151 |
|
| 152 |
response = self.get_resource() |
| 153 |
|
| 154 |
if response: |
| 155 |
if self.state == 'present': |
| 156 |
if self.check_update(response): |
| 157 |
self.update_resource(response) |
| 158 |
elif self.state == 'absent': |
| 159 |
self.delete_resource(response) |
| 160 |
else: |
| 161 |
if self.state == 'present': |
| 162 |
self.create_resource() |
| 163 |
elif self.state == 'absent': |
| 164 |
self.log("try to delete non exist resource") |
| 165 |
|
| 166 |
return self.results |
| 167 |
|
| 168 |
def create_resource(self): |
| 169 |
try: |
| 170 |
response = asyncio.get_event_loop().run_until_complete(self.create_service_principal()) |
| 171 |
self.results['changed'] = True |
| 172 |
self.results.update(self.to_dict(response)) |
| 173 |
return response |
| 174 |
except Exception as ge: |
| 175 |
self.fail("Error creating service principle, app id {0} - {1}".format(self.app_id, str(ge))) |
| 176 |
|
| 177 |
def update_resource(self, old_response): |
| 178 |
try: |
| 179 |
request_body = ServicePrincipal( |
| 180 |
app_role_assignment_required=None, |
| 181 |
) |
| 182 |
if self.app_role_assignment_required is not None: |
| 183 |
request_body = ServicePrincipal( |
| 184 |
app_role_assignment_required=self.app_role_assignment_required |
| 185 |
) |
| 186 |
|
| 187 |
asyncio.get_event_loop().run_until_complete(self.update_service_principal(old_response, request_body)) |
| 188 |
self.results['changed'] = True |
| 189 |
self.results.update(self.get_resource()) |
| 190 |
|
| 191 |
except Exception as ge: |
| 192 |
self.fail("Error updating the service principal app_id {0} - {1}".format(self.app_id, str(ge))) |
| 193 |
|
| 194 |
def delete_resource(self, response): |
| 195 |
try: |
| 196 |
asyncio.get_event_loop().run_until_complete(self.delete_service_principal(response)) |
| 197 |
self.results['changed'] = True |
| 198 |
return True |
| 199 |
except Exception as ge: |
| 200 |
self.fail("Error deleting service principal app_id {0} - {1}".format(self.app_id, str(ge))) |
| 201 |
|
| 202 |
def get_resource(self): |
| 203 |
try: |
| 204 |
sps = asyncio.get_event_loop().run_until_complete(self.get_service_principals()) |
| 205 |
result = list(sps.value) |
| 206 |
if not result: |
| 207 |
return False |
| 208 |
result = result[0] |
| 209 |
return self.to_dict(result) |
| 210 |
except Exception as ge: |
| 211 |
self.log("Did not find the graph instance instance {0} - {1}".format(self.app_id, str(ge))) |
| 212 |
return False |
| 213 |
|
| 214 |
def check_update(self, response): |
| 215 |
app_assignment_changed = self.app_role_assignment_required is not None and \ |
| 216 |
self.app_role_assignment_required != response.get('app_role_assignment_required', None) |
| 217 |
|
| 218 |
return app_assignment_changed |
| 219 |
|
| 220 |
def to_dict(self, object): |
| 221 |
return dict( |
| 222 |
account_enabled=object.account_enabled, |
| 223 |
app_id=object.app_id, |
| 224 |
notes=object.notes, |
| 225 |
object_id=object.id, |
| 226 |
app_display_name=object.display_name, |
| 227 |
app_role_assignment_required=object.app_role_assignment_required, |
| 228 |
service_principal_type=object.service_principal_type |
| 229 |
) |
| 230 |
|
| 231 |
async def create_service_principal(self): |
| 232 |
request_body = ServicePrincipal( |
| 233 |
app_id=self.app_id, |
| 234 |
account_enabled=self.account_enabled, |
| 235 |
notes=self.notes, |
| 236 |
service_principal_type=self.service_principal_type |
| 237 |
) |
| 238 |
return await self._client.service_principals.post(body=request_body) |
| 239 |
|
| 240 |
async def update_service_principal(self, old_response, request_body): |
| 241 |
return await self._client.service_principals.by_service_principal_id(old_response['object_id']).patch( |
| 242 |
body=request_body) |
| 243 |
|
| 244 |
async def delete_service_principal(self, response): |
| 245 |
return await self._client.service_principals.by_service_principal_id(response.get('object_id')).delete() |
| 246 |
|
| 247 |
async def get_service_principals(self): |
| 248 |
request_configuration = ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetRequestConfiguration( |
| 249 |
query_parameters=ServicePrincipalsRequestBuilder.ServicePrincipalsRequestBuilderGetQueryParameters( |
| 250 |
filter="servicePrincipalNames/any(c:c eq '{0}')".format(self.app_id), |
| 251 |
) |
| 252 |
) |
| 253 |
return await self._client.service_principals.get(request_configuration=request_configuration) |
| 254 |
|
| 255 |
|
| 256 |
def main(): |
| 257 |
AzureRMADServicePrincipal() |
| 258 |
|
| 259 |
|
| 260 |
if __name__ == '__main__': |
| 261 |
main() |