diff --git a/Dockerfile b/Dockerfile index f949273..1dd2b50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,8 +28,12 @@ RUN apk add --no-cache \ bash \ openssh \ socat \ + expect \ && rm -rf /var/cache/apk/* +# Copy expect script +COPY ssh-add-pass.sh /ssh-add-pass.sh + # Copy entrypoint script to container COPY entry.sh /entry.sh diff --git a/entry.sh b/entry.sh index 30fdefd..f58db2e 100755 --- a/entry.sh +++ b/entry.sh @@ -76,6 +76,49 @@ case "$1" in # Return first command exit code exit ${PIPESTATUS[0]} + ;; + ssh-add-pass) + shift # remove argument from array + + # .ssh folder from host is expected to be mounted on /.ssh + # We copy keys from there into /root/.ssh and fix permissions (necessary on Windows hosts) + host_ssh_path="/.ssh" + if [ -d $host_ssh_path ]; then + debug_msg "Copying host SSH keys and setting proper permissions..." + cp -av $host_ssh_path/. ~/.ssh/ + chmod 700 ~/.ssh + chmod 600 ~/.ssh/* + chmod 644 ~/.ssh/*.pub + fi + + # Make sure the key exists if provided. + # When $ssh_key_path is empty, ssh-agent will be looking for both id_rsa and id_dsa in the home directory. + ssh_key_path="" + if [ -n "$1" ] && [ -f "/root/.ssh/$1" ]; then + ssh_key_path="/root/.ssh/$1" + shift # remove argument from array + fi + + # Calling ssh-add. This should handle all cases. + _command="/ssh-add-pass.sh $ssh_key_path $@" + debug_msg "Executing: $_command" + + # When $key_path is empty, ssh-agent will be looking for both id_rsa and id_dsa in the home directory. + # NOTE: We do a sed hack here to strip out '/root/.ssh' from the key path in the output from ssh-add, since this + # path may confuse people. + # echo "Press ENTER or CTRL+C to skip entering passphrase (if any)." + $_command 2>&1 0>&1 | sed 's/\/root\/.ssh\///g' + + # Return first command exit code + exit ${PIPESTATUS[0]} + ;; + ssh-add-list) + shift # remove argument from array + + _command="ssh-add -l" + + $_command 2>&1 0>&1 | sed 's/\/root\/.ssh\///g' + exit ${PIPESTATUS[0]} ;; *) exec $@ diff --git a/ssh-add-pass.sh b/ssh-add-pass.sh new file mode 100755 index 0000000..c21a2cb --- /dev/null +++ b/ssh-add-pass.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +if [ $# -ne 2 ] ; then + echo "Usage: ssh-add-pass.sh keyfile passfile" + exit 1 +fi + +pass=$(cat $2) + +expect << EOF + spawn ssh-add $1 + expect "Enter passphrase" + send "$pass\r" + expect eof +EOF \ No newline at end of file