From 5b4950e2cb9c3e9587fa1196d4e80c0d75e941d0 Mon Sep 17 00:00:00 2001 From: He Guimin Date: Fri, 5 Jun 2020 14:48:58 +0800 Subject: [PATCH] backend(oss): supports setting HTTPS_PROXY --- backend/remote-state/oss/backend.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backend/remote-state/oss/backend.go b/backend/remote-state/oss/backend.go index 5e7268470..b8681df6f 100644 --- a/backend/remote-state/oss/backend.go +++ b/backend/remote-state/oss/backend.go @@ -30,6 +30,8 @@ import ( "github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/terraform/version" "github.com/mitchellh/go-homedir" + "net/url" + "regexp" ) // New creates a new backend for OSS remote state. @@ -334,6 +336,11 @@ func (b *Backend) configure(ctx context.Context) error { } options = append(options, oss.UserAgent(fmt.Sprintf("%s/%s", TerraformUA, TerraformVersion))) + proxyUrl := getHttpProxyUrl() + if proxyUrl != nil { + options = append(options, oss.Proxy(proxyUrl.String())) + } + client, err := oss.New(endpoint, accessKey, secretKey, options...) b.ossClient = client otsEndpoint := d.Get("tablestore_endpoint").(string) @@ -610,3 +617,20 @@ 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 + } + } + return nil +}