Skip to content

Commit ff51675

Browse files
committed
Add ssh identity support
Signed-off-by: Jason Dictos <jdictos@een.com>
1 parent c87d1c0 commit ff51675

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ RUN apt update && apt install -y --no-install-recommends \
4545
docker-ce \
4646
docker-buildx-plugin \
4747
jq \
48+
openssh-client \
4849
ca-certificates \
4950
xz-utils \
5051
iproute2 \

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,27 @@ version is the image's digest.
277277
* `pull_tag`: *Optional.* **DEPRECATED. Use `get` and `load` instead.** Default
278278
`latest`. The tag of the repository to pull down via `pull_repository`.
279279

280+
* `ssh_identity`: *Optional.* Set to an openssh private SSH key (it can be a file
281+
or an inline key). This identity will be passed to `docker build` via the
282+
`--ssh default` argument through a temporary `ssh-agent` instance.
283+
284+
Examples:
285+
286+
```yaml
287+
ssh_identity: |
288+
-----BEGIN OPENSSH PRIVATE KEY-----
289+
0000000000000000000000000000000000000000000000000000000000000000000000
290+
0000000000000000000000000000000000000000000000000000000000000000000000
291+
0000000000000000000000000000000000000000000000000000000000000000000000
292+
0000000000000000000000000000000000000000000000000000000000000000000000
293+
000000000000000000000000000000000000000000000000000000==
294+
-----END OPENSSH PRIVATE KEY-----
295+
```
296+
297+
```yaml
298+
ssh_identity: /path/to/key
299+
```
300+
280301
* `tag`: **DEPRECATED - Use `tag_file` instead**
281302
* `tag_file`: *Optional.* The value should be a path to a file containing the name
282303
of the tag. When not set, the Docker build will be pushed with tag value set by

assets/out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ import_file=$(jq -r '.params.import_file // ""' < $payload)
126126

127127
pull_repository=$(jq -r '.params.pull_repository // ""' < $payload)
128128
pull_tag=$(jq -r '.params.pull_tag // "latest"' < $payload)
129+
ssh_identity=$(jq -r '.params.ssh_identity // ""' < $payload)
129130
target_name=$(jq -r '.params.target_name // ""' < $payload)
130131

131132
if [ -n "$load" ]; then
@@ -237,6 +238,20 @@ elif [ -n "$build" ]; then
237238
fi
238239
fi
239240

241+
ssh_args=()
242+
if [ -n "$ssh_identity" ]; then
243+
export DOCKER_BUILDKIT=1
244+
eval "$(ssh-agent)"
245+
trap "ssh-agent -k; $( trap -p EXIT | cut -f2 -d \' )" EXIT
246+
if [ -f "$ssh_identity" ]; then
247+
ssh-add "$ssh_identity"
248+
else
249+
ssh-add <(echo "$ssh_identity")
250+
fi
251+
ssh_args+=("--ssh")
252+
ssh_args+=("default")
253+
fi
254+
240255
target=()
241256
if [ -n "${target_name}" ]; then
242257
target+=("--target")

tests/fixtures/ssh_identity

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN OPENSSH PRIVATE KEY-----
2+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
3+
QyNTUxOQAAACCTcY7/Q4JTr+zc5uuLSndCM8uiMBdf2H3JHTaCw1POrQAAAJiSPsoAkj7K
4+
AAAAAAtzc2gtZWQyNTUxOQAAACCTcY7/Q4JTr+zc5uuLSndCM8uiMBdf2H3JHTaCw1POrQ
5+
AAAEBhwFGOegUZ/wTf18i/9SNbDgZ0P/BJtPUoGHdvi2bNtJNxjv9DglOv7Nzm64tKd0Iz
6+
y6IwF1/YfckdNoLDU86tAAAAE3NvbWVvbmVAZXhhbXBsZS5jb20BAg==
7+
-----END OPENSSH PRIVATE KEY-----

tests/out_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,24 @@ var _ = Describe("Out", func() {
484484
})
485485
})
486486

487+
Context("When specifying ssh_identity", func() {
488+
It("should set ssh args", func() {
489+
session := put(map[string]interface{}{
490+
"source": map[string]interface{}{
491+
"repository": "test",
492+
},
493+
"params": map[string]interface{}{
494+
"build": "/docker-image-resource/tests/fixtures/build",
495+
"additional_tags": "/docker-image-resource/tests/fixtures/tags",
496+
"ssh_identity": "/docker-image-resource/tests/fixtures/ssh_identity",
497+
},
498+
},
499+
)
500+
Expect(session.Err).To(gbytes.Say(dockerarg(`--ssh`)))
501+
Expect(session.Err).To(gbytes.Say(dockerarg(`default`)))
502+
})
503+
})
504+
487505
Context("When passing additional_tags ", func() {
488506
It("should push add the additional_tags", func() {
489507
session := put(map[string]interface{}{

0 commit comments

Comments
 (0)