mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-16 19:14:41 -07:00
@@ -1896,6 +1896,69 @@ func TestClusterEmptyWhenNotDefined(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoutezPermissions(t *testing.T) {
|
||||
opts := DefaultMonitorOptions()
|
||||
opts.Cluster.Host = "127.0.0.1"
|
||||
opts.Cluster.Port = -1
|
||||
opts.Cluster.Permissions = &RoutePermissions{
|
||||
Import: &SubjectPermission{
|
||||
Allow: []string{"foo"},
|
||||
},
|
||||
Export: &SubjectPermission{
|
||||
Allow: []string{"*"},
|
||||
Deny: []string{"foo", "nats"},
|
||||
},
|
||||
}
|
||||
|
||||
s1 := RunServer(opts)
|
||||
defer s1.Shutdown()
|
||||
|
||||
opts = DefaultMonitorOptions()
|
||||
opts.Cluster.Host = "127.0.0.1"
|
||||
opts.Cluster.Port = -1
|
||||
routeURL, _ := url.Parse(fmt.Sprintf("nats-route://127.0.0.1:%d", s1.ClusterAddr().Port))
|
||||
opts.Routes = []*url.URL{routeURL}
|
||||
opts.HTTPPort = -1
|
||||
|
||||
s2 := RunServer(opts)
|
||||
defer s2.Shutdown()
|
||||
|
||||
checkClusterFormed(t, s1, s2)
|
||||
|
||||
urls := []string{
|
||||
fmt.Sprintf("http://127.0.0.1:%d/routez", s1.MonitorAddr().Port),
|
||||
fmt.Sprintf("http://127.0.0.1:%d/routez", s2.MonitorAddr().Port),
|
||||
}
|
||||
servers := []*Server{s1, s2}
|
||||
|
||||
for i, url := range urls {
|
||||
for mode := 0; mode < 2; mode++ {
|
||||
rz := pollRoutez(t, servers[i], mode, url, nil)
|
||||
// For server 1, we expect to see imports and exports
|
||||
if i == 0 {
|
||||
if rz.Imports == nil || rz.Imports.Allow == nil ||
|
||||
len(rz.Imports.Allow) != 1 || rz.Imports.Allow[0] != "foo" ||
|
||||
rz.Imports.Deny != nil {
|
||||
t.Fatalf("Unexpected Imports %v", rz.Imports)
|
||||
}
|
||||
if rz.Exports == nil || rz.Exports.Allow == nil || rz.Exports.Deny == nil ||
|
||||
len(rz.Exports.Allow) != 1 || rz.Exports.Allow[0] != "*" ||
|
||||
len(rz.Exports.Deny) != 2 || rz.Exports.Deny[0] != "foo" || rz.Exports.Deny[1] != "nats" {
|
||||
t.Fatalf("Unexpected Exports %v", rz.Exports)
|
||||
}
|
||||
} else {
|
||||
// We expect to see NO imports and exports for server B.
|
||||
if rz.Imports != nil {
|
||||
t.Fatal("Routez body should NOT contain \"imports\" information.")
|
||||
}
|
||||
if rz.Exports != nil {
|
||||
t.Fatal("Routez body should NOT contain \"exports\" information.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark our Connz generation. Don't use HTTP here, just measure server endpoint.
|
||||
func Benchmark_Connz(b *testing.B) {
|
||||
runtime.MemProfileRate = 0
|
||||
|
||||
@@ -18,7 +18,6 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@@ -869,64 +868,6 @@ func TestRouteBasicPermissions(t *testing.T) {
|
||||
defer srvB.Shutdown()
|
||||
|
||||
checkClusterFormed(t, srvA, srvB)
|
||||
resetPreviousHTTPConnections()
|
||||
|
||||
// Check for proper monitoring reporting for permissions.
|
||||
murl := fmt.Sprintf("http://%s:%d/", optsA.HTTPHost, optsA.HTTPPort)
|
||||
resp, err := http.Get(murl + "routez")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: Got %v\n", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Got an error reading the body: %v\n", err)
|
||||
}
|
||||
// We expect to see imports and exports
|
||||
if !strings.Contains(string(body), "imports") {
|
||||
t.Fatal("Routez body should contain \"imports\" information.")
|
||||
}
|
||||
if !strings.Contains(string(body), "exports") {
|
||||
t.Fatal("Routez body should contain \"exports\" information.")
|
||||
}
|
||||
rz := &server.Routez{}
|
||||
if err := json.Unmarshal(body, rz); err != nil {
|
||||
t.Fatalf("Got an error unmarshalling the body: %v\n", err)
|
||||
}
|
||||
if rz.Imports == nil || rz.Imports.Allow == nil ||
|
||||
len(rz.Imports.Allow) != 1 || rz.Imports.Allow[0] != "foo" ||
|
||||
rz.Imports.Deny != nil {
|
||||
t.Fatalf("Unexpected Imports %v\n", rz.Imports)
|
||||
}
|
||||
if rz.Exports == nil || rz.Exports.Allow == nil || rz.Exports.Deny == nil ||
|
||||
len(rz.Exports.Allow) != 1 || rz.Exports.Allow[0] != "*" ||
|
||||
len(rz.Exports.Deny) != 2 || rz.Exports.Deny[0] != "foo" || rz.Exports.Deny[1] != "nats" {
|
||||
t.Fatalf("Unexpected Exports %v\n", rz.Exports)
|
||||
}
|
||||
|
||||
murl = fmt.Sprintf("http://%s:%d/", optsB.HTTPHost, optsB.HTTPPort)
|
||||
resp, err = http.Get(murl + "routez")
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error: Got %v\n", err)
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Fatalf("Expected a 200 response, got %d\n", resp.StatusCode)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Got an error reading the body: %v\n", err)
|
||||
}
|
||||
// We expect to see NO imports and exports for server B.
|
||||
if strings.Contains(string(body), "imports") {
|
||||
t.Fatal("Routez body should NOT contain \"imports\" information.")
|
||||
}
|
||||
if strings.Contains(string(body), "exports") {
|
||||
t.Fatal("Routez body should NOT contain \"exports\" information.")
|
||||
}
|
||||
|
||||
// Create a connection to server B
|
||||
ncb, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", optsB.Port))
|
||||
|
||||
Reference in New Issue
Block a user