Skip to content

Commit

Permalink
Merge pull request concourse#141 from boazy/propgate_proxy_settings
Browse files Browse the repository at this point in the history
Propagate worker proxy settings when building docker images
  • Loading branch information
vito authored Dec 19, 2017
2 parents bc4dc2e + ffbb60b commit 181b855
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
8 changes: 8 additions & 0 deletions assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ elif [ -n "$build" ]; then

expanded_build_args=()

# propagate proxy settings to image builder
for proxy_var in http_proxy https_proxy no_proxy ; do
if [ -n "${!proxy_var:-}" ]; then
expanded_build_args+=("--build-arg")
expanded_build_args+=("${proxy_var}=${!proxy_var}")
fi
done

build_arg_keys=($(echo "$build_args" | jq -r 'keys | join(" ")'))
if [ "${#build_arg_keys[@]}" -gt 0 ]; then
for key in "${build_arg_keys[@]}"; do
Expand Down
72 changes: 71 additions & 1 deletion tests/out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package docker_image_resource_test

import (
"bytes"
"fmt"
"os/exec"

"encoding/json"
Expand All @@ -20,9 +21,21 @@ var _ = Describe("Out", func() {
os.Setenv("LOG_FILE", "/dev/stderr")
})

put := func(params map[string]interface{}) *gexec.Session {
putWithEnv := func(params map[string]interface{}, extraEnv map[string]string) *gexec.Session {
command := exec.Command("/opt/resource/out", "/tmp")

// Get current process environment variables
newEnv := os.Environ()
if extraEnv != nil {
// Append each extra environment variable to new process environment
// variable list
for name, value := range extraEnv {
newEnv = append(newEnv, fmt.Sprintf("%s=%s", name, value))
}
}

command.Env = newEnv

resourceInput, err := json.Marshal(params)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -34,6 +47,10 @@ var _ = Describe("Out", func() {
return session
}

put := func(params map[string]interface{}) *gexec.Session {
return putWithEnv(params, nil)
}

dockerarg := func(cmd string) string {
return "DOCKER ARG: " + cmd
}
Expand Down Expand Up @@ -145,4 +162,57 @@ var _ = Describe("Out", func() {
Expect(session.Err).To(gbytes.Say(docker("pull 123123.dkr.ecr.us-west-2.amazonaws.com:443/testing")))
})
})

Context("When all proxy settings are provided with build args", func() {
It("passes the arguments correctly to the docker daemon", func() {
session := putWithEnv(map[string]interface{}{
"source": map[string]interface{}{
"repository": "test",
},
"params": map[string]interface{}{
"build": "/docker-image-resource/tests/fixtures/build",
"build_args": map[string]string{
"arg1": "arg with space",
"arg2": "arg with\nnewline",
"arg3": "normal",
},
},
}, map[string]string{
"no_proxy": "10.1.1.1",
"http_proxy": "http://admin:[email protected]:8080",
"https_proxy": "http://another.proxy.net",
})

Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`http_proxy=http://admin:[email protected]:8080`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`https_proxy=http://another.proxy.net`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`no_proxy=10.1.1.1`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`arg1=arg with space`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`arg2=arg with\nnewline`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`arg3=normal`)))
})
})

Context("When only http_proxy setting is provided, with no build arguments", func() {
It("passes the arguments correctly to the docker daemon", func() {
session := putWithEnv(map[string]interface{}{
"source": map[string]interface{}{
"repository": "test",
},
"params": map[string]interface{}{
"build": "/docker-image-resource/tests/fixtures/build",
},
}, map[string]string{
"http_proxy": "http://admin:[email protected]:8080",
})

Expect(session.Err).To(gbytes.Say(dockerarg(`--build-arg`)))
Expect(session.Err).To(gbytes.Say(dockerarg(`http_proxy=http://admin:[email protected]:8080`)))
})
})
})

0 comments on commit 181b855

Please sign in to comment.