176 lines
5.0 KiB
Go
176 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"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")
|
|
}
|
|
}
|