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

Add support for qsub permissions #68

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 32 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ func (s Subject) Validate(vr *ValidationResults) {
}
}

// SubscribeSubject is a string that represents a NATS subscription.
type SubscribeSubject string

// Validate checks that a subject string is valid.
func (s SubscribeSubject) Validate(vr *ValidationResults) {
v := string(s)
if v == "" {
vr.AddError("subject cannot be empty")
}
vals := strings.Fields(strings.TrimSpace(v))
if len(vals) > 2 {
vr.AddError("invalid subscription %q", v)
}
}

// HasWildCards is used to check if a subject contains a > or *
func (s Subject) HasWildCards() bool {
v := string(s)
Expand Down Expand Up @@ -220,6 +235,22 @@ func (p *Permission) Validate(vr *ValidationResults) {
}
}

// SubscribePermission defines allow/deny subjects used for subscriptions.
type SubscribePermission struct {
Allow StringList `json:"allow,omitempty"`
Deny StringList `json:"deny,omitempty"`
}

// Validate the allow, deny elements of a permission
func (p *SubscribePermission) Validate(vr *ValidationResults) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the validation doesn't really work because if the subscribe permission is >.a queue the invalid subject is not validated. again this is the problem with this strategy, we cannot really validate, because if the user intended the subject a b, it would be interpreted as subject a queue b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data model for expressing this is not correct - it cannot just add a space when one of the components specifies a space as illegal.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>.a is an invalid subject so we could add the IsValidLiteralSubject logic from the server here though would help the most in nsc to catch these earlier rather than during runtime which would only result in an authentication error rather than invalid creds for example. I think the model is fine I'd say that also comparatively easier to maintain than making the involved types more complex by moving from subject string to object type with subject + queue.

for _, subj := range p.Allow {
SubscribeSubject(subj).Validate(vr)
}
for _, subj := range p.Deny {
SubscribeSubject(subj).Validate(vr)
}
}

// ResponsePermission can be used to allow responses to any reply subject
// that is received on a valid subscription.
type ResponsePermission struct {
Expand All @@ -235,7 +266,7 @@ func (p *ResponsePermission) Validate(vr *ValidationResults) {
// Permissions are used to restrict subject access, either on a user or for everyone on a server by default
type Permissions struct {
Pub Permission `json:"pub,omitempty"`
Sub Permission `json:"sub,omitempty"`
Sub SubscribePermission `json:"sub,omitempty"`
Resp *ResponsePermission `json:"resp,omitempty"`
}

Expand Down
51 changes: 46 additions & 5 deletions user_claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ func TestUserValidation(t *testing.T) {
}

uc.Permissions.Pub.Allow.Remove("bad subject")
uc.Permissions.Sub.Allow.Add("bad subject")
uc.Permissions.Sub.Allow.Add("ok subject")
vr = CreateValidationResults()
uc.Validate(vr)

if vr.IsEmpty() || len(vr.Issues) != 1 || !vr.IsBlocking(true) {
t.Error("bad permission should be invalid")
if !vr.IsEmpty() {
t.Error("valid user permissions should be valid")
}

uc.Permissions.Sub.Allow.Remove("bad subject")
uc.Permissions.Sub.Allow.Remove("ok subject")
uc.Permissions.Pub.Deny.Add("bad subject")
vr = CreateValidationResults()
uc.Validate(vr)
Expand All @@ -256,13 +256,54 @@ func TestUserValidation(t *testing.T) {
}

uc.Permissions.Pub.Deny.Remove("bad subject")
uc.Permissions.Sub.Deny.Add("bad subject")
uc.Permissions.Sub.Deny.Add("ok subject")
vr = CreateValidationResults()
uc.Validate(vr)

if !vr.IsEmpty() {
t.Error("valid user permissions should be valid")
}
}

func TestUserQueueSubscribeValidation(t *testing.T) {
ukp := createUserNKey(t)

uc := NewUserClaims(publicKey(ukp, t))
uc.Permissions.Pub.Allow.Add("_INBOX.>")
uc.Permissions.Pub.Deny.Add("foo")
uc.Permissions.Sub.Allow.Add("foo")
uc.Permissions.Sub.Allow.Add("foo v2.*")
uc.Permissions.Sub.Deny.Add("foo v1")
uc.Permissions.Sub.Deny.Add("> v3")

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

if !vr.IsEmpty() {
t.Error("valid user permissions should be valid")
}

// There should be nothing but whitespace after the name of the queue group.
uc.Permissions.Sub.Allow.Add("foo v2 v3")
vr = CreateValidationResults()
uc.Validate(vr)

if vr.IsEmpty() || len(vr.Issues) != 1 || !vr.IsBlocking(true) {
t.Error("bad permission should be invalid")
}

uc.Permissions.Sub.Allow.Remove("foo v2 v3")

// Any number of spaces is ok since the whitespace is trimmed.
uc.Permissions.Sub.Allow.Add("foo v2")
uc.Permissions.Sub.Allow.Add(" bar v4 ")
uc.Permissions.Sub.Allow.Add(" bar v5 ")
vr = CreateValidationResults()
uc.Validate(vr)

if !vr.IsEmpty() {
t.Error("valid user permissions should be valid")
}
}

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