mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Lock waits: - platform.LockFile and LockFileShared take a platform.Waiting, called once they find the lock held, never when it's free, whose end they call once the wait ends; LockFileWaiting, LockFileSharedWaiting and their delays (ShareNote, OCINote, WaitNote, groupsNote, makingNote) go. - msg.Waiting makes one from a Reporter's Status and a label; msg.WithStatus and msg.StatusOf carry the run's Status in its context for the two packages that take no Reporter, internal/state and internal/trust, which cli's execute sets; forbidigo keeps StatusOf to them, and AGENTS.md says so. - Every lock that waits shows its own label: vm_state_waiting (a VM's state, its deletion), claims_waiting, layout_waiting, git_lock_waiting, ca_waiting, packages_waiting, rebuilds_waiting, ubuntu_pin_waiting, custom_secrets_waiting, forwarder_waiting, agent_store_waiting, and the waits that printed a line after a delay, which now show theirs as a status line: share_waiting, oci_waiting, image_build_waiting, group_waiting, making_waiting. - An agent's login file's lock (agentlogin's lockLogin, a folder's flock that a VM's proxy holds while it refreshes the login) tries first without waiting and shows agentlogin.File's Waiting (agent_login_waiting) while it waits; secrets fills it from its Reporter, for SaveLogins, Claude Code's logins folder and every File. - The structure test's status-lines rule counts a LockFile or LockFileShared whose waiting is nil as slow work; the build port's record, a file just made that never waits, is listed in statusSites. - Without a terminal, or with -v, a status line draws nothing, so the waits that printed an info line after a delay no longer print it there; the CHANGELOG and the troubleshooting page say so. from.txtar's check that a free conversion lock printed no wait goes, as TestLockOCI checks it. The layout's migration runs under a status line (layout_migrating), only when there are migrations to run. Backend calls outside a launch: TestCommandStatusLines runs devvm ls, ports, size, network, tools, git-protect, history export, stop and rm on a running and a stopped VM and fails on a backend call with no status line showing, with a control that each makes one. A project folder devvm can't open and read (vm_folder_unreadable) was taken for another folder's VM (vm_other_folder, which offers rm and adopt, and adopt went on); the finder now keeps why (target.unread), and every command on the folder's VM, from the folder or a subfolder of it, names the folder and why; devvm rm VM run elsewhere goes by the VM's name as before. platform.UniqueFolderID had no caller but its test: FolderStat.Unique, from the open folder, replaced it; it goes, and its test checks StatFolder's Unique instead. Tests: TestLockFileWaiting (rewritten: called only when held, its end once got or given up, shared too), TestVMLockWaits, TestCheckLayoutWaits, TestCheckLayout (migrations under layout_migrating, nothing shown otherwise), TestLockGroups, TestLockShare, TestMaking, TestLockOCI and the build lock's test (status lines, not info lines), TestLockLogin, TestInitCACancelled (ca_waiting), the status-lines rule's lock cases, TestCommandStatusLines, TestUnreadableFolder (a subfolder of it too), TestStatFolderUnique. Docs: Contributing's CLI page (Status lines) and structure page, the cli and codebase-standards skills (the guards), AGENTS.md's Go conventions, internals/images.md and design.md (the waits), using/troubleshooting.md, using/vms.md (a folder devvm can't read); CHANGELOG. Co-Authored-By: Claude Opus 5.5 <[email protected]>
178 lines
6.2 KiB
Go
178 lines
6.2 KiB
Go
package trust
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"errors"
|
|
"math/big"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/paths"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
)
|
|
|
|
// Each VM has a CA of its own, whatever its tier, which its proxy
|
|
// intercepts with and which only that VM trusts, so a key that leaks
|
|
// reaches one VM. A CA lasts caLife, so a leaked key stops working within
|
|
// weeks, and has a successor, the next CA, which every launch installs in
|
|
// the VM beside it. The next CA starts caRenew and caSkew before the
|
|
// current one ends; the proxy signs with it from caSkew after its start
|
|
// (the hour lets a VM whose clock is behind see it started), and the
|
|
// launch after that point makes it the current one and makes another
|
|
// next. A VM that isn't opened again so keeps its proxy's certificates
|
|
// verifying until the next CA ends, at least caLife less caSkew after its
|
|
// last launch.
|
|
const (
|
|
caLife = 30 * 24 * time.Hour
|
|
caRenew = 7 * 24 * time.Hour
|
|
caSkew = time.Hour
|
|
)
|
|
|
|
// InitCA readies the VM's CA in ca (paths.VM.CA) at each launch: its
|
|
// current CA, ca.pem and ca.key, and its next one, next.pem and next.key.
|
|
// A current CA with over caRenew left stays. One with less, or none, is
|
|
// replaced by the next CA when that has started and has over caRenew
|
|
// left, and by a new one valid from now otherwise. A next CA that doesn't
|
|
// follow the current one (ending after it, starting by the time it's
|
|
// replaced) is made again. Certificates are mode 644 and keys, ECDSA ("EC
|
|
// PRIVATE KEY"), mode 600, each written whole, the key first. A CA valid
|
|
// for longer than caLife is replaced too. No CA names a host. It holds the
|
|
// folder's lock (paths.CA.Lock), waiting for another launch's until ctx
|
|
// ends, so two launches at once never leave a key and a certificate of
|
|
// different runs.
|
|
func InitCA(ctx context.Context, ca paths.CA) error {
|
|
if err := state.PrivateDir(ca.Dir); err != nil {
|
|
return err
|
|
}
|
|
unlock, err := platform.LockFile(ctx, ca.Lock(), msg.Waiting(msg.StatusOf(ctx), msg.CAWaiting()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer unlock()
|
|
return initCA(ca)
|
|
}
|
|
|
|
// initCA is InitCA, holding the lock.
|
|
func initCA(ca paths.CA) error {
|
|
now := time.Now()
|
|
cur, err := readPair(ca.Cert(), ca.Key())
|
|
if err != nil || !cur.current(now) {
|
|
next, nerr := readPair(ca.NextCert(), ca.NextKey())
|
|
if nerr == nil && next.current(now) && !next.cert.NotBefore.After(now) {
|
|
err = next.write(ca.Cert(), ca.Key())
|
|
} else {
|
|
next, err = makeAt(ca.Cert(), ca.Key(), now.Add(-caSkew))
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cur = next
|
|
}
|
|
if next, err := readPair(ca.NextCert(), ca.NextKey()); err == nil && next.follows(cur) {
|
|
return nil
|
|
}
|
|
_, err = makeAt(ca.NextCert(), ca.NextKey(), cur.cert.NotAfter.Add(-caRenew-caSkew))
|
|
return err
|
|
}
|
|
|
|
// makeAt makes a CA valid from notBefore (makePair) and writes it to
|
|
// certPath and keyPath.
|
|
func makeAt(certPath, keyPath string, notBefore time.Time) (pair, error) {
|
|
p, err := makePair(notBefore)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
return p, p.write(certPath, keyPath)
|
|
}
|
|
|
|
// pair is a CA as its files hold it: its certificate, parsed, and both
|
|
// files' PEM.
|
|
type pair struct {
|
|
cert *x509.Certificate
|
|
certPEM []byte
|
|
keyPEM []byte
|
|
}
|
|
|
|
// readPair reads the CA in certPath and keyPath: a certificate and its
|
|
// key, which must match.
|
|
func readPair(certPath, keyPath string) (pair, error) {
|
|
certPEM, err := os.ReadFile(certPath) //nolint:gosec // the VM's CA folder (paths.VM.CA)
|
|
if err != nil {
|
|
return pair{}, err
|
|
}
|
|
keyPEM, err := os.ReadFile(keyPath) //nolint:gosec // the VM's CA folder (paths.VM.CA)
|
|
if err != nil {
|
|
return pair{}, err
|
|
}
|
|
return pairOf(certPEM, keyPEM)
|
|
}
|
|
|
|
// pairOf is the CA in certPEM and keyPEM, which must match.
|
|
func pairOf(certPEM, keyPEM []byte) (pair, error) {
|
|
tp, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
if err != nil {
|
|
return pair{}, err
|
|
}
|
|
return pair{cert: tp.Leaf, certPEM: certPEM, keyPEM: keyPEM}, nil
|
|
}
|
|
|
|
// current: the CA has over caRenew left at now, and a life of at most
|
|
// caLife (and caSkew, which a new one starts early by).
|
|
func (p pair) current(now time.Time) bool {
|
|
c := p.cert
|
|
return c.NotAfter.Sub(now) > caRenew && c.NotAfter.Sub(c.NotBefore) <= caLife+caSkew
|
|
}
|
|
|
|
// follows: the CA can be cur's next one, ending after it and starting by
|
|
// the time cur is replaced, with a life of at most caLife.
|
|
func (p pair) follows(cur pair) bool {
|
|
c := p.cert
|
|
return c.NotAfter.After(cur.cert.NotAfter) && !c.NotBefore.After(cur.cert.NotAfter.Add(-caRenew)) &&
|
|
c.NotAfter.Sub(c.NotBefore) <= caLife+caSkew
|
|
}
|
|
|
|
// makePair makes a CA valid from notBefore for caLife and caSkew, with a
|
|
// new ECDSA P-256 key.
|
|
func makePair(notBefore time.Time) (pair, error) {
|
|
// crypto/rand never fails: a system whose randomness does crashes Go.
|
|
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
serial, _ := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
|
// A name of its own, its serial's, so a VM's two CAs never share a subject, which some
|
|
// verifiers look issuers up by.
|
|
tmpl := &x509.Certificate{
|
|
SerialNumber: serial,
|
|
Subject: pkix.Name{CommonName: "devvm proxy CA " + serial.Text(16), Organization: []string{"devvm"}},
|
|
NotBefore: notBefore,
|
|
NotAfter: notBefore.Add(caLife + caSkew),
|
|
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
|
BasicConstraintsValid: true,
|
|
IsCA: true,
|
|
MaxPathLenZero: true,
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
|
|
kder, kerr := x509.MarshalECPrivateKey(key)
|
|
if err := errors.Join(err, kerr); err != nil {
|
|
return pair{}, err
|
|
}
|
|
return pairOf(pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}), pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: kder}))
|
|
}
|
|
|
|
// write writes the CA to certPath and keyPath, each whole. The key goes
|
|
// first: a crash between the two leaves a pair that doesn't match, which
|
|
// the next InitCA replaces.
|
|
func (p pair) write(certPath, keyPath string) error {
|
|
if err := state.WriteFileMode(keyPath, p.keyPEM, 0o600); err != nil {
|
|
return err
|
|
}
|
|
return state.WriteFileMode(certPath, p.certPEM, 0o644)
|
|
}
|