Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: NetApp, Inc
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10 DOCUMENTATION = """
11 module: na_ontap_cifs_privileges
12 short_description: NetApp ONTAP CIFS privileges
13 extends_documentation_fragment:
14 - netapp.ontap.netapp.na_ontap_rest
15 version_added: '22.13.0'
16 author: NetApp Ansible Team (@carchi8py) <[email protected]>
17 description:
18 - Add/modify/reset privileges of the local or Active Directory user or group.
19 options:
20 state:
21 description:
22 - Specifies whether to add/update or reset the specified CIFS privileges.
23 choices: ['present', 'absent']
24 type: str
25 default: present
26
27 name:
28 description:
29 - The name of the local or Active Directory user or group name.
30 required: true
31 type: str
32
33 vserver:
34 description:
35 - the name of the data vserver to use.
36 required: true
37 type: str
38
39 privileges:
40 description:
41 - Specifies the list of privileges to be retained for a user or group.
42 - SeTcbPrivilege - Allows user to act as part of the operating system
43 - SeBackupPrivilege - Allows user to back up files and directories, overriding any ACLs
44 - SeRestorePrivilege - Allows user to restore files and directories, overriding any ACLs
45 - SeTakeOwnershipPrivilege - Allows user to take ownership of files or other objects
46 - SeSecurityPrivilege - Allows user to manage auditing and viewing/dumping/clearing the security log
47 - SeChangeNotifyPrivilege - Allows user to bypass traverse checking
48 type: list
49 elements: str
50
51 lambda_config:
52 description:
53 - Configuration parameters for AWS Lambda proxy functionality.
54 - These option and suboptions are only supported with REST.
55 type: dict
56 version_added: 23.4.0
57 suboptions:
58 function_name:
59 description:
60 - The name of the AWS Lambda function to invoke.
61 type: str
62 required: true
63 aws_region:
64 description:
65 - The name of the AWS region.
66 type: str
67 required: true
68 aws_profile:
69 description:
70 - The name of the AWS profile to use for authentication.
71 type: str
72
73 notes:
74 - Only supported with REST and requires ONTAP 9.10 or later.
75 - Specified C(privileges) will replace all the existing privileges associated with the user or group when state is present.
76 - Supports AWS Lambda proxy functionality. See README for example usage.
77 """
78
79 EXAMPLES = """
80 - name: Add privileges to the specified local user
81 netapp.ontap.na_ontap_cifs_privileges:
82 state: present
83 vserver: ansibleSVM
84 name: CIFS\\local_user1
85 privileges: ["SeTcbPrivilege"]
86 hostname: "{{ netapp_hostname }}"
87 username: "{{ netapp_username }}"
88 password: "{{ netapp_password }}"
89 https: true
90 validate_certs: "{{ validate_certs }}"
91 use_rest: always
92
93 - name: Update privileges of the specified local user
94 netapp.ontap.na_ontap_cifs_privileges:
95 state: present
96 vserver: ansibleSVM
97 name: CIFS\\local_user1
98 privileges: ["SeTcbPrivilege", "SeBackupPrivilege"]
99 hostname: "{{ netapp_hostname }}"
100 username: "{{ netapp_username }}"
101 password: "{{ netapp_password }}"
102 https: true
103 validate_certs: "{{ validate_certs }}"
104 use_rest: always
105
106 - name: Reset privileges of the specified local user
107 netapp.ontap.na_ontap_cifs_privileges:
108 state: absent
109 vserver: ansibleSVM
110 name: CIFS\\local_user1
111 hostname: "{{ netapp_hostname }}"
112 username: "{{ netapp_username }}"
113 password: "{{ netapp_password }}"
114 https: true
115 validate_certs: "{{ validate_certs }}"
116 use_rest: always
117 """
118
119 RETURN = """
120 """
121
122 import traceback
123 from ansible.module_utils.basic import AnsibleModule
124 from ansible.module_utils._text import to_native
125 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
126 from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule
127 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, rest_vserver
128
129
130 class NetAppOntapCifsPrivileges:
131 def __init__(self):
132 self.argument_spec = netapp_utils.na_ontap_rest_only_spec()
133 self.argument_spec.update(dict(
134 state=dict(required=False, type='str', choices=['present', 'absent'], default='present'),
135 name=dict(required=True, type='str'),
136 vserver=dict(required=True, type='str'),
137 privileges=dict(required=False, type='list', elements='str')
138 ))
139 self.argument_spec.update(netapp_utils.na_ontap_lambda_argument_spec())
140
141 self.module = AnsibleModule(
142 argument_spec=self.argument_spec,
143 required_if=[
144 ('state', 'present', ['privileges']),
145 ('use_lambda', True, ['lambda_config']),
146 ],
147 supports_check_mode=True
148 )
149 self.svm_uuid = None
150
151 self.na_helper = NetAppModule(self.module)
152 self.parameters = self.na_helper.check_and_set_parameters(self.module)
153
154 self.rest_api = netapp_utils.OntapRestAPI(self.module)
155 self.rest_api.fail_if_not_rest_minimum_version('na_ontap_cifs_privileges:', 9, 10, 1)
156
157 def get_svm_uuid(self):
158 self.svm_uuid, dummy = rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['vserver'], self.module, True)
159
160 def get_cifs_privileges(self):
161 """ Retrieves privileges of the specified local or Active Directory user or group and SVM """
162 self.get_svm_uuid()
163 api = 'protocols/cifs/users-and-groups/privileges'
164 fields = 'name,privileges'
165 params = {
166 'svm.uuid': self.svm_uuid,
167 'name': self.parameters['name'],
168 'fields': fields
169 }
170 record, error = rest_generic.get_one_record(self.rest_api, api, params)
171 if error:
172 self.module.fail_json(msg="Error fetching CIFS privileges for %s: %s" % (self.parameters['name'], to_native(error)),
173 exception=traceback.format_exc())
174 if record:
175 return {
176 'name': record.get('name'),
177 'privileges': record.get('privileges')
178 }
179 return None
180
181 def add_cifs_privileges(self):
182 """ Add privileges to the local or Active Directory user or group for the SVM """
183 api = 'protocols/cifs/users-and-groups/privileges'
184 body = {
185 'svm.uuid': self.svm_uuid,
186 'name': self.parameters['name'],
187 'privileges': self.parameters['privileges']
188 }
189 dummy, error = rest_generic.post_async(self.rest_api, api, body)
190 if error:
191 self.module.fail_json(msg="Error adding CIFS privileges for %s: %s" % (self.parameters['name'], to_native(error)),
192 exception=traceback.format_exc())
193
194 def modify_cifs_privileges(self, modify, reset=False):
195 """ Update or reset the privileges of the specified local or Active Directory user or group and SVM """
196 if reset:
197 self.parameters['privileges'] = []
198 api = 'protocols/cifs/users-and-groups/privileges'
199 uuids = '%s/%s' % (self.svm_uuid, self.parameters['name'])
200 dummy, error = rest_generic.patch_async(self.rest_api, api, uuids, modify)
201 if error:
202 self.module.fail_json(msg='Error modifying CIFS privileges for %s: %s.' % (self.parameters['name'], to_native(error)),
203 exception=traceback.format_exc())
204
205 def apply(self):
206 current = self.get_cifs_privileges()
207 modify = None
208 cd_action = self.na_helper.get_cd_action(current, self.parameters)
209 if cd_action is None and self.parameters['state'] == 'present':
210 # check if CIFS Server NetBIOS Name is not given in input;
211 # if not add it to the given local user or group name for maintaining idempotency
212 if current['name'] != self.parameters['name'] and current['name'].split('\\')[1] == self.parameters['name']:
213 self.parameters['name'] = current['name']
214 # convert the privilege to lower case to match with GET response
215 self.parameters['privileges'] = [privilege.lower() for privilege in self.parameters['privileges']]
216 modify = self.na_helper.get_modified_attributes(current, self.parameters)
217 if self.na_helper.changed and not self.module.check_mode:
218 if cd_action == 'create':
219 self.add_cifs_privileges()
220 elif cd_action == 'delete':
221 self.modify_cifs_privileges(modify, reset=True)
222 elif modify:
223 self.modify_cifs_privileges(modify)
224 result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify)
225 self.module.exit_json(**result)
226
227
228 def main():
229 cifs_privileges = NetAppOntapCifsPrivileges()
230 cifs_privileges.apply()
231
232
233 if __name__ == '__main__':
234 main()