Skip to content

Commit

Permalink
Detect duplicate service to subjects on imports (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison authored and aricart committed Sep 20, 2019
1 parent 09b136b commit 625014e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

const (
// Version is semantic version.
Version = "0.2.18"
Version = "0.3.0"

// TokenTypeJwt is the JWT token type supported JWT tokens
// encoded and decoded by this library
Expand Down
7 changes: 7 additions & 0 deletions imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,14 @@ type Imports []*Import

// Validate checks if an import is valid for the wrapping account
func (i *Imports) Validate(acctPubKey string, vr *ValidationResults) {
toSet := make(map[Subject]bool, len(*i))
for _, v := range *i {
if v.Type == Service {
if _, ok := toSet[v.To]; ok {
vr.AddError("Duplicate To subjects for %q", v.To)
}
toSet[v.To] = true
}
v.Validate(acctPubKey, vr)
}
}
Expand Down
29 changes: 29 additions & 0 deletions imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,32 @@ func TestImportSubjectValidation(t *testing.T) {
t.Errorf("imports with valid contains subject should be valid")
}
}

func TestImportServiceDoubleToSubjectsValidation(t *testing.T) {
akp := createAccountNKey(t)
akp2 := createAccountNKey(t)
apk := publicKey(akp, t)
apk2 := publicKey(akp2, t)

account := NewAccountClaims(apk)

i := &Import{Subject: "one.two", Account: apk2, To: "foo.bar", Type: Service}
account.Imports.Add(i)

vr := CreateValidationResults()
account.Validate(vr)

if vr.IsBlocking(true) {
t.Fatalf("Expected no blocking validation errors")
}

i2 := &Import{Subject: "two.three", Account: apk2, To: "foo.bar", Type: Service}
account.Imports.Add(i2)

vr = CreateValidationResults()
account.Validate(vr)

if !vr.IsBlocking(true) {
t.Fatalf("Expected multiple import 'to' subjects to produce an error")
}
}

0 comments on commit 625014e

Please sign in to comment.