The reva store cannot return an error for invalid NATS credentials. It only logs the error, and each store operation fails later. The config validation of each service with a store now checks the credentials. An invalid value now stops the service at startup. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
49 lines
1.3 KiB
Go
49 lines
1.3 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", "", ""))
|
|
})
|
|
|
|
t.Run("valid content", func(t *testing.T) {
|
|
require.NoError(t, CheckNatsCredentials("proxy", "", newTestCreds(t)))
|
|
})
|
|
|
|
t.Run("partly written content", func(t *testing.T) {
|
|
creds := newTestCreds(t)
|
|
err := CheckNatsCredentials("proxy", "", creds[:len(creds)/3])
|
|
require.ErrorContains(t, err, "proxy")
|
|
})
|
|
|
|
t.Run("missing file", func(t *testing.T) {
|
|
err := CheckNatsCredentials("proxy", filepath.Join(t.TempDir(), "missing.creds"), "")
|
|
require.ErrorContains(t, err, "proxy")
|
|
})
|
|
}
|