Fix auth bug

This commit is contained in:
Ben Toogood 2020-05-13 16:49:17 +01:00
parent c831b6c03a
commit 410fec8ee4
3 changed files with 17 additions and 9 deletions

View File

@ -53,6 +53,12 @@ func newService(opts ...Option) Service {
server.WrapHandler(wrapper.AuthHandler(authFn)), 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 // set opts
service.opts = options service.opts = options
@ -116,7 +122,10 @@ func (s *service) Init(opts ...Option) {
name := s.opts.Cmd.App().Name name := s.opts.Cmd.App().Name
s.opts.Store.Init(store.Table(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.Auth.Init(auth.WithClient(s.Client()))
s.opts.Registry.Init(registrySrv.WithClient(s.Client())) s.opts.Registry.Init(registrySrv.WithClient(s.Client()))
s.opts.Runtime.Init(runtime.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 { func (s *service) Run() error {
// generate an auth account
if err := s.generateAccount(); err != nil {
return err
}
// register the debug handler // register the debug handler
s.opts.Server.Handle( s.opts.Server.Handle(
s.opts.Server.NewHandler( s.opts.Server.NewHandler(
@ -208,11 +222,6 @@ func (s *service) Run() error {
logger.Infof("Starting [service] %s", s.Name()) 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 { if err := s.Start(); err != nil {
return err return err
} }
@ -232,7 +241,7 @@ func (s *service) Run() error {
return s.Stop() return s.Stop()
} }
func (s *service) registerAuthAccount() error { func (s *service) generateAccount() error {
// generate a new auth account for the service // generate a new auth account for the service
name := fmt.Sprintf("%v-%v", s.Name(), s.Server().Options().Id) name := fmt.Sprintf("%v-%v", s.Name(), s.Server().Options().Id)
opts := []auth.GenerateOption{ opts := []auth.GenerateOption{

View File

@ -134,7 +134,6 @@ func (m *memoryStore) Close() error {
} }
func (m *memoryStore) Init(opts ...store.Option) error { func (m *memoryStore) Init(opts ...store.Option) error {
m.store.Flush()
for _, o := range opts { for _, o := range opts {
o(&m.options) o(&m.options)
} }

View File

@ -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++ { for i := 0; i < 10; i++ {
s.Write(&store.Record{ s.Write(&store.Record{
Key: fmt.Sprintf("a%d", i), Key: fmt.Sprintf("a%d", i),