Skip to content

Commit

Permalink
[fix] Fix the problem that the signature method may lose part of the …
Browse files Browse the repository at this point in the history
…path
  • Loading branch information
shima-park committed Sep 1, 2022
1 parent 878618d commit 7741370
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
1 change: 0 additions & 1 deletion apollo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ func getLocalIP() string {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}

}
}
return ""
Expand Down
41 changes: 24 additions & 17 deletions apollo_client_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"crypto/sha1"
"encoding/base64"
"fmt"
"net/url"
"strconv"
"time"
)

Expand All @@ -18,37 +20,42 @@ const (
type Header map[string]string

type SignatureContext struct {
ConfigServerURL string // 当前访问配置使用的apollo config server的url
AccessKey string // 服务启动时缓存的access key
AppID string // appid
AccessKey string // 服务启动时缓存的access key
ConfigServerURL string // 当前访问配置使用的apollo config server的url
RequestURI string // 请求的uri,domain之后的路径
Cluster string // 请求的集群,默认情况下: default,请求GetConfigServers接口时为""
}

type SignatureFunc func(ctx *SignatureContext) Header

func DefaultSignatureFunc(ctx *SignatureContext) Header {

headers := map[string]string{}
if "" == ctx.AccessKey {
return headers
if ctx.AppID == "" || ctx.AccessKey == "" {
return nil
}

timestamp := fmt.Sprintf("%v", time.Now().UnixNano()/int64(time.Millisecond))
signature := signature(timestamp, ctx.RequestURI, ctx.AccessKey)
apiURL := fmt.Sprintf("%s%s", ctx.ConfigServerURL, ctx.RequestURI)
timestamp := strconv.Itoa(int(time.Now().UnixMilli()))
signature := signature(timestamp, getPathWithQuery(apiURL), ctx.AccessKey)

headers[HTTP_HEADER_AUTHORIZATION] = fmt.Sprintf(AUTHORIZATION_FORMAT, ctx.AppID, signature)
headers[HTTP_HEADER_TIMESTAMP] = timestamp
return map[string]string{
HTTP_HEADER_AUTHORIZATION: fmt.Sprintf(AUTHORIZATION_FORMAT, ctx.AppID, signature),
HTTP_HEADER_TIMESTAMP: timestamp,
}
}

return headers
func getPathWithQuery(uri string) string {
r, err := url.Parse(uri)
if err != nil {
return ""
}
r.Scheme = ""
r.Host = ""
return r.String()
}

func signature(timestamp, url, accessKey string) string {

stringToSign := timestamp + DELIMITER + url

key := []byte(accessKey)
mac := hmac.New(sha1.New, key)
_, _ = mac.Write([]byte(stringToSign))
mac := hmac.New(sha1.New, []byte(accessKey))
_, _ = mac.Write([]byte(timestamp + DELIMITER + url))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
29 changes: 29 additions & 0 deletions apollo_client_signature_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package agollo

import "testing"

func Test_getPathWithQuery(t *testing.T) {
type args struct {
uri string
}
tests := []struct {
name string
args args
want string
}{
{
name: "",
args: args{
uri: "http://apollo.meta/configsvc-dev/services/config?id=1",
},
want: "/configsvc-dev/services/config?id=1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getPathWithQuery(tt.args.uri); got != tt.want {
t.Errorf("getPathWithQuery() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 7741370

Please sign in to comment.