0
0
Fork 0
mirror of https://github.com/atmoz/sftp.git synced 2024-11-17 12:51:33 -05:00
atmoz-sftp/tests/run

360 lines
10 KiB
Text
Raw Normal View History

2015-11-23 07:39:17 -05:00
#!/bin/bash
# See: https://github.com/kward/shunit2
2015-11-23 07:39:17 -05:00
argImage=$1
argOutput=${2:-"quiet"}
argCleanup=${3:-"cleanup"}
testDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
imageName="$argImage"
2020-08-19 17:25:27 -04:00
tmpDir="$(mktemp -d /tmp/atmoz_sftp_XXXX)"
sshKeyPri="$tmpDir/rsa"
sshKeyPub="$tmpDir/rsa.pub"
sshHostEd25519Key="$tmpDir/ssh_host_ed25519_key"
sshHostKeyMountArg="--volume=$sshHostEd25519Key:/etc/ssh/ssh_host_ed25519_key"
sshKnownHosts="$tmpDir/known_hosts"
2015-11-23 07:39:17 -05:00
if [ $UID != 0 ] && ! groups | grep -qw docker; then
echo "Run with sudo/root or add user $USER to group 'docker'"
exit 1
2015-12-07 04:10:24 -05:00
fi
if [ ! -f "$testDir/shunit2/shunit2" ]; then
echo "Could not find shunit2 in $testDir/shunit2."
2020-07-16 19:29:36 -04:00
echo "Run 'git submodule update --init'"
exit 2
fi
if [ -z "$argImage" ]; then
echo "Missing image name"
exit 3
fi
if [ "$argOutput" == "quiet" ]; then
redirect="/dev/null"
else
redirect="/dev/stdout"
fi
# clear argument list (or shunit2 will try to use them)
set --
2016-09-26 14:13:53 -04:00
##############################################################################
## Helper functions
2015-12-07 04:10:24 -05:00
##############################################################################
function oneTimeSetUp() {
# Generate temporary ssh keys for testing
2020-07-12 16:08:12 -04:00
if [ ! -f "$sshKeyPri" ]; then
ssh-keygen -t rsa -f "$sshKeyPri" -N '' > "$redirect" 2>&1
fi
# Private key can not be read by others (sshd will complain)
2020-07-12 16:08:12 -04:00
chmod go-rw "$sshKeyPri"
2020-08-19 17:25:27 -04:00
# Generate host key
ssh-keygen -t ed25519 -f "$sshHostEd25519Key" < /dev/null
2015-11-23 07:39:17 -05:00
}
function setUp() {
# shellcheck disable=SC2154
containerName="atmoz_sftp_${_shunit_test_}"
containerTmpDir="$(mktemp -d "/tmp/${containerName}_XXXX")"
export containerName containerTmpDir
retireContainer "$containerName" # clean up leftover container
}
function tearDown() {
retireContainer "$containerName"
if [ "$argCleanup" == "cleanup" ] && [ -d "$containerTmpDir" ]; then
2020-07-16 19:38:38 -04:00
rm -rf "$containerTmpDir" || true # Can fail on GitHub Actions
fi
}
2015-12-07 04:10:24 -05:00
function retireContainer() {
if [ "$(docker ps -qaf name="$1")" ]; then
if [ "$argOutput" != "quiet" ]; then
echo "Docker log for $1:"
docker logs "$1"
fi
if [ "$argCleanup" == "cleanup" ]; then
docker rm -fv "$1" > "$redirect" 2>&1
fi
2015-12-07 04:10:24 -05:00
fi
2015-11-23 07:39:17 -05:00
}
function getSftpIp() {
docker inspect -f "{{.NetworkSettings.IPAddress}}" "$1"
2015-11-23 07:39:17 -05:00
}
function runSftpCommands() {
ip="$(getSftpIp "$1")"
user="$2"
shift 2
2015-12-07 04:10:24 -05:00
2020-08-19 17:25:27 -04:00
echo "$ip $(cat "$sshHostEd25519Key.pub")" >> "$sshKnownHosts"
2015-12-07 04:10:24 -05:00
commands=""
for cmd in "$@"; do
commands="$commands$cmd"$'\n'
done
echo "$commands" | sftp \
2020-07-12 16:08:12 -04:00
-i "$sshKeyPri" \
2020-08-19 17:25:27 -04:00
-oUserKnownHostsFile="$sshKnownHosts" \
-b - "$user@$ip" \
2015-12-07 04:10:24 -05:00
> "$redirect" 2>&1
2016-06-02 08:56:30 -04:00
status=$?
sleep 1 # wait for commands to finish
return $status
}
function waitForServer() {
containerName="$1"
echo -n "Waiting for $containerName to open port 22 ..."
for _ in {1..30}; do
sleep 1
ip="$(getSftpIp "$containerName")"
echo -n "."
if [ -n "$ip" ] && nc -z "$ip" 22; then
echo " OPEN"
return 0;
fi
done
echo " TIMEOUT"
return 1
2015-11-23 07:39:17 -05:00
}
##############################################################################
## Tests
2015-12-07 04:10:24 -05:00
##############################################################################
function testSmallestUserConfig() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" \
--entrypoint="/bin/sh" \
"$imageName" \
-c "create-sftp-user u: && id u" \
> "$redirect" 2>&1
assertTrue "user created" $?
}
function testCreateUserWithDot() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" \
--entrypoint="/bin/sh" \
"$imageName" \
-c "create-sftp-user user.with.dot: && id user.with.dot" \
> "$redirect" 2>&1
assertTrue "user created" $?
}
function testUserCustomUidAndGid() {
2020-08-19 17:25:27 -04:00
id="$(docker run --name "$containerName" "$sshHostKeyMountArg" \
--entrypoint="/bin/sh" \
"$imageName" \
-c "create-sftp-user u::1234:4321: > /dev/null && id u" )"
2017-06-25 09:16:41 -04:00
echo "$id" | grep -q 'uid=1234('
assertTrue "custom UID" $?
2017-06-25 09:16:41 -04:00
echo "$id" | grep -q 'gid=4321('
assertTrue "custom GID" $?
# Here we also check group name
assertEquals "uid=1234(u) gid=4321(group_4321) groups=4321(group_4321)" "$id"
}
function testCommandPassthrough() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" \
"$imageName" test 1 -eq 1 \
> "$redirect" 2>&1
assertTrue "command passthrough" $?
}
function testUsersConf() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
-v "$testDir/files/users.conf:/etc/sftp/users.conf:ro" \
"$imageName" \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
2015-12-20 17:45:28 -05:00
docker exec "$containerName" id user-from-conf > /dev/null
assertTrue "user-from-conf" $?
2015-12-20 17:45:28 -05:00
docker exec "$containerName" id test > /dev/null
assertTrue "test" $?
2015-12-20 17:45:28 -05:00
docker exec "$containerName" id user.with.dot > /dev/null
assertTrue "user.with.dot" $?
docker exec "$containerName" test -d /home/test/dir1 -a -d /home/test/dir2
assertTrue "dirs exists" $?
}
2015-12-20 17:45:28 -05:00
function testLegacyUsersConf() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
-v "$testDir/files/users.conf:/etc/sftp-users.conf:ro" \
"$imageName" \
> "$redirect" 2>&1
2015-12-20 17:45:28 -05:00
waitForServer "$containerName"
assertTrue "waitForServer" $?
2015-12-20 17:45:28 -05:00
docker exec "$containerName" id user-from-conf > /dev/null
assertTrue "user-from-conf" $?
2015-12-20 17:45:28 -05:00
}
function testCreateUsersUsingEnv() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
-e "SFTP_USERS=user-from-env: user-from-env-2:" \
"$imageName" \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
docker exec "$containerName" id user-from-env > /dev/null
assertTrue "user-from-env" $?
docker exec "$containerName" id user-from-env-2 > /dev/null
assertTrue "user-from-env-2" $?
}
function testCreateUsersUsingCombo() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
-v "$testDir/files/users.conf:/etc/sftp-users.conf:ro" \
-e "SFTP_USERS=user-from-env:" \
"$imageName" \
user-from-cmd: \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
docker exec "$containerName" id user-from-conf > /dev/null
assertTrue "user-from-conf" $?
docker exec "$containerName" id user-from-env > /dev/null
assertTrue "user-from-env" $?
docker exec "$containerName" id user-from-cmd > /dev/null
assertTrue "user-from-cmd" $?
}
function testWriteAccessToAutocreatedDirs() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
2020-07-12 16:08:12 -04:00
-v "$sshKeyPub":/home/test/.ssh/keys/id_rsa.pub:ro \
2018-11-16 10:04:28 -05:00
"$imageName" "test::::testdir,dir with spaces" \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
runSftpCommands "$containerName" "test" \
2018-11-16 10:04:28 -05:00
"cd testdir" \
"mkdir test" \
2018-11-16 10:04:28 -05:00
"cd '../dir with spaces'" \
"mkdir test" \
"exit"
assertTrue "runSftpCommands" $?
2018-11-16 10:04:28 -05:00
docker exec "$containerName" test -d /home/test/testdir/test
assertTrue "testdir write access" $?
docker exec "$containerName" test -d "/home/test/dir with spaces/test"
assertTrue "dir with spaces write access" $?
}
function testWriteAccessToLimitedChroot() {
# Modified sshd_config with chrooted home subdir
tmpConfig="$(mktemp)"
sed 's/^ChrootDirectory.*/ChrootDirectory %h\/sftp/' \
< "$testDir/../files/sshd_config" > "$tmpConfig"
# Set correct permissions on chroot
tmpScript="$(mktemp)"
cat > "$tmpScript" <<EOF
mkdir -p /home/*/sftp
chown root:root /home/*/sftp
chmod 755 /home/*/sftp
EOF
chmod +x "$tmpScript"
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
-v "$sshKeyPub":/home/test/.ssh/keys/id_rsa.pub:ro \
-v "$tmpConfig:/etc/ssh/sshd_config" \
-v "$tmpScript:/etc/sftp.d/limited_home_dir" \
"$imageName" "test::::sftp/upload" \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
runSftpCommands "$containerName" "test" \
"cd upload" \
"mkdir test" \
"exit"
assertTrue "runSftpCommands" $?
docker exec "$containerName" test -d /home/test/sftp/upload/test
assertTrue "limited chroot write access" $?
}
function testBindmountDirScript() {
mkdir -p "$containerTmpDir/custom/bindmount"
echo "mkdir -p /home/custom/bindmount && \
chown custom /custom /home/custom/bindmount && \
mount --bind /custom /home/custom/bindmount" \
> "$containerTmpDir/mount.sh"
chmod +x "$containerTmpDir/mount.sh"
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
--privileged=true \
2020-07-12 16:08:12 -04:00
-v "$sshKeyPub":/home/custom/.ssh/keys/id_rsa.pub:ro \
-v "$containerTmpDir/custom/bindmount":/custom \
-v "$containerTmpDir/mount.sh":/etc/sftp.d/mount.sh \
"$imageName" custom:123 \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
runSftpCommands "$containerName" "custom" \
"cd bindmount" \
"mkdir test" \
"exit"
assertTrue "runSftpCommands" $?
docker exec "$containerName" test -d /home/custom/bindmount/test
assertTrue "directory exist" $?
}
2020-07-12 16:08:12 -04:00
function testDuplicateSshKeys() {
2020-08-19 17:25:27 -04:00
docker run --name "$containerName" "$sshHostKeyMountArg" -d \
2020-07-12 16:08:12 -04:00
-v "$sshKeyPub":/home/user/.ssh/keys/key1.pub:ro \
-v "$sshKeyPub":/home/user/.ssh/keys/key2.pub:ro \
"$imageName" "user:" \
> "$redirect" 2>&1
waitForServer "$containerName"
assertTrue "waitForServer" $?
lines="$(docker exec "$containerName" sh -c \
"wc -l < /home/user/.ssh/authorized_keys")"
assertEquals "1" "$lines"
}
##############################################################################
## Run
2015-12-07 04:10:24 -05:00
##############################################################################
2021-05-15 14:59:49 -04:00
# shellcheck disable=SC1091
source "$testDir/shunit2/shunit2"
2015-11-23 07:39:17 -05:00
# Nothing happens after this