Debian Codegraph @

ansible_collections/netapp/ontap/plugins/modules/na_ontap_ntfs_dacl.py

raw
1 #!/usr/bin/python
2
3 # (c) 2020-2025, NetApp, Inc
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7
8 __metaclass__ = type
9
10 ANSIBLE_METADATA = {
11 'metadata_version': '1.1',
12 'status': ['preview'],
13 'supported_by': 'certified'
14 }
15
16 DOCUMENTATION = """
17 module: na_ontap_ntfs_dacl
18 author: NetApp Ansible Team (@carchi8py) <[email protected]>
19 short_description: NetApp Ontap create, delete or modify NTFS DACL (discretionary access control list)
20 extends_documentation_fragment:
21 - netapp.ontap.netapp.na_ontap
22 version_added: '20.4.0'
23 description:
24 - Create, modify, or destroy a NTFS DACL
25
26 options:
27 state:
28 description:
29 - Whether the specified NTFS DACL should exist or not.
30 choices: ['present', 'absent']
31 default: present
32 type: str
33
34 vserver:
35 description:
36 - Specifies the vserver for the NTFS DACL.
37 required: true
38 type: str
39
40 security_descriptor:
41 description:
42 - Specifies the NTFS security descriptor.
43 required: true
44 type: str
45
46 access_type:
47 description:
48 - Specifies DACL ACE's access type. Possible values.
49 choices: ['allow', 'deny']
50 required: true
51 type: str
52
53 account:
54 description:
55 - Specifies DACL ACE's SID or domain account name of NTFS security descriptor.
56 required: true
57 type: str
58
59 rights:
60 description:
61 - Specifies DACL ACE's access rights. Mutually exclusive with advanced_access_rights.
62 choices: ['no_access', 'full_control', 'modify', 'read_and_execute', 'read', 'write']
63 type: str
64
65 apply_to:
66 description:
67 - Specifies apply DACL entry.
68 choices: ['this_folder', 'sub_folders', 'files']
69 type: list
70 elements: str
71
72 advanced_access_rights:
73 description:
74 - Specifies DACL ACE's Advanced access rights. Mutually exclusive with rights.
75 choices: ['read_data', 'write_data', 'append_data', 'read_ea', 'write_ea', 'execute_file', 'delete_child',
76 'read_attr', 'write_attr', 'delete', 'read_perm', 'write_perm', 'write_owner', 'full_control']
77 type: list
78 elements: str
79
80 """
81
82 EXAMPLES = """
83 - name: Add NTFS DACL
84 netapp.ontap.na_ontap_ntfs_dacl:
85 state: present
86 vserver: SVM1
87 security_descriptor: ansible_sd
88 access_type: allow
89 account: DOMAIN\\Account
90 rights: modify
91 hostname: "{{ netapp_hostname }}"
92 username: "{{ netapp_username }}"
93 password: "{{ netapp_password }}"
94
95 - name: Modify NTFS DACL
96 netapp.ontap.na_ontap_ntfs_dacl:
97 state: present
98 vserver: SVM1
99 security_descriptor: ansible_sd
100 access_type: full_control
101 account: DOMAIN\\Account
102 rights: modify
103 hostname: "{{ netapp_hostname }}"
104 username: "{{ netapp_username }}"
105 password: "{{ netapp_password }}"
106
107 - name: Remove NTFS DACL
108 netapp.ontap.na_ontap_ntfs_dacl:
109 state: absent
110 vserver: SVM1
111 security_descriptor: ansible_sd
112 account: DOMAIN\\Account
113 hostname: "{{ netapp_hostname }}"
114 username: "{{ netapp_username }}"
115 password: "{{ netapp_password }}"
116 """
117
118 RETURN = """
119 """
120
121 import traceback
122 from ansible.module_utils.basic import AnsibleModule
123 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
124 from ansible.module_utils._text import to_native
125 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
126
127 HAS_NETAPP_LIB = netapp_utils.has_netapp_lib()
128
129
130 class NetAppOntapNtfsDacl(object):
131 """
132 Creates, Modifies and Destroys an NTFS DACL
133 """
134
135 def __init__(self):
136 """
137 Initialize the Ontap NTFS DACL class
138 """
139
140 self.argument_spec = netapp_utils.na_ontap_host_argument_spec()
141 self.argument_spec.update(dict(
142 state=dict(required=False, choices=['present', 'absent'], default='present'),
143 vserver=dict(required=True, type='str'),
144 security_descriptor=dict(required=True, type='str'),
145 access_type=dict(required=True, choices=['allow', 'deny'], type='str'),
146 account=dict(required=True, type='str'),
147 rights=dict(required=False,
148 choices=['no_access', 'full_control', 'modify', 'read_and_execute', 'read', 'write'],
149 type='str'),
150 apply_to=dict(required=False, choices=['this_folder', 'sub_folders', 'files'], type='list', elements='str'),
151 advanced_access_rights=dict(required=False,
152 choices=['read_data', 'write_data', 'append_data', 'read_ea', 'write_ea',
153 'execute_file', 'delete_child', 'read_attr', 'write_attr', 'delete',
154 'read_perm', 'write_perm', 'write_owner', 'full_control'],
155 type='list', elements='str'),
156 ))
157
158 self.module = AnsibleModule(
159 argument_spec=self.argument_spec,
160 supports_check_mode=True,
161 mutually_exclusive=[('rights', 'advanced_access_rights')],
162 )
163
164 # set up variables
165 self.na_helper = NetAppModule()
166 self.parameters = self.na_helper.set_parameters(self.module.params)
167
168 if HAS_NETAPP_LIB is False:
169 self.module.fail_json(msg='The python NetApp-Lib module is required')
170 else:
171 self.server = netapp_utils.setup_na_ontap_zapi(module=self.module, vserver=self.parameters['vserver'])
172
173 def get_dacl(self):
174
175 dacl_entry = None
176 advanced_access_list = None
177
178 dacl_get_iter = netapp_utils.zapi.NaElement('file-directory-security-ntfs-dacl-get-iter')
179 dacl_info = netapp_utils.zapi.NaElement('file-directory-security-ntfs-dacl')
180 dacl_info.add_new_child('vserver', self.parameters['vserver'])
181 dacl_info.add_new_child('ntfs-sd', self.parameters['security_descriptor'])
182 dacl_info.add_new_child('access-type', self.parameters['access_type'])
183 dacl_info.add_new_child('account', self.parameters['account'])
184 query = netapp_utils.zapi.NaElement('query')
185 query.add_child_elem(dacl_info)
186 dacl_get_iter.add_child_elem(query)
187 try:
188 result = self.server.invoke_successfully(dacl_get_iter, True)
189 except netapp_utils.zapi.NaApiError as error:
190 self.module.fail_json(msg='Error fetching %s DACL for account %s for security descriptor %s: %s' % (
191 self.parameters['access_type'], self.parameters['account'], self.parameters['security_descriptor'],
192 to_native(error)), exception=traceback.format_exc())
193
194 if result.get_child_by_name('num-records') and int(result.get_child_content('num-records')) >= 1:
195 attributes_list = result.get_child_by_name('attributes-list')
196
197 if attributes_list is None:
198 return None
199
200 dacl = attributes_list.get_child_by_name('file-directory-security-ntfs-dacl')
201
202 apply_to_list = []
203 apply_to = dacl.get_child_by_name('apply-to')
204 for apply_child in apply_to.get_children():
205 inheritance_level = apply_child.get_content()
206
207 apply_to_list.append(inheritance_level)
208
209 if dacl.get_child_by_name('advanced-rights'):
210
211 advanced_access_list = []
212 advanced_access = dacl.get_child_by_name('advanced-rights')
213 for right in advanced_access.get_children():
214 advanced_access_right = right.get_content()
215 advanced_right = {
216 'advanced_access_rights': advanced_access_right
217 }
218 advanced_access_list.append(advanced_right)
219
220 dacl_entry = {
221 'access_type': dacl.get_child_content('access-type'),
222 'account': dacl.get_child_content('account'),
223 'apply_to': apply_to_list,
224 'security_descriptor': dacl.get_child_content('ntfs-sd'),
225 'readable_access_rights': dacl.get_child_content('readable-access-rights'),
226 'vserver': dacl.get_child_content('vserver'),
227 }
228
229 if advanced_access_list is not None:
230 dacl_entry['advanced_rights'] = advanced_access_list
231 else:
232 dacl_entry['rights'] = dacl.get_child_content('rights')
233 return dacl_entry
234
235 def add_dacl(self):
236 """
237 Adds a new NTFS DACL to an existing NTFS security descriptor
238 """
239
240 dacl_obj = netapp_utils.zapi.NaElement("file-directory-security-ntfs-dacl-add")
241 dacl_obj.add_new_child("access-type", self.parameters['access_type'])
242 dacl_obj.add_new_child("account", self.parameters['account'])
243 dacl_obj.add_new_child("ntfs-sd", self.parameters['security_descriptor'])
244
245 if 'rights' not in self.parameters.keys() and 'advanced_access_rights' not in self.parameters.keys():
246 self.module.fail_json(msg='Either rights or advanced_access_rights must be specified.')
247
248 if self.parameters.get('apply_to'):
249 apply_to_obj = netapp_utils.zapi.NaElement("apply-to")
250
251 for apply_entry in self.parameters['apply_to']:
252 apply_to_obj.add_new_child('inheritance-level', apply_entry)
253 dacl_obj.add_child_elem(apply_to_obj)
254
255 if self.parameters.get('advanced_access_rights'):
256 access_rights_obj = netapp_utils.zapi.NaElement("advanced-rights")
257
258 for right in self.parameters['advanced_access_rights']:
259 access_rights_obj.add_new_child('advanced-access-rights', right)
260
261 dacl_obj.add_child_elem(access_rights_obj)
262
263 if self.parameters.get('rights'):
264 dacl_obj.add_new_child("rights", self.parameters['rights'])
265
266 try:
267 self.server.invoke_successfully(dacl_obj, True)
268 except netapp_utils.zapi.NaApiError as error:
269 self.module.fail_json(msg='Error adding %s DACL for account %s for security descriptor %s: %s' % (
270 self.parameters['access_type'], self.parameters['account'], self.parameters['security_descriptor'], to_native(error)),
271 exception=traceback.format_exc())
272
273 def remove_dacl(self):
274 """
275 Deletes a NTFS DACL from an existing NTFS security descriptor
276 """
277 dacl_obj = netapp_utils.zapi.NaElement("file-directory-security-ntfs-dacl-remove")
278 dacl_obj.add_new_child("access-type", self.parameters['access_type'])
279 dacl_obj.add_new_child("account", self.parameters['account'])
280 dacl_obj.add_new_child("ntfs-sd", self.parameters['security_descriptor'])
281
282 try:
283 self.server.invoke_successfully(dacl_obj, True)
284 except netapp_utils.zapi.NaApiError as error:
285 self.module.fail_json(msg='Error deleting %s DACL for account %s for security descriptor %s: %s' % (
286 self.parameters['access_type'], self.parameters['account'], self.parameters['security_descriptor'], to_native(error)),
287 exception=traceback.format_exc())
288
289 def modify_dacl(self):
290 """
291 Modifies a NTFS DACL on an existing NTFS security descriptor
292 """
293
294 dacl_obj = netapp_utils.zapi.NaElement("file-directory-security-ntfs-dacl-modify")
295 dacl_obj.add_new_child("access-type", self.parameters['access_type'])
296 dacl_obj.add_new_child("account", self.parameters['account'])
297 dacl_obj.add_new_child("ntfs-sd", self.parameters['security_descriptor'])
298
299 if self.parameters.get('apply_to'):
300 apply_to_obj = netapp_utils.zapi.NaElement("apply-to")
301
302 for apply_entry in self.parameters['apply_to']:
303 apply_to_obj.add_new_child('inheritance-level', apply_entry)
304 dacl_obj.add_child_elem(apply_to_obj)
305
306 if self.parameters.get('advanced_access_rights'):
307 access_rights_obj = netapp_utils.zapi.NaElement("advanced-rights")
308
309 for right in self.parameters['advanced_access_rights']:
310 access_rights_obj.add_new_child('advanced-access-rights', right)
311
312 dacl_obj.add_child_elem(access_rights_obj)
313
314 if self.parameters.get('rights'):
315 dacl_obj.add_new_child("rights", self.parameters['rights'])
316
317 try:
318 self.server.invoke_successfully(dacl_obj, True)
319 except netapp_utils.zapi.NaApiError as error:
320 self.module.fail_json(msg='Error modifying %s DACL for account %s for security descriptor %s: %s' % (
321 self.parameters['access_type'], self.parameters['account'], self.parameters['security_descriptor'],
322 to_native(error)), exception=traceback.format_exc())
323
324 def apply(self):
325 current, modify = self.get_dacl(), None
326 cd_action = self.na_helper.get_cd_action(current, self.parameters)
327 if cd_action is None and self.parameters['state'] == 'present':
328 modify = self.na_helper.get_modified_attributes(current, self.parameters)
329
330 if self.na_helper.changed:
331 if self.module.check_mode:
332 pass
333 else:
334 if cd_action == 'create':
335 self.add_dacl()
336 elif cd_action == 'delete':
337 self.remove_dacl()
338 elif modify:
339 self.modify_dacl()
340 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
341 self.module.exit_json(**result)
342
343
344 def main():
345 """
346 Creates the NetApp Ontap NTFS DACL object and runs the correct play task
347 """
348 obj = NetAppOntapNtfsDacl()
349 obj.apply()
350
351
352 if __name__ == '__main__':
353 main()