diff --git a/README.md b/README.md index 81878e2..5c2f86e 100644 --- a/README.md +++ b/README.md @@ -184,7 +184,6 @@ Rule | Description [212](https://github.com/warpnet/salt-lint/wiki/212) | Most files should not contain irregular spaces [213](https://github.com/warpnet/salt-lint/wiki/213) | SaltStack recommends using `cmd.run` together with `onchanges`, rather than `cmd.wait` [214](https://github.com/warpnet/salt-lint/wiki/214) | SLS file with a period in the name (besides the suffix period) can not be referenced -[215](https://github.com/warpnet/salt-lint/wiki/215) | Using `replace: False` is required when not specifying content ### Jinja diff --git a/saltlint/rules/FileManagedReplaceContentRule.py b/saltlint/rules/FileManagedReplaceContentRule.py deleted file mode 100644 index 5e56bca..0000000 --- a/saltlint/rules/FileManagedReplaceContentRule.py +++ /dev/null @@ -1,54 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2020 Warpnet B.V. - -import re -from saltlint.linter.rule import Rule -from saltlint.utils import get_rule_skips_from_text -from saltlint.utils import LANGUAGE_SLS - - -class FileManagedReplaceContentRule(Rule): - id = '215' - shortdesc = "Using 'replace: False' is required when not specifying content" - description = "Using 'replace: False' is required when not specifying content" - - severity = 'HIGH' - languages = [LANGUAGE_SLS] - tags = ['formatting'] - version_added = 'develop' - - # Find the full file.managed state - regex = re.compile(r"^\s{2}file\.managed:.*(?:\n\s{4}.+)*", re.MULTILINE) - # Regex for finding the content source option - regex_options = re.compile( - r"^\s{4}-\s(?:source:|contents:|contents_pillar:|contents_grains:|replace:\s[F|f]alse).*$", - re.MULTILINE - ) - - def matchtext(self, file, text): - results = [] - - # Find all file.managed states in the specified sls file - for match in re.finditer(self.regex, text): - # Continue if the file.managed state includes a content source - # or replace is set to False - if re.search(self.regex_options, match.group(0)): - continue - - # Get the location of the regex match - start = match.start() - end = match.end() - - # Get the line number of the first character - lines = text[:start].splitlines() - line_no = len(lines) + 1 - - # Skip result if noqa for this rule ID is found in section - section = text[start:end] - if self.id in get_rule_skips_from_text(section): - continue - - # Append the match to the results - results.append((line_no, section.splitlines()[0], self.shortdesc)) - - return results diff --git a/tests/unit/TestFileManagedReplaceContentRule.py b/tests/unit/TestFileManagedReplaceContentRule.py deleted file mode 100644 index 76d453e..0000000 --- a/tests/unit/TestFileManagedReplaceContentRule.py +++ /dev/null @@ -1,107 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2020 Warpnet B.V. - -import unittest - -from saltlint.linter.collection import RulesCollection -from saltlint.rules.FileManagedReplaceContentRule import FileManagedReplaceContentRule -from tests import RunFromText - - -GOOD_FILE_STATE = ''' -cis_grub.cfg: - file.managed: - - name: /boot/grub.cfg - - user: root - - group: root - - mode: '0700' - - source: salt://grub/files/grub.cfg - -cis_systemid_only_set_once: - file.managed: - - name: /tmp/systemid - - user: root - - group: root - - replace: False - - contents_grains: osmajorrelease - -user: - user.present: - - name: "salt-lint" - file.managed: - - name: /user/salt-lint/.bashrc - - user: root - - group: root - - mode: '0700' - - contents_pillar: bashrc - -cis_grub.cfg_managerights: - file.managed: - - name: /boot/grub.cfg - - user: root - - group: root - - mode: '0700' - - replace: False - -cis_grub_permissions: - file.managed: - - name: /boot/grub.cfg - - replace: false - - user: root - - group: root - - mode: '0700' -''' - -BAD_FILE_STATE = ''' -cis_grub.cfg: - file.managed: - - name: /boot/grub.cfg - - user: root - - group: root - - mode: '0700' - -cis_systemid_only_set_once: - file.managed: - - name: /tmp/systemid - - user: root - - group: root - - replace: True - -user: - user.present: - - name: "salt-lint" - file.managed: - - name: /user/salt-lint/.bashrc - - user: root - - group: root - - mode: '0700' - -cis_grub_permissions: - file.managed: # noqa: 215 - - name: /boot/grub.cfg - - user: root - - group: root - - mode: '0700' -''' - - -class TestFileManagedReplaceContentRule(unittest.TestCase): - collection = RulesCollection() - - def setUp(self): - self.collection.register(FileManagedReplaceContentRule()) - - def test_statement_positive(self): - runner = RunFromText(self.collection) - results = runner.run_state(GOOD_FILE_STATE) - self.assertEqual(0, len(results)) - - def test_statement_negative(self): - runner = RunFromText(self.collection) - results = runner.run_state(BAD_FILE_STATE) - self.assertEqual(3, len(results)) - - # Check line numbers of the results - self.assertEqual(3, results[0].linenumber) - self.assertEqual(10, results[1].linenumber) - self.assertEqual(19, results[2].linenumber)