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

known_hosts path customization #233

Merged
merged 5 commits into from
Feb 20, 2023
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ Default: `false`

**Important:** enabling this option will share `BUILDKITE_AGENT_TOKEN` environment variable (and others) with the container

### `mount-ssh-agent` (optional, boolean)
### `mount-ssh-agent` (optional, boolean or string)

Whether to automatically mount the ssh-agent socket from the host agent machine into the container (at `/ssh-agent`and `/root/.ssh/known_hosts` respectively), allowing git operations to work correctly.
Whether to mount the ssh-agent socket (at `/ssh-agent`) from the host agent machine into the container or not. Instead of just `true` or `false`, you can specify absolute path in the container for the home directory of the user used to run on which the agent's `.ssh/known_hosts` will be mounted (by default, `/root`).

Default: `false`

Expand Down
16 changes: 9 additions & 7 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ tty_default='on'
interactive_default='on'
init_default='on'
mount_agent_default='off'
mount_ssh_agent=''
pwd_default="$PWD"
workdir_default="/workdir"
agent_mount_folder="/usr/bin/buildkite-agent"
Expand Down Expand Up @@ -238,23 +237,26 @@ if [[ -n "${BUILDKITE_PLUGIN_DOCKER_USERNS:-}" ]]; then
fi

# Mount ssh-agent socket and known_hosts
if [[ "${BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT:-$mount_ssh_agent}" =~ ^(true|on|1)$ ]] ; then
if [[ ! "${BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT:-false}" = 'false' ]] ; then
if [[ -z "${SSH_AUTH_SOCK:-}" ]] ; then
echo "+++ 🚨 \$SSH_AUTH_SOCK isn't set, has ssh-agent started?"
exit 1
fi
if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then
echo "+++ 🚨 There isn't any file at ${SSH_AUTH_SOCK}, has ssh-agent started?"
echo "+++ 🚨 There file at ${SSH_AUTH_SOCK} does not exist or is not a socket, has ssh-agent started?"
exit 1
fi
if [[ ! -S "${SSH_AUTH_SOCK}" ]] ; then
echo "+++ 🚨 The file at ${SSH_AUTH_SOCK} isn't a socket, has ssh-agent started?"
exit 1

if [[ "${BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT:-''}" =~ ^(true|on|1)$ ]]; then
MOUNT_PATH=/root
else
MOUNT_PATH="${BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT}"
fi

args+=(
"--env" "SSH_AUTH_SOCK=/ssh-agent"
"--volume" "${SSH_AUTH_SOCK}:/ssh-agent"
"--volume" "${HOME}/.ssh/known_hosts:/root/.ssh/known_hosts"
"--volume" "${HOME}/.ssh/known_hosts:${MOUNT_PATH}/.ssh/known_hosts"
)
fi

Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ configuration:
mount-buildkite-agent:
type: boolean
mount-ssh-agent:
type: boolean
type: [ boolean, string ]
mount-checkout:
type: boolean
network:
Expand Down
34 changes: 34 additions & 0 deletions tests/command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1167,3 +1167,37 @@ EOF

unstub docker
}

@test "Use ssh agent (true)" {
skip 'Can not create a socket for testing :('
export BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT=true
export SSH_AUTH_SOCK="/tmp/sock"
touch /tmp/sock # does not work as the hook checks that this is a socket

stub docker \
"run -t -i --rm --init --volume \* --workdir \* --env SSH_AUTH_SOCK=/ssh-agent --volume /tmp/sock:/ssh-agent --volume /root/.ssh/known_hosts:/root/.ssh/known_hosts --label \* image:tag /bin/sh -e -c 'pwd' : echo ran command in docker"

run "$PWD"/hooks/command

assert_success
assert_output --partial "ran command in docker"

unstub docker
}

@test "Use ssh agent (with path)" {
skip 'Can not create a socket for testing :('
export BUILDKITE_PLUGIN_DOCKER_MOUNT_SSH_AGENT=/test/path
export SSH_AUTH_SOCK="/tmp/sock"
touch /tmp/sock # does not work as the hook checks that this is a socket

stub docker \
"run -t -i --rm --init --volume \* --workdir \* --env SSH_AUTH_SOCK=/ssh-agent --volume /tmp/sock:/ssh-agent --volume /root/.ssh/known_hosts:/test/path/.ssh/known_hosts --label \* image:tag /bin/sh -e -c 'pwd' : echo ran command in docker"

run "$PWD"/hooks/command

assert_success
assert_output --partial "ran command in docker"

unstub docker
}