Skip to content

Commit

Permalink
Move auth account creation to config/cmd (micro#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-toogood authored and domwong committed Jun 12, 2020
1 parent f45cdba commit fde1aa9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
130 changes: 68 additions & 62 deletions config/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -510,14 +510,80 @@ func (c *cmd) Before(ctx *cli.Context) error {
*c.opts.Tracer = r()
}

// Set the client
if name := ctx.String("client"); len(name) > 0 {
// only change if we have the client and type differs
if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name {
*c.opts.Client = cl()
}
}

// Set the server
if name := ctx.String("server"); len(name) > 0 {
// only change if we have the server and type differs
if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name {
*c.opts.Server = s()
}
}

// 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(auth.WithClient(microClient))
*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
Expand All @@ -530,22 +596,6 @@ func (c *cmd) Before(ctx *cli.Context) error {
*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
if cl, ok := c.opts.Clients[name]; ok && (*c.opts.Client).String() != name {
*c.opts.Client = cl()
}
}

// Set the server
if name := ctx.String("server"); len(name) > 0 {
// only change if we have the server and type differs
if s, ok := c.opts.Servers[name]; ok && (*c.opts.Server).String() != name {
*c.opts.Server = s()
}
}

// Set the broker
if name := ctx.String("broker"); len(name) > 0 && (*c.opts.Broker).String() != name {
b, ok := c.opts.Brokers[name]
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 0 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit fde1aa9

Please sign in to comment.