Skip to content

Commit

Permalink
Add support for allow/deny permissions (#136)
Browse files Browse the repository at this point in the history
* Add support for allow/deny in permissions

* Add example with allow/deny in auth

* Fix for logtime default

Signed-off-by: Waldemar Quevedo <[email protected]>
  • Loading branch information
wallyqs authored Mar 6, 2019
1 parent 914a8b3 commit b93df89
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 19 deletions.
21 changes: 15 additions & 6 deletions example/clients-auth-permissions.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
{
"users": [
{ "username": "user1", "password": "user1secret" },
{ "username": "user2", "password": "user2secret",
"permissions": {
"publish": ["hello.*"],
"subscribe": ["hello.world"]
{ "username": "user0", "password": "user0secret",
"permissions": {
"publish": {
"allow": ["public.>"],
"deny": ["private.>"]
}
}
},
{ "username": "user1", "password": "user1secret"
},
{ "username": "user2", "password": "user2secret",
"permissions": {
"publish": ["hello.*"],
"subscribe": ["hello.world"]
}
}
}
],
"default_permissions": {
"publish": ["SANDBOX.*"],
Expand Down
5 changes: 3 additions & 2 deletions pkg/conf/natsconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ type User struct {
// Permissions are the allowed subjects on a per
// publish or subscribe basis.
type Permissions struct {
Publish []string `json:"publish,omitempty"`
Subscribe []string `json:"subscribe,omitempty"`
// Can be either a map with allow/deny or an array.
Publish interface{} `json:"publish,omitempty"`
Subscribe interface{} `json:"subscribe,omitempty"`
}

// Marshal takes a server configuration and returns its
Expand Down
74 changes: 63 additions & 11 deletions pkg/conf/natsconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ func TestConfMarshal(t *testing.T) {
err error
}{
{
input: &ServerConfig{},
output: "{}",
err: nil,
input: &ServerConfig{},
output: `{
"logtime": false
}`,
err: nil,
},
{
input: &ServerConfig{
HTTPPort: 8222,
},
output: `{
"http_port": 8222
"http_port": 8222,
"logtime": false
}`,
err: nil,
},
Expand All @@ -30,7 +33,8 @@ func TestConfMarshal(t *testing.T) {
Port: 4222,
},
output: `{
"port": 4222
"port": 4222,
"logtime": false
}`,
err: nil,
},
Expand All @@ -41,15 +45,18 @@ func TestConfMarshal(t *testing.T) {
},
output: `{
"port": 4222,
"http_port": 8222
"http_port": 8222,
"logtime": false
}`,
err: nil,
},
{
input: &ServerConfig{
LameDuckDuration: "2m",
Logtime: true,
},
output: `{
"logtime": true,
"lame_duck_duration": "2m"
}`,
},
Expand All @@ -66,7 +73,8 @@ func TestConfMarshal(t *testing.T) {
"http_port": 8222,
"cluster": {
"port": 6222
}
},
"logtime": false
}`,
err: nil,
},
Expand All @@ -93,7 +101,8 @@ func TestConfMarshal(t *testing.T) {
"nats://nats-2.default.svc:6222",
"nats://nats-3.default.svc:6222"
]
}
},
"logtime": false
}`,
err: nil,
},
Expand Down Expand Up @@ -124,7 +133,8 @@ func TestConfMarshal(t *testing.T) {
]
},
"debug": true,
"trace": true
"trace": true,
"logtime": false
}`,
err: nil,
},
Expand Down Expand Up @@ -161,7 +171,8 @@ func TestConfMarshal(t *testing.T) {
"cert_file": "/etc/nats-tls/server.pem",
"key_file": "/etc/nats-tls/server-key.pem"
}
}
},
"logtime": false
}`,
err: nil,
},
Expand All @@ -179,6 +190,7 @@ func TestConfMarshal(t *testing.T) {
output: `{
"port": 4222,
"http_port": 8222,
"logtime": false,
"authorization": {
"default_permissions": {
"publish": [
Expand All @@ -192,6 +204,46 @@ func TestConfMarshal(t *testing.T) {
}`,
err: nil,
},
{
input: &ServerConfig{
Authorization: &AuthorizationConfig{
DefaultPermissions: &Permissions{
Publish: map[string][]string{
"allow": []string{"hello", "world"},
"deny": []string{"foo.*", "bar.>"},
},
Subscribe: map[string][]string{
"allow": []string{"hi", "everyone"},
},
},
},
},
output: `{
"logtime": false,
"authorization": {
"default_permissions": {
"publish": {
"allow": [
"hello",
"world"
],
"deny": [
"foo.*",
"bar.>"
]
},
"subscribe": {
"allow": [
"hi",
"everyone"
]
}
}
}
}`,

err: nil,
},
}

for _, tt := range tests {
Expand All @@ -202,7 +254,7 @@ func TestConfMarshal(t *testing.T) {
}
o := strings.TrimSpace(string(res))
if o != tt.output {
t.Errorf("Unexpected output: %v", o)
t.Errorf("Expected %+v, got: %+v", tt.output, o)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ func CreateConfigSecret(kubecli corev1client.CoreV1Interface, operatorcli natsal
sconfig.MaxSubscriptions = cluster.ServerConfig.MaxSubscriptions
sconfig.MaxControlLine = cluster.ServerConfig.MaxControlLine
sconfig.Logtime = !cluster.ServerConfig.DisableLogtime
} else {
sconfig.Logtime = true
}

if cluster.ExtraRoutes != nil {
Expand Down Expand Up @@ -507,6 +509,8 @@ func UpdateConfigSecret(
sconfig.MaxSubscriptions = cluster.ServerConfig.MaxSubscriptions
sconfig.MaxControlLine = cluster.ServerConfig.MaxControlLine
sconfig.Logtime = !cluster.ServerConfig.DisableLogtime
} else {
sconfig.Logtime = true
}

if cluster.Pod != nil && cluster.Pod.AdvertiseExternalIP {
Expand Down

0 comments on commit b93df89

Please sign in to comment.