|
|
9be3b2 |
From bf018e9f8327b231b967db8ec74fabf01802b6a8 Mon Sep 17 00:00:00 2001
|
|
|
9be3b2 |
From: Watson Sato <wsato@redhat.com>
|
|
|
9be3b2 |
Date: Wed, 11 Aug 2021 09:45:04 +0200
|
|
|
9be3b2 |
Subject: [PATCH 1/3] Add test for ansible files removed and readded
|
|
|
9be3b2 |
|
|
|
9be3b2 |
Check if any playbook removes a file and then add it back again.
|
|
|
9be3b2 |
The file removal is based on the 'file' module with 'state: absent', and
|
|
|
9be3b2 |
the reintroduction of the file is based on 'lineinfile', 'blockinfile'
|
|
|
9be3b2 |
and 'copy' modules.
|
|
|
9be3b2 |
---
|
|
|
9be3b2 |
CMakeLists.txt | 2 +
|
|
|
9be3b2 |
tests/CMakeLists.txt | 8 ++
|
|
|
9be3b2 |
tests/test_ansible_file_removed_and_added.py | 97 ++++++++++++++++++++
|
|
|
9be3b2 |
3 files changed, 107 insertions(+)
|
|
|
9be3b2 |
create mode 100644 tests/test_ansible_file_removed_and_added.py
|
|
|
9be3b2 |
|
|
|
9be3b2 |
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
|
|
9be3b2 |
index 330b869d0f9..e41f2caa630 100644
|
|
|
9be3b2 |
--- a/CMakeLists.txt
|
|
|
9be3b2 |
+++ b/CMakeLists.txt
|
|
|
9be3b2 |
@@ -129,6 +129,7 @@ find_python_module(jinja2 REQUIRED)
|
|
|
9be3b2 |
find_python_module(pytest)
|
|
|
9be3b2 |
find_python_module(pytest_cov)
|
|
|
9be3b2 |
find_python_module(json2html)
|
|
|
9be3b2 |
+find_python_module(yamlpath)
|
|
|
9be3b2 |
|
|
|
9be3b2 |
# sphinx documentation requirements
|
|
|
9be3b2 |
find_python_module(sphinx)
|
|
|
9be3b2 |
@@ -231,6 +232,7 @@ message(STATUS "python pytest module (optional): ${PY_PYTEST}")
|
|
|
9be3b2 |
message(STATUS "ansible-playbook module (optional): ${ANSIBLE_PLAYBOOK_EXECUTABLE}")
|
|
|
9be3b2 |
message(STATUS "ansible-lint module (optional): ${ANSIBLE_LINT_EXECUTABLE}")
|
|
|
9be3b2 |
message(STATUS "yamllint module (optional): ${YAMLLINT_EXECUTABLE}")
|
|
|
9be3b2 |
+message(STATUS "yamlpath module (optional): ${PY_YAMLPATH}")
|
|
|
9be3b2 |
message(STATUS "BATS framework (optional): ${BATS_EXECUTABLE}")
|
|
|
9be3b2 |
message(STATUS "python sphinx module (optional): ${PY_SPHINX}")
|
|
|
9be3b2 |
message(STATUS "python sphinxcontrib.autojinja module (optional): ${PY_SPHINXCONTRIB.AUTOJINJA}")
|
|
|
9be3b2 |
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
|
|
|
9be3b2 |
index 3e2d8a4ec31..739cc124035 100644
|
|
|
9be3b2 |
--- a/tests/CMakeLists.txt
|
|
|
9be3b2 |
+++ b/tests/CMakeLists.txt
|
|
|
9be3b2 |
@@ -121,3 +121,11 @@ add_test(
|
|
|
9be3b2 |
)
|
|
|
9be3b2 |
set_tests_properties("fix_rules-sort_subkeys" PROPERTIES LABELS quick)
|
|
|
9be3b2 |
set_tests_properties("fix_rules-sort_subkeys" PROPERTIES DEPENDS "test-rule-dir-json")
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+if (PY_YAMLPATH)
|
|
|
9be3b2 |
+ add_test(
|
|
|
9be3b2 |
+ NAME "ansible-file-removed-and-added"
|
|
|
9be3b2 |
+ COMMAND env "PYTHONPATH=$ENV{PYTHONPATH}" "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/test_ansible_file_removed_and_added.py" --ansible_dir "${CMAKE_BINARY_DIR}/ansible"
|
|
|
9be3b2 |
+ )
|
|
|
9be3b2 |
+ set_tests_properties("fix_rules-sort_subkeys" PROPERTIES LABELS quick)
|
|
|
9be3b2 |
+endif()
|
|
|
9be3b2 |
diff --git a/tests/test_ansible_file_removed_and_added.py b/tests/test_ansible_file_removed_and_added.py
|
|
|
9be3b2 |
new file mode 100644
|
|
|
9be3b2 |
index 00000000000..23f6f888bda
|
|
|
9be3b2 |
--- /dev/null
|
|
|
9be3b2 |
+++ b/tests/test_ansible_file_removed_and_added.py
|
|
|
9be3b2 |
@@ -0,0 +1,97 @@
|
|
|
9be3b2 |
+#!/usr/bin/python3
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+import argparse
|
|
|
9be3b2 |
+import os
|
|
|
9be3b2 |
+import sys
|
|
|
9be3b2 |
+from types import SimpleNamespace
|
|
|
9be3b2 |
+from yamlpath import Processor
|
|
|
9be3b2 |
+from yamlpath import YAMLPath
|
|
|
9be3b2 |
+from yamlpath.common import Parsers
|
|
|
9be3b2 |
+from yamlpath.exceptions import YAMLPathException
|
|
|
9be3b2 |
+from yamlpath.wrappers import ConsolePrinter
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def parse_command_line_args():
|
|
|
9be3b2 |
+ parser = argparse.ArgumentParser(
|
|
|
9be3b2 |
+ description="Checks if an Ansible Playbook removes a file and then adds it again.")
|
|
|
9be3b2 |
+ parser.add_argument("--ansible_dir", required=True,
|
|
|
9be3b2 |
+ help="Directory containing Ansible Playbooks")
|
|
|
9be3b2 |
+ args = parser.parse_args()
|
|
|
9be3b2 |
+ return args
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def check_playbook_file_removed_and_added(playbook_path):
|
|
|
9be3b2 |
+ playbook_ok = True
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ yaml_parser = Parsers.get_yaml_editor()
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ logging_args = SimpleNamespace(quiet=False, verbose=False, debug=False)
|
|
|
9be3b2 |
+ log = ConsolePrinter(logging_args)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ # Find every path removed by a file Task (also matches tasks within blocks)
|
|
|
9be3b2 |
+ files_absent_string = "tasks.**.file[state=absent][parent()].path"
|
|
|
9be3b2 |
+ files_absent_yamlpath = YAMLPath(files_absent_string)
|
|
|
9be3b2 |
+ path_editing_tasks_yamlpath = ""
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ log.info("Info: Evaluating playbook '{}'".format(playbook_path))
|
|
|
9be3b2 |
+ (yaml_data, doc_loaded) = Parsers.get_yaml_data(yaml_parser, log, playbook_path)
|
|
|
9be3b2 |
+ if not doc_loaded:
|
|
|
9be3b2 |
+ # There was an issue loading the file; an error message has already been
|
|
|
9be3b2 |
+ # printed via ConsolePrinter.
|
|
|
9be3b2 |
+ return False
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ processor = Processor(log, yaml_data)
|
|
|
9be3b2 |
+ try:
|
|
|
9be3b2 |
+ for node in processor.get_nodes(files_absent_yamlpath, mustexist=False):
|
|
|
9be3b2 |
+ path = str(node)
|
|
|
9be3b2 |
+ # 'node' is a NodeCoords.
|
|
|
9be3b2 |
+ if path == 'None':
|
|
|
9be3b2 |
+ continue
|
|
|
9be3b2 |
+ elif "{{" in path:
|
|
|
9be3b2 |
+ # Identified path is a Jinja expression, unfortunately there is no easy way to get
|
|
|
9be3b2 |
+ # the actual path without making this test very complicated
|
|
|
9be3b2 |
+ continue
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ # Check if this paths is used in any of the following ansible modules
|
|
|
9be3b2 |
+ ansible_modules = ["lineinfile", "blockinfile", "copy"]
|
|
|
9be3b2 |
+ path_editing_tasks_string = "tasks.**.[.=~/{modules}/][*='{path}'][parent()].name"
|
|
|
9be3b2 |
+ path_editing_tasks_yamlpath = YAMLPath(path_editing_tasks_string.format(
|
|
|
9be3b2 |
+ modules="|".join(ansible_modules),
|
|
|
9be3b2 |
+ path=node)
|
|
|
9be3b2 |
+ )
|
|
|
9be3b2 |
+ for task in processor.get_nodes(path_editing_tasks_yamlpath, mustexist=False):
|
|
|
9be3b2 |
+ log.info("Error: Task '{}' manipulates a file that is removed by another task"
|
|
|
9be3b2 |
+ .format(task))
|
|
|
9be3b2 |
+ playbook_ok = False
|
|
|
9be3b2 |
+ except YAMLPathException as ex:
|
|
|
9be3b2 |
+ no_file_msg = ("Cannot add PathSegmentTypes.TRAVERSE subreference to lists at 'None' "
|
|
|
9be3b2 |
+ "in '{}'.")
|
|
|
9be3b2 |
+ if str(ex) == no_file_msg.format(files_absent_string):
|
|
|
9be3b2 |
+ log.info("Info: Playbook {} has no 'file' tasks.".format(playbook_path))
|
|
|
9be3b2 |
+ elif path_editing_tasks_yamlpath and str(ex) == no_file_msg.format(
|
|
|
9be3b2 |
+ path_editing_tasks_yamlpath):
|
|
|
9be3b2 |
+ log.info("Info: Playbook {} has no '{}' tasks.".format(
|
|
|
9be3b2 |
+ playbook_path, " ".join(ansible_modules)))
|
|
|
9be3b2 |
+ else:
|
|
|
9be3b2 |
+ log.info("Error: {}.".format(ex))
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ return playbook_ok
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def main():
|
|
|
9be3b2 |
+ args = parse_command_line_args()
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ all_playbooks_ok = True
|
|
|
9be3b2 |
+ for dir_item in os.listdir(args.ansible_dir):
|
|
|
9be3b2 |
+ if dir_item.endswith(".yml"):
|
|
|
9be3b2 |
+ playbook_path = os.path.join(args.ansible_dir, dir_item)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ if not check_playbook_file_removed_and_added(playbook_path):
|
|
|
9be3b2 |
+ all_playbooks_ok = False
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ if not all_playbooks_ok:
|
|
|
9be3b2 |
+ sys.exit(1)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+if __name__ == "__main__":
|
|
|
9be3b2 |
+ main()
|
|
|
9be3b2 |
|
|
|
9be3b2 |
From e6d727762ba446cad94f1e002fa7a7fef0f1a4cb Mon Sep 17 00:00:00 2001
|
|
|
9be3b2 |
From: Watson Sato <wsato@redhat.com>
|
|
|
9be3b2 |
Date: Wed, 11 Aug 2021 09:48:14 +0200
|
|
|
9be3b2 |
Subject: [PATCH 2/3] Unit tests the function for file removed and added
|
|
|
9be3b2 |
|
|
|
9be3b2 |
Add a unit test for the core function that checks if any playbook
|
|
|
9be3b2 |
removes a file and then reintroduces it back.
|
|
|
9be3b2 |
---
|
|
|
9be3b2 |
tests/CMakeLists.txt | 6 ++
|
|
|
9be3b2 |
.../file_block_removed_and_added.yml | 69 +++++++++++++++++++
|
|
|
9be3b2 |
.../file_not_removed_and_added.yml | 49 +++++++++++++
|
|
|
9be3b2 |
.../file_removed_and_added.yml | 62 +++++++++++++++++
|
|
|
9be3b2 |
.../file_removed_and_not_added.yml | 46 +++++++++++++
|
|
|
9be3b2 |
...t_check_playbook_file_removed_and_added.py | 39 +++++++++++
|
|
|
9be3b2 |
6 files changed, 271 insertions(+)
|
|
|
9be3b2 |
create mode 100644 tests/ansible_file_removed_and_added/file_block_removed_and_added.yml
|
|
|
9be3b2 |
create mode 100644 tests/ansible_file_removed_and_added/file_not_removed_and_added.yml
|
|
|
9be3b2 |
create mode 100644 tests/ansible_file_removed_and_added/file_removed_and_added.yml
|
|
|
9be3b2 |
create mode 100644 tests/ansible_file_removed_and_added/file_removed_and_not_added.yml
|
|
|
9be3b2 |
create mode 100644 tests/test_check_playbook_file_removed_and_added.py
|
|
|
9be3b2 |
|
|
|
9be3b2 |
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
|
|
|
9be3b2 |
index 739cc124035..000a1b1385d 100644
|
|
|
9be3b2 |
--- a/tests/CMakeLists.txt
|
|
|
9be3b2 |
+++ b/tests/CMakeLists.txt
|
|
|
9be3b2 |
@@ -123,6 +123,12 @@ set_tests_properties("fix_rules-sort_subkeys" PROPERTIES LABELS quick)
|
|
|
9be3b2 |
set_tests_properties("fix_rules-sort_subkeys" PROPERTIES DEPENDS "test-rule-dir-json")
|
|
|
9be3b2 |
|
|
|
9be3b2 |
if (PY_YAMLPATH)
|
|
|
9be3b2 |
+ if (PY_PYTEST)
|
|
|
9be3b2 |
+ add_test(
|
|
|
9be3b2 |
+ NAME "test-function-check_playbook_file_removed_and_added"
|
|
|
9be3b2 |
+ COMMAND "${PYTHON_EXECUTABLE}" -m pytest ${PYTEST_COVERAGE_OPTIONS} "${CMAKE_CURRENT_SOURCE_DIR}/test_check_playbook_file_removed_and_added.py"
|
|
|
9be3b2 |
+ )
|
|
|
9be3b2 |
+ endif()
|
|
|
9be3b2 |
add_test(
|
|
|
9be3b2 |
NAME "ansible-file-removed-and-added"
|
|
|
9be3b2 |
COMMAND env "PYTHONPATH=$ENV{PYTHONPATH}" "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/test_ansible_file_removed_and_added.py" --ansible_dir "${CMAKE_BINARY_DIR}/ansible"
|
|
|
9be3b2 |
diff --git a/tests/ansible_file_removed_and_added/file_block_removed_and_added.yml b/tests/ansible_file_removed_and_added/file_block_removed_and_added.yml
|
|
|
9be3b2 |
new file mode 100644
|
|
|
9be3b2 |
index 00000000000..8863b333129
|
|
|
9be3b2 |
--- /dev/null
|
|
|
9be3b2 |
+++ b/tests/ansible_file_removed_and_added/file_block_removed_and_added.yml
|
|
|
9be3b2 |
@@ -0,0 +1,69 @@
|
|
|
9be3b2 |
+---
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+- hosts: all
|
|
|
9be3b2 |
+ vars:
|
|
|
9be3b2 |
+ var_system_crypto_policy: !!str FUTURE
|
|
|
9be3b2 |
+ var_sudo_logfile: !!str /var/log/sudo.log
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ tasks:
|
|
|
9be3b2 |
+ - name: Modify the System Login Banner - add correct banner
|
|
|
9be3b2 |
+ lineinfile:
|
|
|
9be3b2 |
+ dest: /etc/issue
|
|
|
9be3b2 |
+ line: '{{ login_banner_text | regex_replace("^\^(.*)\$$", "\1") | regex_replace("^\((.*)\|.*\)$",
|
|
|
9be3b2 |
+ "\1") | regex_replace("\[\\s\\n\]\+"," ") | regex_replace("\(\?:\[\\n\]\+\|\(\?:\\\\n\)\+\)",
|
|
|
9be3b2 |
+ "\n") | regex_replace("\\", "") | wordwrap() }}'
|
|
|
9be3b2 |
+ create: true
|
|
|
9be3b2 |
+ when: ansible_virtualization_type not in ["docker", "lxc", "openvz", "podman", "container"]
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - banner_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - medium_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+ - unknown_strategy
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Test for existence /etc/issue
|
|
|
9be3b2 |
+ stat:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ register: file_exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Ensure permission 0644 on /etc/issue
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ mode: '0644'
|
|
|
9be3b2 |
+ when: file_exists.stat is defined and file_exists.stat.exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - block:
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Remove Rsh Trust Files
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ path: /root/shosts.equiv
|
|
|
9be3b2 |
+ state: absent
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Add line to /root/shosts.equiv
|
|
|
9be3b2 |
+ lineinfile:
|
|
|
9be3b2 |
+ dest: /root/shosts.equiv
|
|
|
9be3b2 |
+ line: 'test host'
|
|
|
9be3b2 |
+ create: true
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - high_severity
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+ - no_rsh_trust_files
|
|
|
9be3b2 |
+ - restrict_strategy
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
diff --git a/tests/ansible_file_removed_and_added/file_not_removed_and_added.yml b/tests/ansible_file_removed_and_added/file_not_removed_and_added.yml
|
|
|
9be3b2 |
new file mode 100644
|
|
|
9be3b2 |
index 00000000000..3d3e53b958f
|
|
|
9be3b2 |
--- /dev/null
|
|
|
9be3b2 |
+++ b/tests/ansible_file_removed_and_added/file_not_removed_and_added.yml
|
|
|
9be3b2 |
@@ -0,0 +1,49 @@
|
|
|
9be3b2 |
+---
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+- hosts: all
|
|
|
9be3b2 |
+ vars:
|
|
|
9be3b2 |
+ var_system_crypto_policy: !!str FUTURE
|
|
|
9be3b2 |
+ var_sudo_logfile: !!str /var/log/sudo.log
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ tasks:
|
|
|
9be3b2 |
+ - name: Modify the System Login Banner - add correct banner
|
|
|
9be3b2 |
+ lineinfile:
|
|
|
9be3b2 |
+ dest: /etc/issue
|
|
|
9be3b2 |
+ line: '{{ login_banner_text | regex_replace("^\^(.*)\$$", "\1") | regex_replace("^\((.*)\|.*\)$",
|
|
|
9be3b2 |
+ "\1") | regex_replace("\[\\s\\n\]\+"," ") | regex_replace("\(\?:\[\\n\]\+\|\(\?:\\\\n\)\+\)",
|
|
|
9be3b2 |
+ "\n") | regex_replace("\\", "") | wordwrap() }}'
|
|
|
9be3b2 |
+ create: true
|
|
|
9be3b2 |
+ when: ansible_virtualization_type not in ["docker", "lxc", "openvz", "podman", "container"]
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - banner_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - medium_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+ - unknown_strategy
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Test for existence /etc/issue
|
|
|
9be3b2 |
+ stat:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ register: file_exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Ensure permission 0644 on /etc/issue
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ mode: '0644'
|
|
|
9be3b2 |
+ when: file_exists.stat is defined and file_exists.stat.exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
diff --git a/tests/ansible_file_removed_and_added/file_removed_and_added.yml b/tests/ansible_file_removed_and_added/file_removed_and_added.yml
|
|
|
9be3b2 |
new file mode 100644
|
|
|
9be3b2 |
index 00000000000..a44c39a9db2
|
|
|
9be3b2 |
--- /dev/null
|
|
|
9be3b2 |
+++ b/tests/ansible_file_removed_and_added/file_removed_and_added.yml
|
|
|
9be3b2 |
@@ -0,0 +1,62 @@
|
|
|
9be3b2 |
+---
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+- hosts: all
|
|
|
9be3b2 |
+ vars:
|
|
|
9be3b2 |
+ var_system_crypto_policy: !!str FUTURE
|
|
|
9be3b2 |
+ var_sudo_logfile: !!str /var/log/sudo.log
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ tasks:
|
|
|
9be3b2 |
+ - name: Modify the System Login Banner - remove incorrect banner
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ state: absent
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ when: ansible_virtualization_type not in ["docker", "lxc", "openvz", "podman", "container"]
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - banner_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - medium_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+ - unknown_strategy
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Modify the System Login Banner - add correct banner
|
|
|
9be3b2 |
+ lineinfile:
|
|
|
9be3b2 |
+ dest: /etc/issue
|
|
|
9be3b2 |
+ line: '{{ login_banner_text | regex_replace("^\^(.*)\$$", "\1") | regex_replace("^\((.*)\|.*\)$",
|
|
|
9be3b2 |
+ "\1") | regex_replace("\[\\s\\n\]\+"," ") | regex_replace("\(\?:\[\\n\]\+\|\(\?:\\\\n\)\+\)",
|
|
|
9be3b2 |
+ "\n") | regex_replace("\\", "") | wordwrap() }}'
|
|
|
9be3b2 |
+ create: true
|
|
|
9be3b2 |
+ when: ansible_virtualization_type not in ["docker", "lxc", "openvz", "podman", "container"]
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - banner_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - medium_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+ - unknown_strategy
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Test for existence /etc/issue
|
|
|
9be3b2 |
+ stat:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ register: file_exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Ensure permission 0644 on /etc/issue
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ mode: '0644'
|
|
|
9be3b2 |
+ when: file_exists.stat is defined and file_exists.stat.exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
diff --git a/tests/ansible_file_removed_and_added/file_removed_and_not_added.yml b/tests/ansible_file_removed_and_added/file_removed_and_not_added.yml
|
|
|
9be3b2 |
new file mode 100644
|
|
|
9be3b2 |
index 00000000000..08cda7e5063
|
|
|
9be3b2 |
--- /dev/null
|
|
|
9be3b2 |
+++ b/tests/ansible_file_removed_and_added/file_removed_and_not_added.yml
|
|
|
9be3b2 |
@@ -0,0 +1,46 @@
|
|
|
9be3b2 |
+---
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+- hosts: all
|
|
|
9be3b2 |
+ vars:
|
|
|
9be3b2 |
+ var_system_crypto_policy: !!str FUTURE
|
|
|
9be3b2 |
+ var_sudo_logfile: !!str /var/log/sudo.log
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ tasks:
|
|
|
9be3b2 |
+ - name: Modify the System Login Banner - remove incorrect banner
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ state: absent
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ when: ansible_virtualization_type not in ["docker", "lxc", "openvz", "podman", "container"]
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - banner_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - medium_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+ - unknown_strategy
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Test for existence /etc/issue
|
|
|
9be3b2 |
+ stat:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ register: file_exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+ - name: Ensure permission 0644 on /etc/issue
|
|
|
9be3b2 |
+ file:
|
|
|
9be3b2 |
+ path: /etc/issue
|
|
|
9be3b2 |
+ mode: '0644'
|
|
|
9be3b2 |
+ when: file_exists.stat is defined and file_exists.stat.exists
|
|
|
9be3b2 |
+ tags:
|
|
|
9be3b2 |
+ - configure_strategy
|
|
|
9be3b2 |
+ - file_permissions_etc_issue
|
|
|
9be3b2 |
+ - low_complexity
|
|
|
9be3b2 |
+ - low_disruption
|
|
|
9be3b2 |
+ - medium_severity
|
|
|
9be3b2 |
+ - no_reboot_needed
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
diff --git a/tests/test_check_playbook_file_removed_and_added.py b/tests/test_check_playbook_file_removed_and_added.py
|
|
|
9be3b2 |
new file mode 100644
|
|
|
9be3b2 |
index 00000000000..181bb14ed46
|
|
|
9be3b2 |
--- /dev/null
|
|
|
9be3b2 |
+++ b/tests/test_check_playbook_file_removed_and_added.py
|
|
|
9be3b2 |
@@ -0,0 +1,39 @@
|
|
|
9be3b2 |
+import os
|
|
|
9be3b2 |
+import pytest
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+from .test_ansible_file_removed_and_added import check_playbook_file_removed_and_added
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def test_file_removed_and_added():
|
|
|
9be3b2 |
+ playbook_path = os.path.join(os.path.dirname(__file__),
|
|
|
9be3b2 |
+ "ansible_file_removed_and_added",
|
|
|
9be3b2 |
+ "file_removed_and_added.yml")
|
|
|
9be3b2 |
+ assert not check_playbook_file_removed_and_added(playbook_path)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def test_file_removed_and_not_added():
|
|
|
9be3b2 |
+ playbook_path = os.path.join(os.path.dirname(__file__),
|
|
|
9be3b2 |
+ "ansible_file_removed_and_added",
|
|
|
9be3b2 |
+ "file_removed_and_not_added.yml")
|
|
|
9be3b2 |
+ assert check_playbook_file_removed_and_added(playbook_path)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def test_file_not_removed_and_added():
|
|
|
9be3b2 |
+ playbook_path = os.path.join(os.path.dirname(__file__),
|
|
|
9be3b2 |
+ "ansible_file_removed_and_added",
|
|
|
9be3b2 |
+ "file_not_removed_and_added.yml")
|
|
|
9be3b2 |
+ assert check_playbook_file_removed_and_added(playbook_path)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def test_file_block_removed_and_added():
|
|
|
9be3b2 |
+ playbook_path = os.path.join(os.path.dirname(__file__),
|
|
|
9be3b2 |
+ "ansible_file_removed_and_added",
|
|
|
9be3b2 |
+ "file_block_removed_and_added.yml")
|
|
|
9be3b2 |
+ assert not check_playbook_file_removed_and_added(playbook_path)
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+def test_file_block_removed_and_not_added():
|
|
|
9be3b2 |
+ playbook_path = os.path.join(os.path.dirname(__file__),
|
|
|
9be3b2 |
+ "ansible_file_removed_and_added",
|
|
|
9be3b2 |
+ "file_block_removed_and_not_added.yml")
|
|
|
9be3b2 |
+ assert check_playbook_file_removed_and_added(playbook_path)
|
|
|
9be3b2 |
|
|
|
9be3b2 |
From 741ec823ac39341f8aa0649031b72d2ac36e8a64 Mon Sep 17 00:00:00 2001
|
|
|
9be3b2 |
From: Watson Sato <wsato@redhat.com>
|
|
|
9be3b2 |
Date: Thu, 12 Aug 2021 10:36:47 +0200
|
|
|
9be3b2 |
Subject: [PATCH 3/3] Mention Ansible static yamlpath test in docs
|
|
|
9be3b2 |
|
|
|
9be3b2 |
---
|
|
|
9be3b2 |
.../developer/02_building_complianceascode.md | 14 ++++++++++++++
|
|
|
9be3b2 |
1 file changed, 14 insertions(+)
|
|
|
9be3b2 |
|
|
|
9be3b2 |
diff --git a/docs/manual/developer/02_building_complianceascode.md b/docs/manual/developer/02_building_complianceascode.md
|
|
|
9be3b2 |
index d536df0a259..87469bf5f9b 100644
|
|
|
9be3b2 |
--- a/docs/manual/developer/02_building_complianceascode.md
|
|
|
9be3b2 |
+++ b/docs/manual/developer/02_building_complianceascode.md
|
|
|
9be3b2 |
@@ -64,6 +64,20 @@ yum install yamllint ansible-lint
|
|
|
9be3b2 |
apt-get install yamllint ansible-lint
|
|
|
9be3b2 |
```
|
|
|
9be3b2 |
|
|
|
9be3b2 |
+### Static Ansible Playbooks tests
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+Install `yamlpath` and `pytest` to run tests cases that analyse the Ansible
|
|
|
9be3b2 |
+Playbooks' yaml nodes.
|
|
|
9be3b2 |
+```bash
|
|
|
9be3b2 |
+pip3 install yamlpath
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+# Fedora/RHEL
|
|
|
9be3b2 |
+yum install python3-pytest
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
+# Ubuntu/Debian
|
|
|
9be3b2 |
+apt-get install python-pytest
|
|
|
9be3b2 |
+```
|
|
|
9be3b2 |
+
|
|
|
9be3b2 |
### Ninja (Faster Builds)
|
|
|
9be3b2 |
|
|
|
9be3b2 |
Install the `ninja` build system if you want to use it instead of
|