| 1 | //go:build hostedlive |
| 2 | |
| 3 | package hostedclient |
| 4 | |
| 5 | // Live drivers for Task 17. They talk to a running equipo-hosted-controller |
| 6 | // through this package's client, so they launch and stop real VMs. They run |
| 7 | // only by hand, with -tags hostedlive and the environment below; without |
| 8 | // EQUIPO_HOSTED_LIVE_URL they skip. The logic is liveDriver |
| 9 | // (driver_test.go), which the offline tests also run. |
| 10 | // |
| 11 | // EQUIPO_HOSTED_LIVE_URL https origin of the controller, such as https://127.0.0.1:8443 |
| 12 | // EQUIPO_HOSTED_LIVE_CA_FILE PEM certificate the controller's TLS certificate chains to |
| 13 | // EQUIPO_HOSTED_LIVE_KEY_ID the instance key's ID in the controller's verify_keys |
| 14 | // EQUIPO_HOSTED_LIVE_KEY_FILE the instance key: a 32-byte ed25519 seed in standard base64 |
| 15 | // EQUIPO_HOSTED_LIVE_CONTROLLER_KEY_FILE the controller's public key: 32 bytes in standard base64 |
| 16 | // EQUIPO_HOSTED_LIVE_IMAGE the image digest to launch (sha256:...), in the allowlist |
| 17 | // |
| 18 | // TestLiveCrashRecovery also reads EQUIPO_HOSTED_LIVE_CRASH_STEP, |
| 19 | // EQUIPO_HOSTED_LIVE_ATTEMPT and, for its recover step, |
| 20 | // EQUIPO_HOSTED_LIVE_OPERATOR_KEY_ID and EQUIPO_HOSTED_LIVE_OPERATOR_KEY_FILE. |
| 21 | |
| 22 | import ( |
| 23 | "context" |
| 24 | "crypto/ed25519" |
| 25 | "crypto/rand" |
| 26 | "crypto/tls" |
| 27 | "crypto/x509" |
| 28 | "encoding/base64" |
| 29 | "encoding/hex" |
| 30 | "fmt" |
| 31 | "net/http" |
| 32 | "os" |
| 33 | "strings" |
| 34 | "testing" |
| 35 | "time" |
| 36 | |
| 37 | "amadan.net/oficina/equipo/internal/hostedwire" |
| 38 | "amadan.net/oficina/equipo/internal/isolatedjob" |
| 39 | ) |
| 40 | |
| 41 | const ( |
| 42 | liveURLEnv = "EQUIPO_HOSTED_LIVE_URL" |
| 43 | liveCAFileEnv = "EQUIPO_HOSTED_LIVE_CA_FILE" |
| 44 | liveKeyIDEnv = "EQUIPO_HOSTED_LIVE_KEY_ID" |
| 45 | liveKeyFileEnv = "EQUIPO_HOSTED_LIVE_KEY_FILE" |
| 46 | liveCtlKeyFileEnv = "EQUIPO_HOSTED_LIVE_CONTROLLER_KEY_FILE" |
| 47 | liveImageEnv = "EQUIPO_HOSTED_LIVE_IMAGE" |
| 48 | liveCrashStepEnv = "EQUIPO_HOSTED_LIVE_CRASH_STEP" |
| 49 | liveAttemptEnv = "EQUIPO_HOSTED_LIVE_ATTEMPT" |
| 50 | liveOpKeyIDEnv = "EQUIPO_HOSTED_LIVE_OPERATOR_KEY_ID" |
| 51 | liveOpKeyFileEnv = "EQUIPO_HOSTED_LIVE_OPERATOR_KEY_FILE" |
| 52 | liveCallTimeout = 2 * time.Minute // a launch waits on the provider |
| 53 | ) |
| 54 | |
| 55 | // newLiveDriver builds the driver for the controller the environment names. |
| 56 | func newLiveDriver(t *testing.T) *liveDriver { |
| 57 | t.Helper() |
| 58 | origin := os.Getenv(liveURLEnv) |
| 59 | if origin == "" { |
| 60 | t.Skip(liveURLEnv + " is not set") |
| 61 | } |
| 62 | pem, err := os.ReadFile(liveNeed(t, liveCAFileEnv)) |
| 63 | if err != nil { |
| 64 | t.Fatalf("CA file: %v", err) |
| 65 | } |
| 66 | pool := x509.NewCertPool() |
| 67 | if !pool.AppendCertsFromPEM(pem) { |
| 68 | t.Fatalf("%s holds no PEM certificate", liveCAFileEnv) |
| 69 | } |
| 70 | rt := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}} |
| 71 | ctl, err := readLiveB64(liveNeed(t, liveCtlKeyFileEnv), ed25519.PublicKeySize) |
| 72 | if err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | seed, err := readLiveB64(liveNeed(t, liveKeyFileEnv), ed25519.SeedSize) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | d := &liveDriver{ |
| 80 | origin: strings.TrimSuffix(origin, "/"), rt: rt, ctl: ed25519.PublicKey(ctl), |
| 81 | image: isolatedjob.ImageDigest(liveNeed(t, liveImageEnv)), |
| 82 | lifetime: time.Hour, poll: 5 * time.Second, stateBound: 5 * time.Minute, cleanupBound: 5 * time.Minute, |
| 83 | callTimeout: liveCallTimeout, |
| 84 | logf: t.Logf, errorf: t.Errorf, deferFn: t.Cleanup, |
| 85 | } |
| 86 | t.Setenv(FlagEnv, "1") |
| 87 | d.cl, err = New(Config{BaseURL: origin, KeyID: hostedwire.KeyID(liveNeed(t, liveKeyIDEnv)), |
| 88 | Key: ed25519.NewKeyFromSeed(seed), ControllerKey: d.ctl, |
| 89 | HTTP: &http.Client{Timeout: liveCallTimeout, Transport: rt}}) |
| 90 | if err != nil { |
| 91 | t.Fatalf("hostedclient.New: %v", err) |
| 92 | } |
| 93 | return d |
| 94 | } |
| 95 | |
| 96 | func liveNeed(t *testing.T, name string) string { |
| 97 | t.Helper() |
| 98 | v := os.Getenv(name) |
| 99 | if v == "" { |
| 100 | t.Fatalf("%s is not set", name) |
| 101 | } |
| 102 | return v |
| 103 | } |
| 104 | |
| 105 | // readLiveB64 reads size bytes in standard base64: an ed25519 seed (the |
| 106 | // format of the controller's signing_key_file) or public key (a |
| 107 | // verify_keys public_key). |
| 108 | func readLiveB64(path string, size int) ([]byte, error) { |
| 109 | raw, err := os.ReadFile(path) |
| 110 | if err != nil { |
| 111 | return nil, fmt.Errorf("key file: %w", err) |
| 112 | } |
| 113 | b, err := base64.StdEncoding.Strict().DecodeString(strings.TrimSpace(string(raw))) |
| 114 | if err != nil || len(b) != size { |
| 115 | return nil, fmt.Errorf("key file %s must hold %d bytes in standard base64", path, size) |
| 116 | } |
| 117 | return b, nil |
| 118 | } |
| 119 | |
| 120 | // TestLiveControllerSequence drives one Small VM through the controller |
| 121 | // (liveDriver.sequence) and logs each timing and the epoch. |
| 122 | func TestLiveControllerSequence(t *testing.T) { |
| 123 | d := newLiveDriver(t) |
| 124 | attempt := os.Getenv(liveAttemptEnv) |
| 125 | if attempt == "" { |
| 126 | b := make([]byte, 6) |
| 127 | if _, err := rand.Read(b); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | attempt = "task17-" + hex.EncodeToString(b) |
| 131 | } |
| 132 | if err := d.sequence(context.Background(), attempt); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // TestLiveCrashRecovery is the "kill the controller between the provider |
| 138 | // call and transaction 2" check, in two steps against two controller runs |
| 139 | // that share one database: |
| 140 | // |
| 141 | // - EQUIPO_HOSTED_LIVE_CRASH_STEP=launch, against a controller built with |
| 142 | // -tags hostedlive and run with EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1: the |
| 143 | // launch reaches the provider, the controller exits before saving the |
| 144 | // Ref, and the call fails in transport. Any answer instead fails the |
| 145 | // test and the VM is stopped. |
| 146 | // - EQUIPO_HOSTED_LIVE_CRASH_STEP=recover, after restarting the controller |
| 147 | // without EQUIPO_HOSTED_CRASH_AFTER_LAUNCH: the reaper's replay claims the |
| 148 | // VM, the operator's VM list shows exactly one VM for the attempt, and |
| 149 | // the VM is stopped with proof. A failure, a bad operator key included, |
| 150 | // still stops it. |
| 151 | // |
| 152 | // Both steps need the same EQUIPO_HOSTED_LIVE_ATTEMPT. |
| 153 | func TestLiveCrashRecovery(t *testing.T) { |
| 154 | d := newLiveDriver(t) |
| 155 | attempt := liveNeed(t, liveAttemptEnv) |
| 156 | var err error |
| 157 | switch step := liveNeed(t, liveCrashStepEnv); step { |
| 158 | case "launch": |
| 159 | err = d.crashLaunch(context.Background(), attempt) |
| 160 | case "recover": |
| 161 | err = d.crashRecover(context.Background(), attempt, func() (hostedwire.KeyID, ed25519.PrivateKey, error) { |
| 162 | id := os.Getenv(liveOpKeyIDEnv) |
| 163 | if id == "" { |
| 164 | return "", nil, fmt.Errorf("%s is not set", liveOpKeyIDEnv) |
| 165 | } |
| 166 | path := os.Getenv(liveOpKeyFileEnv) |
| 167 | if path == "" { |
| 168 | return "", nil, fmt.Errorf("%s is not set", liveOpKeyFileEnv) |
| 169 | } |
| 170 | seed, err := readLiveB64(path, ed25519.SeedSize) |
| 171 | if err != nil { |
| 172 | return "", nil, err |
| 173 | } |
| 174 | return hostedwire.KeyID(id), ed25519.NewKeyFromSeed(seed), nil |
| 175 | }) |
| 176 | default: |
| 177 | t.Fatalf("%s = %q, want launch or recover", liveCrashStepEnv, step) |
| 178 | } |
| 179 | if err != nil { |
| 180 | t.Fatal(err) |
| 181 | } |
| 182 | } |
| 183 | |