mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 21:36:40 -10:00
Any work that can take noticeable time now runs under the UI's status line: a Reporter's Status with a label message, or a LoggedCmd step. The UI: - status lines are a stack: a Status or LoggedCmd inside another shows its own line, then the outer one is drawn again, where before it ended the outer one for good - a message (Info, Warn, Note, Say, data on Stdout/Stderr) clears the line, prints and draws it again below; an unended line holds it - a question (Ask, Confirm, Answer, Secret), the picker and ui.Terminal hold it until they're done - no status line starts once the run is interrupted, as nothing would clear it - the run log's path is read without the log's lock, as the status line reads it under outMu Covered, by site and label: - finder.target: finding_vm; Lima's limactl --version: checking_backend - bringUp's checks (seeds, instance, recordFolder, hostChecks, ssh, requirements) and a running VM's go-on (proxy, pins, mount check): checking_vm; startOnce after the agent: starting; the self-check's probes: checking_network; pinHostKeys: reading_host_keys - setUpVM (sync, ssh config, links, clone seed): setting_up_vm; each devcontainer command: running_script; makeMachine: creating; buildOnce: checking_image; paste's count and rm: reading_pasted - volumeOf's lookup (diskutil), for launch, adopt, move, ls and rm: looking_up_volume - vm.ListAll (devvm ls): reading_vms; devvm doctor's checks: doctor_checking - devvm agent import: agent_import_reading (walk, links, opencode), agent_store_saving (copy, marketplace git checks, tmutil); devvm agent save: agent_save_reading, agent_save_comparing, agent_store_saving - devvm stop (from its pins check on): checking_vm; a delete, an orphan's teardown and Lima's DeleteImage: deleting; devvm git-protect and clone on/off's .git work (leftBehind, mountFolder, unmountFolder): git_protecting; git-protect's re-read: finding_vm - devvm clone add: clone_reading, clone_adding; clone fetch: clone_fetching; planClone: clone_planning; the clone check before rm or a remake: clone_checking; history export and import: history_exporting, history_importing; a remake's home: saving_home, restoring_home - devvm upgrade's install: upgrade_installing; the shared tools' step back and .NET's downloads: resolving_tools; gc's caches: reading_space; the retention settings (daily and in gc): retaining Work that takes the terminal stays off: the shell, ssh-add's passphrase (checkAgent, Agent.Ensure, reloadAgent) and an agent's login. Tests and guard: - msg.Fake: OnStatus and Showing; its LoggedCmd shows its step - backendtest.Fake: OnCall - TestLaunchStatusLines: every backend call of a new, running and stopped VM's launch runs under a status line but the shell's and Running's (pid files) - TestSSHAddOffStatusLine: ssh-add, on the user's agent or devvm's, never runs under one; chflagsCalls fails a .git lock or unlock with none - the structure test's status-lines rule (statusSites, statusFree) - TestStatusLinesNest, TestMessagesClearTheStatusLine, TestQuestionsHoldTheStatusLine, TestSecretAndPickerHoldTheStatusLine, TestTerminalHoldsTheStatusLine, TestNoStatusLineOnceInterrupted, TestFakeStatus, TestAgentImportStatus, TestAgentSaveStatus, TestDoctorReport, TestProblems, TestList, TestListLooksUpVolumesOnce, TestVolumeOfCache, TestWithSDKDownloads, TestRetainStatus, TestSetCloneOn, TestTeardownLeavesNothing The rule is written in the cli skill (rule 14, checklist, review), the codebase-standards skill (Show what devvm waits for, its checklist line and rules.md's row), Contributing's CLI page (Status lines), its tests and structure pages, and the troubleshooting page. Co-Authored-By: Claude Opus 5.5 <[email protected]>
188 lines
6.3 KiB
Go
188 lines
6.3 KiB
Go
package netpolicy
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// Results of the self-check, in the VM's state.
|
|
const (
|
|
CheckOK = "ok"
|
|
CheckUnenforced = "unenforced"
|
|
)
|
|
|
|
// directArgv connects from the VM straight to an address on the internet,
|
|
// past its proxy settings: it must fail.
|
|
var directArgv = []string{"curl", "--noproxy", "*", "-sS", "-m", "3", "-o", "/dev/null", "https://1.1.1.1"}
|
|
|
|
// proxyArgv asks the VM's proxy for its blocks, through the proxy its
|
|
// guest environment names (HTTPS_PROXY, from /etc/environment, which a
|
|
// command run in the VM may not have read): it fails when the VM has no
|
|
// proxy set, or its proxy doesn't answer it. The password stays in the
|
|
// VM: the command names the variable, not its value.
|
|
var proxyArgv = []string{"sh", "-c", `set -a; . /etc/environment 2>/dev/null; set +a; ` +
|
|
`[ -n "${HTTPS_PROXY:-}" ] && curl -sSf -m 3 -o /dev/null --proxy "$HTTPS_PROXY" http://devvm.proxy/blocked`}
|
|
|
|
// SelfCheck checks, after a VM starts, that its tier holds. An open VM's
|
|
// proxy is set in the VM and answers it (checkOpen). On a confined tier,
|
|
// it checks that its policy is enforced: on the host, that the VM runs on the process its backend
|
|
// started confined (Confined), then that a direct connection from the VM
|
|
// fails (the VM answers that one, so it only catches the honest case). The
|
|
// result goes in the VM's state.
|
|
//
|
|
// A VM devvm started confined that still gets out has another way out (a
|
|
// network devvm didn't set up), which a restart wouldn't change. One
|
|
// started some other way, or before it left the open tier, is
|
|
// offered a restart, confined (restart starts
|
|
// it again); where devvm can't ask, or if it's still unenforced after the
|
|
// restart, devvm stops. Either way the VM is stopped first, so it isn't
|
|
// left reaching the internet. A restart declined is warned about, and the
|
|
// VM is used as it is.
|
|
func (p *Proxy) SelfCheck(ctx context.Context, b backend.Backend, st *state.VM, restart func(context.Context) error) error {
|
|
vm := st.Name()
|
|
if !TierOf(st).Confined() {
|
|
p.checkOpen(ctx, b, vm)
|
|
return nil
|
|
}
|
|
ok, err := enforced(ctx, p, b, vm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if ok {
|
|
return setCheck(ctx, st, CheckOK)
|
|
}
|
|
if err := setCheck(ctx, st, CheckUnenforced); err != nil {
|
|
return err
|
|
}
|
|
run, err := b.Running(ctx, vm)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if run.Confined {
|
|
return p.stop(ctx, b, vm, msg.NetDirectRoute(vm))
|
|
}
|
|
yes, err := p.Confirm(msg.NetUnenforcedRestart(vm).AnsweredBy("devvm restart"))
|
|
if err != nil {
|
|
return p.stop(ctx, b, vm, msg.NetUnenforcedStop(vm))
|
|
}
|
|
if !yes {
|
|
p.Warn(msg.NetUnenforced(vm))
|
|
return nil
|
|
}
|
|
if err := b.Stop(ctx, vm); err != nil {
|
|
return err
|
|
}
|
|
if err := restart(ctx); err != nil {
|
|
return err
|
|
}
|
|
if ok, err := enforced(ctx, p, b, vm); err != nil {
|
|
return err
|
|
} else if !ok {
|
|
return p.stop(ctx, b, vm, msg.NetUnenforcedStop(vm))
|
|
}
|
|
return setCheck(ctx, st, CheckOK)
|
|
}
|
|
|
|
// checkOpen is an open VM's self-check: its proxy is set in the VM and
|
|
// answers it (proxyArgv). An open VM isn't confined, so one whose proxy
|
|
// doesn't answer it reaches what it reaches all the same, but its tools
|
|
// that use the proxy fail: a warning, and the VM is used as it is.
|
|
func (p *Proxy) checkOpen(ctx context.Context, b backend.Backend, vm vmname.VM) {
|
|
defer p.Status(msg.CheckingNetwork(vm))()
|
|
if err := b.Exec(ctx, vm, proxyArgv, backend.ExecOpts{}); err != nil && ctx.Err() == nil {
|
|
p.Warn(msg.NetProxyUnreachable(vm))
|
|
}
|
|
}
|
|
|
|
// setCheck records the self-check's result in the VM's state.
|
|
func setCheck(ctx context.Context, st *state.VM, result string) error {
|
|
return st.Update(ctx, func(st *state.VM) error {
|
|
n, err := Of(st)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n.Made.Check = result
|
|
return Set(st, n)
|
|
})
|
|
}
|
|
|
|
// Listed is a VM's tier as devvm ls shows it: its name, or unenforced: a
|
|
// confined tier, but the last self-check failed, or the VM runs but not
|
|
// confined (started some other way since). A stopped VM starts confined
|
|
// next time, unless that self-check failed. "" when its creation didn't
|
|
// finish.
|
|
func Listed(ctx context.Context, b backend.Backend, st *state.VM, running bool) (string, error) {
|
|
n, err := Of(st)
|
|
switch {
|
|
case err != nil:
|
|
return "", err
|
|
case !st.Finished():
|
|
return "", nil
|
|
case !n.Tier.Confined():
|
|
return string(n.Tier), nil
|
|
case n.Made.Check == CheckUnenforced:
|
|
return CheckUnenforced, nil
|
|
case !running:
|
|
return string(n.Tier), nil
|
|
}
|
|
run, err := b.Running(ctx, st.Name())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !run.Confined {
|
|
return CheckUnenforced, nil
|
|
}
|
|
return string(n.Tier), nil
|
|
}
|
|
|
|
// enforced reports whether the VM runs confined and can't connect out
|
|
// directly, checked under r's status line.
|
|
func enforced(ctx context.Context, r msg.Reporter, b backend.Backend, vm vmname.VM) (bool, error) {
|
|
defer r.Status(msg.CheckingNetwork(vm))()
|
|
run, err := b.Running(ctx, vm)
|
|
if err != nil || !run.Confined {
|
|
return false, err
|
|
}
|
|
return !Direct(ctx, b, vm), nil
|
|
}
|
|
|
|
// Direct reports whether the VM reaches the internet without its proxy (it
|
|
// shouldn't). A VM that doesn't answer doesn't.
|
|
func Direct(ctx context.Context, b backend.Backend, vm vmname.VM) bool {
|
|
return b.Exec(ctx, vm, directArgv, backend.ExecOpts{}) == nil
|
|
}
|
|
|
|
// unproxied stops a running VM whose proxy, p's, couldn't start (err): a
|
|
// VM never runs on without its proxy (PrepareRunning). Its proxy, if one
|
|
// still runs, stops too. It warns that it stopped it and returns err; if the stop fails, the stop's failure, after
|
|
// warning err. The stop isn't cancelled with ctx: an interrupt never
|
|
// leaves the VM running.
|
|
func unproxied(ctx context.Context, p *Proxy, b backend.Backend, vm vmname.VM, err error) error {
|
|
if serr := b.Stop(context.WithoutCancel(ctx), vm); serr != nil {
|
|
var m msg.M
|
|
if errors.As(err, &m) {
|
|
p.Warn(m)
|
|
}
|
|
return serr
|
|
}
|
|
// One still running, which doesn't enforce the VM's tier, goes too.
|
|
p.Stop(vm)
|
|
p.Warn(msg.NetProxyDownStopped(vm))
|
|
return err
|
|
}
|
|
|
|
// stop stops the VM, whose network tier doesn't hold, and returns why:
|
|
// m, or the stop's failure after warning m.
|
|
func (p *Proxy) stop(ctx context.Context, b backend.Backend, vm vmname.VM, m msg.M) error {
|
|
if err := b.Stop(ctx, vm); err != nil {
|
|
p.Warn(m)
|
|
return err
|
|
}
|
|
return m
|
|
}
|