diff --git a/client.go b/client.go index 6c1d243c..c1adbe03 100644 --- a/client.go +++ b/client.go @@ -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 @@ -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 { @@ -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 } diff --git a/client_request.go b/client_request.go index d8064180..e4954557 100644 --- a/client_request.go +++ b/client_request.go @@ -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) @@ -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 diff --git a/log_project.go b/log_project.go index f5c4082b..8a1a7b6b 100644 --- a/log_project.go +++ b/log_project.go @@ -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. diff --git a/request.go b/request.go index 30cac2fe..61be2192 100644 --- a/request.go +++ b/request.go @@ -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) @@ -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) diff --git a/signature.go b/signature.go index 4ec3a3c6..7e8d8aab 100644 --- a/signature.go +++ b/signature.go @@ -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 { @@ -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 + } + } +}