package release import ( "crypto/sha256" _ "embed" "encoding/hex" "encoding/json" "errors" "io" "os" "path/filepath" ) //go:embed manifest.json var manifest string // BinaryDigest pins one executable the runner starts. The provider // programs and bubblewrap are pinned in manifest.json; the tool proxy is // built with the release, and its digest comes from that build. type BinaryDigest struct { Name string `json:"name"` Version string `json:"version"` SHA256 string `json:"sha256"` } // Manifest names what one release is made of: its targets, the Go that // built it, and the pinned executables. Nothing in it is typed in from a // live run; the one digest that moves with a build, the tool proxy's, is // recorded by make release-runner (see toolProxySHA256). type Manifest struct { Schema int `json:"schema"` RunnerTarget string `json:"runner_target"` InstanceTarget string `json:"instance_target"` Go string `json:"go"` Claude string `json:"claude"` Codex string `json:"codex"` LauncherVersion string `json:"launcher_version"` BwrapVersion string `json:"bwrap_version"` PolicyVersion string `json:"policy_version"` Binaries []BinaryDigest `json:"binaries"` } // toolProxySHA256 is the SHA-256 of the tool proxy built beside this // binary, from the same commit. make release-runner builds the proxy // first and links its digest into the runner and the instance: // // -X amadan.net/oficina/equipo/internal/release.toolProxySHA256= // // A binary built any other way has none, so it can neither install a // runner nor serve one. var toolProxySHA256 string // BuildManifest is manifest.json plus the tool proxy this build recorded. func BuildManifest() Manifest { var value Manifest if err := json.Unmarshal([]byte(manifest), &value); err != nil { panic(err) } if sha256Hex(toolProxySHA256) { value.Binaries = append(value.Binaries, BinaryDigest{Name: "equipo-tool-proxy", Version: value.LauncherVersion, SHA256: toolProxySHA256}) } return value } // VerifyInstalled binds each separately installed executable to the release. // A build with no recorded tool proxy cannot authorize provider execution. func (m Manifest) VerifyInstalled(paths map[string]string) error { if m.Schema != 3 || m.LauncherVersion == "" || m.PolicyVersion == "" || len(m.Binaries) != 5 { return errManifest } versions := map[string]string{"claude": m.Claude, "codex": m.Codex, "codex-code-mode-host": m.Codex, "bwrap": m.BwrapVersion, "equipo-tool-proxy": m.LauncherVersion} for _, binary := range m.Binaries { version, ok := versions[binary.Name] if !ok || version == "" || binary.Version != version || len(binary.SHA256) != 64 { return errManifest } path := paths[binary.Name] if !filepath.IsAbs(path) { return errManifest } f, err := os.Open(path) if err != nil { return errManifest } info, err := f.Stat() if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 { f.Close() return errManifest } hash := sha256.New() _, err = io.Copy(hash, f) closeErr := f.Close() if err != nil || closeErr != nil || hex.EncodeToString(hash.Sum(nil)) != binary.SHA256 { return errManifest } delete(versions, binary.Name) } if len(versions) != 0 { return errManifest } return nil } var errManifest = errors.New("release_manifest_unverified")