Skip to content

Commit

Permalink
Ignore non-socks5 ALL_PROXY env var when checking docker status
Browse files Browse the repository at this point in the history
Signed-off-by: Ling Samuel <[email protected]>
  • Loading branch information
lingsamuel committed Jan 8, 2021
1 parent 6d763a1 commit e127bc2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/minikube/cmd/docker-env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"net"
"net/url"
"os"
"os/exec"
"strconv"
Expand Down Expand Up @@ -455,9 +456,41 @@ func dockerEnvVarsList(ec DockerEnvConfig) []string {
}
}

func isValidDockerProxy(env string) bool {
val := os.Getenv(env)
if val == "" {
return true
}

u, err := url.Parse(val)
if err != nil {
klog.Warningf("Parsing proxy env variable %s=%s error: %v", env, val, err)
return false
}
switch u.Scheme {
// See moby/moby#25740
case "socks5", "socks5h":
return true
default:
return false
}
}

func removeInvalidDockerProxy() {
for _, env := range []string{"ALL_PROXY", "all_proxy"} {
if !isValidDockerProxy(env) {
klog.Warningf("Ignoring non socks5 proxy env variable %s=%s", env, os.Getenv(env))
os.Unsetenv(env)
}
}
}

// tryDockerConnectivity will try to connect to docker env from user's POV to detect the problem if it needs reset or not
func tryDockerConnectivity(bin string, ec DockerEnvConfig) ([]byte, error) {
c := exec.Command(bin, "version", "--format={{.Server}}")

// See #10098 for details
removeInvalidDockerProxy()
c.Env = append(os.Environ(), dockerEnvVarsList(ec)...)
klog.Infof("Testing Docker connectivity with: %v", c)
return c.CombinedOutput()
Expand Down
35 changes: 35 additions & 0 deletions cmd/minikube/cmd/docker-env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cmd

import (
"bytes"
"os"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -306,3 +307,37 @@ MINIKUBE_ACTIVE_DOCKERD
})
}
}

func TestValidDockerProxy(t *testing.T) {
var tests = []struct {
proxy string
isValid bool
}{
{
proxy: "socks5://192.168.0.1:1080",
isValid: true,
},
{
proxy: "",
isValid: true,
},
{
proxy: "socks://192.168.0.1:1080",
isValid: false,
},
{
proxy: "http://192.168.0.1:1080",
isValid: false,
},
}

for _, tc := range tests {
os.Setenv("ALL_PROXY", tc.proxy)
valid := isValidDockerProxy("ALL_PROXY")
if tc.isValid && valid != tc.isValid {
t.Errorf("Expect %#v to be valid docker proxy", tc.proxy)
} else if !tc.isValid && valid != tc.isValid {
t.Errorf("Expect %#v to be invalid docker proxy", tc.proxy)
}
}
}

0 comments on commit e127bc2

Please sign in to comment.