Skip to content

Commit

Permalink
Add option to set TLS from environment variable
Browse files Browse the repository at this point in the history
Add option to set TLS in default docker config from environment variable DOCKER_TLS, same as docker cli (docker/cli#863)
Adds tests
  • Loading branch information
mtatheonly authored and Marcos Tadeu de Andrade committed Feb 12, 2020
1 parent ecf5524 commit cbcefe1
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/docker/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ func GetDefaultDockerConfig() *api.DockerConfig {
cfg.TLSVerify = true
}

if useTLS := os.Getenv("DOCKER_TLS"); useTLS != "" {
cfg.UseTLS = true
}

return cfg
}

Expand Down
66 changes: 66 additions & 0 deletions pkg/docker/util_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package docker

import (
"os"
"testing"

cliconfig "github.com/docker/docker/cli/config"
"github.com/openshift/source-to-image/pkg/api/constants"
"github.com/openshift/source-to-image/pkg/util/user"
)
Expand Down Expand Up @@ -195,3 +197,67 @@ func TestCheckAllowedUser(t *testing.T) {
}
}
}

func TestGetDefaultDockerConfig(t *testing.T) {
tests := []struct {
envHost string
envCertPath string
envTLSVerify string
envTLS string
expectedHost string
expectedCertFile string
expectedTLSVerify bool
expectedTLS bool
}{
{
envHost: "tcp://docker:2376",
envCertPath: "/expected/cert/path",
envTLSVerify: "true",
envTLS: "true",

expectedHost: "tcp://docker:2376",
expectedCertFile: "/expected/cert/path/cert.pem",
expectedTLSVerify: true,
expectedTLS: true,
},
{
envHost: "",
envCertPath: "",
envTLSVerify: "",
envTLS: "",

expectedHost: "unix:///var/run/docker.sock",
expectedCertFile: cliconfig.Dir() + "/cert.pem",
expectedTLSVerify: false,
expectedTLS: false,
},
}
for _, tc := range tests {
oldHost := os.Getenv("DOCKER_HOST")
oldCertPath := os.Getenv("DOCKER_CERT_PATH")
oldTLSVerify := os.Getenv("DOCKER_TLS_VERIFY")
oldTLS := os.Getenv("DOCKER_TLS")
os.Setenv("DOCKER_HOST", tc.envHost)
os.Setenv("DOCKER_CERT_PATH", tc.envCertPath)
os.Setenv("DOCKER_TLS_VERIFY", tc.envTLSVerify)
os.Setenv("DOCKER_TLS", tc.envTLS)
defer os.Setenv("DOCKER_HOST", oldHost)
defer os.Setenv("DOCKER_CERT_PATH", oldCertPath)
defer os.Setenv("DOCKER_TLS_VERIFY", oldTLSVerify)
defer os.Setenv("DOCKER_TLS", oldTLS)

cfg := GetDefaultDockerConfig()
if tc.expectedHost != cfg.Endpoint {
t.Errorf("Endpoint: expected '%s', but got '%s'", tc.expectedHost, cfg.Endpoint)
}
if tc.expectedCertFile != cfg.CertFile {
t.Errorf("CertFile: expected '%s', but got '%s'", tc.expectedCertFile, cfg.CertFile)
}
if tc.expectedTLSVerify != cfg.TLSVerify {
t.Errorf("TLSVerify: expected '%t', but got '%t'", tc.expectedTLSVerify, cfg.TLSVerify)
}
if tc.expectedTLS != cfg.UseTLS {
t.Errorf("UseTLS: expected '%t', but got '%t'", tc.expectedTLS, cfg.UseTLS)
}
}
}

0 comments on commit cbcefe1

Please sign in to comment.