Skip to content

Commit

Permalink
use internal utils methods and deprecated old utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and peze committed Aug 19, 2024
1 parent a56d9ac commit 3589f7b
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 10 deletions.
2 changes: 1 addition & 1 deletion credentials/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion credentials/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion credentials/ecs_ram_role_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down
36 changes: 36 additions & 0 deletions credentials/internal/utils/runtime.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
24 changes: 24 additions & 0 deletions credentials/internal/utils/runtime_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
2 changes: 1 addition & 1 deletion credentials/oidc_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion credentials/oidc_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion credentials/ram_role_arn_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion credentials/ram_role_arn_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion credentials/rsa_key_pair_credentials_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion credentials/rsa_key_pair_credentials_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion credentials/uri_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions credentials/utils/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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{
Expand Down
6 changes: 6 additions & 0 deletions credentials/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -88,13 +92,15 @@ 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)

return time.Now().In(gmt).Format("2006-01-02T15:04:05Z")
}

// 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 {
Expand Down

0 comments on commit 3589f7b

Please sign in to comment.