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