Skip to content

Commit 55e9b58

Browse files
authored
add-empty-sign-and-keyprovider (aliyun#234)
1 parent 6d4e2ab commit 55e9b58

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

client.go

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const DefaultLogUserAgent = "golang-sdk-v0.1.0"
3939
type AuthVersionType string
4040

4141
const (
42+
AuthV0 AuthVersionType = "v0"
4243
// AuthV1 v1
4344
AuthV1 AuthVersionType = "v1"
4445
// AuthV4 v4
@@ -112,6 +113,7 @@ type Client struct {
112113
// When conflict with sdk pre-defined headers, the value will
113114
// be ignored
114115
CommonHeaders map[string]string
116+
KeyProvider string
115117
}
116118

117119
func convert(c *Client, projName string) *LogProject {
@@ -133,6 +135,7 @@ func convertLocked(c *Client, projName string) *LogProject {
133135
p.AuthVersion = c.AuthVersion
134136
p.Region = c.Region
135137
p.CommonHeaders = c.CommonHeaders
138+
p.KeyProvider = c.KeyProvider
136139
if c.HTTPClient != nil {
137140
p.httpClient = c.HTTPClient
138141
}

client_request.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ func (c *Client) request(project, method, uri string, headers map[string]string,
7777
return nil, fmt.Errorf("Can't find 'Content-Type' header")
7878
}
7979
}
80+
if c.KeyProvider != "" && c.AuthVersion != AuthV4 {
81+
headers["x-log-keyprovider"] = c.KeyProvider
82+
}
8083
var signer Signer
8184
if authVersion == AuthV4 {
8285
headers[HTTPHeaderLogDate] = dateTimeISO8601()
8386
signer = NewSignerV4(accessKeyID, accessKeySecret, region)
87+
} else if authVersion == AuthV0 {
88+
signer = NewSignerV0()
8489
} else {
8590
headers[HTTPHeaderDate] = nowRFC1123()
8691
signer = NewSignerV1(accessKeyID, accessKeySecret)
@@ -89,11 +94,7 @@ func (c *Client) request(project, method, uri string, headers map[string]string,
8994
return nil, err
9095
}
9196

92-
for k, v := range c.CommonHeaders {
93-
if _, ok := headers[k]; !ok {
94-
headers[k] = v
95-
}
96-
}
97+
addHeadersAfterSign(c.CommonHeaders, headers)
9798
// Initialize http request
9899
reader := bytes.NewReader(body)
99100
var urlStr string

log_project.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ type LogProject struct {
6161
//
6262
// When conflict with sdk pre-defined headers, the value will
6363
// be ignored
64-
CommonHeaders map[string]string
64+
CommonHeaders map[string]string
65+
KeyProvider string
6566
}
6667

6768
// NewLogProject creates a new SLS project.

request.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,15 @@ func realRequest(ctx context.Context, project *LogProject, method, uri string, h
178178
}
179179
}
180180

181+
if project.KeyProvider != "" && project.AuthVersion != AuthV4 {
182+
headers["x-log-keyprovider"] = project.KeyProvider
183+
}
181184
var signer Signer
182185
if project.AuthVersion == AuthV4 {
183186
headers[HTTPHeaderLogDate] = dateTimeISO8601()
184187
signer = NewSignerV4(accessKeyID, accessKeySecret, project.Region)
188+
} else if project.AuthVersion == AuthV0 {
189+
signer = NewSignerV0()
185190
} else {
186191
headers[HTTPHeaderDate] = nowRFC1123()
187192
signer = NewSignerV1(accessKeyID, accessKeySecret)
@@ -190,11 +195,7 @@ func realRequest(ctx context.Context, project *LogProject, method, uri string, h
190195
return nil, err
191196
}
192197

193-
for k, v := range project.CommonHeaders {
194-
if _, ok := headers[k]; !ok {
195-
headers[k] = v
196-
}
197-
}
198+
addHeadersAfterSign(project.CommonHeaders, headers)
198199

199200
// Initialize http request
200201
reader := bytes.NewReader(body)

signature.go

+20
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ var gmtLoc = time.FixedZone("GMT", 0)
4242
func nowRFC1123() string {
4343
return time.Now().In(gmtLoc).Format(time.RFC1123)
4444
}
45+
func NewSignerV0() *SignerV0 {
46+
return &SignerV0{}
47+
}
48+
49+
type SignerV0 struct{}
50+
51+
func (s *SignerV0) Sign(method, uriWithQuery string, headers map[string]string, body []byte) error {
52+
// do nothing
53+
return nil
54+
}
4555

4656
// SignerV1 version v1
4757
type SignerV1 struct {
@@ -138,3 +148,13 @@ func (s *SignerV1) Sign(method, uri string, headers map[string]string, body []by
138148
headers[HTTPHeaderAuthorization] = auth
139149
return nil
140150
}
151+
152+
// add commonHeaders to headers after signature if not conflict
153+
func addHeadersAfterSign(commonHeaders, headers map[string]string) {
154+
for k, v := range commonHeaders {
155+
lowerKey := strings.ToLower(k)
156+
if _, ok := headers[lowerKey]; !ok {
157+
headers[k] = v
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)