forked from uber-common/docker-ssh-agent-forward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinata-ssh-forward.sh
executable file
·59 lines (49 loc) · 1.47 KB
/
pinata-ssh-forward.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env bash
set -eo pipefail
IMAGE_NAME=uber/ssh-agent-forward:latest
CONTAINER_NAME=pinata-sshd
VOLUME_NAME=ssh-agent
HOST_PORT=2244
AUTHORIZED_KEYS=$(ssh-add -L | base64 | tr -d '\n')
KNOWN_HOSTS_FILE=$(mktemp -t dsaf.XXX)
trap 'rm ${KNOWN_HOSTS_FILE}' EXIT
docker rm -f "${CONTAINER_NAME}" >/dev/null 2>&1 || true
docker volume create --name "${VOLUME_NAME}"
docker run \
--name "${CONTAINER_NAME}" \
-e AUTHORIZED_KEYS="${AUTHORIZED_KEYS}" \
-v ${VOLUME_NAME}:/ssh-agent \
-d \
-p "${HOST_PORT}:22" \
"${IMAGE_NAME}" >/dev/null \
;
if [ "${DOCKER_HOST}" ]; then
HOST_IP=$(echo "$DOCKER_HOST" | awk -F '//' '{print $2}' | awk -F ':' '{print $1}')
else
HOST_IP=127.0.0.1
fi
count=0
while (! ssh-keyscan -p "${HOST_PORT}" "${HOST_IP}" >"${KNOWN_HOSTS_FILE}" 2>/dev/null) && [ $count -le 10 ]; do let count++ || true; sleep 0.5; done
# show the keys that are being forwarded
ssh \
-A \
-o "UserKnownHostsFile=${KNOWN_HOSTS_FILE}" \
-p "${HOST_PORT}" \
-S none \
"root@${HOST_IP}" \
ssh-add -l
# keep the agent running
ssh \
-A \
-f \
-o "UserKnownHostsFile=${KNOWN_HOSTS_FILE}" \
-p "${HOST_PORT}" \
-S none \
"root@${HOST_IP}" \
/ssh-entrypoint.sh
echo 'Agent forwarding successfully started.'
echo 'Run "pinata-ssh-mount" to get a command-line fragment that'
echo 'can be added to "docker run" to mount the SSH agent socket.'
echo ""
echo 'For example:'
echo "docker run -it \$(pinata-ssh-mount) uber/ssh-agent-forward ssh -T [email protected]"