mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Each launch and each devvm agent login claude copied the store's VM copy of Claude Code's OAuth login (claudeAiOauth only) over the VM's ~/.claude/.credentials.json, where Claude Code also keeps its logins to MCP servers (mcpOAuth: their tokens and, mid-login, the PKCE verifier). So an MCP login failed when the VM was set up again before it finished, and a finished one was gone at the next launch. The setup now gets the copy as claude-credentials.json (claudeSyncName), and sync.sh's merge_claude_credentials merges it in the VM: the copy's claudeAiOauth replaces the VM's (or takes it out when the copy has none), every other key of the VM's file stays. It reads the VM's file with one jq call (--slurpfile, so two JSON values or a non-object are caught) and writes it whole, mode 600, through a mktemp file in ~/.cache, which no history export or remake carries, then mv; a failed cat or mv removes the temp file and fails the setup. A link, or a file that's empty, not JSON or not one object, is replaced by the copy and said (a link removed first, so mv never writes through it into a folder); a folder is left and said. With a Claude Code token (setup-token), devvm sends no copy, as before, so the VM's file stays as it is: Claude Code prefers the token in its environment, and a leftover login's placeholder gets the token at api.anthropic.com and nothing at the connectors' host. mcpOAuth stays in the VM: devvm agent save reads only settings and the .claude.json allowlist, history export excludes .credentials.json (HistoryExcludes) and import skips it, and a remake leaves login files out (homeSkips), all unchanged. Tests: TestStageClaudeOAuthMerged (the copy is staged to merge, never at home/.claude/.credentials.json); TestClaudeSyncNamesMerged (sync.sh merges each claudeSyncName name into its place); guest.txtar runs merge_claude_credentials (mcpOAuth and other keys kept, claudeAiOauth replaced or removed, a key with a \u escape, empty, not JSON, two values, an array, a link to a file or folder, a folder, a failed move, the warnings, mode 600, no temp left); agent_login.txtar reads the new name. Docs: Coding agents' MCP servers' logins (real tokens the VM's code can read, never leaving it) and its Copies, How it works' Claude Code's login and the network page's sentences, what a VM made again keeps, the sync.sh header, and the safety pages' claim that a VM never holds a refresh token, narrowed to the logins devvm keeps. Co-Authored-By: Claude Opus 5.5 <[email protected]>
365 lines
15 KiB
Go
365 lines
15 KiB
Go
package vm
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"maps"
|
|
"net/netip"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/devvmhq/devvm/internal/agents"
|
|
"github.com/devvmhq/devvm/internal/backend"
|
|
"github.com/devvmhq/devvm/internal/backend/backendtest"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/netpolicy"
|
|
"github.com/devvmhq/devvm/internal/secrets"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// A VM's links are checked before its setup gets them: addresses in
|
|
// 127.77.0.0/16, each once; names that are one lowercase DNS label, not
|
|
// localhost or the VM's hostname, each once.
|
|
func TestCheckLinks(t *testing.T) {
|
|
a := netip.MustParseAddr
|
|
db, cache := GuestLink{a("127.77.0.2"), []string{"db"}}, GuestLink{a("127.77.1.3"), []string{"cache", "redis-2"}}
|
|
for _, c := range []struct {
|
|
name string
|
|
links []GuestLink
|
|
want string
|
|
}{
|
|
{"none", nil, ""},
|
|
{"two targets, one with two names", []GuestLink{db, cache}, ""},
|
|
{"the range's last address", []GuestLink{{a("127.77.255.254"), []string{"x"}}}, ""},
|
|
{"outside the range", []GuestLink{{a("127.0.0.1"), []string{"db"}}}, "link_setup_invalid address=127.0.0.1 name=db vm=v"},
|
|
{"the next range", []GuestLink{{a("127.78.0.2"), []string{"db"}}}, "link_setup_invalid address=127.78.0.2 name=db vm=v"},
|
|
{"IPv6", []GuestLink{{a("::ffff:127.77.0.2"), []string{"db"}}}, "link_setup_invalid address=::ffff:127.77.0.2 name=db vm=v"},
|
|
{"no address", []GuestLink{{Names: []string{"db"}}}, "link_setup_invalid address=invalid IP name=db vm=v"},
|
|
{"an address twice", []GuestLink{db, {a("127.77.0.2"), []string{"web"}}}, "link_setup_invalid address=127.77.0.2 name=web vm=v"},
|
|
{"no names", []GuestLink{{a("127.77.0.2"), nil}}, "link_setup_invalid address=127.77.0.2 name= vm=v"},
|
|
{"a name twice", []GuestLink{db, {a("127.77.0.3"), []string{"db"}}}, "link_setup_invalid address=127.77.0.3 name=db vm=v"},
|
|
{"localhost", []GuestLink{{a("127.77.0.2"), []string{"localhost"}}}, "link_setup_invalid address=127.77.0.2 name=localhost vm=v"},
|
|
{"the hostname", []GuestLink{{a("127.77.0.2"), []string{"web"}}}, "link_setup_invalid address=127.77.0.2 name=web vm=v"},
|
|
{"a dot", []GuestLink{{a("127.77.0.2"), []string{"db.devvm.internal"}}}, "link_setup_invalid address=127.77.0.2 name=db.devvm.internal vm=v"},
|
|
{"capitals", []GuestLink{{a("127.77.0.2"), []string{"DB"}}}, "link_setup_invalid address=127.77.0.2 name=DB vm=v"},
|
|
{"a space", []GuestLink{{a("127.77.0.2"), []string{"db x"}}}, "link_setup_invalid address=127.77.0.2 name=db x vm=v"},
|
|
{"a newline", []GuestLink{{a("127.77.0.2"), []string{"db\n1.2.3.4"}}}, "link_setup_invalid address=127.77.0.2 name=db1.2.3.4 vm=v"},
|
|
{"a leading dash", []GuestLink{{a("127.77.0.2"), []string{"-db"}}}, "link_setup_invalid address=127.77.0.2 name=-db vm=v"},
|
|
{"a trailing dash", []GuestLink{{a("127.77.0.2"), []string{"db-"}}}, "link_setup_invalid address=127.77.0.2 name=db- vm=v"},
|
|
{"an empty name", []GuestLink{{a("127.77.0.2"), []string{""}}}, "link_setup_invalid address=127.77.0.2 name= vm=v"},
|
|
{"63 characters", []GuestLink{{a("127.77.0.2"), []string{strings.Repeat("a", 63)}}}, ""},
|
|
{"64 characters", []GuestLink{{a("127.77.0.2"), []string{strings.Repeat("a", 64)}}}, "link_setup_invalid address=127.77.0.2 name=" + strings.Repeat("a", 64) + " vm=v"},
|
|
} {
|
|
if got := msg.NamesOf(checkLinks("v", "web", c.links)); got != c.want {
|
|
t.Errorf("%s: checkLinks = %q, want %q", c.name, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The setup's hosts file: a line per target, each name and its
|
|
// NAME.devvm.internal; empty without links.
|
|
func TestLinkHosts(t *testing.T) {
|
|
links := []GuestLink{
|
|
{netip.MustParseAddr("127.77.0.2"), []string{"db"}},
|
|
{netip.MustParseAddr("127.77.0.3"), []string{"cache", "redis"}},
|
|
}
|
|
want := "127.77.0.2 db db.devvm.internal\n127.77.0.3 cache cache.devvm.internal redis redis.devvm.internal\n"
|
|
if got := string(linkHosts(links)); got != want {
|
|
t.Errorf("linkHosts = %q, want %q", got, want)
|
|
}
|
|
if got := linkHosts(nil); len(got) != 0 {
|
|
t.Errorf("linkHosts(nil) = %q", got)
|
|
}
|
|
}
|
|
|
|
// With links (Env.Links, a hand-made list), a VM with network policy
|
|
// starts with no link name in its NO_PROXY, as its HTTP clients reach
|
|
// links through its proxy, and its setup gets the hosts file and the same
|
|
// NO_PROXY; links that aren't as devvm makes them fail the start, before
|
|
// the VM starts.
|
|
func TestUpLinks(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
f.LifelineFile = filepath.Join(e.Dirs.Lima, "vm", "ha.pid")
|
|
f.ExecFunc = noDirect
|
|
e.Links = func(*state.VM) ([]GuestLink, error) {
|
|
return []GuestLink{{netip.MustParseAddr("127.77.0.2"), []string{"db", "pg"}}}, nil
|
|
}
|
|
if err := (Launch{e}).Up(t.Context(), Options{Network: "allowlist"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
want := "localhost,127.0.0.1,::1"
|
|
c, _ := call(f, "Start").Args[2].(backend.Confinement)
|
|
if c.Env["NO_PROXY"] != want || c.Env["no_proxy"] != want {
|
|
t.Errorf("Start's env = %q", c.Env)
|
|
}
|
|
got := staged(t, f)
|
|
if got["hosts"] != "127.77.0.2 db db.devvm.internal pg pg.devvm.internal\n" || !strings.Contains(got["vm"], "\nDEVVM_NO_PROXY='"+want+"'\n") ||
|
|
!strings.Contains(got["vm"], "\nDEVVM_LOCAL_PROXY='127.0.77.1:3128'\n") {
|
|
t.Errorf("staged hosts = %q, vm = %q", got["hosts"], got["vm"])
|
|
}
|
|
}
|
|
|
|
// The local proxy's address goes to a VM with a proxy, and none to one without.
|
|
func TestLocalProxy(t *testing.T) {
|
|
t.Parallel()
|
|
if got := localProxy("http://devvm:[email protected]:41000"); got != "127.0.77.1:3128" {
|
|
t.Errorf("localProxy(a proxy) = %q", got)
|
|
}
|
|
if got := localProxy(""); got != "" {
|
|
t.Errorf("localProxy(none) = %q", got)
|
|
}
|
|
}
|
|
|
|
// Links that aren't as devvm makes them stop a VM's start before its
|
|
// backend starts it, and its proxy with it.
|
|
func TestUpLinksInvalid(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
f.LifelineFile = filepath.Join(e.Dirs.Lima, "vm", "ha.pid")
|
|
f.ExecFunc = noDirect
|
|
e.Links = func(*state.VM) ([]GuestLink, error) {
|
|
return []GuestLink{{netip.MustParseAddr("127.0.0.1"), []string{"db"}}}, nil
|
|
}
|
|
err := Launch{e}.Up(t.Context(), Options{Network: "allowlist"})
|
|
spec, _ := call(f, "CreateVM").Args[1].(backend.VMSpec)
|
|
if msg.NamesOf(err) != "link_setup_invalid address=127.0.0.1 name=db vm="+spec.Name.String() {
|
|
t.Fatalf("Up = %v", msg.NamesOf(err))
|
|
}
|
|
if slices.Contains(f.Methods(), "Start") {
|
|
t.Errorf("calls = %q", f.Methods())
|
|
}
|
|
if _, ok := e.Proxy.PID(spec.Name); ok {
|
|
t.Error("its proxy runs")
|
|
}
|
|
}
|
|
|
|
// An open VM's setup gets the hosts file too, and its links aren't in its
|
|
// NO_PROXY: it reaches them through its proxy.
|
|
func TestUpLinksNoPolicy(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
e.Links = func(*state.VM) ([]GuestLink, error) {
|
|
return []GuestLink{{netip.MustParseAddr("127.77.0.9"), []string{"api"}}}, nil
|
|
}
|
|
if err := (Launch{e}).Up(t.Context(), Options{Network: "open"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
if c, _ := call(f, "Start").Args[2].(backend.Confinement); c.Env["NO_PROXY"] != netpolicy.NoProxy || !c.GuestProxy() {
|
|
t.Errorf("Start's env = %q", c.Env)
|
|
}
|
|
if got := staged(t, f); got["hosts"] != "127.77.0.9 api api.devvm.internal\n" {
|
|
t.Errorf("staged hosts = %q", got["hosts"])
|
|
}
|
|
}
|
|
|
|
// Every VM's setup is the same for its secrets, open or confined: the
|
|
// VM's own CA and its next, which its proxy puts the secrets in with.
|
|
func TestStageSecretsEveryTier(t *testing.T) {
|
|
for _, o := range []Options{{Network: "open"}, {Network: "internet"}, {Network: "allowlist"}, {Network: "none"}} {
|
|
e, f, _ := upEnv(t, allNative())
|
|
f.LifelineFile = filepath.Join(e.Dirs.Lima, "vm", "ha.pid")
|
|
f.ExecFunc = noDirect
|
|
if err := (Launch{e}).Up(t.Context(), o); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
got := staged(t, f)
|
|
for _, name := range []string{"ca/devvm-proxy.crt", "ca/devvm-proxy-next.crt"} {
|
|
if !strings.Contains(got[name], "BEGIN CERTIFICATE") {
|
|
t.Errorf("%+v: staged %s = %q", o, name, got[name])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// The VM's copy of Claude Code's OAuth login goes to the setup to merge
|
|
// into the VM's own .credentials.json (guest/sync.sh), never to be copied
|
|
// over it, which would take away what Claude Code keeps there of its own,
|
|
// such as its logins to MCP servers.
|
|
func TestStageClaudeOAuthMerged(t *testing.T) {
|
|
e, f, _ := upEnv(t, allNative())
|
|
write(t, filepath.Dir(secrets.LoginFile(e.Dirs, agents.ClaudeOAuth)), map[string]string{ //nolint:gosec // a test's fake login
|
|
".credentials.json": `{"claudeAiOauth": {"accessToken": "sk-ant-oat01-testfake", "refreshToken": "sk-ant-ort01-testfake", "expiresAt": 4070908800000, "scopes": ["user:inference"]}}`,
|
|
})
|
|
if err := (Launch{e}).Up(t.Context(), Options{Agents: &[]string{"claude"}, Network: "open"}); err != nil {
|
|
t.Fatal(msg.NamesOf(err))
|
|
}
|
|
got := staged(t, f)
|
|
if _, ok := got["home/"+secrets.LoginRel(agents.ClaudeOAuth)]; ok {
|
|
t.Errorf("the login is copied over the VM's: staged %q", slices.Sorted(maps.Keys(got)))
|
|
}
|
|
// The name guest/sync.sh merges from.
|
|
c := got["claude-credentials.json"]
|
|
if !strings.Contains(c, `"sk-ant-oat01-devvm-proxy-managed-oauth-`) || strings.Contains(c, "testfake") {
|
|
t.Errorf("staged claude-credentials.json = %q", c)
|
|
}
|
|
}
|
|
|
|
// Each of Claude Code's files that a launch's setup merges (claudeSyncName)
|
|
// is merged by guest/sync.sh from the name it's sent as, into its place
|
|
// below the VM's home folder.
|
|
func TestClaudeSyncNamesMerged(t *testing.T) {
|
|
for kind, merge := range map[string]string{
|
|
agents.Claude: "merge_claude_json ~/.claude.json",
|
|
agents.ClaudeOAuth: "merge_claude_credentials ~/.claude/.credentials.json",
|
|
} {
|
|
name, merged := claudeSyncName(kind)
|
|
if !merged {
|
|
t.Errorf("%s isn't merged", kind)
|
|
continue
|
|
}
|
|
block := between(t, "guest/sync.sh", `if [[ -f "$s/`+name+`" ]]; then`, "fi")
|
|
if !strings.Contains(block, " "+merge+` "$s/`+name+`"`) {
|
|
t.Errorf("sync.sh's merge of %s: %q", name, block)
|
|
}
|
|
}
|
|
if _, merged := claudeSyncName(agents.Codex); merged {
|
|
t.Error("Codex's login is merged")
|
|
}
|
|
}
|
|
|
|
// On every tier, opencode's config folder's files that are neither
|
|
// settings nor Markdown stay on the host; every other agent's file goes.
|
|
func TestWithheld(t *testing.T) {
|
|
for rel, want := range map[string]bool{
|
|
".config/opencode/.env": true,
|
|
".config/opencode/secrets.txt": true,
|
|
".config/opencode/AGENTS.md": false,
|
|
".config/opencode/opencode.json": false,
|
|
".codex/AGENTS.md": false,
|
|
".claude/CLAUDE.md": false,
|
|
".pi/agent/models.json": false,
|
|
".pi/agent/keybindings.json": false,
|
|
} {
|
|
if got := withheld(rel); got != want {
|
|
t.Errorf("withheld(%s) = %v, want %v", rel, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// pi's settings files that can hold a key are redacted as JSON, and its
|
|
// keybindings and Markdown go as they are; every one of pi's files is
|
|
// one of its settings.
|
|
func TestPiSettingsFormat(t *testing.T) {
|
|
for rel, want := range map[string]string{
|
|
".pi/agent/settings.json": "json",
|
|
".pi/agent/models.json": "json",
|
|
".pi/agent/mcp.json": "json",
|
|
".pi/agent/keybindings.json": "",
|
|
".pi/agent/AGENTS.md": "",
|
|
".pi/agent/auth.json": "",
|
|
".pi/agent/trust.json": "",
|
|
".pi/settings.json": "",
|
|
} {
|
|
if got, _ := settingsFormat(rel); got != want {
|
|
t.Errorf("settingsFormat(%s) = %q, want %q", rel, got, want)
|
|
}
|
|
}
|
|
for _, rel := range []string{".pi/agent/settings.json", ".pi/agent/models.json", ".pi/agent/mcp.json"} {
|
|
if !slices.Contains(agentFiles(t.TempDir(), "pi"), rel) {
|
|
t.Errorf("%s isn't among pi's settings files", rel)
|
|
}
|
|
}
|
|
for _, d := range agents.Of("pi").Dirs {
|
|
if strings.HasPrefix(d, piSessions) || !strings.HasPrefix(d, piDir+"/") {
|
|
t.Errorf("pi's settings folder %s", d)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The tool images a VM's report lists: only the VM's own, each once, in
|
|
// the report's order; a line that's anything else, or cut short, doesn't
|
|
// count. capped keeps the read to its limit.
|
|
func TestReported(t *testing.T) {
|
|
node, bun := "node-24.21.0-0123456789ab.erofs", "bun-1.4.2-0123456789ab.erofs"
|
|
files := []string{node, bun}
|
|
cases := []struct {
|
|
name, report string
|
|
want []string
|
|
}{
|
|
{"none", "", nil},
|
|
{"one", node + "\n", []string{node}},
|
|
{"in its order, once", bun + "\n" + node + "\n" + bun + "\n", []string{bun, node}},
|
|
{"not the VM's", "claude-2.1.295-0123456789ab.erofs\n../x\n\n", nil},
|
|
{"more on the line", node + " x\nx " + node + "\n" + node + "\r\n", nil},
|
|
{"cut short", bun + "\n" + node, []string{bun}},
|
|
}
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
if got := reported(files, []byte(c.report)); !slices.Equal(got, c.want) {
|
|
t.Errorf("reported = %q, want %q", got, c.want)
|
|
}
|
|
})
|
|
}
|
|
out := &bounded{max: unmountedMax}
|
|
if _, err := out.Write([]byte(strings.Repeat("x\n", unmountedMax) + node + "\n")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out.Len() != unmountedMax || reported(files, out.Bytes()) != nil {
|
|
t.Errorf("capped kept %d bytes", out.Len())
|
|
}
|
|
}
|
|
|
|
// slowExec is a backend whose Exec writes its report, then holds the call
|
|
// until its context ends, as a VM's root can.
|
|
type slowExec struct {
|
|
*backendtest.Fake
|
|
report string
|
|
hang bool
|
|
}
|
|
|
|
func (s slowExec) Exec(ctx context.Context, _ vmname.VM, _ []string, o backend.ExecOpts) error {
|
|
if _, err := io.WriteString(o.Stdout, s.report); err != nil {
|
|
return err
|
|
}
|
|
if !s.hang {
|
|
return nil
|
|
}
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
|
|
// Reading the report gives up after its wait, and then no tool counts;
|
|
// a read in time counts.
|
|
func TestReadUnmountedWait(t *testing.T) {
|
|
node := "node-24.21.0-0123456789ab.erofs"
|
|
files := []string{node}
|
|
b := slowExec{Fake: backendtest.New("fake", nil), report: node + "\n"}
|
|
if got := readUnmounted(t.Context(), b, "vm", files, "", time.Minute); !slices.Equal(got, files) {
|
|
t.Errorf("in time: %q, want %q", got, files)
|
|
}
|
|
b.hang = true
|
|
start := time.Now()
|
|
if got := readUnmounted(t.Context(), b, "vm", files, "", 50*time.Millisecond); got != nil {
|
|
t.Errorf("hung: %q, want none", got)
|
|
}
|
|
if d := time.Since(start); d > 5*time.Second {
|
|
t.Errorf("hung read took %s", d)
|
|
}
|
|
}
|
|
|
|
// A setup clears the move it followed, and not one another move recorded
|
|
// meanwhile.
|
|
func TestFollowed(t *testing.T) {
|
|
e, _, _ := upEnv(t, allNative())
|
|
st := existingVM(t, e, Options{Network: "open"})
|
|
if err := update(t.Context(), st, movedSection, func(m *movedInVM) error { m.From = "/a"; return nil }); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, sent := range []movedInVM{{}, {From: "/b"}} {
|
|
if err := followed(t.Context(), st, sent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := state.ReadVM(e.Dirs, st.Name()); sectionOf[movedInVM](got, movedSection).From != "/a" {
|
|
t.Errorf("followed %+v cleared /a", sent)
|
|
}
|
|
}
|
|
if err := followed(t.Context(), st, movedInVM{From: "/a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _ := state.ReadVM(e.Dirs, st.Name()); sectionOf[movedInVM](got, movedSection) != (movedInVM{}) {
|
|
t.Errorf("still recorded: %+v", sectionOf[movedInVM](got, movedSection))
|
|
}
|
|
}
|