255 lines
7.8 KiB
Go
255 lines
7.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestServerRequiresTokenAndAllowlistsCommands(t *testing.T) {
|
|
repo := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(repo, "jeannie"), []byte("#!/bin/sh\nexit 0\n"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &server{exec: make(map[string]*execution), repoRoot: repo, token: "secret"}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/v1/execute", s.auth(s.execute))
|
|
requestBody, _ := json.Marshal(executeRequest{Command: "nuke"})
|
|
body := bytes.NewBuffer(requestBody)
|
|
req, err := http.NewRequest(http.MethodPost, "/v1/execute", body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
response := httptest.NewRecorder()
|
|
mux.ServeHTTP(response, req)
|
|
if response.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status = %d", response.Code)
|
|
}
|
|
|
|
body = bytes.NewBuffer(requestBody)
|
|
req, err = http.NewRequest(http.MethodPost, "/v1/execute", body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer secret")
|
|
response = httptest.NewRecorder()
|
|
mux.ServeHTTP(response, req)
|
|
if response.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d", response.Code)
|
|
}
|
|
|
|
requestBody, _ = json.Marshal(executeRequest{Command: "remote"})
|
|
body = bytes.NewBuffer(requestBody)
|
|
req, err = http.NewRequest(http.MethodPost, "/v1/execute", body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer secret")
|
|
response = httptest.NewRecorder()
|
|
mux.ServeHTTP(response, req)
|
|
if response.Code != http.StatusForbidden {
|
|
t.Fatalf("status = %d", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestServerExecutesAllowlistedCommand(t *testing.T) {
|
|
repo := t.TempDir()
|
|
jeannie := filepath.Join(repo, "jeannie")
|
|
if err := os.WriteFile(jeannie, []byte("#!/bin/sh\nprintf 'remote-ok %s\\n' \"$1\"\n"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &server{exec: make(map[string]*execution), repoRoot: repo}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/v1/execute", s.auth(s.execute))
|
|
mux.HandleFunc("/v1/executions/", s.auth(s.execution))
|
|
payload, _ := json.Marshal(executeRequest{Command: "status"})
|
|
req, err := http.NewRequest(http.MethodPost, "/v1/execute", bytes.NewReader(payload))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
response := httptest.NewRecorder()
|
|
mux.ServeHTTP(response, req)
|
|
if response.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d", response.Code)
|
|
}
|
|
var queued execution
|
|
if err := json.NewDecoder(response.Body).Decode(&queued); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var completed execution
|
|
for deadline := time.Now().Add(2 * time.Second); time.Now().Before(deadline); {
|
|
req, err = http.NewRequest(http.MethodGet, "/v1/executions/"+queued.ID, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
response = httptest.NewRecorder()
|
|
mux.ServeHTTP(response, req)
|
|
if err := json.NewDecoder(response.Body).Decode(&completed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if completed.Status == "succeeded" {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
if completed.Status != "succeeded" || !strings.Contains(completed.Output, "remote-ok status") {
|
|
t.Fatalf("unexpected execution: %+v", completed)
|
|
}
|
|
}
|
|
|
|
func TestStorageCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["storage"] {
|
|
t.Fatal("storage must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestClusterBootstrapCommandsAreRemotelyEnabled(t *testing.T) {
|
|
for _, command := range []string{"cluster", "cluster-context", "stop-cluster", "cleanup-debian-k8s", "platform", "apps"} {
|
|
if !allowedCommands[command] {
|
|
t.Fatalf("%s must be exposed through the remote API", command)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMeshCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["mesh"] {
|
|
t.Fatal("mesh must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestGitopsCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["gitops"] {
|
|
t.Fatal("gitops must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestStateCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["state"] {
|
|
t.Fatal("state must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestSecretsCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["secrets"] {
|
|
t.Fatal("secrets must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestPimoxCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["pimox"] {
|
|
t.Fatal("pimox must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestEmailVMCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["email-vm"] {
|
|
t.Fatal("email-vm must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestShutdownCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["shutdown"] {
|
|
t.Fatal("shutdown must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestOCIFoundationCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["oci-foundation"] {
|
|
t.Fatal("oci-foundation must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestOCICommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["oci"] {
|
|
t.Fatal("oci must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestSearxngDeployCommandIsRemotelyEnabled(t *testing.T) {
|
|
if !allowedCommands["deploy-searxng"] {
|
|
t.Fatal("deploy-searxng must be exposed through the remote API")
|
|
}
|
|
if !allowedCommands["deploy-privatebin"] || !allowedCommands["deploy-miniflux"] {
|
|
t.Fatal("privacy services must be exposed through the remote API")
|
|
}
|
|
}
|
|
|
|
func TestPublicJeannieCommandsAreRemotelyEnabled(t *testing.T) {
|
|
data, err := os.ReadFile(filepath.Join("..", "..", "jeannie"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(data)
|
|
start := strings.LastIndex(text, "\ncase \"${1:-}\" in\n")
|
|
if start < 0 {
|
|
t.Fatal("public command dispatcher not found")
|
|
}
|
|
pattern := regexp.MustCompile(`(?m)^ ([a-z][a-z0-9-]*)\)`)
|
|
matches := pattern.FindAllStringSubmatch(text[start:], -1)
|
|
if len(matches) < 100 {
|
|
t.Fatalf("unexpectedly few public commands found: %d", len(matches))
|
|
}
|
|
for _, match := range matches {
|
|
if !allowedCommands[match[1]] {
|
|
t.Errorf("public command %q is missing from API allowlist", match[1])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRemoteGooseGraphRejectsInteractiveActions(t *testing.T) {
|
|
s := &server{exec: make(map[string]*execution), repoRoot: t.TempDir()}
|
|
for _, action := range []string{"learn", "propose", "--"} {
|
|
payload, _ := json.Marshal(executeRequest{Command: "goose-graph", Args: []string{action, "question"}})
|
|
request := httptest.NewRequest(http.MethodPost, "/v1/execute", bytes.NewReader(payload))
|
|
response := httptest.NewRecorder()
|
|
s.execute(response, request)
|
|
if response.Code != http.StatusBadRequest {
|
|
t.Errorf("action %q: got %d, want %d", action, response.Code, http.StatusBadRequest)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDoctorRetrievalLabArgumentsReachAPIWorker(t *testing.T) {
|
|
repo := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(repo, "jeannie"), []byte("#!/bin/sh\nprintf '%s\\n' \"$@\"\n"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &server{exec: make(map[string]*execution), repoRoot: repo}
|
|
payload, _ := json.Marshal(executeRequest{Command: "ai-lab", Args: []string{"doctor-retrieval-bench", "1"}})
|
|
recorder := httptest.NewRecorder()
|
|
s.execute(recorder, httptest.NewRequest(http.MethodPost, "/v1/execute", bytes.NewReader(payload)))
|
|
if recorder.Code != http.StatusAccepted {
|
|
t.Fatalf("status = %d: %s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var queued execution
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &queued); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for deadline := time.Now().Add(2 * time.Second); time.Now().Before(deadline); {
|
|
s.mu.Lock()
|
|
result := *s.exec[queued.ID]
|
|
s.mu.Unlock()
|
|
if result.Status == "succeeded" {
|
|
if !strings.Contains(result.Output, "ai-lab\ndoctor-retrieval-bench\n1") {
|
|
t.Fatalf("arguments not preserved: %q", result.Output)
|
|
}
|
|
return
|
|
}
|
|
if result.Status == "failed" {
|
|
t.Fatalf("execution failed: %+v", result)
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatal("lab API execution did not complete")
|
|
}
|