Skip to content

Commit

Permalink
feat: use the default capabilities of DefaultTransport, including use…
Browse files Browse the repository at this point in the history
…s HTTP proxies as directed by the environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY (or the lowercase versions thereof)
  • Loading branch information
yndu13 committed Nov 20, 2024
1 parent 115dc39 commit b73ef4c
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ['1.12', '1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20', '1.21', '1.22']
go: ['1.13', '1.14', '1.15', '1.16', '1.17', '1.18', '1.19', '1.20', '1.21', '1.22']
fail-fast: false

steps:
Expand Down
2 changes: 1 addition & 1 deletion credentials/internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Do(req *Request) (res *Response, err error) {
httpClient.Timeout = req.ReadTimeout + req.ConnectTimeout
}

transport := &http.Transport{}
transport := http.DefaultTransport.(*http.Transport).Clone()
if req.Proxy != "" {
var proxy *url.URL
proxy, err = url.Parse(req.Proxy)
Expand Down
7 changes: 7 additions & 0 deletions credentials/providers/cli_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,11 @@ func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) {
assert.Equal(t, "aksecret", cc.AccessKeySecret)
assert.Equal(t, "ststoken", cc.SecurityToken)
assert.Equal(t, "cli_profile/ram_role_arn/ram_role_arn/static_ak", cc.ProviderName)

provider.innerProvider = new(testProvider)
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, "test", cc.AccessKeyId)
assert.Equal(t, "test", cc.AccessKeySecret)
assert.Equal(t, "cli_profile/test", cc.ProviderName)
}
1 change: 1 addition & 0 deletions credentials/providers/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (provider *DefaultCredentialsProvider) GetCredentials() (cc *Credentials, e
if provider.lastUsedProvider != nil {
inner, err1 := provider.lastUsedProvider.GetCredentials()
if err1 != nil {
err = err1
return
}

Expand Down
40 changes: 40 additions & 0 deletions credentials/providers/default_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package providers

import (
"errors"
"os"
"path"
"testing"
Expand Down Expand Up @@ -155,4 +156,43 @@ func TestDefaultCredentialsProvider_GetCredentials(t *testing.T) {
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "aksecret", SecurityToken: "ststoken", ProviderName: "default/cli_profile/ram_role_arn/ram_role_arn/static_ak"}, cc)

provider.lastUsedProvider = new(testProvider)
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, "test", cc.AccessKeyId)
assert.Equal(t, "test", cc.AccessKeySecret)
assert.Equal(t, "default/test", cc.ProviderName)

provider.lastUsedProvider = new(testErrorProvider)
_, err = provider.GetCredentials()
assert.Equal(t, "error", err.Error())
}

type testProvider struct {
}

func (provider *testProvider) GetCredentials() (cc *Credentials, err error) {
cc = &Credentials{
AccessKeyId: "test",
AccessKeySecret: "test",
ProviderName: "",
}
return
}

func (provider *testProvider) GetProviderName() string {
return "test"
}

type testErrorProvider struct {
}

func (provider *testErrorProvider) GetCredentials() (cc *Credentials, err error) {
err = errors.New("error")
return
}

func (provider *testErrorProvider) GetProviderName() string {
return "test"
}
11 changes: 11 additions & 0 deletions credentials/providers/profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,15 @@ func TestProfileCredentialsProviderGetCredentials(t *testing.T) {
assert.Equal(t, "aksecret", cc.AccessKeySecret)
assert.Equal(t, "ststoken", cc.SecurityToken)
assert.Equal(t, "profile/ram_role_arn/static_ak", cc.ProviderName)

provider.innerProvider = new(testProvider)
cc, err = provider.GetCredentials()
assert.Nil(t, err)
assert.Equal(t, "test", cc.AccessKeyId)
assert.Equal(t, "test", cc.AccessKeySecret)
assert.Equal(t, "profile/test", cc.ProviderName)

provider.innerProvider = new(testErrorProvider)
_, err = provider.GetCredentials()
assert.Equal(t, "error", err.Error())
}

0 comments on commit b73ef4c

Please sign in to comment.