Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore non-socks5 ALL_PROXY env var when checking docker status #10109

Merged
merged 1 commit into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}