allow authentication for the registry

Signed-off-by: jkoberg <jkoberg@owncloud.com>
This commit is contained in:
jkoberg
2023-12-19 10:44:05 +01:00
parent de4f9d78f4
commit 15efafaf23
3 changed files with 25 additions and 4 deletions
+11
View File
@@ -10,6 +10,7 @@ import (
type storeOptionsKey struct{}
type expiryKey struct{}
type authKey struct{}
// StoreOptions sets the options for the underlying store
func StoreOptions(opts []store.Option) registry.Option {
@@ -30,3 +31,13 @@ func ServiceExpiry(t time.Duration) registry.Option {
o.Context = context.WithValue(o.Context, expiryKey{}, t)
}
}
// Authenticate sets the username/password for the nats connection
func Authenticate(username, password string) registry.Option {
return func(o *registry.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, authKey{}, []string{username, password})
}
}
+8 -1
View File
@@ -8,6 +8,7 @@ import (
"time"
natsjskv "github.com/go-micro/plugins/v4/store/nats-js-kv"
"github.com/nats-io/nats.go"
"go-micro.dev/v4/registry"
"go-micro.dev/v4/store"
"go-micro.dev/v4/util/cmd"
@@ -133,5 +134,11 @@ func storeOptions(opts registry.Options) []store.Option {
if so, ok := opts.Context.Value(storeOptionsKey{}).([]store.Option); ok {
storeoptions = append(storeoptions, so...)
}
return storeoptions
natsOptions := nats.GetDefaultOptions()
natsOptions.Name = "nats-js-kv-registry"
if auth, ok := opts.Context.Value(authKey{}).([]string); ok {
natsOptions.User = auth[0]
natsOptions.Password = auth[1]
}
return append(storeoptions, natsjskv.NatsOptions(natsOptions))
}
+6 -3
View File
@@ -19,8 +19,10 @@ import (
)
const (
registryEnv = "MICRO_REGISTRY"
registryAddressEnv = "MICRO_REGISTRY_ADDRESS"
registryEnv = "MICRO_REGISTRY"
registryAddressEnv = "MICRO_REGISTRY_ADDRESS"
regisryUsernameEnv = "MICRO_REGISTRY_AUTH_USERNAME"
registryPasswordEnv = "MICRO_REGISTRY_AUTH_PASSWORD"
)
var (
@@ -68,9 +70,10 @@ func GetRegistry() mRegistry.Registry {
)
case "memory":
reg = memr.NewRegistry()
case "natsjs":
case "natsjs", "nats-js", "nats-js-kv": // for backwards compatibility - we will stick with one of those
reg = natsjsregistry.NewRegistry(
mRegistry.Addrs(addresses...),
natsjsregistry.Authenticate(os.Getenv(regisryUsernameEnv), os.Getenv(registryPasswordEnv)),
)
default:
reg = mdnsr.NewRegistry()