From 287992cef3e83333a96798cb14bb2a82f10dafa3 Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Thu, 21 May 2020 11:35:07 +0100 Subject: [PATCH] Fix service => service namespace bug --- auth/rules/rules.go | 1 - util/auth/auth.go | 2 +- util/wrapper/wrapper.go | 8 ++++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/auth/rules/rules.go b/auth/rules/rules.go index c3e96fa9..bfe3594b 100644 --- a/auth/rules/rules.go +++ b/auth/rules/rules.go @@ -31,7 +31,6 @@ func Verify(namespace string, rules []*auth.Rule, acc *auth.Account, res *auth.R // filter the rules to the ones which match the criteria above filteredRules := make([]*auth.Rule, 0) for _, rule := range rules { - fmt.Printf("All rules: %v\n", rule.ID) if !include(validTypes, rule.Resource.Type) { continue } diff --git a/util/auth/auth.go b/util/auth/auth.go index ef69f45c..4c7eaa34 100644 --- a/util/auth/auth.go +++ b/util/auth/auth.go @@ -30,7 +30,7 @@ func Generate(id string, name string, a auth.Auth) error { if err != nil { return err } - logger.Infof("Auth [%v] Authenticated as %v in the %v scope", a, name, scope) + logger.Infof("Auth [%v] Authenticated as %v in the %v namespace", a, name, a.Options().Namespace) accID = acc.ID accSecret = acc.Secret diff --git a/util/wrapper/wrapper.go b/util/wrapper/wrapper.go index bf0f4a3a..5ca359e1 100644 --- a/util/wrapper/wrapper.go +++ b/util/wrapper/wrapper.go @@ -3,7 +3,6 @@ package wrapper import ( "context" "strings" - "time" "github.com/micro/go-micro/v2/auth" "github.com/micro/go-micro/v2/client" @@ -156,9 +155,14 @@ func (a *authWrapper) Call(ctx context.Context, req client.Request, rsp interfac return a.Client.Call(ctx, req, rsp, opts...) } + // set the namespace header if it has not been set (e.g. on a service to service request) + if _, ok := metadata.Get(ctx, "Micro-Namespace"); !ok { + ctx = metadata.Set(ctx, "Micro-Namespace", aa.Options().Namespace) + } + // check to see if we have a valid access token aaOpts := aa.Options() - if aaOpts.Token != nil && aaOpts.Token.Expiry.Unix() > time.Now().Unix() { + if aaOpts.Token != nil && !aaOpts.Token.Expired() { ctx = metadata.Set(ctx, "Authorization", auth.BearerScheme+aaOpts.Token.AccessToken) return a.Client.Call(ctx, req, rsp, opts...) }