From c831b6c03a14c4b0f5ce9f42f42e76602ddaa8ca Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Wed, 13 May 2020 16:35:57 +0100 Subject: [PATCH 1/2] Fix --- util/wrapper/wrapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/wrapper/wrapper.go b/util/wrapper/wrapper.go index 285a775f..8b501b12 100644 --- a/util/wrapper/wrapper.go +++ b/util/wrapper/wrapper.go @@ -155,7 +155,7 @@ func (a *authWrapper) Call(ctx context.Context, req client.Request, rsp interfac // if auth is nil we won't be able to get an access token, so we execute // the request without one. aa := a.auth() - if a == nil { + if aa == nil { return a.Client.Call(ctx, req, rsp, opts...) } From 410fec8ee48a5979e1c66da2faa2f4bba062b2a1 Mon Sep 17 00:00:00 2001 From: Ben Toogood Date: Wed, 13 May 2020 16:49:17 +0100 Subject: [PATCH 2/2] Fix auth bug --- service.go | 23 ++++++++++++++++------- store/memory/memory.go | 1 - store/memory/memory_test.go | 2 +- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/service.go b/service.go index 75de60fa..749f53ae 100644 --- a/service.go +++ b/service.go @@ -53,6 +53,12 @@ func newService(opts ...Option) Service { server.WrapHandler(wrapper.AuthHandler(authFn)), ) + // set the client in the service implementations + options.Auth.Init(auth.WithClient(options.Client)) + options.Registry.Init(registrySrv.WithClient(options.Client)) + options.Runtime.Init(runtime.WithClient(options.Client)) + options.Store.Init(store.WithClient(options.Client)) + // set opts service.opts = options @@ -116,7 +122,10 @@ func (s *service) Init(opts ...Option) { name := s.opts.Cmd.App().Name s.opts.Store.Init(store.Table(name)) - // Set the client for the micro clients + // Reset the clients for the micro services, this is done + // previously in newService for micro (since init is never called) + // however it needs to be done again here since for normal go-micro + // services the implementation may have changed by CLI flags. s.opts.Auth.Init(auth.WithClient(s.Client())) s.opts.Registry.Init(registrySrv.WithClient(s.Client())) s.opts.Runtime.Init(runtime.WithClient(s.Client())) @@ -183,6 +192,11 @@ func (s *service) Stop() error { } func (s *service) Run() error { + // generate an auth account + if err := s.generateAccount(); err != nil { + return err + } + // register the debug handler s.opts.Server.Handle( s.opts.Server.NewHandler( @@ -208,11 +222,6 @@ func (s *service) Run() error { logger.Infof("Starting [service] %s", s.Name()) } - // generate an auth account - if err := s.registerAuthAccount(); err != nil { - return err - } - if err := s.Start(); err != nil { return err } @@ -232,7 +241,7 @@ func (s *service) Run() error { return s.Stop() } -func (s *service) registerAuthAccount() error { +func (s *service) generateAccount() error { // generate a new auth account for the service name := fmt.Sprintf("%v-%v", s.Name(), s.Server().Options().Id) opts := []auth.GenerateOption{ diff --git a/store/memory/memory.go b/store/memory/memory.go index cbe6e38d..33ded427 100644 --- a/store/memory/memory.go +++ b/store/memory/memory.go @@ -134,7 +134,6 @@ func (m *memoryStore) Close() error { } func (m *memoryStore) Init(opts ...store.Option) error { - m.store.Flush() for _, o := range opts { o(&m.options) } diff --git a/store/memory/memory_test.go b/store/memory/memory_test.go index 4598a667..91cd2b12 100644 --- a/store/memory/memory_test.go +++ b/store/memory/memory_test.go @@ -248,7 +248,7 @@ func basictest(s store.Store, t *testing.T) { } } - s.Init() + s.Close() // reset the store for i := 0; i < 10; i++ { s.Write(&store.Record{ Key: fmt.Sprintf("a%d", i),