From 09b23895375fafa7240af2981c88ee5c64961159 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 24 Jan 2024 16:39:33 +0100 Subject: [PATCH] move nats authentication to natsjsregistry package Signed-off-by: jkoberg --- changelog/unreleased/fix-nats-registry.md | 6 ++++ ocis-pkg/natsjsregistry/options.go | 11 ------- ocis-pkg/natsjsregistry/registry.go | 38 +++++++++++++++++------ ocis-pkg/registry/registry.go | 13 +++----- 4 files changed, 39 insertions(+), 29 deletions(-) create mode 100644 changelog/unreleased/fix-nats-registry.md diff --git a/changelog/unreleased/fix-nats-registry.md b/changelog/unreleased/fix-nats-registry.md new file mode 100644 index 00000000000..ba5b29d0e38 --- /dev/null +++ b/changelog/unreleased/fix-nats-registry.md @@ -0,0 +1,6 @@ +Bugfix: Fix nats registry + +The nats registry would behave badly when configuring `nats-js-kv` via envvar. Reason is the way go-micro initializes. +It took 5 developers to find the issue and the fix so the details cannot be shared here. Just accept that it is working now + +https://github.com/owncloud/ocis/pull/8281 diff --git a/ocis-pkg/natsjsregistry/options.go b/ocis-pkg/natsjsregistry/options.go index a6e862a62a2..dfcda616a59 100644 --- a/ocis-pkg/natsjsregistry/options.go +++ b/ocis-pkg/natsjsregistry/options.go @@ -10,7 +10,6 @@ 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 { @@ -31,13 +30,3 @@ 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 857b4e5198b..feeac744b9f 100644 --- a/ocis-pkg/natsjsregistry/registry.go +++ b/ocis-pkg/natsjsregistry/registry.go @@ -5,6 +5,8 @@ import ( "context" "encoding/json" "errors" + "os" + "strings" "time" natsjskv "github.com/go-micro/plugins/v4/store/nats-js-kv" @@ -14,7 +16,12 @@ import ( "go-micro.dev/v4/util/cmd" ) -var _registryName = "nats-js-kv" +var ( + _registryName = "nats-js-kv" + _registryAddressEnv = "MICRO_REGISTRY_ADDRESS" + _registryUsernameEnv = "MICRO_REGISTRY_AUTH_USERNAME" + _registryPasswordEnv = "MICRO_REGISTRY_AUTH_PASSWORD" +) func init() { cmd.DefaultRegistries[_registryName] = NewRegistry @@ -31,7 +38,7 @@ func NewRegistry(opts ...registry.Option) registry.Registry { exp, _ := options.Context.Value(expiryKey{}).(time.Duration) return &storeregistry{ opts: options, - store: natsjskv.NewStore(append(storeOptions(options), natsjskv.DefaultMemory())...), + store: natsjskv.NewStore(storeOptions(options)...), typ: _registryName, expiry: exp, } @@ -127,18 +134,31 @@ func (n *storeregistry) String() string { func storeOptions(opts registry.Options) []store.Option { storeoptions := []store.Option{ - store.Nodes(opts.Addrs...), store.Database("service-registry"), store.Table("service-registry"), + natsjskv.DefaultMemory(), } - if so, ok := opts.Context.Value(storeOptionsKey{}).([]store.Option); ok { - storeoptions = append(storeoptions, so...) + + addr := []string{"127.0.0.1:9233"} + if len(opts.Addrs) > 0 { + addr = opts.Addrs + } else if a := strings.Split(os.Getenv(_registryAddressEnv), ","); len(a) > 0 && a[0] != "" { + addr = a } + storeoptions = append(storeoptions, store.Nodes(addr...)) + 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] + natsOptions.User, natsOptions.Password = getAuth() + storeoptions = append(storeoptions, natsjskv.NatsOptions(natsOptions)) + + if so, ok := opts.Context.Value(storeOptionsKey{}).([]store.Option); ok { + storeoptions = append(storeoptions, so...) } - return append(storeoptions, natsjskv.NatsOptions(natsOptions)) + + return storeoptions +} + +func getAuth() (string, string) { + return os.Getenv(_registryUsernameEnv), os.Getenv(_registryPasswordEnv) } diff --git a/ocis-pkg/registry/registry.go b/ocis-pkg/registry/registry.go index 0b6b3e71f03..1672a1a8e17 100644 --- a/ocis-pkg/registry/registry.go +++ b/ocis-pkg/registry/registry.go @@ -20,10 +20,8 @@ import ( ) const ( - registryEnv = "MICRO_REGISTRY" - registryAddressEnv = "MICRO_REGISTRY_ADDRESS" - registryUsernameEnv = "MICRO_REGISTRY_AUTH_USERNAME" - registryPasswordEnv = "MICRO_REGISTRY_AUTH_PASSWORD" + _registryEnv = "MICRO_REGISTRY" + _registryAddressEnv = "MICRO_REGISTRY_ADDRESS" ) var ( @@ -64,7 +62,6 @@ func GetRegistry(opts ...Option) mRegistry.Registry { case "natsjs", "nats-js", "nats-js-kv": // for backwards compatibility - we will stick with one of those _reg = natsjsregistry.NewRegistry( mRegistry.Addrs(cfg.Addresses...), - natsjsregistry.Authenticate(cfg.Username, cfg.Password), ) case "memory": _reg = memr.NewRegistry() @@ -112,15 +109,13 @@ func getEnvs(opts ...Option) *Config { cfg := &Config{ Type: "nats-js-kv", Addresses: []string{"127.0.0.1:9233"}, - Username: os.Getenv(registryUsernameEnv), - Password: os.Getenv(registryPasswordEnv), } - if s := os.Getenv(registryEnv); s != "" { + if s := os.Getenv(_registryEnv); s != "" { cfg.Type = s } - if s := strings.Split(os.Getenv(registryAddressEnv), ","); len(s) > 0 && s[0] != "" { + if s := strings.Split(os.Getenv(_registryAddressEnv), ","); len(s) > 0 && s[0] != "" { cfg.Addresses = s }