Skip to content

Commit

Permalink
Merge 74e271d into d50c137
Browse files Browse the repository at this point in the history
  • Loading branch information
wallyqs committed Jan 4, 2023
2 parents d50c137 + 74e271d commit a29655c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
12 changes: 9 additions & 3 deletions nats.go
Expand Up @@ -1136,6 +1136,12 @@ func UserJWT(userCB UserJWTHandler, sigCB SignatureHandler) Option {
if sigCB == nil {
return ErrUserButNoSigCB
}
// Smoke test the user callback to ensure it is setup properly
// when processing options.
if _, err := userCB(); err != nil {
return err
}

o.UserJWT = userCB
o.SignatureCB = sigCB
return nil
Expand Down Expand Up @@ -5432,12 +5438,12 @@ func wipeSlice(buf []byte) {
func userFromFile(userFile string) (string, error) {
path, err := expandPath(userFile)
if err != nil {
return _EMPTY_, fmt.Errorf("nats: %v", err)
return _EMPTY_, fmt.Errorf("nats: %w", err)
}

contents, err := os.ReadFile(path)
if err != nil {
return _EMPTY_, fmt.Errorf("nats: %v", err)
return _EMPTY_, fmt.Errorf("nats: %w", err)
}
defer wipeSlice(contents)
return nkeys.ParseDecoratedJWT(contents)
Expand Down Expand Up @@ -5486,7 +5492,7 @@ func expandPath(p string) (string, error) {
func nkeyPairFromSeedFile(seedFile string) (nkeys.KeyPair, error) {
contents, err := os.ReadFile(seedFile)
if err != nil {
return nil, fmt.Errorf("nats: %v", err)
return nil, fmt.Errorf("nats: %w", err)
}
defer wipeSlice(contents)
return nkeys.ParseDecoratedNKey(contents)
Expand Down
13 changes: 13 additions & 0 deletions test/auth_test.go
Expand Up @@ -16,6 +16,7 @@ package test
import (
"errors"
"fmt"
"os"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -364,3 +365,15 @@ func TestPermViolation(t *testing.T) {
t.Fatal("Connection should be not be closed")
}
}

func TestConnectMissingCreds(t *testing.T) {
s := RunServerOnPort(-1)
defer s.Shutdown()

// Using TEST-NET sample address from RFC 5737.
testnetaddr := "nats://192.0.2.2:4222"
_, err := nats.Connect(fmt.Sprintf("%s,%s", s.ClientURL(), testnetaddr), nats.UserCredentials("missing"), nats.DontRandomize())
if !errors.Is(err, os.ErrNotExist) {
t.Fatalf("Expected not exists error, got: %v", err)
}
}

0 comments on commit a29655c

Please sign in to comment.