Skip to content

Commit

Permalink
Merge pull request #18936 from BlackHole1/fix-proxy
Browse files Browse the repository at this point in the history
fix(machine): throw error `connection refused` after set proxy
  • Loading branch information
openshift-merge-robot authored Jun 20, 2023
2 parents 3907df6 + 81e6322 commit a77f896
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 5 deletions.
10 changes: 9 additions & 1 deletion pkg/machine/ignition.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"strings"

"github.com/containers/common/pkg/config"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -613,7 +614,14 @@ func GetProxyVariables() map[string]string {
proxyOpts := make(map[string]string)
for _, variable := range config.ProxyEnv {
if value, ok := os.LookupEnv(variable); ok {
proxyOpts[variable] = value
if value == "" {
continue
}

// TODO: use constants for host.containers.internal
v := strings.ReplaceAll(value, "127.0.0.1", "host.containers.internal")
v = strings.ReplaceAll(v, "localhost", "host.containers.internal")
proxyOpts[variable] = v
}
}
return proxyOpts
Expand Down
75 changes: 71 additions & 4 deletions pkg/machine/qemu/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,55 @@ func TestEditCmd(t *testing.T) {
}

func TestPropagateHostEnv(t *testing.T) {
t.Setenv("SSL_CERT_FILE", "/some/foo.cert")
t.Setenv("SSL_CERT_DIR", "/some/my/certs")
t.Setenv("HTTP_PROXY", "proxy")
tests := map[string]struct {
value string
expect string
}{
"HTTP_PROXY": {
"proxy",
"equal",
},
"ftp_proxy": {
"domain.com:8888",
"equal",
},
"FTP_PROXY": {
"proxy",
"equal",
},
"NO_PROXY": {
"localaddress",
"equal",
},
"HTTPS_PROXY": {
"",
"unset",
},
"no_proxy": {
"",
"unset",
},
"http_proxy": {
"127.0.0.1:8888",
"host.containers.internal:8888",
},
"https_proxy": {
"localhost:8888",
"host.containers.internal:8888",
},
"SSL_CERT_FILE": {
"/some/f=oo.cert",
fmt.Sprintf("%s/f=oo.cert", machine.UserCertsTargetPath),
},
"SSL_CERT_DIR": {
"/some/my/certs",
machine.UserCertsTargetPath,
},
}

for key, item := range tests {
t.Setenv(key, item.value)
}

cmdLine := propagateHostEnv(make([]string, 0))

Expand All @@ -36,5 +82,26 @@ func TestPropagateHostEnv(t *testing.T) {
tokens := strings.Split(cmdLine[1], ",string=")
decodeString, err := base64.StdEncoding.DecodeString(tokens[1])
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("HTTP_PROXY=\"proxy\"|SSL_CERT_FILE=\"%s/foo.cert\"|SSL_CERT_DIR=%q", machine.UserCertsTargetPath, machine.UserCertsTargetPath), string(decodeString))

// envsRawArr looks like: ["BAR=\"bar\"", "FOO=\"foo\""]
envsRawArr := strings.Split(string(decodeString), "|")
// envs looks like: {"BAR": "bar", "FOO": "foo"}
envs := make(map[string]string)
for _, env := range envsRawArr {
item := strings.SplitN(env, "=", 2)
envs[item[0]] = strings.Trim(item[1], "\"")
}

for key, test := range tests {
switch test.expect {
case "equal":
assert.Equal(t, envs[key], test.value)
case "unset":
if _, ok := envs[key]; ok {
t.Errorf("env %s should not be set", key)
}
default:
assert.Equal(t, envs[key], test.expect)
}
}
}

0 comments on commit a77f896

Please sign in to comment.