140 lines
4.9 KiB
Go
140 lines
4.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"otche/internal/store"
|
|
)
|
|
|
|
func TestBindingExpiryBlocksReadinessAndAdmission(t *testing.T) {
|
|
url := os.Getenv("OTCHE_TEST_DATABASE_URL")
|
|
if url == "" {
|
|
t.Skip("OTCHE_TEST_DATABASE_URL required for isolated PostgreSQL regression")
|
|
}
|
|
ctx := context.Background()
|
|
admin, err := pgxpool.New(ctx, url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer admin.Close()
|
|
schema := "test_" + strings.ReplaceAll(store.NewID(), "-", "")
|
|
if _, err = admin.Exec(ctx, "CREATE SCHEMA "+schema); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer admin.Exec(ctx, "DROP SCHEMA "+schema+" CASCADE")
|
|
cfg, err := pgxpool.ParseConfig(url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg.ConnConfig.RuntimeParams["search_path"] = schema
|
|
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
if err = store.Migrate(ctx, db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
owner, err := CreateUser(ctx, db, "expiry-admin", "Regression-only-long-password", "admin")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(ctx, `INSERT INTO bindings(owner_id,pool,iso_storage,disk_storage,node,configured,reason,isolation_expires_at)VALUES($1,'pool','iso','disks','node',true,'',now()+interval '1 hour');`, owner); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(ctx, `INSERT INTO heartbeats(name,seen_at,ready,blockers)VALUES('worker',now(),true,'[]')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
upload, profile, revision := store.NewID(), store.NewID(), store.NewID()
|
|
if _, err = db.Exec(ctx, `INSERT INTO uploads(id,owner_id,filename,size,sha256,storage_key)VALUES($1,$2,'benign.exe',1,$3,$4)`, upload, owner, strings.Repeat("a", 64), upload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(ctx, `INSERT INTO profiles(id,name,os,architecture,enabled,qualification,state,current_revision_id)VALUES($1,'isolated expiry fixture','Windows','x64',true,'qualified','published',$2)`, profile, revision); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = db.Exec(ctx, `INSERT INTO revisions(id,profile_id,source_ref,fingerprint,config_digest)VALUES($1,$2,'isolated-test','test','test')`, revision, profile); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s, err := New(db, Config{Origin: "http://localhost", ArtifactRoot: t.TempDir(), MaxQueuedJobs: 20})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
login := httptest.NewRequest("POST", "/api/v1/auth/login", strings.NewReader(`{"username":"expiry-admin","password":"Regression-only-long-password"}`))
|
|
login.Header.Set("Origin", "http://localhost")
|
|
logged := httptest.NewRecorder()
|
|
s.ServeHTTP(logged, login)
|
|
if logged.Code != 200 {
|
|
t.Fatalf("login: %d %s", logged.Code, logged.Body)
|
|
}
|
|
cookie := logged.Result().Cookies()[0]
|
|
var session struct {
|
|
CSRF string `json:"csrf_token"`
|
|
}
|
|
if err = json.Unmarshal(logged.Body.Bytes(), &session); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, state := range []struct {
|
|
expiry string
|
|
want bool
|
|
}{{"now()+interval '1 hour'", true}, {"now()-interval '1 second'", false}, {"NULL", false}} {
|
|
if _, err = db.Exec(ctx, "UPDATE bindings SET configured=true,isolation_expires_at="+state.expiry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, endpoint := range []string{"/api/v1/dashboard", "/api/v1/admin/bindings"} {
|
|
r := httptest.NewRequest("GET", endpoint, nil)
|
|
r.AddCookie(cookie)
|
|
w := httptest.NewRecorder()
|
|
s.ServeHTTP(w, r)
|
|
if w.Code != 200 {
|
|
t.Fatalf("%s: %d %s", endpoint, w.Code, w.Body)
|
|
}
|
|
var body map[string]any
|
|
if err = json.Unmarshal(w.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got bool
|
|
if endpoint == "/api/v1/dashboard" {
|
|
got = body["integration"].(map[string]any)["ready"].(bool)
|
|
} else {
|
|
got = body["items"].([]any)[0].(map[string]any)["configured"].(bool)
|
|
}
|
|
if got != state.want {
|
|
t.Fatalf("%s readiness=%v for expiry %s", endpoint, got, state.expiry)
|
|
}
|
|
}
|
|
requestBody, err := json.Marshal(map[string]any{"upload_id": upload, "profile_ids": []string{profile}, "settings": map[string]any{"internet": "offline", "duration_seconds": 30, "filename": "original", "privilege": "user", "args_mode": "none", "args": []string{}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := httptest.NewRequest("POST", "/api/v1/jobs", strings.NewReader(string(requestBody)))
|
|
r.AddCookie(cookie)
|
|
r.Header.Set("Origin", "http://localhost")
|
|
r.Header.Set("X-CSRF-Token", session.CSRF)
|
|
w := httptest.NewRecorder()
|
|
s.ServeHTTP(w, r)
|
|
if state.want {
|
|
if w.Code != 201 {
|
|
t.Fatalf("valid isolation proof did not admit owned job: %d %s", w.Code, w.Body)
|
|
}
|
|
} else {
|
|
var rejected struct {
|
|
Error struct {
|
|
Code string `json:"code"`
|
|
} `json:"error"`
|
|
}
|
|
if err = json.Unmarshal(w.Body.Bytes(), &rejected); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w.Code != 409 || rejected.Error.Code != "admission_closed" {
|
|
t.Fatalf("expired/null proof did not close public admission: %d %s", w.Code, w.Body)
|
|
}
|
|
}
|
|
}
|
|
}
|