Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib/ssh): make ssh-agent calls nondestructive #48

Merged
merged 1 commit into from
Oct 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,6 @@ function save-vars {
rigger-save-vars -f "${RIGGER_VARS_FILE}" ${@}
}

function setup-ssh-agent {
# generate ssh keys if they don't already exist
if [ ! -f "${DEIS_TEST_AUTH_KEY_FULL}" ]; then
ssh-keygen -t rsa -f "${DEIS_TEST_AUTH_KEY_FULL}" -N ''
fi

if [ ! -f ${HOME}/.ssh/deiskey ]; then
ssh-keygen -q -t rsa -f ~/.ssh/deiskey -N '' -C deiskey
fi

# prepare the SSH agent
rerun_log "Starting ssh-agent and adding keys..."
ssh-add -D 2> /dev/null || eval $(ssh-agent) && ssh-add -D 2> /dev/null
ssh-add "${DEIS_TEST_AUTH_KEY_FULL}" 2> /dev/null
ssh-add "${DEIS_TEST_SSH_KEY}" 2> /dev/null

export GIT_SSH="${DEIS_ROOT}/tests/bin/git-ssh-nokeycheck.sh"
}

function setup-test-hacks {
# cleanup any stale example applications
rm -rf ${DEIS_ROOT}/tests/example-*
Expand Down
33 changes: 32 additions & 1 deletion lib/ssh.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
function ssh-fingerprint {
local private_key_file="${1}"

local fingerprint="$(ssh-keygen -lf "${private_key_file}" 2>/dev/null | awk '{ print $2 }')"
local sshkeygen_string="ssh-keygen -lf"

if ssh-keygen - 2>&1 | grep -q "\-E"; then
sshkeygen_string="ssh-keygen -E md5 -lf"
fi

local fingerprint="$(${sshkeygen_string} "${private_key_file}" 2>/dev/null \
| awk '{ print $2 }' \
| sed s/MD5://)"

if [ $? -ne 0 ]; then
return 1
else
echo "${fingerprint}"
fi
}

function setup-ssh-agent {
# generate ssh keys if they don't already exist
if [ ! -f "${DEIS_TEST_AUTH_KEY_FULL}" ]; then
ssh-keygen -t rsa -f "${DEIS_TEST_AUTH_KEY_FULL}" -N ''
fi

if [ ! -f ${HOME}/.ssh/deiskey ]; then
ssh-keygen -q -t rsa -f ~/.ssh/deiskey -N '' -C deiskey
fi

# prepare the SSH agent
if [ -z ${SSH_AGENT_PID} ]; then
rerun_log "Starting ssh-agent..."
ssh-add -D 2> /dev/null || eval $(ssh-agent) && ssh-add -D 2> /dev/null
fi

rerun_log "Ensuring ssh keys are being served by ssh-agent..."
ssh-add "${DEIS_TEST_AUTH_KEY_FULL}" 2> /dev/null
ssh-add "${DEIS_TEST_SSH_KEY}" 2> /dev/null

export GIT_SSH="${DEIS_ROOT}/tests/bin/git-ssh-nokeycheck.sh"
}
34 changes: 34 additions & 0 deletions tests/ssh-1-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env roundup
#
#/ usage: rerun stubbs:test -m rigger -p ssh [--answers <>]
#

[[ -f ./functions.sh ]] && . ./functions.sh

describe "ssh"

source ../lib/ssh.sh

it_parses_old_ssh_keygen() {
function ssh-keygen {
if [ "${1}" == "-" ]; then
echo
elif [ "${1}" == "-lf" ]; then
echo "4096 e8:b6:fa:d3:6f:25:fe:b6:e3:b8:a5:31:ef:53:22:fb [email protected] (RSA)"
fi
}

[ "$(ssh-fingerprint "test")" == "e8:b6:fa:d3:6f:25:fe:b6:e3:b8:a5:31:ef:53:22:fb" ]
}

it_parses_new_ssh_keygen() {
function ssh-keygen {
if [ "${1}" == "-" ]; then
echo " -E ... "
elif [ "${1}" == "-E" ]; then
echo "2048 MD5:c7:e8:c0:2f:37:8e:e2:87:d2:7a:0c:bc:aa:2d:27:85 [email protected] (RSA)"
fi
}

[ "$(ssh-fingerprint "test")" == "c7:e8:c0:2f:37:8e:e2:87:d2:7a:0c:bc:aa:2d:27:85" ]
}