Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix creds parsing on files that have had their line endings changed to CRLFs. #72

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion creds_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ NKEYs are sensitive and should be treated as secrets.
return w.Bytes(), nil
}

var userConfigRE = regexp.MustCompile(`\s*(?:(?:[-]{3,}[^\n]*[-]{3,}\n)(.+)(?:\n\s*[-]{3,}[^\n]*[-]{3,}\n))`)
var userConfigRE = regexp.MustCompile(`\s*(?:(?:[-]{3,}.*[-]{3,}\r?\n)([\w\-.=]+)(?:\r?\n[-]{3,}.*[-]{3,}\r?\n))`)

// An user config file looks like this:
// -----BEGIN NATS USER JWT-----
Expand Down
63 changes: 63 additions & 0 deletions creds_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,66 @@ func Test_DecorateNKeys(t *testing.T) {
t.Fatal("required error parsing bad nkey")
}
}

func Test_ParseCreds(t *testing.T) {
token, kp := makeJWT(t)
d, err := FormatUserConfig(token, seedKey(kp, t))
if err != nil {
t.Fatal(err)
}
pk, err := kp.PublicKey()
if err != nil {
t.Fatal(err)
}

token2, err := ParseDecoratedJWT(d)
if err != nil {
t.Fatal(err)
}
if token != token2 {
t.Fatal("expected jwts to match")
}
kp2, err := ParseDecoratedUserNKey(d)
if err != nil {
t.Fatal(err)
}
pk2, err := kp2.PublicKey()
if err != nil {
t.Fatal(err)
}
if pk != pk2 {
t.Fatal("expected keys to match")
}
}

func Test_ParseCredsWithCrLfs(t *testing.T) {
token, kp := makeJWT(t)
d, err := FormatUserConfig(token, seedKey(kp, t))
if err != nil {
t.Fatal(err)
}
pk, err := kp.PublicKey()
if err != nil {
t.Fatal(err)
}
d = bytes.ReplaceAll(d, []byte{'\n'}, []byte{'\r', '\n'})

token2, err := ParseDecoratedJWT(d)
if err != nil {
t.Fatal(err)
}
if token != token2 {
t.Fatal("expected jwts to match")
}
kp2, err := ParseDecoratedUserNKey(d)
if err != nil {
t.Fatal(err)
}
pk2, err := kp2.PublicKey()
if err != nil {
t.Fatal(err)
}
if pk != pk2 {
t.Fatal("expected keys to match")
}
}