From d825152c642b57bd9548f55c7c5e45b9c9dc0443 Mon Sep 17 00:00:00 2001 From: Alexander Kuemmel Date: Tue, 1 Jan 2019 16:00:34 +0100 Subject: [PATCH] UPD: user list re-evaluation refactored and tests implemented --- docker-compose.yml | 9 --------- files/entrypoint | 17 ++++++++++------- tests/files/new_users.conf | 2 -- tests/files/previous_users.conf | 1 - tests/run | 31 +++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 19 deletions(-) delete mode 100644 docker-compose.yml delete mode 100644 tests/files/new_users.conf delete mode 100644 tests/files/previous_users.conf diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 311aed0..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -version: '2' - -services: - - sftp: - build: . - volumes: - - ./tests/testusers.conf:/etc/sftp/users.conf - diff --git a/files/entrypoint b/files/entrypoint index 3221c1d..0355118 100755 --- a/files/entrypoint +++ b/files/entrypoint @@ -34,14 +34,16 @@ fi # Check for differences between defined and provisioned users if [ -f "$userProvisionedFinalPath" ]; then set +e - diff "$userProvisionedFinalPath" "$userConfPath" \ - | tail -n+3 \ - | grep -E '^\+.*$' \ - | cut -c2- \ - | grep -v -E '^\s*#' > "$userProvisionedTmpPath" + _raw_diff="$(diff "$userProvisionedFinalPath" "$userConfPath" || true)" set -e - SFTP_USERS="$(cat $userProvisionedTmpPath)" - rm -f "$userProvisionedTmpPath" + if [ -n "$_raw_diff" ]; then + _diff_lines="$(echo "$_raw_diff" | tail -n+2)" + _diff_new_entries="$(echo "$_diff_lines" | grep -E '^[+>].*$' | grep -o '^\s*[^#]*')" + _new_entries="$(echo "$_diff_new_entries" | cut -c2- | sed -e 's/^[ \t]*//')" + echo "$_new_entries" > "$userProvisionedTmpPath" + SFTP_USERS="$SFTP_USERS $(cat $userProvisionedTmpPath)" + rm -f "$userProvisionedTmpPath" + fi fi # Create users on first run and if new entries are present @@ -74,6 +76,7 @@ if [ ! -f "$userConfFinalPath" ] || [ -n "$SFTP_USERS" ]; then while IFS= read -r user || [[ -n "$user" ]]; do create-sftp-user "$user" done < "$userConfFinalPath" + cp "$userConfFinalPath" "$userProvisionedFinalPath" elif $startSshd; then log "FATAL: No users provided!" exit 3 diff --git a/tests/files/new_users.conf b/tests/files/new_users.conf deleted file mode 100644 index 4f70e0f..0000000 --- a/tests/files/new_users.conf +++ /dev/null @@ -1,2 +0,0 @@ -foo:pass:::upload,download -bar:pass:::upload,media diff --git a/tests/files/previous_users.conf b/tests/files/previous_users.conf deleted file mode 100644 index 2eac4de..0000000 --- a/tests/files/previous_users.conf +++ /dev/null @@ -1 +0,0 @@ -foo1:pass:::upload diff --git a/tests/run b/tests/run index 6f92d84..e6760c6 100755 --- a/tests/run +++ b/tests/run @@ -200,6 +200,37 @@ function testUsersConf() { assertTrue "dirs exists" $? } +function testAddUsersConf() { + echo "user-from-conf:" > "$testDir/files/changing-users.conf" + + docker run --name "$containerName" -d \ + -v "$testDir/files/changing-users.conf:/etc/sftp/users.conf:ro" \ + "$imageName" \ + > "$redirect" 2>&1 + + waitForServer "$containerName" + assertTrue "waitForServer" $? + + docker exec "$containerName" id user-from-conf > /dev/null + assertTrue "user-from-conf" $? + + echo "add-user-from-conf:" >> "$testDir/files/changing-users.conf" + echo "add-user2-from-conf:" >> "$testDir/files/changing-users.conf" + + docker restart "$containerName" > "$redirect" 2>&1 + + waitForServer "$containerName" + assertTrue "waitForServer" $? + + docker exec "$containerName" id add-user-from-conf > /dev/null + assertTrue "add-user-from-conf" $? + + docker exec "$containerName" id add-user2-from-conf > /dev/null + assertTrue "add-user2-from-conf" $? + + rm -f "$testDir/files/changing-users.conf" +} + function testLegacyUsersConf() { docker run --name "$containerName" -d \ -v "$testDir/files/users.conf:/etc/sftp-users.conf:ro" \