From f7e8324ba57ed0ec13c41206c35f295aa1f19991 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 10:29:21 +0200 Subject: [PATCH] Embed check-incs and check-last-incs in bkctld --- CHANGELOG.md | 24 ++++++++++----- bkctld | 11 +++++++ check-incs.sh | 61 -------------------------------------- check-last-incs.sh | 20 ------------- lib/bkctld-check-incs | 53 +++++++++++++++++++++++++++++++++ lib/bkctld-check-last-incs | 27 +++++++++++++++++ lib/includes | 9 ++++++ test/checks.bats | 21 +++++++++++++ 8 files changed, 137 insertions(+), 89 deletions(-) delete mode 100644 check-incs.sh delete mode 100644 check-last-incs.sh create mode 100755 lib/bkctld-check-incs create mode 100755 lib/bkctld-check-last-incs diff --git a/CHANGELOG.md b/CHANGELOG.md index 98eae12..ad2cc73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +### Changed + +* check-incs.sh and check-last-incs.sh are embedded in bkctld + +### Deprecated + +### Removed + +### Fixed + +### Security + +## [2.2.0] - 2020-04-17 + +### Added + * Shellcheck directives to have 0 warnings and errors * Ability to override critical/warning thresholds per jail for bkctld-check * Support new location for jail configuration (/etc/evobackup/.d/) @@ -28,11 +44,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Split BATS tests file and use helper functions * Improve "lib" detection * Revamp the README - -### Deprecated - -### Removed - -### Fixed - -### Security diff --git a/bkctld b/bkctld index 67e74b1..080bd29 100755 --- a/bkctld +++ b/bkctld @@ -39,6 +39,17 @@ case "${subcommand}" in "inc" | "rm" | "check" | "stats" | "help" | "list") "${LIBDIR}/bkctld-${subcommand}" ;; + "check-incs") + option="${2:-}" + if [ "${option}" = "all" ] || [ -z "${option}" ]; then + "${LIBDIR}/bkctld-check-incs" + elif [ "${option}" = "last" ]; then + "${LIBDIR}/bkctld-check-last-incs" + else + "${LIBDIR}/bkctld-help" + exit 1 + fi + ;; "init" | "is-on") jail_name="${2:-}" "${LIBDIR}/bkctld-${subcommand}" "${jail_name}" diff --git a/check-incs.sh b/check-incs.sh deleted file mode 100644 index e9ca85a..0000000 --- a/check-incs.sh +++ /dev/null @@ -1,61 +0,0 @@ -#!/bin/sh - -EVOBACKUP_CONFIGS="/etc/evobackup/*" - -relative_date() { - format=$(echo $1 | cut -d'.' -f1) - time_jump=$(echo $1 | cut -d'.' -f2) - - reference_date=$(date "${format}") - past_date=$(date --date "${reference_date} ${time_jump}" +"%Y-%m-%d") - - echo ${past_date} -} -inc_exists() { - ls -d /backup/incs/$1 > /dev/null 2>&1 -} -jail_exists() { - ls -d /backup/jails/$1 > /dev/null 2>&1 -} -# default return value is 0 (succes) -rc=0 -# loop for each configured jail -for file in ${EVOBACKUP_CONFIGS}; do - jail_name=$(basename ${file}) - - # check if jail is present - if jail_exists ${jail_name}; then - today=$(date +"%s") - # get jail last configuration date - jail_config_age=$(date --date "$(stat -c %y ${file})" +"%s") - - # loop for each line in jail configuration - for line in $(cat ${file}); do - # inc date in ISO format - inc_date=$(relative_date ${line}) - # inc date in seconds from epoch - inc_age=$(date --date "${inc_date}" +"%s") - - # skip line if date is inthe future - if [ "${inc_age}" -gt "${today}" ]; then - echo "INFO: no inc expected for ${inc_date} \`${jail_name}'" - else - # check if the configuration changed after the inc date - if [ "${jail_config_age}" -lt "${inc_age}" ]; then - # Error if inc is not found - if ! inc_exists ${jail_name}/${inc_date}*; then - echo "ERROR: inc is missing \`${jail_name}/${inc_date}'" >&2 - rc=1 - fi - else - echo "INFO: no inc expected for ${inc_date} \`${jail_name}'" - fi - fi - done - else - echo "ERROR: jail is missing \`${jail_name}'" >&2 - rc=1 - fi -done - -exit $rc diff --git a/check-last-incs.sh b/check-last-incs.sh deleted file mode 100644 index cefcb89..0000000 --- a/check-last-incs.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -inc_exists() { - ls -d /backup/incs/$1 > /dev/null 2>&1 -} -# default return value is 0 (succes) -rc=0 -# loop for each found jail -for file in /backup/jails/*; do - jail_name=$(basename ${file}) - # inc date in seconds from epoch - inc_date=$(date --date "yesterday" +"%Y-%m-%d") - # Error if inc is not found - if ! inc_exists ${jail_name}/${inc_date}*; then - echo "ERROR: inc is missing \`${jail_name}/${inc_date}'" >&2 - rc=1 - fi -done - -exit $rc diff --git a/lib/bkctld-check-incs b/lib/bkctld-check-incs new file mode 100755 index 0000000..22cf1a8 --- /dev/null +++ b/lib/bkctld-check-incs @@ -0,0 +1,53 @@ +#!/bin/sh + +set -u + +# shellcheck source=./includes +LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes" + +# default return value is 0 (succes) +rc=0 +# loop for each configured jail +for jail_name in $(bkctld list); do + incs_policy_file=$(current_jail_incs_policy_file "${jail_name}") + + # Today in seconds from epoch + today_epoch=$(date +"%s") + # Today in ISO format + today_iso=$(date +"%Y-%m-%d") + + # get jail last configuration date + jail_config_epoch=$(date --date "$(stat -c %y ${incs_policy_file})" +"%s") + + if [ -n "${incs_policy_file}" ]; then + # loop for each line in jail configuration + for line in $(cat ${incs_policy_file}); do + # inc date in ISO format + inc_iso=$(relative_date ${line}) + # inc date in seconds from epoch + inc_epoch=$(date --date "${inc_iso}" +"%s") + + # skip line if date is in the future + if [ "${inc_epoch}" -gt "${today_epoch}" ]; then + echo "INFO: ${jail_name} : no inc expected for ${inc_iso}" + else + # check if the configuration changed after the inc date + # or if it's today's inc + if [ "${jail_config_epoch}" -lt "${inc_epoch}" ] \ + || [ "${today_iso}" = "${inc_iso}" ]; then + # Error if inc is not found + if ! inc_exists "${jail_name}" "${inc_iso}*"; then + echo "ERROR: ${jail_name} : missing inc for ${inc_iso}" >&2 + rc=1 + fi + else + echo "INFO: ${jail_name} : no inc expected for ${inc_iso}" + fi + fi + done + else + echo "INFO: ${jail_name} : no inc expected at all" + fi +done + +exit $rc diff --git a/lib/bkctld-check-last-incs b/lib/bkctld-check-last-incs new file mode 100755 index 0000000..87218a2 --- /dev/null +++ b/lib/bkctld-check-last-incs @@ -0,0 +1,27 @@ +#!/bin/sh + +set -u + +# shellcheck source=./includes +LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes" + +# default return value is 0 (succes) +rc=0 +# loop for each found jail +for jail_name in $(bkctld list); do + incs_policy_file=$(current_jail_incs_policy_file "${jail_name}") + + if [ -n "${incs_policy_file}" ]; then + # inc date in seconds from epoch + inc_date=$(date +"%Y-%m-%d") + # Error if inc is not found + if ! inc_exists "${jail_name}" "${inc_date}*"; then + echo "ERROR: ${jail_name} : missing inc for ${inc_date}" >&2 + rc=1 + fi + else + echo "INFO: ${jail_name} : no inc expected at all" + fi +done + +exit $rc diff --git a/lib/includes b/lib/includes index a33fed6..ebf7b37 100755 --- a/lib/includes +++ b/lib/includes @@ -111,6 +111,15 @@ inc_path() { echo "${INCDIR}/${jail_name}/${inc_name}" } +# Test the existence of an inc pattern for a jail +inc_exists() { + jail_name=${1-?} + inc_pattern=${2-?} + inc_path=$(inc_path "${jail_name}" "${inc_pattern}") + + # inc_path must not be quoted because it can contain globs + ls -d ${inc_path} > /dev/null 2>&1 +} jail_config_dir() { jail_name=${1:?} diff --git a/test/checks.bats b/test/checks.bats index 079d1d7..77a7b4a 100644 --- a/test/checks.bats +++ b/test/checks.bats @@ -165,3 +165,24 @@ OUT run /usr/lib/bkctld/bkctld-check assert_equal "0" "$status" } + +@test "Check-last-incs OK if jail is present" { + /usr/lib/bkctld/bkctld-inc + + run /usr/lib/bkctld/bkctld-check-last-incs + assert_equal "0" "$status" +} + +@test "Check-last-incs Error if jail is missing" { + + run /usr/lib/bkctld/bkctld-check-last-incs + assert_equal "1" "$status" +} + +@test "Check-incs OK" { + /usr/lib/bkctld/bkctld-inc + + run /usr/lib/bkctld/bkctld-check-incs + assert_equal "0" "$status" +} +# TODO: write many more tests for bkctld-check-incs