From fde1aa9d6a17004f7a6dfd496a8397ed1decc715 Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Wed, 3 Jun 2020 09:43:20 +0100 Subject: [PATCH] Move auth account creation to config/cmd (#1676) --- config/cmd/cmd.go | 136 ++++++++++++++++++++++++---------------------- service.go | 6 -- 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/config/cmd/cmd.go b/config/cmd/cmd.go index 7d8841cf..d22034b9 100644 --- a/config/cmd/cmd.go +++ b/config/cmd/cmd.go @@ -27,6 +27,7 @@ import ( "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/transport" + authutil "github.com/micro/go-micro/v2/util/auth" "github.com/micro/go-micro/v2/util/wrapper" // clients @@ -468,7 +469,6 @@ func (c *cmd) Options() Options { func (c *cmd) Before(ctx *cli.Context) error { // If flags are set then use them otherwise do nothing - var authOpts []auth.Option var serverOpts []server.Option var clientOpts []client.Option @@ -510,26 +510,6 @@ func (c *cmd) Before(ctx *cli.Context) error { *c.opts.Tracer = r() } - // Set the auth - if name := ctx.String("auth"); len(name) > 0 { - a, ok := c.opts.Auths[name] - if !ok { - return fmt.Errorf("Unsupported auth: %s", name) - } - *c.opts.Auth = a(auth.WithClient(microClient)) - serverOpts = append(serverOpts, server.Auth(*c.opts.Auth)) - } - - // Set the profile - if name := ctx.String("profile"); len(name) > 0 { - p, ok := c.opts.Profiles[name] - if !ok { - return fmt.Errorf("Unsupported profile: %s", name) - } - - *c.opts.Profile = p() - } - // Set the client if name := ctx.String("client"); len(name) > 0 { // only change if we have the client and type differs @@ -546,6 +526,76 @@ func (c *cmd) Before(ctx *cli.Context) error { } } + // Setup auth + authOpts := []auth.Option{auth.WithClient(microClient)} + + if len(ctx.String("auth_id")) > 0 || len(ctx.String("auth_secret")) > 0 { + authOpts = append(authOpts, auth.Credentials( + ctx.String("auth_id"), ctx.String("auth_secret"), + )) + } + if len(ctx.String("auth_public_key")) > 0 { + authOpts = append(authOpts, auth.PublicKey(ctx.String("auth_public_key"))) + } + if len(ctx.String("auth_private_key")) > 0 { + authOpts = append(authOpts, auth.PrivateKey(ctx.String("auth_private_key"))) + } + if len(ctx.String("service_namespace")) > 0 { + authOpts = append(authOpts, auth.Namespace(ctx.String("service_namespace"))) + } + if name := ctx.String("auth_provider"); len(name) > 0 { + p, ok := DefaultAuthProviders[name] + if !ok { + return fmt.Errorf("AuthProvider %s not found", name) + } + + var provOpts []provider.Option + clientID := ctx.String("auth_provider_client_id") + clientSecret := ctx.String("auth_provider_client_secret") + if len(clientID) > 0 || len(clientSecret) > 0 { + provOpts = append(provOpts, provider.Credentials(clientID, clientSecret)) + } + if e := ctx.String("auth_provider_endpoint"); len(e) > 0 { + provOpts = append(provOpts, provider.Endpoint(e)) + } + if r := ctx.String("auth_provider_redirect"); len(r) > 0 { + provOpts = append(provOpts, provider.Redirect(r)) + } + if s := ctx.String("auth_provider_scope"); len(s) > 0 { + provOpts = append(provOpts, provider.Scope(s)) + } + + authOpts = append(authOpts, auth.Provider(p(provOpts...))) + } + + // Set the auth + if name := ctx.String("auth"); len(name) > 0 { + a, ok := c.opts.Auths[name] + if !ok { + return fmt.Errorf("Unsupported auth: %s", name) + } + *c.opts.Auth = a(authOpts...) + serverOpts = append(serverOpts, server.Auth(*c.opts.Auth)) + } else { + (*c.opts.Auth).Init(authOpts...) + } + + // generate the services auth account + serverID := (*c.opts.Server).Options().Id + if err := authutil.Generate(serverID, c.App().Name, (*c.opts.Auth)); err != nil { + return err + } + + // Set the profile + if name := ctx.String("profile"); len(name) > 0 { + p, ok := c.opts.Profiles[name] + if !ok { + return fmt.Errorf("Unsupported profile: %s", name) + } + + *c.opts.Profile = p() + } + // Set the broker if name := ctx.String("broker"); len(name) > 0 && (*c.opts.Broker).String() != name { b, ok := c.opts.Brokers[name] @@ -691,50 +741,6 @@ func (c *cmd) Before(ctx *cli.Context) error { } } - if len(ctx.String("auth_id")) > 0 || len(ctx.String("auth_secret")) > 0 { - authOpts = append(authOpts, auth.Credentials( - ctx.String("auth_id"), ctx.String("auth_secret"), - )) - } - - if len(ctx.String("auth_namespace")) > 0 { - authOpts = append(authOpts, auth.Namespace(ctx.String("auth_namespace"))) - } - - if len(ctx.String("auth_public_key")) > 0 { - authOpts = append(authOpts, auth.PublicKey(ctx.String("auth_public_key"))) - } - if len(ctx.String("auth_private_key")) > 0 { - authOpts = append(authOpts, auth.PrivateKey(ctx.String("auth_private_key"))) - } - - if name := ctx.String("auth_provider"); len(name) > 0 { - p, ok := DefaultAuthProviders[name] - if !ok { - return fmt.Errorf("AuthProvider %s not found", name) - } - - var provOpts []provider.Option - - clientID := ctx.String("auth_provider_client_id") - clientSecret := ctx.String("auth_provider_client_secret") - if len(clientID) > 0 || len(clientSecret) > 0 { - provOpts = append(provOpts, provider.Credentials(clientID, clientSecret)) - } - if e := ctx.String("auth_provider_endpoint"); len(e) > 0 { - provOpts = append(provOpts, provider.Endpoint(e)) - } - if r := ctx.String("auth_provider_redirect"); len(r) > 0 { - provOpts = append(provOpts, provider.Redirect(r)) - } - if s := ctx.String("auth_provider_scope"); len(s) > 0 { - provOpts = append(provOpts, provider.Scope(s)) - } - - authOpts = append(authOpts, auth.Provider(p(provOpts...))) - } - (*c.opts.Auth).Init(authOpts...) - if ctx.String("config") == "service" { opt := config.WithSource(configSrv.NewSource(configSrc.WithClient(microClient))) if err := (*c.opts.Config).Init(opt); err != nil { diff --git a/service.go b/service.go index 4d793920..c8fe4c76 100644 --- a/service.go +++ b/service.go @@ -17,7 +17,6 @@ import ( "github.com/micro/go-micro/v2/plugin" "github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/store" - authutil "github.com/micro/go-micro/v2/util/auth" signalutil "github.com/micro/go-micro/v2/util/signal" "github.com/micro/go-micro/v2/util/wrapper" ) @@ -176,11 +175,6 @@ func (s *service) Stop() error { } func (s *service) Run() error { - // generate an auth account - if err := authutil.Generate(s.Server().Options().Id, s.Name(), s.Options().Auth); err != nil { - return err - } - // register the debug handler s.opts.Server.Handle( s.opts.Server.NewHandler(