feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,178 @@
|
||||
package executor_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sea-vhost/internal/controlwire"
|
||||
"sea-vhost/internal/executor"
|
||||
"sea-vhost/internal/nodefiles"
|
||||
"sea-vhost/internal/panel"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type testNode struct {
|
||||
server *httptest.Server
|
||||
client *http.Client
|
||||
id uint64
|
||||
credentials controlwire.Credentials
|
||||
sequence int
|
||||
}
|
||||
|
||||
func startNode(t *testing.T, id uint64, root string) *testNode {
|
||||
t.Helper()
|
||||
g := &executor.Gateway{NodeID: id, TenantRoot: root, Direct: panel.NewMock()}
|
||||
s := httptest.NewUnstartedServer(g.Handler())
|
||||
s.Config.ErrorLog = log.New(io.Discard, "", 0)
|
||||
credentials, identity, err := controlwire.Pair(id, "https://"+s.Listener.Addr().String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.TLS, err = controlwire.ServerTLS(identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.StartTLS()
|
||||
t.Cleanup(s.Close)
|
||||
config, err := controlwire.ClientTLS(credentials)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
client := &http.Client{Transport: &http.Transport{TLSClientConfig: config}, Timeout: 10 * time.Second}
|
||||
t.Cleanup(client.CloseIdleConnections)
|
||||
return &testNode{server: s, client: client, id: id, credentials: credentials}
|
||||
}
|
||||
func (n *testNode) request(t *testing.T, route string, body any) (int, []byte) {
|
||||
t.Helper()
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n.sequence++
|
||||
r, err := controlwire.NewRequest("POST", n.server.URL, route, n.id, fmt.Sprintf("%032x", n.sequence), raw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response, err := n.client.Do(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
out, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return response.StatusCode, out
|
||||
}
|
||||
|
||||
func TestTwoNodesArePairedAndStateless(t *testing.T) {
|
||||
rootA := filepath.ToSlash(t.TempDir())
|
||||
rootB := filepath.ToSlash(t.TempDir())
|
||||
for _, root := range []string{rootA, rootB} {
|
||||
if err := os.Mkdir(filepath.Join(root, "sv0123456789"), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a := startNode(t, 2, rootA)
|
||||
b := startNode(t, 3, rootB)
|
||||
empty := json.RawMessage(`{"version":1,"sites":{},"ftp":{},"databases":{}}`)
|
||||
create := func(n *testNode, root, domain string) panel.ScopedResult {
|
||||
t.Helper()
|
||||
request, _ := json.Marshal(map[string]any{"operation": "create_site", "site": panel.SiteSpec{Name: "sv0123456789", Domain: domain, Path: root + "/sv0123456789", RunPath: "/public", Runtime: "PHP", Version: "8.4", RunUser: "sv0123456789", Port: 20000, ProcessLimit: 10}})
|
||||
status, raw := n.request(t, "/v1/panel", panel.ScopedExecution{Request: request, Registry: empty})
|
||||
if status != 200 {
|
||||
t.Fatalf("create: %d %s", status, raw)
|
||||
}
|
||||
var out panel.ScopedResult
|
||||
if json.Unmarshal(raw, &out) != nil {
|
||||
t.Fatal("invalid scoped result")
|
||||
}
|
||||
return out
|
||||
}
|
||||
first := create(a, rootA, "sv0123456789.a.example.com")
|
||||
second := create(b, rootB, "sv0123456789.b.example.com")
|
||||
if string(first.Response) != string(second.Response) {
|
||||
t.Fatalf("expected colliding native IDs across nodes: %s %s", first.Response, second.Response)
|
||||
}
|
||||
req, _ := json.Marshal(map[string]any{"operation": "set_site_state", "id": 1001, "name": "sv0123456789.a.example.com", "enabled": false})
|
||||
status, _ := b.request(t, "/v1/panel", panel.ScopedExecution{Request: req, Registry: first.Registry})
|
||||
if status == 200 {
|
||||
t.Fatal("node B accepted node A root snapshot")
|
||||
}
|
||||
status, _ = a.request(t, "/v1/panel", panel.ScopedExecution{Request: req, Registry: empty})
|
||||
if status == 200 {
|
||||
t.Fatal("node persisted the previous request registry")
|
||||
}
|
||||
status, _ = a.request(t, "/v1/isolation/v1/shell", map[string]any{"command": "id"})
|
||||
if status != 403 {
|
||||
t.Fatalf("shell route: %d", status)
|
||||
}
|
||||
status, _ = a.request(t, "/v1/isolation/v1/prepare", map[string]any{"hostName": "root"})
|
||||
if status != 400 {
|
||||
t.Fatalf("root identity: %d", status)
|
||||
}
|
||||
r, _ := controlwire.NewRequest("GET", b.server.URL, "/v1/status", b.id, strings.Repeat("a", 32), nil)
|
||||
if response, err := a.client.Do(r); err == nil {
|
||||
response.Body.Close()
|
||||
t.Fatal("node A credentials authenticated to node B")
|
||||
}
|
||||
r, _ = controlwire.NewRequest("GET", a.server.URL, "/v1/status", b.id, strings.Repeat("b", 32), nil)
|
||||
response, err := a.client.Do(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != 403 {
|
||||
t.Fatal("destination identity not checked")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesStayOnTargetNodeAndRejectReplay(t *testing.T) {
|
||||
rootA := t.TempDir()
|
||||
rootB := t.TempDir()
|
||||
host := "sv0123456789"
|
||||
for _, root := range []string{rootA, rootB} {
|
||||
if err := os.Mkdir(filepath.Join(root, host), 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a := startNode(t, 2, rootA)
|
||||
b := startNode(t, 3, rootB)
|
||||
status, raw := a.request(t, "/v1/files", nodefiles.Request{HostName: host, Operation: "write", Path: "marker.txt", Data: []byte("node-A-only")})
|
||||
if status != 200 {
|
||||
t.Fatalf("write %d %s", status, raw)
|
||||
}
|
||||
status, raw = b.request(t, "/v1/files", nodefiles.Request{HostName: host, Operation: "read", Path: "marker.txt"})
|
||||
if status != 200 || !strings.Contains(string(raw), "not_found") {
|
||||
t.Fatalf("cross-node read: %d %s", status, raw)
|
||||
}
|
||||
for _, name := range []string{"../marker.txt", "/etc/passwd", "a/../../x", "x\\..\\y"} {
|
||||
_, raw = a.request(t, "/v1/files", nodefiles.Request{HostName: host, Operation: "read", Path: name})
|
||||
if !strings.Contains(string(raw), "denied") {
|
||||
t.Fatalf("unsafe path accepted %q: %s", name, raw)
|
||||
}
|
||||
}
|
||||
body, _ := json.Marshal(nodefiles.Request{HostName: host, Operation: "stat", Path: "marker.txt"})
|
||||
r, _ := controlwire.NewRequest("POST", a.server.URL, "/v1/files", 2, strings.Repeat("e", 32), body)
|
||||
response, err := a.client.Do(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
r, _ = controlwire.NewRequest("POST", a.server.URL, "/v1/files", 2, strings.Repeat("e", 32), body)
|
||||
response, err = a.client.Do(r)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != 409 {
|
||||
t.Fatalf("replay accepted: %d", response.StatusCode)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user