From 4b99afa8192f103ca28a6e16cafe3db3bceecf03 Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Wed, 13 Mar 2019 17:27:39 -0700 Subject: [PATCH 1/4] Fixing the output when there are changes for the ACL state so the permissions are shown and not the octal number. --- salt/states/linux_acl.py | 13 +++++++++++-- tests/unit/states/test_linux_acl.py | 18 +++++++++--------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/salt/states/linux_acl.py b/salt/states/linux_acl.py index 38ff1c083016..7f68f3ebc5b5 100644 --- a/salt/states/linux_acl.py +++ b/salt/states/linux_acl.py @@ -29,6 +29,7 @@ # Import Python libs from __future__ import absolute_import, print_function, unicode_literals +import logging import os # Import salt libs @@ -36,6 +37,8 @@ from salt.exceptions import CommandExecutionError import salt.utils.path +log = logging.getLogger(__name__) + __virtualname__ = 'acl' @@ -60,6 +63,11 @@ def present(name, acl_type, acl_name='', perms='', recurse=False): 'comment': ''} _octal = {'r': 4, 'w': 2, 'x': 1, '-': 0} + _octal_lookup = {'1': 'x', + '2': 'w', + '4': 'r', + '5': 'rx', + '7': 'rwx'} if not os.path.exists(name): ret['comment'] = '{0} does not exist'.format(name) @@ -111,18 +119,19 @@ def present(name, acl_type, acl_name='', perms='', recurse=False): if not need_refresh: ret['comment'] = 'Permissions are in the desired state' else: + new_perms = _octal_lookup[six.text_type(user[_search_name]['octal'])] changes = {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, - 'perms': six.text_type(user[_search_name]['octal'])}} + 'perms': new_perms}} if __opts__['test']: ret.update({'comment': 'Updated permissions will be applied for ' '{0}: {1} -> {2}'.format( acl_name, - six.text_type(user[_search_name]['octal']), + new_perms, perms), 'result': None, 'pchanges': changes}) return ret diff --git a/tests/unit/states/test_linux_acl.py b/tests/unit/states/test_linux_acl.py index 168ce4b76251..cf4c51005936 100644 --- a/tests/unit/states/test_linux_acl.py +++ b/tests/unit/states/test_linux_acl.py @@ -43,17 +43,17 @@ def test_present(self): perms = 'rwx' mock = MagicMock(side_effect=[{name: {acl_type: [{acl_name: - {'octal': 'A'}}]}}, + {'octal': 5}}]}}, {name: {acl_type: [{acl_name: - {'octal': 'A'}}]}}, + {'octal': 5}}]}}, {name: {acl_type: [{acl_name: - {'octal': 'A'}}]}}, + {'octal': 5}}]}}, {name: {acl_type: [{}]}}, {name: {acl_type: [{}]}}, {name: {acl_type: [{}]}}, { name: {acl_type: [{acl_name: {'octal': 7}}]}, - name+"/foo": {acl_type: [{acl_name: {'octal': 'A'}}]} + name+"/foo": {acl_type: [{acl_name: {'octal': 5}}]} }, { name: {acl_type: [{acl_name: {'octal': 7}}]}, @@ -65,7 +65,7 @@ def test_present(self): with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}): # Update - test=True with patch.dict(linux_acl.__opts__, {'test': True}): - comt = ('Updated permissions will be applied for {0}: A -> {1}' + comt = ('Updated permissions will be applied for {0}: rx -> {1}' ''.format(acl_name, perms)) ret = {'name': name, 'comment': comt, @@ -75,7 +75,7 @@ def test_present(self): 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, - 'perms': 'A'}}, + 'perms': 'rx'}}, 'result': None} self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, @@ -91,7 +91,7 @@ def test_present(self): 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, - 'perms': 'A'}}, + 'perms': 'rx'}}, 'pchanges': {}, 'result': True} self.assertDictEqual(linux_acl.present(name, acl_type, @@ -159,7 +159,7 @@ def test_present(self): with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}): # Update - test=True with patch.dict(linux_acl.__opts__, {'test': True}): - comt = ('Updated permissions will be applied for {0}: 7 -> {1}' + comt = ('Updated permissions will be applied for {0}: rwx -> {1}' ''.format(acl_name, perms)) ret = {'name': name, 'comment': comt, @@ -169,7 +169,7 @@ def test_present(self): 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, - 'perms': '7'}}, + 'perms': 'rwx'}}, 'result': None} self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, From 24c907be017e01da6047eb8fdef94b6b2f860456 Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Wed, 13 Mar 2019 17:48:31 -0700 Subject: [PATCH 2/4] Adding additional permissions to the lookup. --- salt/states/linux_acl.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/salt/states/linux_acl.py b/salt/states/linux_acl.py index 7f68f3ebc5b5..76b1c86c8bed 100644 --- a/salt/states/linux_acl.py +++ b/salt/states/linux_acl.py @@ -63,10 +63,13 @@ def present(name, acl_type, acl_name='', perms='', recurse=False): 'comment': ''} _octal = {'r': 4, 'w': 2, 'x': 1, '-': 0} - _octal_lookup = {'1': 'x', + _octal_lookup = {'0': '-', + '1': 'x', '2': 'w', + '3': 'wx', '4': 'r', '5': 'rx', + '6': 'rw', '7': 'rwx'} if not os.path.exists(name): From bfdb6691ffb02e71f8c35969c0ede69f278424d3 Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Thu, 14 Mar 2019 10:21:42 -0700 Subject: [PATCH 3/4] Updating the reverse octal lookup dictionary. Updating tests. --- salt/states/linux_acl.py | 14 +++++--------- tests/unit/states/test_linux_acl.py | 6 +++--- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/salt/states/linux_acl.py b/salt/states/linux_acl.py index 76b1c86c8bed..72fa1266d766 100644 --- a/salt/states/linux_acl.py +++ b/salt/states/linux_acl.py @@ -63,14 +63,7 @@ def present(name, acl_type, acl_name='', perms='', recurse=False): 'comment': ''} _octal = {'r': 4, 'w': 2, 'x': 1, '-': 0} - _octal_lookup = {'0': '-', - '1': 'x', - '2': 'w', - '3': 'wx', - '4': 'r', - '5': 'rx', - '6': 'rw', - '7': 'rwx'} + _octal_lookup = {0: '-', 1: 'r', 2: 'w', 4: 'x'} if not os.path.exists(name): ret['comment'] = '{0} does not exist'.format(name) @@ -122,7 +115,10 @@ def present(name, acl_type, acl_name='', perms='', recurse=False): if not need_refresh: ret['comment'] = 'Permissions are in the desired state' else: - new_perms = _octal_lookup[six.text_type(user[_search_name]['octal'])] + _num = user[_search_name]['octal'] + new_perms = '{}{}{}'.format(_octal_lookup[_num&1], + _octal_lookup[_num&2], + _octal_lookup[_num&4]) changes = {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms}, diff --git a/tests/unit/states/test_linux_acl.py b/tests/unit/states/test_linux_acl.py index cf4c51005936..8d60f9c80660 100644 --- a/tests/unit/states/test_linux_acl.py +++ b/tests/unit/states/test_linux_acl.py @@ -65,7 +65,7 @@ def test_present(self): with patch.dict(linux_acl.__salt__, {'acl.getfacl': mock}): # Update - test=True with patch.dict(linux_acl.__opts__, {'test': True}): - comt = ('Updated permissions will be applied for {0}: rx -> {1}' + comt = ('Updated permissions will be applied for {0}: r-x -> {1}' ''.format(acl_name, perms)) ret = {'name': name, 'comment': comt, @@ -75,7 +75,7 @@ def test_present(self): 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, - 'perms': 'rx'}}, + 'perms': 'r-x'}}, 'result': None} self.assertDictEqual(linux_acl.present(name, acl_type, acl_name, @@ -91,7 +91,7 @@ def test_present(self): 'perms': perms}, 'old': {'acl_name': acl_name, 'acl_type': acl_type, - 'perms': 'rx'}}, + 'perms': 'r-x'}}, 'pchanges': {}, 'result': True} self.assertDictEqual(linux_acl.present(name, acl_type, From 889660f9840ad5c679bd84f221571312e4e47dd6 Mon Sep 17 00:00:00 2001 From: "Gareth J. Greenaway" Date: Thu, 14 Mar 2019 16:21:47 -0700 Subject: [PATCH 4/4] Fixing lint. --- salt/states/linux_acl.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/salt/states/linux_acl.py b/salt/states/linux_acl.py index 72fa1266d766..f7408514b75d 100644 --- a/salt/states/linux_acl.py +++ b/salt/states/linux_acl.py @@ -116,9 +116,9 @@ def present(name, acl_type, acl_name='', perms='', recurse=False): ret['comment'] = 'Permissions are in the desired state' else: _num = user[_search_name]['octal'] - new_perms = '{}{}{}'.format(_octal_lookup[_num&1], - _octal_lookup[_num&2], - _octal_lookup[_num&4]) + new_perms = '{}{}{}'.format(_octal_lookup[_num & 1], + _octal_lookup[_num & 2], + _octal_lookup[_num & 4]) changes = {'new': {'acl_name': acl_name, 'acl_type': acl_type, 'perms': perms},