Skip to content

Fix #36897: Resolve NO_PROXY recognition issue in oss remote backend #36898

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 11 additions & 16 deletions internal/backend/remote-state/oss/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"runtime"
"strconv"
"strings"
Expand All @@ -35,6 +34,7 @@ import (
"github.com/hashicorp/terraform/internal/backend"
"github.com/hashicorp/terraform/internal/legacy/helper/schema"
"github.com/hashicorp/terraform/version"
"golang.org/x/net/http/httpproxy"
)

// Deprecated in favor of flattening assume_role_* options
Expand Down Expand Up @@ -414,7 +414,10 @@ func (b *Backend) configure(ctx context.Context) error {
}
options = append(options, oss.UserAgent(fmt.Sprintf("%s/%s", TerraformUA, TerraformVersion)))

proxyUrl := getHttpProxyUrl()
proxyUrl, err := getHttpProxyUrl(endpoint)
if err != nil {
return err
}
if proxyUrl != nil {
options = append(options, oss.Proxy(proxyUrl.String()))
}
Expand Down Expand Up @@ -706,19 +709,11 @@ func getAuthCredentialByEcsRoleName(ecsRoleName string) (accessKey, secretKey, t
return accessKeyId.(string), accessKeySecret.(string), securityToken.(string), nil
}

func getHttpProxyUrl() *url.URL {
for _, v := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"} {
value := strings.Trim(os.Getenv(v), " ")
if value != "" {
if !regexp.MustCompile(`^http(s)?://`).MatchString(value) {
value = fmt.Sprintf("https://%s", value)
}
proxyUrl, err := url.Parse(value)
if err == nil {
return proxyUrl
}
break
}
func getHttpProxyUrl(rawUrl string) (*url.URL, error) {
pc := httpproxy.FromEnvironment()
u, err := url.Parse(rawUrl)
if err != nil {
return nil, err
}
return nil
return pc.ProxyFunc()(u)
}
100 changes: 100 additions & 0 deletions internal/backend/remote-state/oss/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,103 @@ func deleteTablestoreTable(t *testing.T, otsClient *tablestore.TableStoreClient,
t.Logf("WARNING: Failed to delete the test TableStore table %q. It has been left in your Alibaba Cloud account and may incur charges. (error was %s)", tableName, err)
}
}

func TestGetHttpProxyUrl(t *testing.T) {
tests := []struct {
name string
rawUrl string
httpProxy string
httpsProxy string
noProxy string
expectedProxyURL string
}{
{
name: "should set proxy using http_proxy environment variable",
rawUrl: "http://example.com",
httpProxy: "http://foo.bar:3128",
httpsProxy: "https://secure.example.com",
noProxy: "",
expectedProxyURL: "http://foo.bar:3128",
},
{
name: "should set proxy using http_proxy environment variable",
rawUrl: "http://example.com",
httpProxy: "http://foo.barr",
httpsProxy: "https://secure.example.com",
noProxy: "",
expectedProxyURL: "http://foo.barr",
},
{
name: "should set proxy using https_proxy environment variable",
rawUrl: "https://secure.example.com",
httpProxy: "http://foo.bar",
httpsProxy: "https://foo.bar.com:3128",
noProxy: "",
expectedProxyURL: "https://foo.bar.com:3128",
},
{
name: "should set proxy using https_proxy environment variable",
rawUrl: "https://secure.example.com",
httpProxy: "",
httpsProxy: "http://foo.baz",
noProxy: "",
expectedProxyURL: "http://foo.baz",
},
{
name: "should not set http proxy if NO_PROXY contains the host",
rawUrl: "http://example.internal",
httpProxy: "http://foo.bar:3128",
httpsProxy: "",
noProxy: "example.internal",
expectedProxyURL: "",
},
{
name: "should not set HTTP proxy when NO_PROXY matches the domain with suffix",
rawUrl: "http://qqu.example.internal",
httpProxy: "http://foo.bar:3128",
httpsProxy: "",
noProxy: ".example.internal",
expectedProxyURL: "",
},
{
name: "should not set https proxy if NO_PROXY contains the host",
rawUrl: "https://secure.internal",
httpProxy: "",
httpsProxy: "https://foo.baz:3128",
noProxy: "secure.internal",
expectedProxyURL: "",
},
{
name: "should not set https proxy if NO_PROXY matches the domain with suffix",
rawUrl: "https://ss.qcsc.secure.internal",
httpProxy: "",
httpsProxy: "https://foo.baz:3128",
noProxy: ".secure.internal",
expectedProxyURL: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set environment variables
t.Setenv("HTTP_PROXY", tt.httpProxy)
t.Setenv("HTTPS_PROXY", tt.httpsProxy)
t.Setenv("NO_PROXY", tt.noProxy)

proxyUrl, err := getHttpProxyUrl(tt.rawUrl)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if tt.expectedProxyURL == "" {
if proxyUrl != nil {
t.Fatalf("unexpected proxy URL, want nil, got: %s", proxyUrl)
}
} else {
if tt.expectedProxyURL != proxyUrl.String() {
t.Fatalf("unexpected proxy URL, want: %s, got: %s", tt.expectedProxyURL, proxyUrl.String())
}
}
})
}
}
2 changes: 1 addition & 1 deletion internal/backend/remote-state/oss/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/hashicorp/terraform/internal/legacy v0.0.0-00010101000000-000000000000
github.com/jmespath/go-jmespath v0.4.0
github.com/mitchellh/go-homedir v1.1.0
golang.org/x/net v0.38.0
)

require (
Expand Down Expand Up @@ -41,7 +42,6 @@ require (
github.com/stretchr/testify v1.8.4 // indirect
github.com/zclconf/go-cty v1.16.2 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
Expand Down
Loading