Skip to content

Commit

Permalink
add-empty-sign-and-keyprovider (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanLjp authored Oct 11, 2023
1 parent 6d4e2ab commit 55e9b58
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const DefaultLogUserAgent = "golang-sdk-v0.1.0"
type AuthVersionType string

const (
AuthV0 AuthVersionType = "v0"
// AuthV1 v1
AuthV1 AuthVersionType = "v1"
// AuthV4 v4
Expand Down Expand Up @@ -112,6 +113,7 @@ type Client struct {
// When conflict with sdk pre-defined headers, the value will
// be ignored
CommonHeaders map[string]string
KeyProvider string
}

func convert(c *Client, projName string) *LogProject {
Expand All @@ -133,6 +135,7 @@ func convertLocked(c *Client, projName string) *LogProject {
p.AuthVersion = c.AuthVersion
p.Region = c.Region
p.CommonHeaders = c.CommonHeaders
p.KeyProvider = c.KeyProvider
if c.HTTPClient != nil {
p.httpClient = c.HTTPClient
}
Expand Down
11 changes: 6 additions & 5 deletions client_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,15 @@ func (c *Client) request(project, method, uri string, headers map[string]string,
return nil, fmt.Errorf("Can't find 'Content-Type' header")
}
}
if c.KeyProvider != "" && c.AuthVersion != AuthV4 {
headers["x-log-keyprovider"] = c.KeyProvider
}
var signer Signer
if authVersion == AuthV4 {
headers[HTTPHeaderLogDate] = dateTimeISO8601()
signer = NewSignerV4(accessKeyID, accessKeySecret, region)
} else if authVersion == AuthV0 {
signer = NewSignerV0()
} else {
headers[HTTPHeaderDate] = nowRFC1123()
signer = NewSignerV1(accessKeyID, accessKeySecret)
Expand All @@ -89,11 +94,7 @@ func (c *Client) request(project, method, uri string, headers map[string]string,
return nil, err
}

for k, v := range c.CommonHeaders {
if _, ok := headers[k]; !ok {
headers[k] = v
}
}
addHeadersAfterSign(c.CommonHeaders, headers)
// Initialize http request
reader := bytes.NewReader(body)
var urlStr string
Expand Down
3 changes: 2 additions & 1 deletion log_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type LogProject struct {
//
// When conflict with sdk pre-defined headers, the value will
// be ignored
CommonHeaders map[string]string
CommonHeaders map[string]string
KeyProvider string
}

// NewLogProject creates a new SLS project.
Expand Down
11 changes: 6 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,15 @@ func realRequest(ctx context.Context, project *LogProject, method, uri string, h
}
}

if project.KeyProvider != "" && project.AuthVersion != AuthV4 {
headers["x-log-keyprovider"] = project.KeyProvider
}
var signer Signer
if project.AuthVersion == AuthV4 {
headers[HTTPHeaderLogDate] = dateTimeISO8601()
signer = NewSignerV4(accessKeyID, accessKeySecret, project.Region)
} else if project.AuthVersion == AuthV0 {
signer = NewSignerV0()
} else {
headers[HTTPHeaderDate] = nowRFC1123()
signer = NewSignerV1(accessKeyID, accessKeySecret)
Expand All @@ -190,11 +195,7 @@ func realRequest(ctx context.Context, project *LogProject, method, uri string, h
return nil, err
}

for k, v := range project.CommonHeaders {
if _, ok := headers[k]; !ok {
headers[k] = v
}
}
addHeadersAfterSign(project.CommonHeaders, headers)

// Initialize http request
reader := bytes.NewReader(body)
Expand Down
20 changes: 20 additions & 0 deletions signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ var gmtLoc = time.FixedZone("GMT", 0)
func nowRFC1123() string {
return time.Now().In(gmtLoc).Format(time.RFC1123)
}
func NewSignerV0() *SignerV0 {
return &SignerV0{}
}

type SignerV0 struct{}

func (s *SignerV0) Sign(method, uriWithQuery string, headers map[string]string, body []byte) error {
// do nothing
return nil
}

// SignerV1 version v1
type SignerV1 struct {
Expand Down Expand Up @@ -138,3 +148,13 @@ func (s *SignerV1) Sign(method, uri string, headers map[string]string, body []by
headers[HTTPHeaderAuthorization] = auth
return nil
}

// add commonHeaders to headers after signature if not conflict
func addHeadersAfterSign(commonHeaders, headers map[string]string) {
for k, v := range commonHeaders {
lowerKey := strings.ToLower(k)
if _, ok := headers[lowerKey]; !ok {
headers[k] = v
}
}
}

0 comments on commit 55e9b58

Please sign in to comment.