broker/service: use wrapped micro client to authenticate requests (#1782)

This commit is contained in:
ben-toogood 2020-07-02 17:54:53 +01:00 committed by GitHub
parent b5314829fa
commit c58ac35dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 1 deletions

22
broker/service/options.go Normal file
View File

@ -0,0 +1,22 @@
package service
import (
"context"
"github.com/micro/go-micro/v2/broker"
"github.com/micro/go-micro/v2/client"
)
type clientKey struct{}
// Client to call broker service
func Client(c client.Client) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.WithValue(context.Background(), clientKey{}, c)
return
}
o.Context = context.WithValue(o.Context, clientKey{}, c)
}
}

View File

@ -138,6 +138,12 @@ func NewBroker(opts ...broker.Option) broker.Broker {
cli := client.DefaultClient
// get options client from the context. We set this in the context to prevent an import loop, as
// the client depends on the broker
if c, ok := options.Context.Value(clientKey{}).(client.Client); ok {
cli = c
}
return &serviceBroker{
Addrs: addrs,
Client: pb.NewBrokerService(DefaultName, cli),

View File

@ -643,7 +643,7 @@ func (c *cmd) Before(ctx *cli.Context) error {
}
// Setup broker options.
brokerOpts := []broker.Option{}
brokerOpts := []broker.Option{brokerSrv.Client(microClient)}
if len(ctx.String("broker_address")) > 0 {
brokerOpts = append(brokerOpts, broker.Addrs(ctx.String("broker_address")))
}