diff --git a/credentials/credential.go b/credentials/credential.go index 5ad2871..2ab96f3 100644 --- a/credentials/credential.go +++ b/credentials/credential.go @@ -12,9 +12,9 @@ import ( "github.com/alibabacloud-go/debug/debug" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" "github.com/aliyun/credentials-go/credentials/response" - "github.com/aliyun/credentials-go/credentials/utils" ) var debuglog = debug.Init("credential") diff --git a/credentials/credential_test.go b/credentials/credential_test.go index d5529a2..a33dbbc 100644 --- a/credentials/credential_test.go +++ b/credentials/credential_test.go @@ -5,8 +5,8 @@ import ( "testing" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" - "github.com/aliyun/credentials-go/credentials/utils" "github.com/stretchr/testify/assert" ) diff --git a/credentials/ecs_ram_role_credentials_provider.go b/credentials/ecs_ram_role_credentials_provider.go index 70a0520..5a54d7b 100644 --- a/credentials/ecs_ram_role_credentials_provider.go +++ b/credentials/ecs_ram_role_credentials_provider.go @@ -7,8 +7,8 @@ import ( "time" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" - "github.com/aliyun/credentials-go/credentials/utils" ) var securityCredURL = "http://100.100.100.200/latest/meta-data/ram/security-credentials/" diff --git a/credentials/internal/utils/runtime.go b/credentials/internal/utils/runtime.go new file mode 100644 index 0000000..432395c --- /dev/null +++ b/credentials/internal/utils/runtime.go @@ -0,0 +1,36 @@ +package utils + +import ( + "context" + "net" + "time" +) + +// Runtime is for setting timeout, proxy and host +type Runtime struct { + ReadTimeout int + ConnectTimeout int + Proxy string + Host string + STSEndpoint string +} + +// NewRuntime returns a Runtime +func NewRuntime(readTimeout, connectTimeout int, proxy string, host string) *Runtime { + return &Runtime{ + ReadTimeout: readTimeout, + ConnectTimeout: connectTimeout, + Proxy: proxy, + Host: host, + } +} + +// Timeout is for connect Timeout +func Timeout(connectTimeout time.Duration) func(cxt context.Context, net, addr string) (c net.Conn, err error) { + return func(ctx context.Context, network, address string) (net.Conn, error) { + return (&net.Dialer{ + Timeout: connectTimeout, + DualStack: true, + }).DialContext(ctx, network, address) + } +} diff --git a/credentials/internal/utils/runtime_test.go b/credentials/internal/utils/runtime_test.go new file mode 100644 index 0000000..873744e --- /dev/null +++ b/credentials/internal/utils/runtime_test.go @@ -0,0 +1,24 @@ +package utils + +import ( + "context" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func Test_NewRuntime(t *testing.T) { + runitme := NewRuntime(10, 10, "proxy", "host") + assert.Equal(t, 10, runitme.ReadTimeout) + assert.Equal(t, 10, runitme.ConnectTimeout) + assert.Equal(t, "proxy", runitme.Proxy) + assert.Equal(t, "host", runitme.Host) + + dialContext := Timeout(5 * time.Second) + ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second) + assert.NotNil(t, cancelFunc) + c, err := dialContext(ctx, "127.0.0.1", "127.0.0.2") + assert.Nil(t, c) + assert.Equal(t, "dial 127.0.0.1: unknown network 127.0.0.1", err.Error()) +} diff --git a/credentials/oidc_credentials_provider.go b/credentials/oidc_credentials_provider.go index 8c70343..272a6da 100644 --- a/credentials/oidc_credentials_provider.go +++ b/credentials/oidc_credentials_provider.go @@ -10,8 +10,8 @@ import ( "time" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" - "github.com/aliyun/credentials-go/credentials/utils" ) // OIDCCredential is a kind of credentials diff --git a/credentials/oidc_credentials_provider_test.go b/credentials/oidc_credentials_provider_test.go index b09fe09..7f69818 100644 --- a/credentials/oidc_credentials_provider_test.go +++ b/credentials/oidc_credentials_provider_test.go @@ -9,7 +9,7 @@ import ( "github.com/stretchr/testify/assert" - "github.com/aliyun/credentials-go/credentials/utils" + "github.com/aliyun/credentials-go/credentials/internal/utils" ) func TestNewOidcCredentialsProvider(t *testing.T) { diff --git a/credentials/ram_role_arn_credentials_provider.go b/credentials/ram_role_arn_credentials_provider.go index a909dd3..71ae004 100644 --- a/credentials/ram_role_arn_credentials_provider.go +++ b/credentials/ram_role_arn_credentials_provider.go @@ -8,8 +8,8 @@ import ( "time" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" - "github.com/aliyun/credentials-go/credentials/utils" ) const defaultDurationSeconds = 3600 diff --git a/credentials/ram_role_arn_credentials_provider_test.go b/credentials/ram_role_arn_credentials_provider_test.go index c9f12df..04c66f2 100644 --- a/credentials/ram_role_arn_credentials_provider_test.go +++ b/credentials/ram_role_arn_credentials_provider_test.go @@ -8,7 +8,7 @@ import ( "strconv" "testing" - "github.com/aliyun/credentials-go/credentials/utils" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/stretchr/testify/assert" ) diff --git a/credentials/rsa_key_pair_credentials_provider.go b/credentials/rsa_key_pair_credentials_provider.go index 4b57072..1d86f29 100644 --- a/credentials/rsa_key_pair_credentials_provider.go +++ b/credentials/rsa_key_pair_credentials_provider.go @@ -8,8 +8,8 @@ import ( "time" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" - "github.com/aliyun/credentials-go/credentials/utils" ) // Deprecated: no more recommend to use it diff --git a/credentials/rsa_key_pair_credentials_provider_test.go b/credentials/rsa_key_pair_credentials_provider_test.go index fa28cbc..5687920 100644 --- a/credentials/rsa_key_pair_credentials_provider_test.go +++ b/credentials/rsa_key_pair_credentials_provider_test.go @@ -5,7 +5,7 @@ import ( "net/http" "testing" - "github.com/aliyun/credentials-go/credentials/utils" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/stretchr/testify/assert" ) diff --git a/credentials/uri_credential.go b/credentials/uri_credential.go index 8ac897e..56da596 100644 --- a/credentials/uri_credential.go +++ b/credentials/uri_credential.go @@ -7,8 +7,8 @@ import ( "time" "github.com/alibabacloud-go/tea/tea" + "github.com/aliyun/credentials-go/credentials/internal/utils" "github.com/aliyun/credentials-go/credentials/request" - "github.com/aliyun/credentials-go/credentials/utils" ) // URLCredential is a kind of credential diff --git a/credentials/utils/runtime.go b/credentials/utils/runtime.go index 432395c..43830cd 100644 --- a/credentials/utils/runtime.go +++ b/credentials/utils/runtime.go @@ -7,6 +7,7 @@ import ( ) // Runtime is for setting timeout, proxy and host +// Deprecated: it was used for internal type Runtime struct { ReadTimeout int ConnectTimeout int @@ -16,6 +17,7 @@ type Runtime struct { } // NewRuntime returns a Runtime +// Deprecated: it was used for internal func NewRuntime(readTimeout, connectTimeout int, proxy string, host string) *Runtime { return &Runtime{ ReadTimeout: readTimeout, @@ -26,6 +28,7 @@ func NewRuntime(readTimeout, connectTimeout int, proxy string, host string) *Run } // Timeout is for connect Timeout +// Deprecated: it was used for internal func Timeout(connectTimeout time.Duration) func(cxt context.Context, net, addr string) (c net.Conn, err error) { return func(ctx context.Context, network, address string) (net.Conn, error) { return (&net.Dialer{ diff --git a/credentials/utils/utils.go b/credentials/utils/utils.go index 7468407..66457c3 100644 --- a/credentials/utils/utils.go +++ b/credentials/utils/utils.go @@ -30,6 +30,7 @@ var hookRSA = func(fn func(rand io.Reader, priv *rsa.PrivateKey, hash crypto.Has } // GetUUID returns a uuid +// Deprecated: it was used for internal func GetUUID() (uuidHex string) { uuid := newUUID() uuidHex = hex.EncodeToString(uuid[:]) @@ -46,6 +47,7 @@ func RandStringBytes(n int) string { } // ShaHmac1 return a string which has been hashed +// Deprecated: it was used for internal func ShaHmac1(source, secret string) string { key := []byte(secret) hmac := hmac.New(sha1.New, key) @@ -56,6 +58,7 @@ func ShaHmac1(source, secret string) string { } // Sha256WithRsa return a string which has been hashed with Rsa +// Deprecated: it was used for internal func Sha256WithRsa(source, secret string) string { decodeString, err := base64.StdEncoding.DecodeString(secret) if err != nil { @@ -79,6 +82,7 @@ func Sha256WithRsa(source, secret string) string { } // GetMD5Base64 returns a string which has been base64 +// Deprecated: it was used for internal func GetMD5Base64(bytes []byte) (base64Value string) { md5Ctx := md5.New() md5Ctx.Write(bytes) @@ -88,6 +92,7 @@ func GetMD5Base64(bytes []byte) (base64Value string) { } // GetTimeInFormatISO8601 returns a time string +// Deprecated: it was used for internal func GetTimeInFormatISO8601() (timeStr string) { gmt := time.FixedZone("GMT", 0) @@ -95,6 +100,7 @@ func GetTimeInFormatISO8601() (timeStr string) { } // GetURLFormedMap returns a url encoded string +// Deprecated: it was used for internal func GetURLFormedMap(source map[string]string) (urlEncoded string) { urlEncoder := url.Values{} for key, value := range source {