Skip to content

Commit

Permalink
rename file
Browse files Browse the repository at this point in the history
  • Loading branch information
CodingSinger committed Jan 31, 2020
1 parent 768012f commit c031cb0
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 25 deletions.
6 changes: 3 additions & 3 deletions common/extension/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var (
authenticators = make(map[string]func() filter.Authenticator)
accesskeyStorages = make(map[string]func() filter.AccesskeyStorage)
accesskeyStorages = make(map[string]func() filter.AccessKeyStorage)
)

func SetAuthenticator(name string, fcn func() filter.Authenticator) {
Expand All @@ -20,11 +20,11 @@ func GetAuthenticator(name string) filter.Authenticator {
return authenticators[name]()
}

func SetAccesskeyStorages(name string, fcn func() filter.AccesskeyStorage) {
func SetAccesskeyStorages(name string, fcn func() filter.AccessKeyStorage) {
accesskeyStorages[name] = fcn
}

func GetAccesskeyStorages(name string) filter.AccesskeyStorage {
func GetAccesskeyStorages(name string) filter.AccessKeyStorage {
if accesskeyStorages[name] == nil {
panic("accesskeyStorages for " + name + " is not existing, make sure you have import the package.")
}
Expand Down
6 changes: 6 additions & 0 deletions config/service_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type ServiceConfig struct {
TpsLimitRejectedHandler string `yaml:"tps.limit.rejected.handler" json:"tps.limit.rejected.handler,omitempty" property:"tps.limit.rejected.handler"`
ExecuteLimit string `yaml:"execute.limit" json:"execute.limit,omitempty" property:"execute.limit"`
ExecuteLimitRejectedHandler string `yaml:"execute.limit.rejected.handler" json:"execute.limit.rejected.handler,omitempty" property:"execute.limit.rejected.handler"`
Auth string `yaml:"auth" json:"auth,omitempty" property:"auth"`
ParamSign string `yaml:"param.sign" json:"param.sign,omitempty" property:"param.sign"`

unexported *atomic.Bool
exported *atomic.Bool
Expand Down Expand Up @@ -219,6 +221,10 @@ func (c *ServiceConfig) getUrlMap() url.Values {
// execute limit filter
urlMap.Set(constant.EXECUTE_LIMIT_KEY, c.ExecuteLimit)
urlMap.Set(constant.EXECUTE_REJECTED_EXECUTION_HANDLER_KEY, c.ExecuteLimitRejectedHandler)
c
// auth filter
urlMap.Set(constant.SERVICE_AUTH_KEY, c.Auth)
urlMap.Set(constant.PARAMTER_SIGNATURE_ENABLE_KEY, c.ParamSign)

for _, v := range c.Methods {
prefix := "methods." + v.Name + "."
Expand Down
9 changes: 2 additions & 7 deletions filter/auth_ext.go → filter/access_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ type AccessKeyPair struct {
Options string `yaml:"options" json:"options,omitempty" property:"options"`
}

type Authenticator interface {
Sign(protocol.Invocation, *common.URL) error
Authenticate(protocol.Invocation, *common.URL) error
}

type AccesskeyStorage interface {
GetAccesskeyPair(protocol.Invocation, *common.URL) *AccessKeyPair
type AccessKeyStorage interface {
GetAccessKeyPair(protocol.Invocation, *common.URL) *AccessKeyPair
}
11 changes: 11 additions & 0 deletions filter/authenticator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package filter

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/protocol"
)

type Authenticator interface {
Sign(protocol.Invocation, *common.URL) error
Authenticate(protocol.Invocation, *common.URL) error
}
4 changes: 2 additions & 2 deletions filter/filter_impl/auth/accesskey_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type DefaultAccesskeyStorage struct {
}

func (storage *DefaultAccesskeyStorage) GetAccesskeyPair(invocation protocol.Invocation, url *common.URL) *filter.AccessKeyPair {
func (storage *DefaultAccesskeyStorage) GetAccessKeyPair(invocation protocol.Invocation, url *common.URL) *filter.AccessKeyPair {
return &filter.AccessKeyPair{
AccessKey: url.GetParam(constant.ACCESS_KEY_ID_KEY, ""),
SecretKey: url.GetParam(constant.SECRET_ACCESS_KEY_KEY, ""),
Expand All @@ -22,6 +22,6 @@ func init() {
extension.SetAccesskeyStorages(constant.DEFAULT_ACCESS_KEY_STORAGE, GetDefaultAccesskeyStorage)
}

func GetDefaultAccesskeyStorage() filter.AccesskeyStorage {
func GetDefaultAccesskeyStorage() filter.AccessKeyStorage {
return &DefaultAccesskeyStorage{}
}
2 changes: 1 addition & 1 deletion filter/filter_impl/auth/accesskey_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestDefaultAccesskeyStorage_GetAccesskeyPair(t *testing.T) {
common.WithParamsValue(constant.ACCESS_KEY_ID_KEY, "akey"))
invocation := &invocation2.RPCInvocation{}
storage := GetDefaultAccesskeyStorage()
accesskeyPair := storage.GetAccesskeyPair(invocation, url)
accesskeyPair := storage.GetAccessKeyPair(invocation, url)
assert.Equal(t, "skey", accesskeyPair.SecretKey)
assert.Equal(t, "akey", accesskeyPair.AccessKey)
}
7 changes: 4 additions & 3 deletions filter/filter_impl/auth/consumer_sign.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"context"
"fmt"
)
import (
Expand All @@ -18,7 +19,7 @@ func init() {
extension.SetFilter(constant.CONSUMER_SIGN_FILTER, getConsumerSignFilter)
}

func (csf *ConsumerSignFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
func (csf *ConsumerSignFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
logger.Infof("invoking ConsumerSign filter.")
url := invoker.GetUrl()

Expand All @@ -29,10 +30,10 @@ func (csf *ConsumerSignFilter) Invoke(invoker protocol.Invoker, invocation proto
panic(fmt.Sprintf("Sign for invocation %s # %s failed", url.ServiceKey(), invocation.MethodName()))

}
return invoker.Invoke(invocation)
return invoker.Invoke(ctx, invocation)
}

func (csf *ConsumerSignFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
func (csf *ConsumerSignFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
return result
}
func getConsumerSignFilter() filter.Filter {
Expand Down
4 changes: 2 additions & 2 deletions filter/filter_impl/auth/consumer_sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func TestConsumerSignFilter_Invoke(t *testing.T) {
result := &protocol.RPCResult{}
invoker.EXPECT().Invoke(inv).Return(result).Times(2)
invoker.EXPECT().GetUrl().Return(url).Times(2)
assert.Equal(t, result, filter.Invoke(invoker, inv))
assert.Equal(t, result, filter.Invoke(context.Background(), invoker, inv))

url.SetParam(constant.SERVICE_AUTH_KEY, "true")
assert.Equal(t, result, filter.Invoke(invoker, inv))
assert.Equal(t, result, filter.Invoke(context.Background(), invoker, inv))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"errors"
"fmt"
"github.com/apache/dubbo-go/filter"
"strconv"
"time"
)
Expand All @@ -11,7 +12,6 @@ import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/filter"
"github.com/apache/dubbo-go/protocol"
invocation_impl "github.com/apache/dubbo-go/protocol/invocation"
)
Expand Down Expand Up @@ -89,7 +89,7 @@ func (authenticator *DefaultAuthenticator) Authenticate(invocation protocol.Invo

func getAccessKeyPair(invocation protocol.Invocation, url *common.URL) (*filter.AccessKeyPair, error) {
accesskeyStorage := extension.GetAccesskeyStorages(url.GetParam(constant.ACCESS_KEY_STORAGE_KEY, constant.DEFAULT_ACCESS_KEY_STORAGE))
accessKeyPair := accesskeyStorage.GetAccesskeyPair(invocation, url)
accessKeyPair := accesskeyStorage.GetAccessKeyPair(invocation, url)
if accessKeyPair == nil || IsEmpty(accessKeyPair.AccessKey, false) || IsEmpty(accessKeyPair.SecretKey, true) {
return nil, errors.New("accessKeyId or secretAccessKey not found")
} else {
Expand Down
7 changes: 4 additions & 3 deletions filter/filter_impl/auth/provider_auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"context"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/logger"
Expand All @@ -15,7 +16,7 @@ func init() {
extension.SetFilter(constant.PROVIDER_AUTH_FILTER, getProviderAuthFilter)
}

func (paf *ProviderAuthFilter) Invoke(invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
func (paf *ProviderAuthFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
logger.Infof("invoking providerAuth filter.")
url := invoker.GetUrl()

Expand All @@ -29,10 +30,10 @@ func (paf *ProviderAuthFilter) Invoke(invoker protocol.Invoker, invocation proto
}
}

return invoker.Invoke(invocation)
return invoker.Invoke(ctx, invocation)
}

func (paf *ProviderAuthFilter) OnResponse(result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
func (paf *ProviderAuthFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
return result
}
func getProviderAuthFilter() filter.Filter {
Expand Down
4 changes: 2 additions & 2 deletions filter/filter_impl/auth/provider_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestProviderAuthFilter_Invoke(t *testing.T) {
result := &protocol.RPCResult{}
invoker.EXPECT().Invoke(inv).Return(result).Times(2)
invoker.EXPECT().GetUrl().Return(url).Times(2)
assert.Equal(t, result, filter.Invoke(invoker, inv))
assert.Equal(t, result, filter.Invoke(context.Background(), invoker, inv))
url.SetParam(constant.SERVICE_AUTH_KEY, "true")
assert.Equal(t, result, filter.Invoke(invoker, inv))
assert.Equal(t, result, filter.Invoke(context.Background(), invoker, inv))

}

0 comments on commit c031cb0

Please sign in to comment.