Skip to content

Commit

Permalink
docker: whitelist default host strings (#5752)
Browse files Browse the repository at this point in the history
fixes #5751
  • Loading branch information
nicks authored May 1, 2022
1 parent feb62f6 commit 7acc38a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 13 deletions.
54 changes: 44 additions & 10 deletions internal/docker/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type fakeClientCreator struct {
func (c fakeClientCreator) FromCLI(ctx context.Context) (DaemonClient, error) {
host := os.Getenv("DOCKER_HOST")
if host == "" {
host = "cli"
host = "unix:///var/run/docker.sock"
}
return hostClient{Host: host}, nil
}
Expand All @@ -133,10 +133,10 @@ func TestProvideClusterProduct(t *testing.T) {
cases := []provideEnvTestCase{
{
expectedCluster: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
expectedLocal: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
{
Expand All @@ -160,17 +160,17 @@ func TestProvideClusterProduct(t *testing.T) {
BuildToKubeContexts: []string{"microk8s-me"},
},
expectedLocal: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
{
env: clusterid.ProductMicroK8s,
runtime: container.RuntimeCrio,
expectedCluster: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
expectedLocal: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
{
Expand All @@ -194,7 +194,7 @@ func TestProvideClusterProduct(t *testing.T) {
BuildToKubeContexts: []string{"minikube-me"},
},
expectedLocal: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
{
Expand All @@ -218,7 +218,7 @@ func TestProvideClusterProduct(t *testing.T) {
BuildToKubeContexts: []string{"minikube-me"},
},
expectedLocal: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
{
Expand Down Expand Up @@ -278,10 +278,10 @@ func TestProvideClusterProduct(t *testing.T) {
"DOCKER_API_VERSION": "1.35",
},
expectedCluster: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
expectedLocal: Env{
Client: hostClient{Host: "cli"},
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
{
Expand All @@ -299,6 +299,40 @@ func TestProvideClusterProduct(t *testing.T) {
Client: hostClient{Host: "localhost:2376"},
},
},
{
env: clusterid.ProductDockerDesktop,
runtime: container.RuntimeDocker,
expectedCluster: Env{
Client: hostClient{Host: "unix:///var/run/docker.sock"},
BuildToKubeContexts: []string{"docker-desktop-me"},
},
expectedLocal: Env{
Client: hostClient{Host: "unix:///var/run/docker.sock"},
BuildToKubeContexts: []string{"docker-desktop-me", "docker-desktop-me"},
},
},
{
env: clusterid.ProductRancherDesktop,
runtime: container.RuntimeDocker,
expectedCluster: Env{
Client: hostClient{Host: "unix:///var/run/docker.sock"},
BuildToKubeContexts: []string{"rancher-desktop-me"},
},
expectedLocal: Env{
Client: hostClient{Host: "unix:///var/run/docker.sock"},
BuildToKubeContexts: []string{"rancher-desktop-me"},
},
},
{
env: clusterid.ProductRancherDesktop,
runtime: container.RuntimeContainerd,
expectedCluster: Env{
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
expectedLocal: Env{
Client: hostClient{Host: "unix:///var/run/docker.sock"},
},
},
}

for i, c := range cases {
Expand Down
22 changes: 19 additions & 3 deletions internal/docker/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,32 @@ func isOldMinikube(ctx context.Context, minikubeClient k8s.MinikubeClient) bool

func isDefaultHost(e Env) bool {
host := e.DaemonHost()
if host == "" {
isStandardHost :=
// Check all the "standard" docker localhosts.
host == "" ||

// https://github.com/docker/cli/blob/a32cd16160f1b41c1c4ae7bee4dac929d1484e59/opts/hosts.go#L22
host == "tcp://localhost:2375" ||
host == "tcp://localhost:2376" ||
host == "tcp://127.0.0.1:2375" ||
host == "tcp://127.0.0.1:2376" ||

// https://github.com/moby/moby/blob/master/client/client_windows.go#L4
host == "npipe:////./pipe/docker_engine" ||

// https://github.com/moby/moby/blob/master/client/client_unix.go#L6
host == "unix:///var/run/docker.sock"
if isStandardHost {
return true
}

defaultHost, err := opts.ParseHost(true, "")
defaultParseHost, err := opts.ParseHost(true, "")
if err != nil {
return false
}

return host == defaultHost
return host == defaultParseHost

}

func willBuildToKubeContext(product clusterid.Product, kubeContext k8s.KubeContext, env Env) bool {
Expand Down

0 comments on commit 7acc38a

Please sign in to comment.