Skip to content

Commit

Permalink
[IMPROVED] Connect error when given an invalid user creds file
Browse files Browse the repository at this point in the history
The failure to connect would be "no nkey seed found". It will now
be more explicit:
```
error signing nonce: unable to extract key pair from file "myfile.creds": no nkey seed found
```

Signed-off-by: Ivan Kozlovic <ivan@synadia.com>
  • Loading branch information
kozlovic committed Feb 18, 2022
1 parent 0096b1b commit abfa624
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ func (nc *Conn) connectProto() (string, error) {
}
sigraw, err := o.SignatureCB([]byte(nc.info.Nonce))
if err != nil {
return _EMPTY_, err
return _EMPTY_, fmt.Errorf("error signing nonce: %v", err)
}
sig = base64.RawURLEncoding.EncodeToString(sigraw)
}
Expand Down Expand Up @@ -5237,7 +5237,7 @@ func nkeyPairFromSeedFile(seedFile string) (nkeys.KeyPair, error) {
func sigHandler(nonce []byte, seedFile string) ([]byte, error) {
kp, err := nkeyPairFromSeedFile(seedFile)
if err != nil {
return nil, err
return nil, fmt.Errorf("unable to extract key pair from file %q: %v", seedFile, err)
}
// Wipe our key on exit.
defer kp.Wipe()
Expand Down
11 changes: 11 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,17 @@ func TestUserCredentialsChainedFile(t *testing.T) {
t.Fatalf("Expected to connect, got %v", err)
}
nc.Close()

chainedFile = createTmpFile(t, []byte("invalid content"))
defer os.Remove(chainedFile)
nc, err = Connect(url, UserCredentials(chainedFile))
if err == nil || !strings.Contains(err.Error(),
"error signing nonce: unable to extract key pair from file") {
if nc != nil {
nc.Close()
}
t.Fatalf("Expected error about invalid creds file, got %q", err)
}
}

func TestExpiredAuthentication(t *testing.T) {
Expand Down

0 comments on commit abfa624

Please sign in to comment.