Skip to content

Commit

Permalink
add WithAccessKeySecret to support using access key
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyper committed Aug 22, 2021
1 parent 2f56259 commit 5111ac9
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,15 @@ app := lunar.New("myAppID")
app.UseCache(lunar.NewFileCache("myAppID", "/tmp"))
```

## Enable Access Key

You can use `WithAccessKeySecret` to enable access key feature:

```
app := lunar.New(
"myAppID",
lunar.WithServer("localhost:8080"),
lunar.WithAccessKeySecret("mySecret"),
)
```
19 changes: 12 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ func NewApolloClient(appID string, opts ...Option) *ApolloClient {
return c
}

func (c *ApolloClient) get(url string, result interface{}) error {
func (c *ApolloClient) get(pathWithQuery string, result interface{}) error {
url := c.Server + pathWithQuery
c.Logger.Printf("%s", url)

req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}

if len(c.AccessKeySecret) > 0 {
headers := buildHeaders(pathWithQuery, c.AppID, c.AccessKeySecret)
for k, v := range headers {
req.Header.Add(k, v)
}
}

resp, err := c.Client.Do(req)
if err != nil {
return err
Expand All @@ -73,8 +81,7 @@ func (c *ApolloClient) get(url string, result interface{}) error {

// GetCachedItems gets cached configs from apollo
func (c *ApolloClient) GetCachedItems(namespace string) (Items, error) {
url := fmt.Sprintf("%s/configfiles/json/%s/%s/%s?ip=%s",
c.Server,
url := fmt.Sprintf("/configfiles/json/%s/%s/%s?ip=%s",
url.QueryEscape(c.AppID),
url.QueryEscape(c.Cluster),
url.QueryEscape(namespace),
Expand Down Expand Up @@ -102,8 +109,7 @@ func (c *ApolloClient) GetNamespace(namespace string, releaseKey string) (*Names
namespace = defaultNamespace
}

url := fmt.Sprintf("%s/configs/%s/%s/%s?releaseKey=%s&ip=%s",
c.Server,
url := fmt.Sprintf("/configs/%s/%s/%s?releaseKey=%s&ip=%s",
url.QueryEscape(c.AppID),
url.QueryEscape(c.Cluster),
url.QueryEscape(namespace),
Expand Down Expand Up @@ -138,8 +144,7 @@ func (c *ApolloClient) GetNotifications(ns Notifications) (Notifications, error)
ns = append(ns, Notification{Namespace: defaultNamespace, NotificationID: defaultNotificationID})
}

url := fmt.Sprintf("%s/notifications/v2?appId=%s&cluster=%s&notifications=%s",
c.Server,
url := fmt.Sprintf("/notifications/v2?appId=%s&cluster=%s&notifications=%s",
url.QueryEscape(c.AppID),
url.QueryEscape(c.Cluster),
url.QueryEscape(ns.String()),
Expand Down
7 changes: 6 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ func TestApolloClientTestSuite(t *testing.T) {

// SetupSuite run once at the very start of the testing suite, before any tests are run.
func (ts *ApolloClientTestSuite) SetupSuite() {
ts.client = NewApolloClient("SampleApp", WithServer("localhost:8080"), WithLogger(Printf))
ts.client = NewApolloClient(
"SampleApp",
WithServer("localhost:8080"),
WithLogger(Printf),
WithAccessKeySecret("12848b38781e4daf9d05054580282a8e"),
)
}

// TearDownSuite run once at the very end of the testing suite, after all tests have been run.
Expand Down
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package lunar

import (
"strings"
"time"
)

Expand All @@ -18,6 +19,7 @@ const (
type Options struct {
Server string
Cluster string
AccessKeySecret string
Logger Logger
ClientTimeout time.Duration
LongPollInterval time.Duration
Expand Down Expand Up @@ -76,3 +78,10 @@ func WithLongPollInterval(interval time.Duration) Option {
o.LongPollInterval = interval
}
}

// WithAccessKeySecret sets access key secret
func WithAccessKeySecret(secret string) Option {
return func(o *Options) {
o.AccessKeySecret = strings.TrimSpace(secret)
}
}
35 changes: 35 additions & 0 deletions sign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lunar

import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"time"
)

const (
AuthorizationFormat = "Apollo %s:%s"
Delimiter = "\n"
)

func sign(timestamp, pathWithQuery, secret string) string {
stringToSign := timestamp + Delimiter + pathWithQuery

h := hmac.New(sha1.New, []byte(secret))
h.Write([]byte(stringToSign))

return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

func buildHeaders(pathWithQuery, appID, secret string) map[string]string {
ms := time.Now().UnixNano() / int64(time.Millisecond)
timestamp := fmt.Sprintf("%d", ms)
signature := sign(timestamp, pathWithQuery, secret)

m := make(map[string]string)
m["Authorization"] = fmt.Sprintf(AuthorizationFormat, appID, signature)
m["Timestamp"] = timestamp

return m
}

0 comments on commit 5111ac9

Please sign in to comment.