Skip to content

Commit

Permalink
Make risky-file-permissions rule does not ignore FQCN (#1528)
Browse files Browse the repository at this point in the history
Address the bug that the risky-file-permissions rule ignores the modules
passed as FQCN like ansible.builtin.copy.

Signed-Off-By: Satoru SATOH <satoru.satoh@gmail.com>
  • Loading branch information
ssato committed May 21, 2021
1 parent 8bef056 commit 49d0321
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
47 changes: 30 additions & 17 deletions src/ansiblelint/rules/MissingFilePermissionsRule.py
Expand Up @@ -17,7 +17,7 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from typing import TYPE_CHECKING, Any, Dict, Union
from typing import TYPE_CHECKING, Any, Dict, Set, Union

from ansiblelint.rules import AnsibleLintRule

Expand All @@ -33,6 +33,33 @@
'template',
)

_MODULES: Set[str] = {
'archive',
'community.general.archive',
'assemble',
'ansible.builtin.assemble',
'copy', # supports preserve
'ansible.builtin.copy',
'file',
'ansible.builtin.file',
'replace', # implicit preserve behavior but mode: preserve is invalid
'ansible.builtin.replace',
'template', # supports preserve
'ansible.builtin.template',
# 'unarchive', # disabled because .tar.gz files can have permissions inside
}

_MODULES_WITH_CREATE: Dict[str, bool] = {
'blockinfile': False,
'ansible.builtin.blockinfile': False,
'htpasswd': True,
'community.general.htpasswd': True,
'ini_file': True,
'community.general.ini_file': True,
'lineinfile': False,
'ansible.builtin.lineinfile': False,
}


class MissingFilePermissionsRule(AnsibleLintRule):
id = "risky-file-permissions"
Expand All @@ -49,22 +76,8 @@ class MissingFilePermissionsRule(AnsibleLintRule):
tags = ['unpredictability', 'experimental']
version_added = 'v4.3.0'

_modules = {
'archive',
'assemble',
'copy', # supports preserve
'file',
'replace', # implicit preserve behavior but mode: preserve is invalid
'template', # supports preserve
# 'unarchive', # disabled because .tar.gz files can have permissions inside
}

_modules_with_create = {
'blockinfile': False,
'htpasswd': True,
'ini_file': True,
'lineinfile': False,
}
_modules = _MODULES
_modules_with_create = _MODULES_WITH_CREATE

def matchtask(
self, task: Dict[str, Any], file: 'Optional[Lintable]' = None
Expand Down
21 changes: 20 additions & 1 deletion test/TestMissingFilePermissionsRule.py
Expand Up @@ -54,6 +54,15 @@
file:
state: directory
recurse: yes
- name: permissions not missing and numeric (fqcn)
ansible.builtin.file:
path: bar
mode: 755
- name: file edit when create is false (fqcn)
ansible.builtin.lineinfile:
path: foo
create: false
line: some content here
'''

FAIL_TASKS = '''\
Expand Down Expand Up @@ -90,6 +99,14 @@
# path: foo
# create: true
# mode: preserve
- name: permissions are missing (fqcn)
ansible.builtin.file:
path: bar
- name: lineinfile when create is true (fqcn)
ansible.builtin.lineinfile:
path: foo
create: true
line: some content here
'''


Expand All @@ -108,11 +125,13 @@ def test_success(rule_runner) -> None:
def test_fail(rule_runner) -> None:
"""Validate that missing mode triggers the rule."""
results = rule_runner.run_playbook(FAIL_TASKS)
assert len(results) == 5
assert len(results) == 7
assert results[0].linenumber == 4
assert results[1].linenumber == 8
assert results[2].linenumber == 12
# assert results[3].linenumber == 16
assert results[3].linenumber == 20
assert results[4].linenumber == 25
# assert results[6].linenumber == 29
assert results[5].linenumber == 34
assert results[6].linenumber == 37

0 comments on commit 49d0321

Please sign in to comment.