| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022-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 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
module: na_ontap_file_security_permissions |
| 11 |
short_description: NetApp ONTAP NTFS file security permissions |
| 12 |
extends_documentation_fragment: |
| 13 |
- netapp.ontap.netapp.na_ontap_rest |
| 14 |
version_added: 22.0.0 |
| 15 |
author: NetApp Ansible Team (@carchi8py) <[email protected]> |
| 16 |
description: |
| 17 |
- Create, delete, or modify NTFS file security and audit policies of file or directory on NetApp ONTAP. |
| 18 |
- Note that ACLs are mached based on ('user', 'access', 'access_control', 'apply_to'). |
| 19 |
In order to modify any of these 4 properties, the module deletes the ACL and creates a new one. |
| 20 |
|
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Whether the specified file security permission should exist or not. |
| 25 |
- When absent, all ACLs are deleted, irrespective of the contents of C(acls). |
| 26 |
- See C(access_control) to only delete all SLAG ACLS, or only delete file-directory ACLs. |
| 27 |
- Inherited ACLs are ignored, they can't be deleted or modified. |
| 28 |
choices: ['present', 'absent'] |
| 29 |
type: str |
| 30 |
default: 'present' |
| 31 |
|
| 32 |
path: |
| 33 |
description: |
| 34 |
- The path of the file or directory on which to apply security permissions. |
| 35 |
type: str |
| 36 |
required: true |
| 37 |
|
| 38 |
vserver: |
| 39 |
description: |
| 40 |
- Name of the vserver to use. |
| 41 |
type: str |
| 42 |
required: true |
| 43 |
|
| 44 |
owner: |
| 45 |
description: |
| 46 |
- Specifies the owner of the NTFS security descriptor (SD). |
| 47 |
- You can specify the owner using either a user name or security identifier (SID). |
| 48 |
- The owner of the SD can modify the permissions on the file (or folder) or files (or folders) to which the SD is applied and |
| 49 |
can give other users the right to take ownership of the object or objects to which the SD is applied. |
| 50 |
type: str |
| 51 |
|
| 52 |
control_flags: |
| 53 |
description: |
| 54 |
- Specifies the control flags in the SD. It is a Hexadecimal Value. |
| 55 |
type: str |
| 56 |
|
| 57 |
group: |
| 58 |
description: |
| 59 |
- Specifies the owner's primary group. |
| 60 |
- Specify the owner group using either a group name or SID. |
| 61 |
type: str |
| 62 |
|
| 63 |
ignore_paths: |
| 64 |
description: |
| 65 |
- For each file or directory in the list, specifies that permissions on this file or directory cannot be replaced. |
| 66 |
type: list |
| 67 |
elements: str |
| 68 |
|
| 69 |
propagation_mode: |
| 70 |
description: |
| 71 |
- Specifies how to propagate security settings to child subfolders and files. |
| 72 |
- Defaults to propagate. |
| 73 |
choices: ['propagate', 'replace'] |
| 74 |
type: str |
| 75 |
|
| 76 |
access_control: |
| 77 |
description: |
| 78 |
- An Access Control Level specifies the access control of the task to be applied. |
| 79 |
- Valid values are "file-directory" or "Storage-Level Access Guard (SLAG)". |
| 80 |
- SLAG is used to apply the specified security descriptors with the task for the volume or qtree. |
| 81 |
- Otherwise, the security descriptors are applied on files and directories at the specified path. |
| 82 |
- The value slag is not supported on FlexGroups volumes. The default value is "file-directory". |
| 83 |
- This field requires ONTAP 9.10.1 or later. This defaults to "file_directory". |
| 84 |
- When state is present, all ACLs not listed in C(acls) are deleted when this option is absent. |
| 85 |
If this option is present, only ACLs matching its value are deleted. |
| 86 |
- When state is absent, all ACLs are deleted when this option is absent. |
| 87 |
If this option is present, only ACLs matching its value are deleted. |
| 88 |
choices: ['file_directory', 'slag'] |
| 89 |
type: str |
| 90 |
|
| 91 |
acls: |
| 92 |
description: |
| 93 |
- A discretionary access security list (DACL) identifies the trustees that are allowed or denied access to a securable object. |
| 94 |
- When a process tries to access a securable object, the system checks the access control entries (ACEs) |
| 95 |
in the object's DACL to determine whether to grant access to it. |
| 96 |
type: list |
| 97 |
elements: dict |
| 98 |
suboptions: |
| 99 |
access_control: |
| 100 |
description: |
| 101 |
- An Access Control Level specifies the access control of the task to be applied. |
| 102 |
- Valid values are "file-directory" or "Storage-Level Access Guard (SLAG)". |
| 103 |
- SLAG is used to apply the specified security descriptors with the task for the volume or qtree. |
| 104 |
- Otherwise, the security descriptors are applied on files and directories at the specified path. |
| 105 |
- The value slag is not supported on FlexGroups volumes. The default value is "file-directory". |
| 106 |
- This field requires ONTAP 9.10.1 or later. This defaults to "file_directory". |
| 107 |
choices: ['file_directory', 'slag'] |
| 108 |
type: str |
| 109 |
access: |
| 110 |
description: |
| 111 |
- Specifies whether the ACL is for DACL or SACL. |
| 112 |
- Currently tested with access_allow, access_deny for DACL and audit_failure, audit_success for SACL. |
| 113 |
choices: [access_allow, access_deny, |
| 114 |
access_allowed_callback, access_denied_callback, access_allowed_callback_object, access_denied_callback_object, |
| 115 |
system_audit_callback, system_audit_callback_object, system_resource_attribute, system_scoped_policy_id, |
| 116 |
audit_failure, audit_success, audit_success_and_failure] |
| 117 |
type: str |
| 118 |
required: true |
| 119 |
user: |
| 120 |
description: |
| 121 |
- Specifies the account to which the ACE applies. Specify either name or SID. |
| 122 |
- As of 21.24.0, the module is not idempotent when using a SID. |
| 123 |
- To make it easier when also using C(na_ontap_file_security_permissions_acl), this is aliased to C(acl_user). |
| 124 |
type: str |
| 125 |
required: true |
| 126 |
aliases: ['acl_user'] |
| 127 |
rights: |
| 128 |
description: |
| 129 |
- Specifies the access right controlled by the ACE for the account specified. |
| 130 |
- The "rights" parameter is mutually exclusive with the "advanced_rights" parameter. |
| 131 |
- ONTAP translates rights into advanced_rights and this module is not idempotent when rights are used. |
| 132 |
- Make sure to use C(advanced_rights) to maintain idempotency. C(rights) can be used to discover the mapping to C(advanced_rights). |
| 133 |
choices: ['no_access', 'full_control', 'modify', 'read_and_execute', 'read', 'write'] |
| 134 |
type: str |
| 135 |
apply_to: |
| 136 |
description: |
| 137 |
- Specifies where to apply the DACL or SACL entries. |
| 138 |
- At least one suboption must be set to true. Suboptions that are not set are assumed to be false. |
| 139 |
- With SLAGs, ONTAP accepts the three suboptions to be set to true, but creates 2 ACLs. |
| 140 |
This module requires the 2 ACLs to be present to preserve idempotency. |
| 141 |
See also C(validate_changes). |
| 142 |
type: dict |
| 143 |
required: false |
| 144 |
suboptions: |
| 145 |
files: |
| 146 |
description: |
| 147 |
- Apply to Files. |
| 148 |
type: bool |
| 149 |
default: false |
| 150 |
sub_folders: |
| 151 |
description: |
| 152 |
- Apply to all sub-folders. |
| 153 |
type: bool |
| 154 |
default: false |
| 155 |
this_folder: |
| 156 |
description: |
| 157 |
- Apply only to this folder |
| 158 |
type: bool |
| 159 |
default: false |
| 160 |
advanced_rights: |
| 161 |
description: |
| 162 |
- Specifies the advanced access right controlled by the ACE for the account specified. |
| 163 |
type: dict |
| 164 |
suboptions: |
| 165 |
append_data: |
| 166 |
description: |
| 167 |
- Append Data. |
| 168 |
type: bool |
| 169 |
delete: |
| 170 |
description: |
| 171 |
- Delete. |
| 172 |
type: bool |
| 173 |
delete_child: |
| 174 |
description: |
| 175 |
- Delete Child. |
| 176 |
type: bool |
| 177 |
execute_file: |
| 178 |
description: |
| 179 |
- Execute File. |
| 180 |
type: bool |
| 181 |
full_control: |
| 182 |
description: |
| 183 |
- Full Control. |
| 184 |
type: bool |
| 185 |
read_attr: |
| 186 |
description: |
| 187 |
- Read Attributes. |
| 188 |
type: bool |
| 189 |
read_data: |
| 190 |
description: |
| 191 |
- Read Data. |
| 192 |
type: bool |
| 193 |
read_ea: |
| 194 |
description: |
| 195 |
- Read Extended Attributes. |
| 196 |
type: bool |
| 197 |
read_perm: |
| 198 |
description: |
| 199 |
- Read Permissions. |
| 200 |
type: bool |
| 201 |
write_attr: |
| 202 |
description: |
| 203 |
- Write Attributes. |
| 204 |
type: bool |
| 205 |
write_data: |
| 206 |
description: |
| 207 |
- Write Data. |
| 208 |
type: bool |
| 209 |
write_ea: |
| 210 |
description: |
| 211 |
- Write Extended Attributes. |
| 212 |
type: bool |
| 213 |
write_owner: |
| 214 |
description: |
| 215 |
- Write Owner. |
| 216 |
type: bool |
| 217 |
write_perm: |
| 218 |
description: |
| 219 |
- Write Permission. |
| 220 |
type: bool |
| 221 |
ignore_paths: |
| 222 |
description: |
| 223 |
- For each file or directory in the list, specifies that permissions on this file or directory cannot be replaced. |
| 224 |
type: list |
| 225 |
elements: str |
| 226 |
propagation_mode: |
| 227 |
description: |
| 228 |
- Specifies how to propagate security settings to child subfolders and files. |
| 229 |
- Defaults to propagate. |
| 230 |
- This option valid only in create ACL. |
| 231 |
choices: ['propagate', 'replace'] |
| 232 |
type: str |
| 233 |
|
| 234 |
validate_changes: |
| 235 |
description: |
| 236 |
- ACLs may not be applied as expected. |
| 237 |
- For instance, if Everyone is inherited will all permissions, additional users will be granted all permissions, regardless of the request. |
| 238 |
- For this specific example, you can either delete the top level Everyone, or create a new ACL for Everyone at a lower level. |
| 239 |
- When using C(rights), ONTAP translates them into C(advanced_rights) so the validation will always fail. |
| 240 |
- Valid values are C(ignore), no checking; C(warn) to issue a warning; C(error) to fail the module. |
| 241 |
- With SLAGS, ONTAP may split one ACL into two ACLs depending on the C(apply_to) settings. To maintain idempotency, please provide 2 ACLs as input. |
| 242 |
choices: ['ignore', 'warn', 'error'] |
| 243 |
type: str |
| 244 |
default: error |
| 245 |
|
| 246 |
notes: |
| 247 |
- Supports check_mode. |
| 248 |
- Only supported with REST and requires ONTAP 9.9.1 or later.. |
| 249 |
- SLAG requires ONTAP 9.10.1 or later. |
| 250 |
- When state is present, if an ACL is inherited, and a desired ACL matches, a new ACL is created as the inherited cannot be modified. |
| 251 |
- When state is absent, inherited ACLs are ignored. |
| 252 |
''' |
| 253 |
|
| 254 |
EXAMPLES = """ |
| 255 |
- name: Create file directory security permissions. |
| 256 |
netapp.ontap.na_ontap_file_security_permissions: |
| 257 |
state: present |
| 258 |
vserver: svm1 |
| 259 |
access_control: file_directory |
| 260 |
path: /vol200/newfile.txt |
| 261 |
owner: "{{ user }}" |
| 262 |
# Note, wihout quotes, use a single backslash in AD user names |
| 263 |
# with quotes, it needs to be escaped as a double backslash |
| 264 |
# user: "ANSIBLE_CIFS\\user1" |
| 265 |
# we can't show an example with a single backslash as this is a python file, but it works in YAML. |
| 266 |
acls: |
| 267 |
- access: access_deny |
| 268 |
user: "{{ user }}" |
| 269 |
apply_to: |
| 270 |
files: true |
| 271 |
hostname: "{{ netapp_hostname }}" |
| 272 |
username: "{{ netapp_username }}" |
| 273 |
password: "{{ netapp_password }}" |
| 274 |
|
| 275 |
- name: Modify file directory security permissions. |
| 276 |
netapp.ontap.na_ontap_file_security_permissions: |
| 277 |
state: present |
| 278 |
vserver: svm1 |
| 279 |
access_control: file_directory |
| 280 |
path: /vol200/newfile.txt |
| 281 |
acls: |
| 282 |
- access: access_deny |
| 283 |
user: "{{ user }}" |
| 284 |
apply_to: |
| 285 |
files: true |
| 286 |
- access: access_allow |
| 287 |
user: "{{ user }}" |
| 288 |
apply_to: |
| 289 |
files: true |
| 290 |
hostname: "{{ netapp_hostname }}" |
| 291 |
username: "{{ netapp_username }}" |
| 292 |
password: "{{ netapp_password }}" |
| 293 |
|
| 294 |
- name: Delete file directory security ACLs. |
| 295 |
netapp.ontap.na_ontap_file_security_permissions: |
| 296 |
state: absent |
| 297 |
vserver: svm1 |
| 298 |
access_control: file_directory |
| 299 |
path: /vol200/newfile.txt |
| 300 |
acls: |
| 301 |
- access: access_deny |
| 302 |
user: "{{ user }}" |
| 303 |
apply_to: |
| 304 |
files: true |
| 305 |
- access: access_allow |
| 306 |
user: "{{ user }}" |
| 307 |
apply_to: |
| 308 |
files: true |
| 309 |
hostname: "{{ netapp_hostname }}" |
| 310 |
username: "{{ netapp_username }}" |
| 311 |
password: "{{ netapp_password }}" |
| 312 |
""" |
| 313 |
|
| 314 |
RETURN = """ |
| 315 |
""" |
| 316 |
|
| 317 |
import traceback |
| 318 |
from ansible.module_utils.basic import AnsibleModule |
| 319 |
from ansible.module_utils._text import to_native |
| 320 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 321 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule |
| 322 |
from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic, rest_vserver |
| 323 |
|
| 324 |
|
| 325 |
class NetAppOntapFileSecurityPermissions: |
| 326 |
def __init__(self): |
| 327 |
self.argument_spec = netapp_utils.na_ontap_rest_only_spec() |
| 328 |
self.argument_spec.update(dict( |
| 329 |
state=dict(required=False, type='str', choices=['present', 'absent'], default='present'), |
| 330 |
vserver=dict(required=True, type='str'), |
| 331 |
path=dict(required=True, type='str'), |
| 332 |
owner=dict(required=False, type='str'), |
| 333 |
control_flags=dict(required=False, type='str'), |
| 334 |
group=dict(required=False, type='str'), |
| 335 |
access_control=dict(required=False, type='str', choices=['file_directory', 'slag']), |
| 336 |
ignore_paths=dict(required=False, type='list', elements='str'), |
| 337 |
propagation_mode=dict(required=False, type='str', choices=['propagate', 'replace']), |
| 338 |
acls=dict(type='list', elements='dict', options=dict( |
| 339 |
access=dict(required=True, type='str', choices=[ |
| 340 |
'access_allow', 'access_deny', |
| 341 |
'access_allowed_callback', 'access_denied_callback', 'access_allowed_callback_object', 'access_denied_callback_object', |
| 342 |
'system_audit_callback', 'system_audit_callback_object', 'system_resource_attribute', 'system_scoped_policy_id', |
| 343 |
'audit_failure', 'audit_success', 'audit_success_and_failure']), |
| 344 |
access_control=dict(required=False, type='str', choices=['file_directory', 'slag']), |
| 345 |
user=dict(required=True, type='str', aliases=['acl_user']), |
| 346 |
rights=dict(required=False, |
| 347 |
choices=['no_access', 'full_control', 'modify', 'read_and_execute', 'read', 'write'], |
| 348 |
type='str'), |
| 349 |
apply_to=dict(required=False, type='dict', options=dict( |
| 350 |
files=dict(required=False, type='bool', default=False), |
| 351 |
sub_folders=dict(required=False, type='bool', default=False), |
| 352 |
this_folder=dict(required=False, type='bool', default=False), |
| 353 |
)), |
| 354 |
advanced_rights=dict(required=False, type='dict', options=dict( |
| 355 |
append_data=dict(required=False, type='bool'), |
| 356 |
delete=dict(required=False, type='bool'), |
| 357 |
delete_child=dict(required=False, type='bool'), |
| 358 |
execute_file=dict(required=False, type='bool'), |
| 359 |
full_control=dict(required=False, type='bool'), |
| 360 |
read_attr=dict(required=False, type='bool'), |
| 361 |
read_data=dict(required=False, type='bool'), |
| 362 |
read_ea=dict(required=False, type='bool'), |
| 363 |
read_perm=dict(required=False, type='bool'), |
| 364 |
write_attr=dict(required=False, type='bool'), |
| 365 |
write_data=dict(required=False, type='bool'), |
| 366 |
write_ea=dict(required=False, type='bool'), |
| 367 |
write_owner=dict(required=False, type='bool'), |
| 368 |
write_perm=dict(required=False, type='bool'), |
| 369 |
)), |
| 370 |
ignore_paths=dict(required=False, type='list', elements='str'), |
| 371 |
propagation_mode=dict(required=False, type='str', choices=['propagate', 'replace']), |
| 372 |
)), |
| 373 |
validate_changes=dict(required=False, type='str', choices=['ignore', 'warn', 'error'], default='error'), |
| 374 |
)) |
| 375 |
self.module = AnsibleModule( |
| 376 |
argument_spec=self.argument_spec, |
| 377 |
supports_check_mode=True |
| 378 |
) |
| 379 |
self.svm_uuid = None |
| 380 |
self.na_helper = NetAppModule(self) |
| 381 |
self.parameters = self.na_helper.check_and_set_parameters(self.module) |
| 382 |
self.rest_api = netapp_utils.OntapRestAPI(self.module) |
| 383 |
self.rest_api.fail_if_not_rest_minimum_version('na_ontap_file_security_permissions', 9, 9, 1) |
| 384 |
dummy, error = self.rest_api.is_rest(partially_supported_rest_properties=[['access_control', (9, 10, 1)], ['acls.access_control', (9, 10, 1)]], |
| 385 |
parameters=self.parameters) |
| 386 |
if error: |
| 387 |
self.module.fail_json(msg=error) |
| 388 |
self.parameters = self.na_helper.filter_out_none_entries(self.parameters) |
| 389 |
self.apply_to_keys = ['files', 'sub_folders', 'this_folder'] |
| 390 |
# POST at SD level only expects a subset of keys in ACL |
| 391 |
self.post_acl_keys = ['access', 'advanced_rights', 'apply_to', 'rights', 'user'] |
| 392 |
if self.parameters['state'] == 'present': |
| 393 |
self.validate_acls() |
| 394 |
|
| 395 |
def validate_acls(self): |
| 396 |
if 'acls' not in self.parameters: |
| 397 |
return |
| 398 |
self.parameters['acls'] = self.na_helper.filter_out_none_entries(self.parameters['acls']) |
| 399 |
for acl in self.parameters['acls']: |
| 400 |
if 'rights' in acl: |
| 401 |
if 'advanced_rights' in acl: |
| 402 |
self.module.fail_json(msg="Error: suboptions 'rights' and 'advanced_rights' are mutually exclusive.") |
| 403 |
self.module.warn('This module is not idempotent when "rights" is used, make sure to use "advanced_rights".') |
| 404 |
# validate that at least one suboption is true |
| 405 |
if self.na_helper.safe_get(acl, ['apply_to']) is not None \ |
| 406 |
and not any(self.na_helper.safe_get(acl, ['apply_to', key]) for key in self.apply_to_keys): |
| 407 |
self.module.fail_json(msg="Error: at least one suboption must be true for apply_to. Got: %s" % acl) |
| 408 |
# add default suboptions values if apply_to is not given |
| 409 |
if acl.get('apply_to') is None: |
| 410 |
apply_to = {'apply_to': { |
| 411 |
'sub_folders': True, |
| 412 |
'this_folder': True, |
| 413 |
'files': True |
| 414 |
} |
| 415 |
} |
| 416 |
acl.update(apply_to) |
| 417 |
# error if identical acls are set. |
| 418 |
self.match_acl_with_acls(acl, self.parameters['acls']) |
| 419 |
for option in ('access_control', 'ignore_paths', 'propagation_mode'): |
| 420 |
value = self.parameters.get(option) |
| 421 |
if value is not None: |
| 422 |
for acl in self.parameters['acls']: |
| 423 |
if acl.get(option) not in (None, value): |
| 424 |
self.module.fail_json(msg="Error: mismatch between top level value and ACL value for %s: %s vs %s" |
| 425 |
% (option, value, acl.get(option))) |
| 426 |
# make sure options are set in ach ACL, so we can match easily desired ACLs with current ACLs |
| 427 |
acl[option] = value |
| 428 |
|
| 429 |
@staticmethod |
| 430 |
def url_encode(url_fragment): |
| 431 |
""" |
| 432 |
replace special characters with URL encoding: |
| 433 |
%2F for /, %5C for backslash |
| 434 |
""" |
| 435 |
# \ is the escape character in python, so \\ means \ |
| 436 |
return url_fragment.replace("/", "%2F").replace("\\", "%5C") |
| 437 |
|
| 438 |
def get_svm_uuid(self): |
| 439 |
self.svm_uuid, dummy = rest_vserver.get_vserver_uuid(self.rest_api, self.parameters['vserver'], self.module, True) |
| 440 |
|
| 441 |
def get_file_security_permissions(self): |
| 442 |
api = 'protocols/file-security/permissions/%s/%s' % (self.svm_uuid, self.url_encode(self.parameters['path'])) |
| 443 |
fields = 'acls,control_flags,group,owner' |
| 444 |
record, error = rest_generic.get_one_record(self.rest_api, api, {'fields': fields}) |
| 445 |
# If we get 655865 the path we gave was not found, so we don't want to fail we want to return None |
| 446 |
if error: |
| 447 |
# if path not exists and state absent, return None and changed is False. |
| 448 |
if '655865' in error and self.parameters['state'] == 'absent': |
| 449 |
return None |
| 450 |
self.module.fail_json(msg="Error fetching file security permissions %s: %s" % (self.parameters['path'], to_native(error)), |
| 451 |
exception=traceback.format_exc()) |
| 452 |
return self.form_current(record) if record else None |
| 453 |
|
| 454 |
def form_current(self, record): |
| 455 |
current = { |
| 456 |
'group': self.na_helper.safe_get(record, ['group']), |
| 457 |
'owner': self.na_helper.safe_get(record, ['owner']), |
| 458 |
'control_flags': self.na_helper.safe_get(record, ['control_flags']), |
| 459 |
'path': record['path'] |
| 460 |
} |
| 461 |
acls = [] |
| 462 |
|
| 463 |
def form_acl(acl): |
| 464 |
advanced_rights_keys = ['append_data', 'delete', 'delete_child', 'execute_file', 'full_control', 'read_attr', |
| 465 |
'read_data', 'read_ea', 'read_perm', 'write_attr', 'write_data', 'write_ea', 'write_owner', 'write_perm'] |
| 466 |
advanced_rights = {} |
| 467 |
apply_to = {} |
| 468 |
if 'advanced_rights' in acl: |
| 469 |
for key in advanced_rights_keys: |
| 470 |
# REST does not return the keys when the value is False |
| 471 |
advanced_rights[key] = acl['advanced_rights'].get(key, False) |
| 472 |
if 'apply_to' in acl: |
| 473 |
for key in self.apply_to_keys: |
| 474 |
# REST does not return the keys when the value is False |
| 475 |
apply_to[key] = acl['apply_to'].get(key, False) |
| 476 |
return { |
| 477 |
'advanced_rights': advanced_rights or None, |
| 478 |
'apply_to': apply_to or None |
| 479 |
} |
| 480 |
|
| 481 |
for acl in record.get('acls', []): |
| 482 |
each_acl = { |
| 483 |
'access': self.na_helper.safe_get(acl, ['access']), |
| 484 |
'access_control': self.na_helper.safe_get(acl, ['access_control']), |
| 485 |
'inherited': self.na_helper.safe_get(acl, ['inherited']), |
| 486 |
'rights': self.na_helper.safe_get(acl, ['rights']), |
| 487 |
'user': self.na_helper.safe_get(acl, ['user']), |
| 488 |
} |
| 489 |
each_acl.update(form_acl(acl)) |
| 490 |
acls.append(each_acl) |
| 491 |
current['acls'] = acls or None |
| 492 |
return current |
| 493 |
|
| 494 |
@staticmethod |
| 495 |
def has_acls(current): |
| 496 |
return bool(current and current.get('acls')) |
| 497 |
|
| 498 |
def set_option(self, body, option): |
| 499 |
if self.parameters.get(option) is not None: |
| 500 |
body[option] = self.parameters[option] |
| 501 |
return True |
| 502 |
return False |
| 503 |
|
| 504 |
def sanitize_acl_for_post(self, acl): |
| 505 |
''' some fields like access_control, propagation_mode are not accepted for POST operation ''' |
| 506 |
post_acl = dict(acl) |
| 507 |
for key in acl: |
| 508 |
if key not in self.post_acl_keys: |
| 509 |
post_acl.pop(key) |
| 510 |
return post_acl |
| 511 |
|
| 512 |
def sanitize_acls_for_post(self, acls): |
| 513 |
''' some fields like access_control, propagation_mode are not accepted for POST operation ''' |
| 514 |
return [self.sanitize_acl_for_post(acl) for acl in acls] |
| 515 |
|
| 516 |
def create_file_security_permissions(self): |
| 517 |
api = 'protocols/file-security/permissions/%s/%s' % (self.svm_uuid, self.url_encode(self.parameters['path'])) |
| 518 |
body = {} |
| 519 |
for option in ('access_control', 'control_flags', 'group', 'owner', 'ignore_paths', 'propagation_mode'): |
| 520 |
self.set_option(body, option) |
| 521 |
body['acls'] = self.sanitize_acls_for_post(self.parameters.get('acls', [])) |
| 522 |
dummy, error = rest_generic.post_async(self.rest_api, api, body, job_timeout=120) |
| 523 |
if error: |
| 524 |
self.module.fail_json(msg='Error creating file security permissions %s: %s' % (self.parameters['path'], to_native(error)), |
| 525 |
exception=traceback.format_exc()) |
| 526 |
|
| 527 |
def add_file_security_permissions_acl(self, acl): |
| 528 |
api = 'protocols/file-security/permissions/%s/%s/acl' % (self.svm_uuid, self.url_encode(self.parameters['path'])) |
| 529 |
for option in ('access_control', 'propagation_mode'): |
| 530 |
# we already verified these options are consistent when present, so it's OK to overrid |
| 531 |
self.set_option(acl, option) |
| 532 |
dummy, error = rest_generic.post_async(self.rest_api, api, acl, timeout=0) |
| 533 |
if error: |
| 534 |
self.module.fail_json(msg='Error adding file security permissions acl %s: %s' % (self.parameters['path'], to_native(error)), |
| 535 |
exception=traceback.format_exc()) |
| 536 |
|
| 537 |
def modify_file_security_permissions_acl(self, acl): |
| 538 |
api = 'protocols/file-security/permissions/%s/%s/acl' % (self.svm_uuid, self.url_encode(self.parameters['path'])) |
| 539 |
acl = dict(acl) |
| 540 |
user = acl.pop('user') |
| 541 |
for option in ('access_control', 'propagation_mode'): |
| 542 |
# we already verified these options are consistent when present, so it's OK to overrid |
| 543 |
self.set_option(acl, option) |
| 544 |
dummy, error = rest_generic.patch_async(self.rest_api, api, self.url_encode(user), acl, {'return_records': 'true'}) |
| 545 |
if error: |
| 546 |
self.module.fail_json(msg='Error modifying file security permissions acl %s: %s' % (self.parameters['path'], to_native(error)), |
| 547 |
exception=traceback.format_exc()) |
| 548 |
|
| 549 |
def delete_file_security_permissions_acl(self, acl): |
| 550 |
api = 'protocols/file-security/permissions/%s/%s/acl' % (self.svm_uuid, self.url_encode(self.parameters['path'])) |
| 551 |
# some fieds are set to None when not present |
| 552 |
acl = self.na_helper.filter_out_none_entries(acl) |
| 553 |
# drop keys not accepted in body |
| 554 |
user = acl.pop('user') |
| 555 |
acl.pop('advanced_rights', None) |
| 556 |
acl.pop('rights', None) |
| 557 |
acl.pop('inherited', None) |
| 558 |
for option in ('access_control', 'propagation_mode'): |
| 559 |
# we already verified these options are consistent when present, so it's OK to override |
| 560 |
self.set_option(acl, option) |
| 561 |
dummy, error = rest_generic.delete_async(self.rest_api, api, self.url_encode(user), {'return_records': 'true'}, acl, timeout=0) |
| 562 |
if error: |
| 563 |
self.module.fail_json(msg='Error deleting file security permissions acl %s: %s' % (self.parameters['path'], to_native(error)), |
| 564 |
exception=traceback.format_exc()) |
| 565 |
|
| 566 |
def modify_file_security_permissions(self, modify): |
| 567 |
api = 'protocols/file-security/permissions/%s/%s' % (self.svm_uuid, self.url_encode(self.parameters['path'])) |
| 568 |
body = {} |
| 569 |
for option in modify: |
| 570 |
self.set_option(body, option) |
| 571 |
dummy, error = rest_generic.patch_async(self.rest_api, api, None, body, job_timeout=120) |
| 572 |
if error: |
| 573 |
self.module.fail_json(msg='Error modifying file security permissions %s: %s' % (self.parameters['path'], to_native(error)), |
| 574 |
exception=traceback.format_exc()) |
| 575 |
|
| 576 |
def match_acl_with_acls(self, acl, acls): |
| 577 |
""" return acl if user and access and apply_to are matched, otherwiese None """ |
| 578 |
matches = [] |
| 579 |
for an_acl in acls: |
| 580 |
# with 9.9.1, access_control is not supported. It will be set to None in received ACLs, and omitted in desired ACLs |
| 581 |
# but we can assume the user would like to see file_directory. |
| 582 |
# We can't modify inherited ACLs. But we can create a new one at a lower scope. |
| 583 |
inherited = an_acl['inherited'] if 'inherited' in an_acl else False and (acl['inherited'] if 'inherited' in acl else False) |
| 584 |
if (acl['user'] == an_acl['user'] |
| 585 |
and acl['access'] == an_acl['access'] |
| 586 |
and acl.get('access_control', 'file_directory') == an_acl.get('access_control', 'file_directory') |
| 587 |
and acl.get('apply_to') == an_acl.get('apply_to') |
| 588 |
and not inherited): |
| 589 |
matches.append(an_acl) |
| 590 |
if len(matches) > 1: |
| 591 |
self.module.fail_json(msg='Error: found more than one desired ACLs with same user, access, access_control and apply_to %s' % matches) |
| 592 |
return matches[0] if matches else None |
| 593 |
|
| 594 |
def get_acl_actions_on_modify(self, modify, current): |
| 595 |
acl_actions = {'patch-acls': [], 'post-acls': [], 'delete-acls': []} |
| 596 |
if not self.has_acls(current): |
| 597 |
acl_actions['post-acls'] = modify['acls'] |
| 598 |
return acl_actions |
| 599 |
for acl in modify['acls']: |
| 600 |
current_acl = self.match_acl_with_acls(acl, current['acls']) |
| 601 |
if current_acl: |
| 602 |
# if exact match of 2 acl found, look for modify in that matched desired and current acl. |
| 603 |
if self.is_modify_acl_required(acl, current_acl): |
| 604 |
acl_actions['patch-acls'].append(acl) |
| 605 |
else: |
| 606 |
acl_actions['post-acls'].append(acl) |
| 607 |
# Ignore inherited ACLs |
| 608 |
for acl in current['acls']: |
| 609 |
desired_acl = self.match_acl_with_acls(acl, self.parameters['acls']) |
| 610 |
if not desired_acl and not acl.get('inherited') and self.parameters.get('access_control') in (None, acl.get('access_control')): |
| 611 |
# only delete ACLs that matches the desired access_control, or all ACLs if not set |
| 612 |
acl_actions['delete-acls'].append(acl) |
| 613 |
return acl_actions |
| 614 |
|
| 615 |
def is_modify_acl_required(self, desired_acl, current_acl): |
| 616 |
current_acl_copy = current_acl.copy() |
| 617 |
current_acl_copy.pop('user') |
| 618 |
modify = self.na_helper.get_modified_attributes(current_acl_copy, desired_acl) |
| 619 |
return bool(modify) |
| 620 |
|
| 621 |
def get_acl_actions_on_delete(self, current): |
| 622 |
acl_actions = {'patch-acls': [], 'post-acls': [], 'delete-acls': []} |
| 623 |
self.na_helper.changed = False |
| 624 |
if current.get('acls'): |
| 625 |
for acl in current['acls']: |
| 626 |
# only delete ACLs that matches the desired access_control, or all ACLs if not set |
| 627 |
if not acl.get('inherited') and self.parameters.get('access_control') in (None, acl.get('access_control')): |
| 628 |
self.na_helper.changed = True |
| 629 |
acl_actions['delete-acls'].append(acl) |
| 630 |
return acl_actions |
| 631 |
|
| 632 |
def get_modify_actions(self, current): |
| 633 |
modify = self.na_helper.get_modified_attributes(current, self.parameters) |
| 634 |
if 'path' in modify: |
| 635 |
self.module.fail_json(msg='Error: mismatch on path values: desired: %s, received: %s' % (self.parameters['path'], current['path'])) |
| 636 |
if 'acls' in modify: |
| 637 |
acl_actions = self.get_acl_actions_on_modify(modify, current) |
| 638 |
# validate_modify function will check a modify in acl is required or not. |
| 639 |
# if neither patch-acls or post-acls required and modify None, set changed to False. |
| 640 |
del modify['acls'] |
| 641 |
else: |
| 642 |
acl_actions = {'patch-acls': [], 'post-acls': [], 'delete-acls': []} |
| 643 |
if not any((acl_actions['patch-acls'], acl_actions['post-acls'], acl_actions['delete-acls'], modify)): |
| 644 |
self.na_helper.changed = False |
| 645 |
return modify, acl_actions |
| 646 |
|
| 647 |
def get_acl_actions_on_create(self): |
| 648 |
""" |
| 649 |
POST does not accept access_control and propagation_mode at the ACL level, these are global values for all ACLs. |
| 650 |
Since the user could have a list of ACLs with mixed property we should useP OST the create the SD and 1 group of ACLs |
| 651 |
then loop over the remaining ACLS. |
| 652 |
""" |
| 653 |
# split ACLs into four categories |
| 654 |
acls_groups = {} |
| 655 |
preferred_group = (None, None) |
| 656 |
special_accesses = [] |
| 657 |
for acl in self.parameters.get('acls', []): |
| 658 |
access_control = acl.get('access_control', 'file_directory') |
| 659 |
propagation_mode = acl.get('propagation_mode', 'propagate') |
| 660 |
if access_control not in acls_groups: |
| 661 |
acls_groups[access_control] = {} |
| 662 |
if propagation_mode not in acls_groups[access_control]: |
| 663 |
acls_groups[access_control][propagation_mode] = [] |
| 664 |
acls_groups[access_control][propagation_mode].append(acl) |
| 665 |
access = acl.get('access') |
| 666 |
if access not in ('access_allow', 'access_deny', 'audit_success', 'audit_failure'): |
| 667 |
if preferred_group == (None, None): |
| 668 |
preferred_group = (access_control, propagation_mode) |
| 669 |
if preferred_group != (access_control, propagation_mode): |
| 670 |
self.module.fail_json(msg="Error: acl %s with access %s conflicts with other ACLs using accesses: %s " |
| 671 |
"with different access_control or propagation_mode: %s." |
| 672 |
% (acl, access, special_accesses, preferred_group)) |
| 673 |
special_accesses.append(access) |
| 674 |
|
| 675 |
if preferred_group == (None, None): |
| 676 |
# find a non empty list of ACLs |
| 677 |
# use sorted to make this deterministic |
| 678 |
for acc_key, acc_value in sorted(acls_groups.items()): |
| 679 |
for prop_key, prop_value in sorted(acc_value.items()): |
| 680 |
if prop_value: |
| 681 |
preferred_group = (acc_key, prop_key) |
| 682 |
break |
| 683 |
if preferred_group != (None, None): |
| 684 |
break |
| 685 |
|
| 686 |
# keep one category for create, and move the remaining ACLs into post-acls |
| 687 |
create_acls = [] |
| 688 |
acl_actions = {'patch-acls': [], 'post-acls': [], 'delete-acls': []} |
| 689 |
# use sorted to make this deterministic |
| 690 |
for acc_key, acc_value in sorted(acls_groups.items()): |
| 691 |
for prop_key, prop_value in sorted(acc_value.items()): |
| 692 |
if (acc_key, prop_key) == preferred_group: |
| 693 |
create_acls = prop_value |
| 694 |
self.parameters['access_control'] = acc_key |
| 695 |
self.parameters['propagation_mode'] = prop_key |
| 696 |
elif prop_value: |
| 697 |
acl_actions['post-acls'].extend(prop_value) |
| 698 |
self.parameters['acls'] = create_acls |
| 699 |
return acl_actions |
| 700 |
|
| 701 |
def get_actions(self): |
| 702 |
current = self.get_file_security_permissions() |
| 703 |
cd_action = self.na_helper.get_cd_action(current, self.parameters) |
| 704 |
modify, acl_actions = self.get_modify_actions(current) if cd_action is None else (None, {}) |
| 705 |
if cd_action == 'create' and self.parameters.get('access_control') is None: |
| 706 |
acl_actions = self.get_acl_actions_on_create() |
| 707 |
if cd_action == 'delete': |
| 708 |
# delete is not supported by the API, or rather a DELETE will only delete the SLAG ACLs and nothing else. |
| 709 |
# so we just loop through all the ACLs |
| 710 |
acl_actions = self.get_acl_actions_on_delete(current) |
| 711 |
cd_action = None |
| 712 |
return cd_action, modify, acl_actions |
| 713 |
|
| 714 |
def apply(self): |
| 715 |
|
| 716 |
self.get_svm_uuid() |
| 717 |
cd_action, modify, acl_actions = self.get_actions() |
| 718 |
if self.na_helper.changed and not self.module.check_mode: |
| 719 |
if cd_action == 'create': |
| 720 |
self.create_file_security_permissions() |
| 721 |
if modify: |
| 722 |
self.modify_file_security_permissions(modify) |
| 723 |
# delete ACLs first, to avoid conflicts with new or modified rules |
| 724 |
for delete_acl in acl_actions.get('delete-acls', []): |
| 725 |
self.delete_file_security_permissions_acl(delete_acl) |
| 726 |
# PATCH call succeeds, but its not working: changes are not reflecting |
| 727 |
# modify before adding new rules to avoid conflicts |
| 728 |
for patch_acl in acl_actions.get('patch-acls', []): |
| 729 |
self.modify_file_security_permissions_acl(patch_acl) |
| 730 |
for post_acl in acl_actions.get('post-acls', []): |
| 731 |
self.add_file_security_permissions_acl(post_acl) |
| 732 |
changed = self.na_helper.changed |
| 733 |
self.validate_changes(cd_action, modify) |
| 734 |
self.na_helper.changed = changed |
| 735 |
result = netapp_utils.generate_result(self.na_helper.changed, cd_action, modify) |
| 736 |
self.module.exit_json(**result) |
| 737 |
|
| 738 |
def validate_changes(self, cd_action, modify): |
| 739 |
if self.parameters['validate_changes'] == 'ignore': |
| 740 |
return |
| 741 |
new_cd_action, new_modify, acl_actions = self.get_actions() |
| 742 |
errors = [] |
| 743 |
if new_cd_action is not None: |
| 744 |
errors.append('%s still required after %s (with modify: %s)' % (new_cd_action, cd_action, modify)) |
| 745 |
if new_modify: |
| 746 |
errors.append('modify: %s still required after %s' % (new_modify, modify)) |
| 747 |
# changes in ACLs |
| 748 |
errors.extend('%s still required for %s' % (key, value) for key, value in acl_actions.items() if value) |
| 749 |
if errors: |
| 750 |
msg = 'Error - %s' % ' - '.join(errors) |
| 751 |
if self.parameters['validate_changes'] == 'error': |
| 752 |
self.module.fail_json(msg=msg) |
| 753 |
if self.parameters['validate_changes'] == 'warn': |
| 754 |
self.module.warn(msg) |
| 755 |
|
| 756 |
|
| 757 |
def main(): |
| 758 |
"""Apply volume operations from playbook""" |
| 759 |
obj = NetAppOntapFileSecurityPermissions() |
| 760 |
obj.apply() |
| 761 |
|
| 762 |
|
| 763 |
if __name__ == '__main__': |
| 764 |
main() |