Skip to content

Commit

Permalink
add test for http proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and yndu13 committed Aug 23, 2024
1 parent b0bdc55 commit b5116a2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 deletions.
13 changes: 5 additions & 8 deletions credentials/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,6 @@ func NewCredential(config *Config) (credential Credential, err error) {
case "credentials_uri":
credential = newURLCredential(tea.StringValue(config.Url))
case "oidc_role_arn":
runtime := &utils.Runtime{
Host: tea.StringValue(config.Host),
Proxy: tea.StringValue(config.Proxy),
ReadTimeout: tea.IntValue(config.Timeout),
ConnectTimeout: tea.IntValue(config.ConnectTimeout),
}

provider, err := providers.NewOIDCCredentialsProviderBuilder().
WithRoleArn(tea.StringValue(config.RoleArn)).
WithOIDCTokenFilePath(tea.StringValue(config.OIDCTokenFilePath)).
Expand All @@ -220,7 +213,11 @@ func NewCredential(config *Config) (credential Credential, err error) {
WithPolicy(tea.StringValue(config.Policy)).
WithRoleSessionName(tea.StringValue(config.RoleSessionName)).
WithSTSEndpoint(tea.StringValue(config.STSEndpoint)).
WithRuntime(runtime).
WithHttpOptions(&providers.HttpOptions{
Proxy: tea.StringValue(config.Proxy),
ReadTimeout: tea.IntValue(config.Timeout),
ConnectTimeout: tea.IntValue(config.ConnectTimeout),
}).
Build()

if err != nil {
Expand Down
25 changes: 22 additions & 3 deletions credentials/internal/providers/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
Expand All @@ -26,7 +27,8 @@ type OIDCCredentialsProvider struct {
lastUpdateTimestamp int64
expirationTimestamp int64
sessionCredentials *sessionCredentials
runtime *utils.Runtime
// for http options
httpOptions *HttpOptions
}

type OIDCCredentialsProviderBuilder struct {
Expand Down Expand Up @@ -79,8 +81,8 @@ func (b *OIDCCredentialsProviderBuilder) WithSTSEndpoint(stsEndpoint string) *OI
return b
}

func (b *OIDCCredentialsProviderBuilder) WithRuntime(runtime *utils.Runtime) *OIDCCredentialsProviderBuilder {
b.provider.runtime = runtime
func (b *OIDCCredentialsProviderBuilder) WithHttpOptions(httpOptions *HttpOptions) *OIDCCredentialsProviderBuilder {
b.provider.httpOptions = httpOptions
return b
}

Expand Down Expand Up @@ -186,6 +188,23 @@ func (provider *OIDCCredentialsProvider) getCredentials() (session *sessionCrede
httpRequest.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"}
httpClient := &http.Client{}

if provider.httpOptions != nil {
httpClient.Timeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Second
proxy := &url.URL{}
if provider.httpOptions.Proxy != "" {
proxy, err = url.Parse(provider.httpOptions.Proxy)
if err != nil {
return
}
}
trans := &http.Transport{}
if proxy != nil && provider.httpOptions.Proxy != "" {
trans.Proxy = http.ProxyURL(proxy)
}
trans.DialContext = utils.Timeout(time.Duration(provider.httpOptions.ConnectTimeout) * time.Second)
httpClient.Transport = trans
}

httpResponse, err := hookDo(httpClient.Do)(httpRequest)
if err != nil {
return
Expand Down
43 changes: 43 additions & 0 deletions integration/proxy/proxy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package proxy

import (
"os"
"testing"

"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials"
"github.com/stretchr/testify/assert"
)

func TestRAMRoleARNWithInvalidProxy(t *testing.T) {
config := &credentials.Config{
Type: tea.String("ram_role_arn"),
AccessKeyId: tea.String("akid"),
AccessKeySecret: tea.String("aksecret"),
RoleArn: tea.String("rolearn"),
RoleSessionName: tea.String("rolesessionname"),
RoleSessionExpiration: tea.Int(3600),
Proxy: tea.String("https://localhost:3600/"),
}
cred, err := credentials.NewCredential(config)
assert.Nil(t, err)
_, err = cred.GetCredential()
assert.Contains(t, err.Error(), "proxyconnect tcp: dial tcp")
assert.Contains(t, err.Error(), ":3600: connect: connection refused")
}

func TestOIDCWithInvalidProxy(t *testing.T) {
config := &credentials.Config{
Type: tea.String("oidc_role_arn"),
RoleArn: tea.String(os.Getenv("ALIBABA_CLOUD_ROLE_ARN")),
OIDCProviderArn: tea.String(os.Getenv("ALIBABA_CLOUD_OIDC_PROVIDER_ARN")),
OIDCTokenFilePath: tea.String(os.Getenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE")),
RoleSessionName: tea.String("credentials-go-test"),
Proxy: tea.String("https://localhost:3600/"),
}
cred, err := credentials.NewCredential(config)
assert.Nil(t, err)
_, err = cred.GetCredential()
assert.Contains(t, err.Error(), "proxyconnect tcp: dial tcp")
assert.Contains(t, err.Error(), ":3600: connect: connection refused")
}

0 comments on commit b5116a2

Please sign in to comment.