From 00b1d88ed74e2cc90f70dede4e37a5ce9d1fdc6a Mon Sep 17 00:00:00 2001 From: Georg Lauterbach <44545919+georglauterbach@users.noreply.github.com> Date: Sun, 5 Feb 2023 12:39:05 +0100 Subject: [PATCH] removal: configomat (submodule) (#3045) --- .editorconfig | 2 +- .gitmodules | 3 -- Dockerfile | 1 - .../examples/tutorials/docker-build.md | 6 --- target/docker-configomat | 1 - target/scripts/helpers/utils.sh | 51 ++++++++++++++++++ target/scripts/startup/setup-stack.sh | 4 +- .../replace_by_env_in_file.conf | 6 +++ test/linting/lint.sh | 3 +- .../set3/scripts/replace_by_env_in_file.bats | 52 +++++++++++++++++++ test/tests/serial/mail_with_ldap.bats | 2 +- 11 files changed, 114 insertions(+), 17 deletions(-) delete mode 160000 target/docker-configomat create mode 100644 test/config/override-configs/replace_by_env_in_file.conf create mode 100644 test/tests/parallel/set3/scripts/replace_by_env_in_file.bats diff --git a/.editorconfig b/.editorconfig index 69b8beab..fd68e2f2 100644 --- a/.editorconfig +++ b/.editorconfig @@ -30,7 +30,7 @@ trim_trailing_whitespace = false # --- Git Submodules ---------------------------- # ----------------------------------------------- -[{test/bats/**,test/test_helper/**,target/docker-configomat/**}] +[{test/bats/**,test/test_helper/**}] indent_style = none indent_size = none end_of_line = none diff --git a/.gitmodules b/.gitmodules index fc82d31f..13d9362d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,6 +7,3 @@ [submodule "test/test_helper/bats-assert"] path = test/test_helper/bats-assert url = https://github.com/bats-core/bats-assert -[submodule "target/docker-configomat"] - path = target/docker-configomat - url = https://github.com/alinmear/docker-configomat diff --git a/Dockerfile b/Dockerfile index a6158d5b..938428b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -253,7 +253,6 @@ COPY \ target/scripts/*.sh \ target/scripts/startup/*.sh \ target/scripts/wrapper/*.sh \ - target/docker-configomat/configomat.sh \ /usr/local/bin/ RUN chmod +x /usr/local/bin/* diff --git a/docs/content/examples/tutorials/docker-build.md b/docs/content/examples/tutorials/docker-build.md index 98f19be9..62471579 100644 --- a/docs/content/examples/tutorials/docker-build.md +++ b/docs/content/examples/tutorials/docker-build.md @@ -19,12 +19,6 @@ Or, you can clone and retrieve the submodules in one command: git clone --recurse-submodules https://github.com/docker-mailserver/docker-mailserver ``` -Retrieving the git submodules will fix the error: - -```txt -COPY failed: file not found in build context or excluded by .dockerignore: stat target/docker-configomat/configomat.sh: file does not exist -``` - ### About Docker #### Version diff --git a/target/docker-configomat b/target/docker-configomat deleted file mode 160000 index 41555048..00000000 --- a/target/docker-configomat +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 415550485438d7e6ca6136016cca4bbab99ccf6f diff --git a/target/scripts/helpers/utils.sh b/target/scripts/helpers/utils.sh index f74bf934..2ef76def 100644 --- a/target/scripts/helpers/utils.sh +++ b/target/scripts/helpers/utils.sh @@ -63,3 +63,54 @@ function _reload_postfix _adjust_mtime_for_postfix_maincf postfix reload } + +# Replaces values in configuration files given a set of specific environment +# variables. The environment variables follow a naming pattern, whereby every +# variable that is taken into account has a given prefix. The new value in the +# configuration will be the one the environment variable had at the time of +# calling this function. +# +# @option --shutdown-on-error = shutdown in case an error is detected +# @param ${1} = prefix for environment variables +# @param ${2} = file in which substitutions should take place +# +# ## Example +# +# If you want to set a new value for `readme_directory` in Postfix's `main.cf`, +# you can set the environment variable `POSTFIX_README_DIRECTORY='/new/dir/'` +# (`POSTFIX_` is an arbitrary prefix, you can choose the one you like), +# and then call this function: +# `_replace_by_env_in_file 'POSTFIX_' 'PATH TO POSTFIX's main.cf>` +# +# ## Panics +# +# This function will panic, i.e. shut down the whole container, if: +# +# 1. No first and second argument is supplied +# 2. The second argument is a path to a file that does not exist +function _replace_by_env_in_file +{ + if [[ -z ${1+set} ]] + then + dms_panic__invalid_value 'first argument unset' 'utils.sh:_replace_by_env_in_file' + elif [[ -z ${2+set} ]] + then + dms_panic__invalid_value 'second argument unset' 'utils.sh:_replace_by_env_in_file' + elif [[ ! -f ${2} ]] + then + dms_panic__invalid_value "file '${2}' does not exist" 'utils.sh:_replace_by_env_in_file' + fi + + local ENV_PREFIX=${1} CONFIG_FILE=${2} + local ESCAPED_VALUE ESCAPED_KEY + + while IFS='=' read -r KEY VALUE + do + KEY=${KEY#"${ENV_PREFIX}"} # strip prefix + ESCAPED_KEY=$(sed -E 's#([\=\&\|\$\.\*\/\[\\^]|\])#\\\1#g' <<< "${KEY,,}") + ESCAPED_VALUE=$(sed -E 's#([\=\&\|\$\.\*\/\[\\^]|\])#\\\1#g' <<< "${VALUE}") + [[ -n ${ESCAPED_VALUE} ]] && ESCAPED_VALUE=" ${ESCAPED_VALUE}" + _log 'trace' "Setting value of '${KEY}' in '${CONFIG_FILE}' to '${VALUE}'" + sed -i -E "s#^${ESCAPED_KEY}[[:space:]]*=.*#${ESCAPED_KEY} =${ESCAPED_VALUE}#g" "${CONFIG_FILE}" + done < <(env | grep "^${ENV_PREFIX}") +} diff --git a/target/scripts/startup/setup-stack.sh b/target/scripts/startup/setup-stack.sh index cb8c991c..fd1035b1 100644 --- a/target/scripts/startup/setup-stack.sh +++ b/target/scripts/startup/setup-stack.sh @@ -380,7 +380,7 @@ function _setup_ldap [[ ${FILE} =~ ldap-aliases ]] && export LDAP_QUERY_FILTER="${LDAP_QUERY_FILTER_ALIAS}" [[ ${FILE} =~ ldap-domains ]] && export LDAP_QUERY_FILTER="${LDAP_QUERY_FILTER_DOMAIN}" [[ ${FILE} =~ ldap-senders ]] && export LDAP_QUERY_FILTER="${LDAP_QUERY_FILTER_SENDERS}" - _log debug "$(configomat.sh "LDAP_" "${FILE}" 2>&1)" + [[ -f ${FILE} ]] && _replace_by_env_in_file 'LDAP_' "${FILE}" done _log 'trace' "Configuring Dovecot LDAP" @@ -407,7 +407,7 @@ function _setup_ldap export "${VAR}=${DOVECOT_LDAP_MAPPING[${VAR}]}" done - _log debug "$(configomat.sh "DOVECOT_" "/etc/dovecot/dovecot-ldap.conf.ext" 2>&1)" + _replace_by_env_in_file 'DOVECOT_' '/etc/dovecot/dovecot-ldap.conf.ext' _log 'trace' 'Enabling Dovecot LDAP authentication' diff --git a/test/config/override-configs/replace_by_env_in_file.conf b/test/config/override-configs/replace_by_env_in_file.conf new file mode 100644 index 00000000..7862b2f5 --- /dev/null +++ b/test/config/override-configs/replace_by_env_in_file.conf @@ -0,0 +1,6 @@ +key_1 = value +key_2 = +key_3 =value +key_4= value +key_5=value +key_6 = value diff --git a/test/linting/lint.sh b/test/linting/lint.sh index cab2f404..39c86250 100755 --- a/test/linting/lint.sh +++ b/test/linting/lint.sh @@ -51,8 +51,7 @@ function _shellcheck # File paths for shellcheck: F_SH=$(find . -type f -iname '*.sh' \ -not -path './test/bats/*' \ - -not -path './test/test_helper/*' \ - -not -path './target/docker-configomat/*' + -not -path './test/test_helper/*' ) # shellcheck disable=SC2248 F_BIN=$(find 'target/bin' -type f -not -name '*.py') diff --git a/test/tests/parallel/set3/scripts/replace_by_env_in_file.bats b/test/tests/parallel/set3/scripts/replace_by_env_in_file.bats new file mode 100644 index 00000000..cb34413a --- /dev/null +++ b/test/tests/parallel/set3/scripts/replace_by_env_in_file.bats @@ -0,0 +1,52 @@ +load "${REPOSITORY_ROOT}/test/helper/common" +source "${REPOSITORY_ROOT}/target/scripts/helpers/log.sh" +source "${REPOSITORY_ROOT}/target/scripts/helpers/utils.sh" + +BATS_TEST_NAME_PREFIX='[Helper function] (_replace_by_env_in_file) ' + +function setup_file() { + export TMP_CONFIG_FILE=$(mktemp) + cp "${REPOSITORY_ROOT}/test/config/override-configs/replace_by_env_in_file.conf" "${TMP_CONFIG_FILE}" +} + +function teardown_file() { rm "${TMP_CONFIG_FILE}" ; } + +@test "substitute key-value pair (01) (normal substitutions)" { + export TEST_KEY_1='new_value_1' + _do_work "key_1 = ${TEST_KEY_1}" + export TEST_KEY_1='(&(objectClass=PostfixBookMailAccount)(|(uniqueIdentifier=%n)(mail=%u)))' + _do_work "key_1 = ${TEST_KEY_1}" + export TEST_KEY_1="*+=/_-%&" + _do_work "key_1 = ${TEST_KEY_1}" + export TEST_KEY_1='(&(objectClass=mailAccount)(uid=%n))' + _do_work "key_1 = ${TEST_KEY_1}" + export TEST_KEY_1='=home=/var/mail/%{ldap:mail}, =mail=maildir:/var/mail/%{ldap:mail}/Maildir' + _do_work "key_1 = ${TEST_KEY_1}" + export TEST_KEY_1='(&(objectClass=mailAccount)(uid=%n))' + _do_work "key_1 = ${TEST_KEY_1}" + export TEST_KEY_1='uid=user,userPassword=password' + _do_work "key_1 = ${TEST_KEY_1}" +} + +@test "substitute key-value pair (08) (spaces / no values)" { + export TEST_KEY_2='new_value_2' + _do_work "key_2 = ${TEST_KEY_2}" + export TEST_KEY_3='new_value_3' + _do_work "key_3 = ${TEST_KEY_3}" + export TEST_KEY_4="new_value_4" + _do_work "key_4 = ${TEST_KEY_4}" + export TEST_KEY_5="new_value_5" + _do_work "key_5 = ${TEST_KEY_5}" + export TEST_KEY_6= + _do_work "key_6 =" + run grep -q -F "key_6 = " "${TMP_CONFIG_FILE}" + assert_failure +} + +function _do_work() { + local FILTER_STRING=${1:?No string to filter by was provided} + run _replace_by_env_in_file 'TEST_' "${TMP_CONFIG_FILE}" + assert_success + run grep -q -F "${FILTER_STRING}" "${TMP_CONFIG_FILE}" + assert_success +} diff --git a/test/tests/serial/mail_with_ldap.bats b/test/tests/serial/mail_with_ldap.bats index 438c9a9e..00860add 100644 --- a/test/tests/serial/mail_with_ldap.bats +++ b/test/tests/serial/mail_with_ldap.bats @@ -23,7 +23,7 @@ function setup_file() { --tty \ ldap # Image name - # _setup_ldap uses configomat with .ext files and ENV vars like DOVECOT_TLS with a prefix (eg DOVECOT_ or LDAP_) + # _setup_ldap uses _replace_by_env_in_file with ENV vars like DOVECOT_TLS with a prefix (eg. DOVECOT_ or LDAP_) local PRIVATE_CONFIG PRIVATE_CONFIG=$(duplicate_config_for_container .) docker run -d --name mail_with_ldap \