Skip to content

Commit

Permalink
Merge pull request #257 from crimson-gao/region-helper
Browse files Browse the repository at this point in the history
auto parse region and use signature v4 in acdr-ut
  • Loading branch information
crimson-gao authored Feb 27, 2024
2 parents b3bcdc2 + b431e4f commit ef4ec3b
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 6 deletions.
11 changes: 11 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"

"github.com/aliyun/aliyun-log-go-sdk/util"
)

// GlobalForceUsingHTTP if GlobalForceUsingHTTP is true, then all request will use HTTP(ignore LogProject's UsingHTTP flag)
Expand Down Expand Up @@ -400,3 +403,11 @@ func (c *Client) DeleteProject(name string) error {
func (c *Client) Close() error {
return nil
}

func (c *Client) setSignV4IfInAcdr(endpoint string) {
region, err := util.ParseRegion(endpoint)
if err == nil && strings.Contains(region, "-acdr-ut-") {
c.AuthVersion = AuthV4
c.Region = region
}
}
12 changes: 9 additions & 3 deletions client_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package sls
import (
"net/http"
"time"

"github.com/aliyun/aliyun-log-go-sdk/util"
)

// CreateNormalInterface create a normal client.
Expand All @@ -14,7 +16,7 @@ import (
// provider := NewStaticCredentailsProvider(accessKeyID, accessKeySecret, securityToken)
// client := CreateNormalInterfaceV2(endpoint, provider)
func CreateNormalInterface(endpoint, accessKeyID, accessKeySecret, securityToken string) ClientInterface {
return &Client{
client := &Client{
Endpoint: endpoint,
AccessKeyID: accessKeyID,
AccessKeySecret: accessKeySecret,
Expand All @@ -26,6 +28,8 @@ func CreateNormalInterface(endpoint, accessKeyID, accessKeySecret, securityToken
securityToken,
),
}
client.setSignV4IfInAcdr(endpoint)
return client
}

// CreateNormalInterfaceV2 create a normal client, with a CredentialsProvider.
Expand All @@ -35,13 +39,15 @@ func CreateNormalInterface(endpoint, accessKeyID, accessKeySecret, securityToken
//
// See [credentials_provider.go] for more details.
func CreateNormalInterfaceV2(endpoint string, credentialsProvider CredentialsProvider) ClientInterface {
return &Client{
client := &Client{
Endpoint: endpoint,
credentialsProvider: credentialsProvider,
}
client.setSignV4IfInAcdr(endpoint)
return client
}

type UpdateTokenFunction = func() (accessKeyID, accessKeySecret, securityToken string, expireTime time.Time, err error)
type UpdateTokenFunction = util.UpdateTokenFunction

// CreateTokenAutoUpdateClient create a TokenAutoUpdateClient,
// this client will auto fetch security token and retry when operation is `Unauthorized`
Expand Down
17 changes: 17 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,20 @@ func (s *ClientTestSuite) TestMeteringMode() {
s.Require().NoError(err)
s.Require().Equal(CHARGE_BY_FUNCTION, res.MeteringMode)
}

func (s *ClientTestSuite) TestSignv4Acdr() {
{
client := CreateNormalInterface("https://xx-test-acdr-ut-1-intranet.log.aliyuncs.com", "", "", "")
c := client.(*Client)
s.Equal(c.Region, "xx-test-acdr-ut-1")
s.Equal(c.AuthVersion, AuthV4)
}

{
client := CreateNormalInterface("https://cn-hangzhou-intranet.log.aliyuncs.com", "", "", "")
c := client.(*Client)
s.Equal(c.Region, "")
s.EqualValues(c.AuthVersion, "")
}

}
22 changes: 22 additions & 0 deletions example/signv4/signv4.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
sls "github.com/aliyun/aliyun-log-go-sdk"
"github.com/aliyun/aliyun-log-go-sdk/util"
)

func main() {
accessKeyId, accessKeySecret := "", "" // replace with your access key and secret
endpoint := "cn-hangzhou-intranet.log.aliyuncs.com" // replace with your endpoint

client := sls.CreateNormalInterfaceV2(endpoint,
sls.NewStaticCredentialsProvider(accessKeyId, accessKeySecret, ""))
region, err := util.ParseRegion(endpoint) // parse region from endpoint
if err != nil {
panic(err)
}
client.SetRegion(region) // region must be set if using signature v4
client.SetAuthVersion(sls.AuthV4) // set signature v4

client.GetProject("example-project") // call client API
}
26 changes: 26 additions & 0 deletions util/region.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package util

import (
"fmt"
"regexp"
"strings"
)

const ENDPOINT_REGEX_PATTERN = `^(?:http[s]?:\/\/)?([a-z-0-9]+)\.(?:sls|log)\.aliyuncs\.com$`

var regionSuffixs = []string{"-intranet", "-share", "-vpc"}

func ParseRegion(endpoint string) (string, error) {
var re = regexp.MustCompile(ENDPOINT_REGEX_PATTERN)
groups := re.FindStringSubmatch(endpoint)
if groups == nil {
return "", fmt.Errorf("invalid endpoint format: %s", endpoint)
}
region := groups[1]
for _, suffix := range regionSuffixs {
if strings.HasSuffix(region, suffix) {
return region[:len(region)-len(suffix)], nil
}
}
return region, nil
}
32 changes: 32 additions & 0 deletions util/region_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package util

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseRegion(t *testing.T) {
region, err := ParseRegion("xx-test-acdr-ut-1-intranet.log.aliyuncs.com")
assert.NoError(t, err)
assert.Equal(t, "xx-test-acdr-ut-1", region)

region, err = ParseRegion("http://cn-hangzhou-intranet.log.aliyuncs.com")
assert.NoError(t, err)
assert.Equal(t, "cn-hangzhou", region)

region, err = ParseRegion("https://cn-hangzhou.log.aliyuncs.com")
assert.NoError(t, err)
assert.Equal(t, "cn-hangzhou", region)

region, err = ParseRegion("ap-southease-1-intranet.log.aliyuncs.com")
assert.NoError(t, err)
assert.Equal(t, "ap-southease-1", region)

region, err = ParseRegion("cn-shanghai-corp.sls.aliyuncs.com")
assert.NoError(t, err)
assert.Equal(t, "cn-shanghai-corp", region)

_, err = ParseRegion("sls.aliyuncs.com")
assert.Error(t, err)
}
6 changes: 3 additions & 3 deletions util/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"os"
"strings"
"time"

sls "github.com/aliyun/aliyun-log-go-sdk"
)

const (
aliyunECSRamURL = "http://100.100.100.200/latest/meta-data/ram/security-credentials/"
expirationTimeFormat = "2006-01-02T15:04:05Z"
)

type UpdateTokenFunction = func() (accessKeyID, accessKeySecret, securityToken string, expireTime time.Time, err error)

var errNoFile = errors.New("no secret file")

// AKInfo ...
Expand Down Expand Up @@ -183,7 +183,7 @@ func updateTokenFunction(configFilePath string) (accessKeyID, accessKeySecret, s
}

// NewTokenUpdateFunc create a token update function for ACK or ECS
func NewTokenUpdateFunc(role string, configFilePath string) (tokenUpdateFunc sls.UpdateTokenFunction, shutdown chan struct{}) {
func NewTokenUpdateFunc(role string, configFilePath string) (tokenUpdateFunc UpdateTokenFunction, shutdown chan struct{}) {
return func() (accessKeyID string, accessKeySecret string, securityToken string, expireTime time.Time, err error) {
return updateTokenFunction(configFilePath)
}, make(chan struct{})
Expand Down

0 comments on commit ef4ec3b

Please sign in to comment.