diff --git a/ocis-pkg/natsjsregistry/options.go b/ocis-pkg/natsjsregistry/options.go index dfcda616a..a6e862a62 100644 --- a/ocis-pkg/natsjsregistry/options.go +++ b/ocis-pkg/natsjsregistry/options.go @@ -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}) + } +} diff --git a/ocis-pkg/natsjsregistry/registry.go b/ocis-pkg/natsjsregistry/registry.go index 1685515a2..d751229b1 100644 --- a/ocis-pkg/natsjsregistry/registry.go +++ b/ocis-pkg/natsjsregistry/registry.go @@ -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)) } diff --git a/ocis-pkg/registry/registry.go b/ocis-pkg/registry/registry.go index ed9ece2ae..cee918bf8 100644 --- a/ocis-pkg/registry/registry.go +++ b/ocis-pkg/registry/registry.go @@ -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()