| 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_roledefinition |
| 14 |
version_added: "0.1.2" |
| 15 |
short_description: Manage Azure Role Definition |
| 16 |
description: |
| 17 |
- Create, update and delete instance of Azure Role Definition. |
| 18 |
|
| 19 |
options: |
| 20 |
name: |
| 21 |
description: |
| 22 |
- Unique name of role definition. |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
permissions: |
| 26 |
description: |
| 27 |
- Set of role definition permissions. |
| 28 |
- See U(https://docs.microsoft.com/en-us/azure/app-service/app-service-web-overview) for more info. |
| 29 |
type: list |
| 30 |
elements: dict |
| 31 |
suboptions: |
| 32 |
actions: |
| 33 |
description: |
| 34 |
- List of allowed actions. |
| 35 |
type: list |
| 36 |
elements: str |
| 37 |
not_actions: |
| 38 |
description: |
| 39 |
- List of denied actions. |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
data_actions: |
| 43 |
description: |
| 44 |
- List of allowed data actions. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
not_data_actions: |
| 48 |
description: |
| 49 |
- List of denied data actions. |
| 50 |
type: list |
| 51 |
elements: str |
| 52 |
assignable_scopes: |
| 53 |
description: |
| 54 |
- List of assignable scopes of this definition. |
| 55 |
elements: str |
| 56 |
type: list |
| 57 |
scope: |
| 58 |
description: |
| 59 |
- The scope of the role definition. |
| 60 |
type: str |
| 61 |
description: |
| 62 |
description: |
| 63 |
- The role definition description. |
| 64 |
type: str |
| 65 |
state: |
| 66 |
description: |
| 67 |
- Assert the state of the role definition. |
| 68 |
- Use C(present) to create or update a role definition; use C(absent) to delete it. |
| 69 |
type: str |
| 70 |
default: present |
| 71 |
choices: |
| 72 |
- absent |
| 73 |
- present |
| 74 |
|
| 75 |
extends_documentation_fragment: |
| 76 |
- azure.azcollection.azure |
| 77 |
|
| 78 |
author: |
| 79 |
- Yunge Zhu(@yungezz) |
| 80 |
|
| 81 |
''' |
| 82 |
|
| 83 |
EXAMPLES = ''' |
| 84 |
- name: Create a role definition |
| 85 |
azure_rm_roledefinition: |
| 86 |
name: myTestRole |
| 87 |
scope: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myresourceGroup |
| 88 |
permissions: |
| 89 |
- actions: |
| 90 |
- "Microsoft.Compute/virtualMachines/read" |
| 91 |
data_actions: |
| 92 |
- "Microsoft.Storage/storageAccounts/blobServices/containers/blobs/write" |
| 93 |
assignable_scopes: |
| 94 |
- "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 95 |
''' |
| 96 |
|
| 97 |
RETURN = ''' |
| 98 |
id: |
| 99 |
description: |
| 100 |
- ID of current role definition. |
| 101 |
returned: always |
| 102 |
type: str |
| 103 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/providers/Microsoft.Authorization/roleDefinitions/roleDefinitionId" |
| 104 |
''' |
| 105 |
|
| 106 |
import uuid |
| 107 |
from ansible.module_utils._text import to_native |
| 108 |
|
| 109 |
try: |
| 110 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase |
| 111 |
from azure.core.polling import LROPoller |
| 112 |
from azure.mgmt.authorization import AuthorizationManagementClient |
| 113 |
|
| 114 |
except ImportError: |
| 115 |
|
| 116 |
pass |
| 117 |
|
| 118 |
|
| 119 |
permission_spec = dict( |
| 120 |
actions=dict( |
| 121 |
elements='str', |
| 122 |
type='list' |
| 123 |
), |
| 124 |
not_actions=dict( |
| 125 |
type='list', |
| 126 |
elements='str' |
| 127 |
), |
| 128 |
data_actions=dict( |
| 129 |
type='list', |
| 130 |
elements='str' |
| 131 |
), |
| 132 |
not_data_actions=dict( |
| 133 |
type='list', |
| 134 |
elements='str' |
| 135 |
), |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def roledefinition_to_dict(role): |
| 140 |
result = dict( |
| 141 |
id=role.id, |
| 142 |
name=role.name, |
| 143 |
type=role.role_type, |
| 144 |
assignable_scopes=role.assignable_scopes, |
| 145 |
description=role.description, |
| 146 |
role_name=role.role_name |
| 147 |
) |
| 148 |
if role.permissions: |
| 149 |
result['permissions'] = [dict( |
| 150 |
actions=p.actions, |
| 151 |
not_actions=p.not_actions, |
| 152 |
data_actions=p.data_actions, |
| 153 |
not_data_actions=p.not_data_actions |
| 154 |
) for p in role.permissions] |
| 155 |
return result |
| 156 |
|
| 157 |
|
| 158 |
class Actions: |
| 159 |
NoAction, CreateOrUpdate, Delete = range(3) |
| 160 |
|
| 161 |
|
| 162 |
class AzureRMRoleDefinition(AzureRMModuleBase): |
| 163 |
"""Configuration class for an Azure RM Role definition resource""" |
| 164 |
|
| 165 |
def __init__(self): |
| 166 |
self.module_arg_spec = dict( |
| 167 |
name=dict( |
| 168 |
type='str', |
| 169 |
required=True |
| 170 |
), |
| 171 |
scope=dict( |
| 172 |
type='str' |
| 173 |
), |
| 174 |
permissions=dict( |
| 175 |
type='list', |
| 176 |
elements='dict', |
| 177 |
options=permission_spec |
| 178 |
), |
| 179 |
assignable_scopes=dict( |
| 180 |
type='list', |
| 181 |
elements='str' |
| 182 |
), |
| 183 |
description=dict( |
| 184 |
type='str' |
| 185 |
), |
| 186 |
state=dict( |
| 187 |
type='str', |
| 188 |
default='present', |
| 189 |
choices=['present', 'absent'] |
| 190 |
), |
| 191 |
) |
| 192 |
|
| 193 |
self.name = None |
| 194 |
self.scope = None |
| 195 |
self.permissions = None |
| 196 |
self.description = None |
| 197 |
self.assignable_scopes = None |
| 198 |
|
| 199 |
self.results = dict( |
| 200 |
changed=False, |
| 201 |
id=None, |
| 202 |
) |
| 203 |
self.state = None |
| 204 |
self.to_do = Actions.NoAction |
| 205 |
|
| 206 |
self.role = None |
| 207 |
|
| 208 |
self._client = None |
| 209 |
|
| 210 |
super(AzureRMRoleDefinition, self).__init__(derived_arg_spec=self.module_arg_spec, |
| 211 |
supports_check_mode=True, |
| 212 |
supports_tags=False) |
| 213 |
|
| 214 |
def exec_module(self, **kwargs): |
| 215 |
"""Main module execution method""" |
| 216 |
|
| 217 |
for key in list(self.module_arg_spec.keys()): |
| 218 |
if hasattr(self, key): |
| 219 |
setattr(self, key, kwargs[key]) |
| 220 |
|
| 221 |
old_response = None |
| 222 |
response = None |
| 223 |
|
| 224 |
|
| 225 |
self._client = self.get_mgmt_svc_client(AuthorizationManagementClient, |
| 226 |
base_url=self._cloud_environment.endpoints.resource_manager, |
| 227 |
api_version="2022-04-01") |
| 228 |
|
| 229 |
self.scope = self.build_scope() |
| 230 |
|
| 231 |
|
| 232 |
old_response = self.get_roledefinition() |
| 233 |
|
| 234 |
if old_response: |
| 235 |
self.results['id'] = old_response['id'] |
| 236 |
self.role = old_response |
| 237 |
|
| 238 |
if self.state == 'present': |
| 239 |
|
| 240 |
if not old_response: |
| 241 |
self.log("Role definition doesn't exist in this scope") |
| 242 |
|
| 243 |
self.to_do = Actions.CreateOrUpdate |
| 244 |
|
| 245 |
else: |
| 246 |
|
| 247 |
self.log("Role definition already exists") |
| 248 |
self.log('Result: {0}'.format(old_response)) |
| 249 |
|
| 250 |
|
| 251 |
if self.check_update(old_response): |
| 252 |
self.to_do = Actions.CreateOrUpdate |
| 253 |
|
| 254 |
elif self.state == 'absent': |
| 255 |
if old_response: |
| 256 |
self.log("Delete role definition") |
| 257 |
self.results['changed'] = True |
| 258 |
|
| 259 |
if self.check_mode: |
| 260 |
return self.results |
| 261 |
|
| 262 |
self.delete_roledefinition(old_response['name']) |
| 263 |
|
| 264 |
self.log('role definition deleted') |
| 265 |
|
| 266 |
else: |
| 267 |
self.log("role definition {0} not exists.".format(self.name)) |
| 268 |
|
| 269 |
if self.to_do == Actions.CreateOrUpdate: |
| 270 |
self.log('Need to Create/Update role definition') |
| 271 |
self.results['changed'] = True |
| 272 |
|
| 273 |
if self.check_mode: |
| 274 |
return self.results |
| 275 |
|
| 276 |
response = self.create_update_roledefinition() |
| 277 |
self.results['id'] = response['id'] |
| 278 |
|
| 279 |
return self.results |
| 280 |
|
| 281 |
|
| 282 |
def build_scope(self): |
| 283 |
subscription_scope = '/subscriptions/' + self.subscription_id |
| 284 |
if self.scope is None: |
| 285 |
return subscription_scope |
| 286 |
return self.scope |
| 287 |
|
| 288 |
|
| 289 |
def check_update(self, old_definition): |
| 290 |
if self.description and self.description != old_definition['description']: |
| 291 |
return True |
| 292 |
if self.permissions: |
| 293 |
if len(self.permissions) != len(old_definition['permissions']): |
| 294 |
return True |
| 295 |
existing_permissions = self.permissions_to_set(old_definition['permissions']) |
| 296 |
new_permissions = self.permissions_to_set(self.permissions) |
| 297 |
if existing_permissions != new_permissions: |
| 298 |
return True |
| 299 |
if self.assignable_scopes and self.assignable_scopes != old_definition['assignable_scopes']: |
| 300 |
return True |
| 301 |
return False |
| 302 |
|
| 303 |
def permissions_to_set(self, permissions): |
| 304 |
new_permissions = [str(dict( |
| 305 |
actions=(set([to_native(a) for a in item.get('actions')]) if item.get('actions') else None), |
| 306 |
not_actions=(set([to_native(a) for a in item.get('not_actions')]) if item.get('not_actions') else None), |
| 307 |
data_actions=(set([to_native(a) for a in item.get('data_actions')]) if item.get('data_actions') else None), |
| 308 |
not_data_actions=(set([to_native(a) for a in item.get('not_data_actions')]) if item.get('not_data_actions') else None), |
| 309 |
)) for item in permissions] |
| 310 |
return set(new_permissions) |
| 311 |
|
| 312 |
def create_update_roledefinition(self): |
| 313 |
''' |
| 314 |
Creates or updates role definition. |
| 315 |
|
| 316 |
:return: deserialized role definition |
| 317 |
''' |
| 318 |
self.log("Creating / Updating role definition {0}".format(self.name)) |
| 319 |
|
| 320 |
try: |
| 321 |
permissions = None |
| 322 |
if self.permissions: |
| 323 |
permissions = [AuthorizationManagementClient.models("2018-01-01-preview").Permission( |
| 324 |
actions=p.get('actions', None), |
| 325 |
not_actions=p.get('not_actions', None), |
| 326 |
data_actions=p.get('data_actions', None), |
| 327 |
not_data_actions=p.get('not_data_actions', None) |
| 328 |
) for p in self.permissions] |
| 329 |
role_definition = AuthorizationManagementClient.models("2018-01-01-preview").RoleDefinition( |
| 330 |
role_name=self.name, |
| 331 |
description=self.description, |
| 332 |
permissions=permissions, |
| 333 |
assignable_scopes=self.assignable_scopes, |
| 334 |
role_type='CustomRole') |
| 335 |
if self.role: |
| 336 |
role_definition.name = self.role['name'] |
| 337 |
response = self._client.role_definitions.create_or_update(role_definition_id=self.role['name'] if self.role else str(uuid.uuid4()), |
| 338 |
scope=self.scope, |
| 339 |
role_definition=role_definition) |
| 340 |
if isinstance(response, LROPoller): |
| 341 |
response = self.get_poller_result(response) |
| 342 |
|
| 343 |
except Exception as exc: |
| 344 |
self.log('Error attempting to create role definition.') |
| 345 |
self.fail("Error creating role definition: {0}".format(str(exc))) |
| 346 |
return roledefinition_to_dict(response) |
| 347 |
|
| 348 |
def delete_roledefinition(self, role_definition_id): |
| 349 |
''' |
| 350 |
Deletes specified role definition. |
| 351 |
|
| 352 |
:return: True |
| 353 |
''' |
| 354 |
self.log("Deleting the role definition {0}".format(self.name)) |
| 355 |
scope = self.build_scope() |
| 356 |
try: |
| 357 |
response = self._client.role_definitions.delete(scope=scope, |
| 358 |
role_definition_id=role_definition_id) |
| 359 |
if isinstance(response, LROPoller): |
| 360 |
response = self.get_poller_result(response) |
| 361 |
except Exception as e: |
| 362 |
self.log('Error attempting to delete the role definition.') |
| 363 |
self.fail("Error deleting the role definition: {0}".format(str(e))) |
| 364 |
|
| 365 |
return True |
| 366 |
|
| 367 |
def get_roledefinition(self): |
| 368 |
''' |
| 369 |
Gets the properties of the specified role definition. |
| 370 |
|
| 371 |
:return: deserialized role definition state dictionary |
| 372 |
''' |
| 373 |
self.log("Checking if the role definition {0} is present".format(self.name)) |
| 374 |
|
| 375 |
response = None |
| 376 |
|
| 377 |
try: |
| 378 |
response = list(self._client.role_definitions.list(scope=self.scope)) |
| 379 |
|
| 380 |
if len(response) > 0: |
| 381 |
self.log("Response : {0}".format(response)) |
| 382 |
roles = [] |
| 383 |
for r in response: |
| 384 |
if r.role_name == self.name: |
| 385 |
roles.append(r) |
| 386 |
|
| 387 |
if len(roles) == 1: |
| 388 |
self.log("role definition : {0} found".format(self.name)) |
| 389 |
return roledefinition_to_dict(roles[0]) |
| 390 |
if len(roles) > 1: |
| 391 |
self.fail("Found multiple role definitions: {0}".format(roles)) |
| 392 |
|
| 393 |
except Exception as ex: |
| 394 |
self.log("Didn't find role definition {0}".format(self.name)) |
| 395 |
|
| 396 |
return False |
| 397 |
|
| 398 |
|
| 399 |
def main(): |
| 400 |
"""Main execution""" |
| 401 |
AzureRMRoleDefinition() |
| 402 |
|
| 403 |
|
| 404 |
if __name__ == '__main__': |
| 405 |
main() |