- A service-specific username or password now takes precedence over the global NATS JWT credentials. Before, OCIS_NATS_CREDS_FILE always replaced a value like GRAPH_CACHE_AUTH_USERNAME. The service registry uses the same rule for MICRO_REGISTRY_AUTH_USERNAME and MICRO_REGISTRY_AUTH_PASSWORD. - The NATS health check keeps one connection and uses it again for each probe. Before, each probe read the .creds file and opened a new connection. A probe could read a rotated file before the write was complete. The proxy, ocs and gateway services now build the check once. - The startup check of the store credentials runs only for the nats-js and nats-js-kv store types. Other store types do not use the credentials. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
60 lines
1.7 KiB
Go
60 lines
1.7 KiB
Go
package shared
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/nats-io/jwt/v2"
|
|
"github.com/nats-io/nkeys"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func newTestCreds(t *testing.T) string {
|
|
t.Helper()
|
|
accountKP, err := nkeys.CreateAccount()
|
|
require.NoError(t, err)
|
|
userKP, err := nkeys.CreateUser()
|
|
require.NoError(t, err)
|
|
userPub, err := userKP.PublicKey()
|
|
require.NoError(t, err)
|
|
userJWT, err := jwt.NewUserClaims(userPub).Encode(accountKP)
|
|
require.NoError(t, err)
|
|
seed, err := userKP.Seed()
|
|
require.NoError(t, err)
|
|
creds, err := jwt.FormatUserConfig(userJWT, seed)
|
|
require.NoError(t, err)
|
|
return string(creds)
|
|
}
|
|
|
|
func TestCheckNatsCredentials(t *testing.T) {
|
|
t.Run("no credentials", func(t *testing.T) {
|
|
require.NoError(t, CheckNatsCredentials("proxy", "nats-js-kv", "", ""))
|
|
})
|
|
|
|
t.Run("valid content", func(t *testing.T) {
|
|
require.NoError(t, CheckNatsCredentials("proxy", "nats-js-kv", "", newTestCreds(t)))
|
|
})
|
|
|
|
t.Run("partly written content", func(t *testing.T) {
|
|
creds := newTestCreds(t)
|
|
err := CheckNatsCredentials("proxy", "nats-js-kv", "", creds[:len(creds)/3])
|
|
require.ErrorContains(t, err, "proxy")
|
|
})
|
|
|
|
t.Run("missing file", func(t *testing.T) {
|
|
err := CheckNatsCredentials("proxy", "nats-js-kv", filepath.Join(t.TempDir(), "missing.creds"), "")
|
|
require.ErrorContains(t, err, "proxy")
|
|
})
|
|
|
|
t.Run("store without nats", func(t *testing.T) {
|
|
for _, store := range []string{"memory", "redis-sentinel", "noop", ""} {
|
|
require.NoError(t, CheckNatsCredentials("proxy", store, filepath.Join(t.TempDir(), "missing.creds"), ""), store)
|
|
}
|
|
})
|
|
|
|
t.Run("nats-js store", func(t *testing.T) {
|
|
err := CheckNatsCredentials("proxy", "nats-js", filepath.Join(t.TempDir(), "missing.creds"), "")
|
|
require.ErrorContains(t, err, "proxy")
|
|
})
|
|
}
|