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

fix(machine): throw error connection refused after set proxy #18936

Merged
merged 1 commit into from
Jun 20, 2023
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
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
BlackHole1 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
}