Skip to content

Commit

Permalink
Merge pull request #2 from docksal/develop
Browse files Browse the repository at this point in the history
Release 1.2.0
  • Loading branch information
lmakarov committed Oct 18, 2018
2 parents 46fe0f6 + d8a6a76 commit 0108e1e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ language: generic
env:
REPO: docksal/ssh-agent
IMAGE_DNS: ${REPO}:dev
DOCKSAL_VERSION: develop
DOCKSAL_VERSION: feature/ssh-key

services:
- docker
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ RUN apk add --no-cache \
socat \
&& rm -rf /var/cache/apk/*

COPY run.sh /run.sh
COPY bin /usr/local/bin

ENV SSH_DIR /.ssh
ENV SOCKET_DIR /.ssh-agent
ENV SSH_AUTH_SOCK ${SOCKET_DIR}/socket
ENV SSH_AUTH_PROXY_SOCK ${SOCKET_DIR}/proxy-socket

VOLUME ${SOCKET_DIR}

ENTRYPOINT ["/run.sh"]
ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["ssh-agent"]
20 changes: 20 additions & 0 deletions bin/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

set -e # Abort if anything fails

# Create the temporary key storage directory
mkdir -p ${SSH_DIR}

# Service mode
if [[ "$1" == "ssh-agent" ]]; then
# Create proxy-socket for ssh-agent (to give anyone accees to the ssh-agent socket)
echo "Creating proxy socket..."
rm ${SSH_AUTH_SOCK} ${SSH_AUTH_PROXY_SOCK} || true
socat UNIX-LISTEN:${SSH_AUTH_PROXY_SOCK},perm=0666,fork UNIX-CONNECT:${SSH_AUTH_SOCK} &
echo "Launching ssh-agent..."
# Start ssh-agent
exec /usr/bin/ssh-agent -a ${SSH_AUTH_SOCK} -d
# Command mode
else
exec "$@"
fi
107 changes: 107 additions & 0 deletions bin/ssh-key
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bash

DEBUG=${DEBUG:-0}
# Print a debug message if debug mode is on
# @param message
echo_debug ()
{
[[ "${DEBUG}" != 0 ]] && echo "$(date +"%F %H:%M:%S") | $@"
}

# Helper function to check whether an key is already loaded in the ssh-agent
# This is useful for keys with a passphrase, which otherwise would require the user to re-enter it.
# @param $1 ssh key name
ssh_key_loaded ()
{
# Get fingerprints for keys already loaded in the agent
# This function may be called multiple times, so we should cache this value
if [[ "$existing_fingerprints" == "" ]]; then
existing_fingerprints=$(ssh-add -l)
export existing_fingerprints
fi

new_fingerprint=$(ssh-keygen -lf ${SSH_DIR}/${1} | awk '{print $2}')

[[ ${existing_fingerprints} == *${new_fingerprint}* ]]
}

ssh_key_add ()
{
# Fix permissions on keys before trying to add them to the agent
chmod 700 ${SSH_DIR}
chmod 600 ${SSH_DIR}/* >/dev/null 2>&1 || true
chmod 644 ${SSH_DIR}/*.pub >/dev/null 2>&1 || true

# Make sure the key exists if provided.
# Otherwise we may be getting an argumet, which we'll handle late.
# When $ssh_key_path is empty, ssh-agent will be looking for both id_rsa and id_dsa in the home directory.
if [[ "${1}" != "" ]] && [[ -f "${SSH_DIR}/${1}" ]]; then
ssh_key_name="${1}"
ssh_key_path="${SSH_DIR}/${ssh_key_name}"

# Check whether the key is already loaded in the agent and skip adding if so.
if ssh_key_loaded ${ssh_key_name}; then
echo "Key '${ssh_key_name}' already loaded in the agent. Skipping."
return 0
fi
fi

# Calling ssh-add. This should handle all arguments cases.
_command="ssh-add ${ssh_key_path}"
echo_debug "Executing: ${_command}"
# We do a sed hack here to strip out the key path in the output from ssh-add, since it may confuse people.
${_command} 2>&1 0>&1 | sed "s|${SSH_DIR}/||g"
ret=${PIPESTATUS[0]}

# Remove the key immediately
rm -f /.ssh/${ssh_key_name}

# Return the exit code from ssh-add above
return ${ret}
}

ssh_key_remove ()
{
ssh-add -D
}

ssh_key_list ()
{
# We do a sed hack here to strip out the key path in the output from ssh-add, since it may confuse people.
ssh-add -l 2>&1 0>&1 | sed "s|${SSH_DIR}/||g"
# Return the exit code of th first command in the pipe list
return ${PIPESTATUS[0]}
}

ssh_key_new ()
{
echo "$@"
}

#-------------------------- RUNTIME STARTS HERE ----------------------------

# Parse other parameters
case "$1" in
add)
shift
ssh_key_add "$@"
;;
rm)
shift
ssh_key_remove "$@"
;;
ls)
shift
ssh_key_list "$@"
;;
new)
shift
ssh_key_list "$@"
;;
debug)
shift
eval "$@"
;;
*)
echo "Usage: $0 add|rm|ls|new"
esac
60 changes: 0 additions & 60 deletions run.sh

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ teardown() {
ssh-keygen -t rsa -b 4096 -f ${ssh_key_file} -q -N ""

# Add the key to the agent
run fin ssh-add ${ssh_key_name}
run fin ssh-key add ${ssh_key_name}
# Cleanup garbage \r from the output otherwise there won't be an exact match
[[ "$(echo ${output} | tr -d '\r')" == "Identity added: ${ssh_key_name} (${ssh_key_name})" ]]
unset output

# Check they key is present in the agent
run fin ssh-add -l
run fin ssh-key ls
[[ ${output} == *${ssh_key_name}* ]]
unset output

Expand Down

0 comments on commit 0108e1e

Please sign in to comment.