249 lines
9.4 KiB
Go
249 lines
9.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.ipao.vip/rogee/creator-hub/internal/hub"
|
|
"git.ipao.vip/rogee/creator-hub/internal/phasea"
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func TestExecuteContextStopsOnSIGTERM(t *testing.T) {
|
|
if os.Getenv("CREATORHUB_SIGTERM_HELPER") == "1" {
|
|
command := &cobra.Command{Use: "shutdown-test", RunE: func(command *cobra.Command, _ []string) error {
|
|
_, _ = os.Stdout.WriteString("ready\n")
|
|
<-command.Context().Done()
|
|
_, _ = os.Stdout.WriteString("stopped\n")
|
|
return nil
|
|
}}
|
|
if err := execute(command); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
process := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestExecuteContextStopsOnSIGTERM$")
|
|
process.Env = append(os.Environ(), "CREATORHUB_SIGTERM_HELPER=1")
|
|
stdout, err := process.StdoutPipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := process.Start(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
if !scanner.Scan() || scanner.Text() != "ready" {
|
|
t.Fatalf("helper did not become ready: %q err=%v", scanner.Text(), scanner.Err())
|
|
}
|
|
if err := process.Process.Signal(syscall.SIGTERM); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !scanner.Scan() || scanner.Text() != "stopped" {
|
|
t.Fatalf("helper did not finish cleanup: %q err=%v", scanner.Text(), scanner.Err())
|
|
}
|
|
if err := process.Wait(); err != nil || ctx.Err() != nil {
|
|
t.Fatalf("process did not exit gracefully after SIGTERM: wait=%v context=%v", err, ctx.Err())
|
|
}
|
|
}
|
|
|
|
func TestLoadConfigRejectsInvalidDatabase(t *testing.T) {
|
|
t.Setenv("DATABASE_URL", "file:///tmp/creatorhub.db")
|
|
if _, err := loadConfig(); err == nil {
|
|
t.Fatal("expected non-Postgres database URL to be rejected")
|
|
}
|
|
}
|
|
|
|
func TestPhaseAAccountRequestRejectsSecretsAndUnknownFields(t *testing.T) {
|
|
for name, body := range map[string]string{
|
|
"raw password": `{"id":"account-a","platform":"mock","platform_account_key":"a","authorization_kind":"owned","credential_reference":{"id":"credential-a","provider":"os_keyring","key":"creatorhub/account-a"},"password":"secret"}`,
|
|
"nested secret": `{"id":"account-a","platform":"mock","platform_account_key":"a","authorization_kind":"owned","credential_reference":{"id":"credential-a","provider":"os_keyring","key":"creatorhub/account-a","secret":"secret"}}`,
|
|
"unknown field": `{"id":"account-a","platform":"mock","platform_account_key":"a","authorization_kind":"owned","credential_reference":{"id":"credential-a","provider":"os_keyring","key":"creatorhub/account-a"},"extra":true}`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
app := fiber.New()
|
|
app.Post("/", func(c fiber.Ctx) error {
|
|
var input accountRequest
|
|
if err := decodePhaseA(c, &input); err != nil {
|
|
return phaseAError(c, err)
|
|
}
|
|
return c.SendStatus(http.StatusNoContent)
|
|
})
|
|
response, err := app.Test(httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusBadRequest {
|
|
t.Fatalf("expected sensitive or unknown field to be rejected, got %d", response.StatusCode)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPhaseAAccountHTTPWorkflowRedactsSecrets(t *testing.T) {
|
|
databaseURL := os.Getenv("CREATORHUB_POSTGRES_TEST_URL")
|
|
if databaseURL == "" {
|
|
t.Skip("set CREATORHUB_POSTGRES_TEST_URL to run PostgreSQL integration coverage")
|
|
}
|
|
ctx := context.Background()
|
|
databaseURL = isolatedControlPlaneDatabaseURL(t, databaseURL)
|
|
store, err := phasea.Open(ctx, databaseURL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = store.Close() })
|
|
hubStore, err := hub.Open(ctx, databaseURL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := hubStore.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
app := fiber.New()
|
|
registerPhaseA(app, store, nil)
|
|
credentialKey := "creatorhub/phase-http-secret"
|
|
accountBody := `{"id":"account-http","platform":"mock","platform_account_key":"phase-http","authorization_kind":"owned","credential_reference":{"id":"credential-http","provider":"os_keyring","key":"` + credentialKey + `"}}`
|
|
request := func(method, path, body string, wantStatus int) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
response := do(app, method, path, body)
|
|
if response.Code != wantStatus {
|
|
t.Fatalf("%s %s returned %d, want %d: %s", method, path, response.Code, wantStatus, response.Body.String())
|
|
}
|
|
if strings.Contains(response.Body.String(), credentialKey) {
|
|
t.Fatalf("%s %s leaked credential key: %s", method, path, response.Body.String())
|
|
}
|
|
return response
|
|
}
|
|
|
|
created := request(http.MethodPost, "/api/phase-a/accounts", accountBody, http.StatusCreated)
|
|
var account phasea.Account
|
|
if err := json.Unmarshal(created.Body.Bytes(), &account); err != nil || account.ID != "account-http" || account.RuntimeStatus != "paused" {
|
|
t.Fatalf("unexpected account response: %#v err=%v", account, err)
|
|
}
|
|
request(http.MethodPost, "/api/phase-a/accounts", accountBody, http.StatusConflict)
|
|
request(http.MethodPost, "/api/phase-a/accounts", strings.TrimSuffix(accountBody, "}")+`,"password":"`+credentialKey+`"}`, http.StatusBadRequest)
|
|
request(http.MethodGet, "/api/phase-a/accounts", "", http.StatusOK)
|
|
request(http.MethodGet, "/api/phase-a/accounts/account-http", "", http.StatusOK)
|
|
|
|
db, err := sql.Open("pgx", databaseURL)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
if _, err := db.ExecContext(ctx, `
|
|
INSERT INTO gateway (name, endpoint, token) VALUES ('phase-http', 'http://127.0.0.1:8081', 'phase-http-gateway-token');
|
|
INSERT INTO browser_image (version, image_ref) VALUES ('1', 'example/browser:1');
|
|
INSERT INTO browser_env (alias, name, gateway_name, image_version, fingerprint)
|
|
VALUES ('account-http', 'Phase HTTP', 'phase-http', '1', '{"seed":1}');
|
|
INSERT INTO network_exit (id, protocol, host, port, health_status)
|
|
VALUES ('exit-http', 'socks5', '127.0.0.1', 1080, 'healthy');
|
|
INSERT INTO environment_binding (id, account_id, browser_env_alias, network_exit_id)
|
|
VALUES ('binding-http', 'account-http', 'account-http', 'exit-http')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request(http.MethodPost, "/api/phase-a/accounts/account-http/resume", "", http.StatusNoContent)
|
|
request(http.MethodPost, "/api/phase-a/accounts/account-http/pause", "", http.StatusNoContent)
|
|
request(http.MethodPost, "/api/phase-a/accounts/account-http/resume", "", http.StatusNoContent)
|
|
request(http.MethodPost, "/api/phase-a/accounts/account-http/revoke", "", http.StatusNoContent)
|
|
blocked := request(http.MethodPost, "/api/phase-a/accounts/account-http/resume", "", http.StatusConflict)
|
|
if !strings.Contains(blocked.Body.String(), `"reason_code":"account_revoked"`) {
|
|
t.Fatalf("revoked resume did not return a stable conflict reason: %s", blocked.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPhaseAErrorRedactsInternalDetails(t *testing.T) {
|
|
app := fiber.New()
|
|
app.Get("/", func(c fiber.Ctx) error {
|
|
return phaseAError(c, errors.New("database exposed a secret value"))
|
|
})
|
|
response, err := app.Test(httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer response.Body.Close()
|
|
body, _ := io.ReadAll(response.Body)
|
|
if response.StatusCode != http.StatusInternalServerError || strings.Contains(string(body), "secret") || !strings.Contains(string(body), "phase A operation failed") {
|
|
t.Fatalf("internal error was not redacted: status=%d body=%s", response.StatusCode, body)
|
|
}
|
|
}
|
|
|
|
func TestSPAServesFileAndIndexFallback(t *testing.T) {
|
|
directory := t.TempDir()
|
|
files := map[string]string{
|
|
"index.html": "index",
|
|
"app.js": "asset",
|
|
"hello world.js": "space",
|
|
"应用.js": "unicode",
|
|
}
|
|
for name, body := range files {
|
|
if err := os.WriteFile(filepath.Join(directory, name), []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(directory, "..", "outside-secret.txt"), []byte("secret"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
app := newHandler(directory)
|
|
for path, want := range map[string]string{
|
|
"/app.js": "asset",
|
|
"/hello%20world.js": "space",
|
|
"/%E5%BA%94%E7%94%A8.js": "unicode",
|
|
"/%2e%2e%2foutside-secret.txt": "index",
|
|
"/dashboard": "index",
|
|
} {
|
|
response, err := app.Test(httptest.NewRequest(http.MethodGet, path, nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, readErr := io.ReadAll(response.Body)
|
|
response.Body.Close()
|
|
if readErr != nil || response.StatusCode != http.StatusOK || string(body) != want {
|
|
t.Fatalf("GET %s: status=%d body=%q err=%v", path, response.StatusCode, body, readErr)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestStartupLogIncludesListenAddress(t *testing.T) {
|
|
logger := logrus.StandardLogger()
|
|
previousOutput, previousFormatter, previousLevel := logger.Out, logger.Formatter, logger.Level
|
|
t.Cleanup(func() {
|
|
logrus.SetOutput(previousOutput)
|
|
logrus.SetFormatter(previousFormatter)
|
|
logrus.SetLevel(previousLevel)
|
|
})
|
|
var output bytes.Buffer
|
|
logrus.SetOutput(&output)
|
|
logrus.SetFormatter(&logrus.JSONFormatter{})
|
|
logrus.SetLevel(logrus.InfoLevel)
|
|
|
|
logStartup(config{listenAddr: ":8080"})
|
|
var entry map[string]any
|
|
if err := json.Unmarshal(output.Bytes(), &entry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if entry["listen_addr"] != ":8080" || entry["service"] != "control-plane" {
|
|
t.Fatalf("unexpected startup log: %#v", entry)
|
|
}
|
|
}
|