| 1 | package stagingtest |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/rand" |
| 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "net/http" |
| 11 | "net/url" |
| 12 | "os" |
| 13 | "slices" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 19 | "amadan.net/oficina/equipo/internal/wire" |
| 20 | ) |
| 21 | |
| 22 | // TestStagingSetup is run by scripts/staging-thin.py with the owner's |
| 23 | // staging session cookie. It approves the staging runner's enrolment code |
| 24 | // and signs a device in for the suite, writing the bearer to a 0600 file. |
| 25 | // The cookie and bearer are never printed. |
| 26 | func TestStagingSetup(t *testing.T) { |
| 27 | origin, cookie := os.Getenv("EQUIPO_STAGING_ORIGIN"), os.Getenv("EQUIPO_STAGING_COOKIE") |
| 28 | if origin == "" || cookie == "" { |
| 29 | t.Skip("staging setup only") |
| 30 | } |
| 31 | call := func(method, path, body, ifMatch string, withCookie bool, out any) { |
| 32 | t.Helper() |
| 33 | r, _ := http.NewRequest(method, origin+path, strings.NewReader(body)) |
| 34 | r.Header.Set("Content-Type", "application/json") |
| 35 | r.Header.Set("Idempotency-Key", rand.Text()) |
| 36 | if withCookie { |
| 37 | r.Header.Set("Cookie", cookie) |
| 38 | r.Header.Set("Origin", origin) |
| 39 | r.Header.Set("Sec-Fetch-Site", "same-origin") |
| 40 | } |
| 41 | if ifMatch != "" { |
| 42 | r.Header.Set("If-Match", ifMatch) |
| 43 | } |
| 44 | res, err := (&http.Client{Timeout: 60 * time.Second}).Do(r) |
| 45 | if err != nil { |
| 46 | t.Fatalf("%s %s: %v", method, path, err) |
| 47 | } |
| 48 | defer res.Body.Close() |
| 49 | raw, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20)) |
| 50 | if res.StatusCode/100 != 2 { |
| 51 | t.Fatalf("%s %s: HTTP %d %s", method, path, res.StatusCode, raw) |
| 52 | } |
| 53 | if out != nil { |
| 54 | if err = json.Unmarshal(raw, out); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | var me wire.Me |
| 60 | call("GET", "/api/v1/me", "", "", true, &me) |
| 61 | roster := func() []personapi.RosterMember { |
| 62 | return Roster(func(path string, out *personapi.Page[personapi.RosterMember]) { call("GET", path, "", "", true, out) }) |
| 63 | } |
| 64 | // The suite runs as its own owner, so the owner's own conversation on |
| 65 | // staging stays clean. The owner's session adds it once. |
| 66 | if add := os.Getenv("EQUIPO_STAGING_ADD_OWNER"); add != "" { |
| 67 | if !slices.ContainsFunc(roster(), func(m personapi.RosterMember) bool { return m.Address == add }) { |
| 68 | call("PUT", "/api/v1/roster/"+url.PathEscape(add), `{"role":"owner","active":true}`, `"0"`, true, nil) |
| 69 | t.Log("suite owner added") |
| 70 | } |
| 71 | return |
| 72 | } |
| 73 | // Suite people the harness added are removed by the owner's session |
| 74 | // at the end of their run, or by the one-off cleanup of stale ones: |
| 75 | // a member made inactive has every runner it enrolled revoked. Only |
| 76 | // counts are logged, never an address. |
| 77 | if list := os.Getenv("EQUIPO_STAGING_REMOVE_SUITE"); list != "" { |
| 78 | members := roster() |
| 79 | removed, missing := 0, 0 |
| 80 | for _, address := range strings.Split(list, ",") { |
| 81 | if !SuitePerson(address) { |
| 82 | t.Fatal("refusing to remove someone who is not a suite person") |
| 83 | } |
| 84 | i := slices.IndexFunc(members, func(m personapi.RosterMember) bool { return m.Address == address }) |
| 85 | if i < 0 { |
| 86 | missing++ |
| 87 | continue |
| 88 | } |
| 89 | m := members[i] |
| 90 | call("PUT", "/api/v1/roster/"+url.PathEscape(address), fmt.Sprintf(`{"role":%q,"active":false}`, m.Role), fmt.Sprintf(`"%d"`, m.Revision), true, nil) |
| 91 | removed++ |
| 92 | } |
| 93 | t.Logf("suite people removed: %d; not on the roster: %d", removed, missing) |
| 94 | return |
| 95 | } |
| 96 | if code := os.Getenv("EQUIPO_STAGING_ENROL_CODE"); code != "" { |
| 97 | call("POST", "/api/v1/runners/enrol/approve", fmt.Sprintf(`{"typed_code":%q}`, code), "", true, nil) |
| 98 | t.Log("runner enrolment approved") |
| 99 | } |
| 100 | path := os.Getenv("EQUIPO_STAGING_TOKEN_FILE") |
| 101 | if path == "" { |
| 102 | return |
| 103 | } |
| 104 | pub, key, err := ed25519.GenerateKey(rand.Reader) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | var c personapi.PersonDeviceChallenge |
| 109 | call("POST", "/api/v1/auth/devices", fmt.Sprintf(`{"public_key":%q,"label":"Headless suite (staging)"}`, base64.RawURLEncoding.EncodeToString(pub)), "", false, &c) |
| 110 | call("POST", "/api/v1/auth/devices/"+c.ID+"/approve", fmt.Sprintf(`{"typed_code":%q}`, c.UserCode), `"1"`, true, nil) |
| 111 | p := personapi.PersonDeviceProof{ID: c.ID, DeviceSecret: c.DeviceSecret, Challenge: c.Challenge, Nonce: rand.Text()} |
| 112 | p.Signature = base64.RawURLEncoding.EncodeToString(ed25519.Sign(key, personapi.DeviceProofMessage(me.TeamID, p))) |
| 113 | raw, _ := json.Marshal(p) |
| 114 | var cred personapi.PersonCredential |
| 115 | call("POST", "/api/v1/auth/devices/exchange", string(raw), "", false, &cred) |
| 116 | if cred.Token == "" { |
| 117 | t.Fatal("no device bearer") |
| 118 | } |
| 119 | if err = os.WriteFile(path, []byte(cred.Token), 0600); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | t.Logf("device signed in for %s, expires %s", me.Address, cred.ExpiresAt.Format(time.RFC3339)) |
| 123 | } |
| 124 | |