Skip to content

Commit

Permalink
Add support for securely passing ssh identity to docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
ericb-summit committed Mar 30, 2021
1 parent 6468b14 commit ab8f021
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ version is the image's digest.
* `pull_tag`: *Optional.* **DEPRECATED. Use `get` and `load` instead.** Default
`latest`. The tag of the repository to pull down via `pull_repository`.

* `ssh_identity`: *Optional.* Only applies when `docker_buildkit` is set to `1`.
Set to an openssh private SSH key (i.e. -----BEGIN OPENSSH PRIVATE KEY----- ...),
this identity will be passed to `docker build` via the `--ssh default` argument
through a temporary `ssh-agent` instance.

* `tag`: **DEPRECATED - Use `tag_file` instead**
* `tag_file`: *Optional.* The value should be a path to a file containing the name
of the tag. When not set, the Docker build will be pushed with tag value set by
Expand Down
10 changes: 10 additions & 0 deletions assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import_file=$(jq -r '.params.import_file // ""' < $payload)

pull_repository=$(jq -r '.params.pull_repository // ""' < $payload)
pull_tag=$(jq -r '.params.pull_tag // "latest"' < $payload)
ssh_identity=$(jq -r '.params.ssh_identity // ""' < $payload)
target_name=$(jq -r '.params.target_name // ""' < $payload)

if [ -n "$load" ]; then
Expand Down Expand Up @@ -224,6 +225,15 @@ elif [ -n "$build" ]; then
fi
fi

ssh_args=()
if [ -n "$ssh_identity" ] && [ "$DOCKER_BUILDKIT" -eq 1 ]; then
eval "$(ssh-agent)"
trap "ssh-agent -k; $( trap -p EXIT | cut -f2 -d \' )" EXIT
ssh-add <(echo "$ssh_identity")
ssh_args+=("--ssh")
ssh_args+=("default")
fi

target=()
if [ -n "${target_name}" ]; then
target+=("--target")
Expand Down
1 change: 1 addition & 0 deletions dockerfiles/alpine/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUN apk --no-cache add \
xz \
util-linux \
tar \
openssh-client \
;
COPY --from=builder /assets /opt/resource
RUN ln -s /opt/resource/ecr-login /usr/local/bin/docker-credential-ecr-login
Expand Down
3 changes: 2 additions & 1 deletion dockerfiles/ubuntu/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ RUN apt-get update; \
ca-certificates \
curl \
gnupg-agent \
software-properties-common; \
software-properties-common \
openssh-client; \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - ; \
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
Expand Down

0 comments on commit ab8f021

Please sign in to comment.