| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "runtime" |
| 8 | "strings" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | "golang.org/x/sys/unix" |
| 13 | ) |
| 14 | |
| 15 | type Config struct { |
| 16 | MachineLabel string |
| 17 | ClaudeModel, CodexModel string |
| 18 | ToolProxyPath, BwrapPath, ModuleCache, PreviewCredentialPath, CarlosPath, PreviewHost string |
| 19 | ClaudePath, CodexCodeModeHostPath string |
| 20 | Report func(string) |
| 21 | Origin, StateDir, AmadanCredentialPath, AccountBindingsPath, CodexPath string |
| 22 | GoRoot, GoBinary, WorkspaceRoot, ScratchRoot, GitPath, AmadanPath, Executable string |
| 23 | InstanceID string |
| 24 | // PiRoot is where the pinned Pi is installed (EQUIPO_PI_ROOT, laid |
| 25 | // out as provider/pi.Paths says); empty, this runner has no Pi and |
| 26 | // runs no step on a metered connection. |
| 27 | PiRoot string |
| 28 | // OpenCodeRoot is where the pinned OpenCode is installed |
| 29 | // (EQUIPO_OPENCODE_ROOT, laid out as opencode.Paths says; equipo-runner |
| 30 | // install-opencode makes it). Empty: this runner runs no OpenCode Go |
| 31 | // step. |
| 32 | OpenCodeRoot string |
| 33 | Copy ToolCopy |
| 34 | // WorkerOnly: a runner that can only be a worker (a Mac, or the test |
| 35 | // build's fake Mac). It refuses an instance without workers. |
| 36 | WorkerOnly bool |
| 37 | // EnrolNotify hears how enrolment is going (EnrolNotice); nil is |
| 38 | // silent. |
| 39 | EnrolNotify func(EnrolNotice) |
| 40 | } |
| 41 | |
| 42 | func LoadConfig(getenv func(string) string) (Config, error) { |
| 43 | c := Config{WorkerOnly: runtime.GOOS == "darwin" || getenv("EQUIPO_FAKE_PLATFORM") != "", ClaudeModel: getenv("EQUIPO_CLAUDE_MODEL"), CodexModel: getenv("EQUIPO_CODEX_MODEL"), MachineLabel: getenv("EQUIPO_MACHINE_LABEL"), ToolProxyPath: getenv("EQUIPO_TOOL_PROXY_PATH"), CodexCodeModeHostPath: getenv("EQUIPO_CODEX_CODE_MODE_HOST_PATH"), BwrapPath: getenv("EQUIPO_BWRAP_PATH"), ModuleCache: getenv("EQUIPO_GO_MODULE_CACHE"), PreviewCredentialPath: getenv("EQUIPO_PREVIEW_CREDENTIAL_PATH"), CarlosPath: getenv("EQUIPO_CARLOS_PATH"), PreviewHost: getenv("EQUIPO_PREVIEW_HOST"), ClaudePath: getenv("EQUIPO_CLAUDE_PATH"), Origin: getenv("EQUIPO_ORIGIN"), StateDir: getenv("EQUIPO_RUNNER_STATE_DIR"), AmadanCredentialPath: getenv("EQUIPO_AMADAN_CREDENTIAL_PATH"), AccountBindingsPath: getenv("EQUIPO_ACCOUNT_BINDINGS_PATH"), CodexPath: getenv("EQUIPO_CODEX_PATH"), GoRoot: getenv("EQUIPO_GO_ROOT"), GoBinary: getenv("EQUIPO_GO_BINARY"), WorkspaceRoot: getenv("EQUIPO_WORKSPACE_ROOT"), ScratchRoot: getenv("EQUIPO_SCRATCH_ROOT"), GitPath: getenv("EQUIPO_GIT_PATH"), AmadanPath: getenv("EQUIPO_AMADAN_PATH"), InstanceID: getenv("EQUIPO_INSTANCE_ID"), PiRoot: getenv("EQUIPO_PI_ROOT"), OpenCodeRoot: getenv("EQUIPO_OPENCODE_ROOT")} |
| 44 | if c.PreviewHost == "" { |
| 45 | c.PreviewHost = "devbox" |
| 46 | } |
| 47 | var err error |
| 48 | c.Executable, err = os.Executable() |
| 49 | if err != nil { |
| 50 | return c, err |
| 51 | } |
| 52 | return c, c.validate() |
| 53 | } |
| 54 | func validOrigin(origin string) bool { |
| 55 | u, err := url.Parse(origin) |
| 56 | return err == nil && u.Host != "" && u.User == nil && u.Path == "" && u.RawQuery == "" && !u.ForceQuery && u.Fragment == "" && !strings.Contains(origin, "#") && (u.Scheme == "https" || (u.Scheme == "http" && u.Hostname() == "127.0.0.1")) |
| 57 | } |
| 58 | func (c Config) validate() error { return c.validateFor(runtime.GOOS) } |
| 59 | |
| 60 | // validateFor checks the configuration a runner on goos needs. A Mac |
| 61 | // runs checks only (a worker): no bubblewrap, model harness, forge |
| 62 | // credential or Go toolchain, so it needs its roots, git and itself. |
| 63 | func (c Config) validateFor(goos string) error { |
| 64 | if goos == "darwin" { |
| 65 | return c.validateWorker() |
| 66 | } |
| 67 | if !validOrigin(c.Origin) { |
| 68 | return configIssue("runner.config.origin", "EQUIPO_ORIGIN", provider.ErrContract) |
| 69 | } |
| 70 | for _, v := range []struct{ name, value string }{{"EQUIPO_INSTANCE_ID", c.InstanceID}} { |
| 71 | if v.value == "" { |
| 72 | return configIssue("runner.config.missing", v.name, provider.ErrContract) |
| 73 | } |
| 74 | } |
| 75 | for _, v := range []struct{ name, path string }{ |
| 76 | {"EQUIPO_BWRAP_PATH", c.BwrapPath}, {"EQUIPO_CLAUDE_PATH", c.ClaudePath}, {"EQUIPO_CODEX_CODE_MODE_HOST_PATH", c.CodexCodeModeHostPath}, {"EQUIPO_RUNNER_STATE_DIR", c.StateDir}, {"EQUIPO_AMADAN_CREDENTIAL_PATH", c.AmadanCredentialPath}, {"EQUIPO_ACCOUNT_BINDINGS_PATH", c.AccountBindingsPath}, {"EQUIPO_CODEX_PATH", c.CodexPath}, {"EQUIPO_GO_ROOT", c.GoRoot}, {"EQUIPO_GO_BINARY", c.GoBinary}, {"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}, {"EQUIPO_GIT_PATH", c.GitPath}, {"EQUIPO_AMADAN_PATH", c.AmadanPath}, {"EQUIPO_RUNNER_EXECUTABLE", c.Executable}, |
| 77 | } { |
| 78 | if !filepath.IsAbs(v.path) || filepath.Clean(v.path) != v.path || v.path == "/" { |
| 79 | return configIssue("runner.config.path", v.name, provider.ErrContract) |
| 80 | } |
| 81 | } |
| 82 | if c.PiRoot != "" && (!filepath.IsAbs(c.PiRoot) || filepath.Clean(c.PiRoot) != c.PiRoot || c.PiRoot == "/") { |
| 83 | return configIssue("runner.config.path", "EQUIPO_PI_ROOT", provider.ErrContract) |
| 84 | } |
| 85 | if c.OpenCodeRoot != "" && (!filepath.IsAbs(c.OpenCodeRoot) || filepath.Clean(c.OpenCodeRoot) != c.OpenCodeRoot || c.OpenCodeRoot == "/") { |
| 86 | return configIssue("runner.config.path", "EQUIPO_OPENCODE_ROOT", provider.ErrContract) |
| 87 | } |
| 88 | info, err := os.Stat(c.AmadanPath) |
| 89 | if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 { |
| 90 | return configIssue("runner.config.path", "EQUIPO_AMADAN_PATH", provider.ErrContract) |
| 91 | } |
| 92 | for _, v := range []struct{ name, path string }{{"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}} { |
| 93 | if containsPath(c.StateDir, v.path) || containsPath(v.path, c.StateDir) { |
| 94 | return configIssue("runner.config.overlap", v.name, provider.ErrPolicy) |
| 95 | } |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func (c Config) validateWorker() error { |
| 101 | if !validOrigin(c.Origin) { |
| 102 | return configIssue("runner.config.origin", "EQUIPO_ORIGIN", provider.ErrContract) |
| 103 | } |
| 104 | if c.InstanceID == "" { |
| 105 | return configIssue("runner.config.missing", "EQUIPO_INSTANCE_ID", provider.ErrContract) |
| 106 | } |
| 107 | for _, v := range []struct{ name, path string }{{"EQUIPO_RUNNER_STATE_DIR", c.StateDir}, {"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}, {"EQUIPO_GIT_PATH", c.GitPath}, {"EQUIPO_RUNNER_EXECUTABLE", c.Executable}} { |
| 108 | if !filepath.IsAbs(v.path) || filepath.Clean(v.path) != v.path || v.path == "/" { |
| 109 | return configIssue("runner.config.path", v.name, provider.ErrContract) |
| 110 | } |
| 111 | } |
| 112 | for _, v := range []struct{ name, path string }{{"EQUIPO_WORKSPACE_ROOT", c.WorkspaceRoot}, {"EQUIPO_SCRATCH_ROOT", c.ScratchRoot}} { |
| 113 | if containsPath(c.StateDir, v.path) || containsPath(v.path, c.StateDir) { |
| 114 | return configIssue("runner.config.overlap", v.name, provider.ErrPolicy) |
| 115 | } |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | func containsPath(root, path string) bool { |
| 121 | r, err := filepath.Rel(root, path) |
| 122 | return err == nil && (r == "." || r != ".." && !strings.HasPrefix(r, "../")) |
| 123 | } |
| 124 | func lockState(dir string) (*os.File, error) { |
| 125 | if !filepath.IsAbs(dir) || filepath.Clean(dir) != dir { |
| 126 | return nil, provider.ErrPolicy |
| 127 | } |
| 128 | if err := os.MkdirAll(dir, 0700); err != nil { |
| 129 | return nil, err |
| 130 | } |
| 131 | real, err := filepath.EvalSymlinks(dir) |
| 132 | if err != nil || real != dir { |
| 133 | return nil, provider.ErrPolicy |
| 134 | } |
| 135 | info, err := os.Stat(dir) |
| 136 | if err != nil || info.Mode().Perm() != 0700 { |
| 137 | return nil, provider.ErrPolicy |
| 138 | } |
| 139 | fd, err := unix.Open(filepath.Join(dir, "runner.lock"), unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC|unix.O_NOFOLLOW, 0600) |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | f := os.NewFile(uintptr(fd), "runner.lock") |
| 144 | if err = unix.Flock(fd, unix.LOCK_EX|unix.LOCK_NB); err != nil { |
| 145 | f.Close() |
| 146 | return nil, provider.ErrUncertain |
| 147 | } |
| 148 | return f, nil |
| 149 | } |
| 150 | func privateJSON(path string, out any) error { |
| 151 | info, err := os.Lstat(path) |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > 1<<20 { |
| 156 | return provider.ErrPolicy |
| 157 | } |
| 158 | f, err := os.Open(path) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | defer f.Close() |
| 163 | return runnerproto.DecodeLimit(f, out, 1<<20) |
| 164 | } |
| 165 | func readCredentials(path string) (runnerproto.CredentialRotation, error) { |
| 166 | var value runnerproto.CredentialRotation |
| 167 | err := privateJSON(path, &value) |
| 168 | if err == nil && value.Bearer.Token == "" { |
| 169 | err = provider.ErrIdentity |
| 170 | } |
| 171 | return value, err |
| 172 | } |
| 173 | |
| 174 | // DirectBinding supplies configured provenance inputs. Only OpenAccount can |
| 175 | // validate them and grant DedicatedHome through the returned lease Binding. |
| 176 | func (c Config) DirectBinding(b AccountBinding) (provider.Binding, error) { |
| 177 | if !filepath.IsAbs(c.ScratchRoot) || filepath.Clean(c.ScratchRoot) != c.ScratchRoot || c.ScratchRoot == "/" || containsPath(b.ProviderHome, c.ScratchRoot) || containsPath(c.ScratchRoot, b.ProviderHome) { |
| 178 | return provider.Binding{}, provider.ErrPolicy |
| 179 | } |
| 180 | return provider.Binding{ID: b.ID, Home: b.ProviderHome, AccountID: b.AccountID, Provider: b.Provider, Token: b.Kind == "token", Expected: b.ExpectedIdentity, RuntimeRoot: filepath.Join(c.ScratchRoot, "homes")}, nil |
| 181 | } |
| 182 | |
| 183 | func (c Config) bwrap() string { |
| 184 | if c.BwrapPath != "" { |
| 185 | return c.BwrapPath |
| 186 | } |
| 187 | return "/usr/bin/bwrap" |
| 188 | } |
| 189 | func (c Config) moduleCache() string { |
| 190 | if c.ModuleCache != "" { |
| 191 | return c.ModuleCache |
| 192 | } |
| 193 | home, _ := os.UserHomeDir() |
| 194 | return filepath.Join(home, "go/pkg/mod") |
| 195 | } |
| 196 | |
| 197 | func (c Config) toolProxy() string { |
| 198 | if c.ToolProxyPath != "" { |
| 199 | return c.ToolProxyPath |
| 200 | } |
| 201 | return filepath.Join(filepath.Dir(c.Executable), "equipo-tool-proxy") |
| 202 | } |
| 203 | |