From 2d7f37cd20341850d4723fb767e7c71e37cba0ac Mon Sep 17 00:00:00 2001 From: Adrian Dvergsdal Date: Thu, 17 Mar 2016 21:27:41 +0100 Subject: [PATCH] Document and test bind-mounting with /etc/sftp.d/ --- README.md | 36 +++++++++++++++++++++++++++++++++--- tests/run | 55 +++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a8be32b..36501d3 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Usage Examples -------- -### Simple example +### Simple docker run example ``` docker run \ @@ -42,7 +42,7 @@ sftp: #### Logging in The OpenSSH server runs by default on port 22, and in this example, we are -forwarding the container's port 22 to the host's port 2222. To log in with an +forwarding the container's port 22 to the host's port 2222. To log in with the OpenSSH client, run: `sftp -P 2222 foo@` ### Store users in config @@ -65,7 +65,7 @@ bar:abc:1002 ### Encrypted password -Add `:e` behind password to mark it as encrypted. Use single quotes. +Add `:e` behind password to mark it as encrypted. Use single quotes if using terminal. ``` docker run \ @@ -90,3 +90,33 @@ docker run \ -p 2222:22 -d atmoz/sftp \ foo::1001 ``` + +### Execute custom scripts or applications + +Put your programs in /etc/sftp.d/ and it will automatically run when the container starts. +See next section for an example. + +### Bindmount dirs from another location + +If you are using --volumes-from or just want to make a custom directory +available in user's home directory, you can add a script to /etc/sftp.d/ that +bindmounts after container starts. + +``` +#!/bin/bash +# Just an example (make your own): +function bindmount() { + if [ -d "$1" ]; then + mkdir -p "$2" + fi + mount --bind $3 "$1" "$2" +} + +# Remember permissions, you may have to fix it: +# chown -R :users /data/common + +bindmount /data/admin-tools /home/admin/tools +bindmount /data/common /home/dave/common +bindmount /data/common /home/peter/common +bindmount /data/docs /home/peter/docs --read-only +``` diff --git a/tests/run b/tests/run index 41b8673..2caa06e 100755 --- a/tests/run +++ b/tests/run @@ -57,11 +57,13 @@ function afterTest() { } function getSftpIp() { - docker inspect -f {{.NetworkSettings.IPAddress}} "$sftpContainerName" + docker inspect -f {{.NetworkSettings.IPAddress}} "$1" } function runSftpCommands() { - ip="$(getSftpIp)" + ip="$(getSftpIp $1)" + user="$2" + shift 2 commands="" for cmd in "$@"; do @@ -72,7 +74,7 @@ function runSftpCommands() { -i "$scriptDir/id_rsa" \ -oStrictHostKeyChecking=no \ -oUserKnownHostsFile=/dev/null \ - -b - test@$ip \ + -b - $user@$ip \ > "$redirect" 2>&1 } @@ -86,7 +88,7 @@ function testMinimalContainerStart() { docker run \ --name "$tmpContainerName" \ -d "$sftpImageName" \ - minimal::1111 \ + minimal \ > "$redirect" sleep 1 @@ -120,18 +122,59 @@ function testContainerIsRunning() { function testLoginUsingSshKey() { $skipAllTests && skip && return 0 - runSftpCommands "exit" + runSftpCommands "$sftpContainerName" "test" "exit" assertReturn $? 0 } function testWritePermission() { $skipAllTests && skip && return 0 - runSftpCommands "cd share" "mkdir test" "exit" + runSftpCommands "$sftpContainerName" "test" "cd share" "mkdir test" "exit" test -d "$tmpDir/test" assertReturn $? 0 } +# Bind-mount folder using script in /etc/sftp.d/ +function testCustomContainerStart() { + $skipAllTests && skip && return 0 + + tmpContainerName="$sftpContainerName""_custom" + + mkdir -p "$tmpDir/custom/bindmount" + echo "mkdir -p /home/custom/bindmount && \ + chown custom /home/custom/bindmount && \ + mount --bind /custom /home/custom/bindmount" \ + > "$tmpDir/mount.sh" + chmod +x "$tmpDir/mount.sh" + + docker run \ + --privileged=true \ + --name "$tmpContainerName" \ + -v "$scriptDir/id_rsa.pub":/home/custom/.ssh/keys/id_rsa.pub:ro \ + -v "$tmpDir/custom/bindmount":/custom \ + -v "$tmpDir/mount.sh":/etc/sftp.d/mount.sh \ + --expose 22 \ + -d "$sftpImageName" \ + custom:123 \ + > "$redirect" + sleep 1 + + ps="$(docker ps -q -f name="$tmpContainerName")" + assertNotEqual "$ps" "" + + runSftpCommands "$tmpContainerName" "custom" "cd bindmount" "mkdir test" "exit" + test -d "$tmpDir/custom/bindmount/test" + assertReturn $? 0 + + if [ "$output" != "quiet" ]; then + docker logs "$tmpContainerName" + fi + + if [ "$cleanup" == "cleanup" ]; then + docker rm -fv "$tmpContainerName" > "$redirect" + fi +} + ############################################################################## # Run tests