//go:build hostedlive package hostedclient // Live drivers for Task 17. They talk to a running equipo-hosted-controller // through this package's client, so they launch and stop real VMs. They run // only by hand, with -tags hostedlive and the environment below; without // EQUIPO_HOSTED_LIVE_URL they skip. The logic is liveDriver // (driver_test.go), which the offline tests also run. // // EQUIPO_HOSTED_LIVE_URL https origin of the controller, such as https://127.0.0.1:8443 // EQUIPO_HOSTED_LIVE_CA_FILE PEM certificate the controller's TLS certificate chains to // EQUIPO_HOSTED_LIVE_KEY_ID the instance key's ID in the controller's verify_keys // EQUIPO_HOSTED_LIVE_KEY_FILE the instance key: a 32-byte ed25519 seed in standard base64 // EQUIPO_HOSTED_LIVE_CONTROLLER_KEY_FILE the controller's public key: 32 bytes in standard base64 // EQUIPO_HOSTED_LIVE_IMAGE the image digest to launch (sha256:...), in the allowlist // // TestLiveCrashRecovery also reads EQUIPO_HOSTED_LIVE_CRASH_STEP, // EQUIPO_HOSTED_LIVE_ATTEMPT and, for its recover step, // EQUIPO_HOSTED_LIVE_OPERATOR_KEY_ID and EQUIPO_HOSTED_LIVE_OPERATOR_KEY_FILE. import ( "context" "crypto/ed25519" "crypto/rand" "crypto/tls" "crypto/x509" "encoding/base64" "encoding/hex" "fmt" "net/http" "os" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/hostedwire" "amadan.net/oficina/equipo/internal/isolatedjob" ) const ( liveURLEnv = "EQUIPO_HOSTED_LIVE_URL" liveCAFileEnv = "EQUIPO_HOSTED_LIVE_CA_FILE" liveKeyIDEnv = "EQUIPO_HOSTED_LIVE_KEY_ID" liveKeyFileEnv = "EQUIPO_HOSTED_LIVE_KEY_FILE" liveCtlKeyFileEnv = "EQUIPO_HOSTED_LIVE_CONTROLLER_KEY_FILE" liveImageEnv = "EQUIPO_HOSTED_LIVE_IMAGE" liveCrashStepEnv = "EQUIPO_HOSTED_LIVE_CRASH_STEP" liveAttemptEnv = "EQUIPO_HOSTED_LIVE_ATTEMPT" liveOpKeyIDEnv = "EQUIPO_HOSTED_LIVE_OPERATOR_KEY_ID" liveOpKeyFileEnv = "EQUIPO_HOSTED_LIVE_OPERATOR_KEY_FILE" liveCallTimeout = 2 * time.Minute // a launch waits on the provider ) // newLiveDriver builds the driver for the controller the environment names. func newLiveDriver(t *testing.T) *liveDriver { t.Helper() origin := os.Getenv(liveURLEnv) if origin == "" { t.Skip(liveURLEnv + " is not set") } pem, err := os.ReadFile(liveNeed(t, liveCAFileEnv)) if err != nil { t.Fatalf("CA file: %v", err) } pool := x509.NewCertPool() if !pool.AppendCertsFromPEM(pem) { t.Fatalf("%s holds no PEM certificate", liveCAFileEnv) } rt := &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}} ctl, err := readLiveB64(liveNeed(t, liveCtlKeyFileEnv), ed25519.PublicKeySize) if err != nil { t.Fatal(err) } seed, err := readLiveB64(liveNeed(t, liveKeyFileEnv), ed25519.SeedSize) if err != nil { t.Fatal(err) } d := &liveDriver{ origin: strings.TrimSuffix(origin, "/"), rt: rt, ctl: ed25519.PublicKey(ctl), image: isolatedjob.ImageDigest(liveNeed(t, liveImageEnv)), lifetime: time.Hour, poll: 5 * time.Second, stateBound: 5 * time.Minute, cleanupBound: 5 * time.Minute, callTimeout: liveCallTimeout, logf: t.Logf, errorf: t.Errorf, deferFn: t.Cleanup, } t.Setenv(FlagEnv, "1") d.cl, err = New(Config{BaseURL: origin, KeyID: hostedwire.KeyID(liveNeed(t, liveKeyIDEnv)), Key: ed25519.NewKeyFromSeed(seed), ControllerKey: d.ctl, HTTP: &http.Client{Timeout: liveCallTimeout, Transport: rt}}) if err != nil { t.Fatalf("hostedclient.New: %v", err) } return d } func liveNeed(t *testing.T, name string) string { t.Helper() v := os.Getenv(name) if v == "" { t.Fatalf("%s is not set", name) } return v } // readLiveB64 reads size bytes in standard base64: an ed25519 seed (the // format of the controller's signing_key_file) or public key (a // verify_keys public_key). func readLiveB64(path string, size int) ([]byte, error) { raw, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("key file: %w", err) } b, err := base64.StdEncoding.Strict().DecodeString(strings.TrimSpace(string(raw))) if err != nil || len(b) != size { return nil, fmt.Errorf("key file %s must hold %d bytes in standard base64", path, size) } return b, nil } // TestLiveControllerSequence drives one Small VM through the controller // (liveDriver.sequence) and logs each timing and the epoch. func TestLiveControllerSequence(t *testing.T) { d := newLiveDriver(t) attempt := os.Getenv(liveAttemptEnv) if attempt == "" { b := make([]byte, 6) if _, err := rand.Read(b); err != nil { t.Fatal(err) } attempt = "task17-" + hex.EncodeToString(b) } if err := d.sequence(context.Background(), attempt); err != nil { t.Fatal(err) } } // TestLiveCrashRecovery is the "kill the controller between the provider // call and transaction 2" check, in two steps against two controller runs // that share one database: // // - EQUIPO_HOSTED_LIVE_CRASH_STEP=launch, against a controller built with // -tags hostedlive and run with EQUIPO_HOSTED_CRASH_AFTER_LAUNCH=1: the // launch reaches the provider, the controller exits before saving the // Ref, and the call fails in transport. Any answer instead fails the // test and the VM is stopped. // - EQUIPO_HOSTED_LIVE_CRASH_STEP=recover, after restarting the controller // without EQUIPO_HOSTED_CRASH_AFTER_LAUNCH: the reaper's replay claims the // VM, the operator's VM list shows exactly one VM for the attempt, and // the VM is stopped with proof. A failure, a bad operator key included, // still stops it. // // Both steps need the same EQUIPO_HOSTED_LIVE_ATTEMPT. func TestLiveCrashRecovery(t *testing.T) { d := newLiveDriver(t) attempt := liveNeed(t, liveAttemptEnv) var err error switch step := liveNeed(t, liveCrashStepEnv); step { case "launch": err = d.crashLaunch(context.Background(), attempt) case "recover": err = d.crashRecover(context.Background(), attempt, func() (hostedwire.KeyID, ed25519.PrivateKey, error) { id := os.Getenv(liveOpKeyIDEnv) if id == "" { return "", nil, fmt.Errorf("%s is not set", liveOpKeyIDEnv) } path := os.Getenv(liveOpKeyFileEnv) if path == "" { return "", nil, fmt.Errorf("%s is not set", liveOpKeyFileEnv) } seed, err := readLiveB64(path, ed25519.SeedSize) if err != nil { return "", nil, err } return hostedwire.KeyID(id), ed25519.NewKeyFromSeed(seed), nil }) default: t.Fatalf("%s = %q, want launch or recover", liveCrashStepEnv, step) } if err != nil { t.Fatal(err) } }