Skip to content

Commit

Permalink
Add in route permission support
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Collison <derek@nats.io>
  • Loading branch information
derekcollison committed Aug 23, 2018
1 parent a79806e commit 5955f22
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ type Permissions struct {
// but describe what a server can import/export from and to
// another server.
type RoutePermissions struct {
Import []string `json:"import"`
Export []string `json:"export"`
Import *SubjectPermission `json:"import"`
Export *SubjectPermission `json:"export"`
}

// clone will clone an individual subject permission.
Expand Down
10 changes: 8 additions & 2 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,14 @@ func parseCluster(cm map[string]interface{}, opts *Options) error {
// The parsing sets Import into Publish and Export into Subscribe, convert
// accordingly.
opts.Cluster.Permissions = &RoutePermissions{
Import: auth.defaultPermissions.Publish.Allow,
Export: auth.defaultPermissions.Subscribe.Allow,
Import: &SubjectPermission{
Allow: auth.defaultPermissions.Publish.Allow,
Deny: auth.defaultPermissions.Publish.Deny,
},
Export: &SubjectPermission{
Allow: auth.defaultPermissions.Subscribe.Allow,
Deny: auth.defaultPermissions.Subscribe.Deny,
},
}
}
case "routes":
Expand Down
10 changes: 8 additions & 2 deletions server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,14 @@ func (c *client) setRoutePermissions(perms *RoutePermissions) {
// and Export permission is mapped to Subscribe.
// For meaning of Import/Export, see canImport and canExport.
p := &Permissions{}
p.Publish = &SubjectPermission{Allow: perms.Import}
p.Subscribe = &SubjectPermission{Allow: perms.Export}
p.Publish = &SubjectPermission{
Allow: perms.Import.Allow,
Deny: perms.Import.Deny,
}
p.Subscribe = &SubjectPermission{
Allow: perms.Export.Allow,
Deny: perms.Export.Deny,
}
c.setPermissions(p)
}

Expand Down
7 changes: 5 additions & 2 deletions test/configs/srv_a_perms.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cluster Server A
# Cluster Server A with Permissions

listen: 127.0.0.1:5222

Expand All @@ -11,7 +11,10 @@ cluster {
timeout: 0.5
permissions {
import: "foo"
export: ["bar", "baz"]
export: {
allow: "*"
deny: ["foo", "nats"]
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion test/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,8 @@ func TestRouteBasicPermissions(t *testing.T) {
cb := func(_ *nats.Msg) {
ch <- true
}
// Subscribe on on "bar" and "baz", which should be accepted by server A
// Subscribe on Server B on "bar" and "baz", which should be accepted by server A across the route
// Due to allowing "*"
subBbar, err := ncb.Subscribe("bar", cb)
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
Expand Down

0 comments on commit 5955f22

Please sign in to comment.