| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | _ "embed" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | ) |
| 13 | |
| 14 | //go:embed manifest.json |
| 15 | var manifest string |
| 16 | |
| 17 | // BinaryDigest pins one executable the runner starts. The provider |
| 18 | // programs and bubblewrap are pinned in manifest.json; the tool proxy is |
| 19 | // built with the release, and its digest comes from that build. |
| 20 | type BinaryDigest struct { |
| 21 | Name string `json:"name"` |
| 22 | Version string `json:"version"` |
| 23 | SHA256 string `json:"sha256"` |
| 24 | } |
| 25 | |
| 26 | // Manifest names what one release is made of: its targets, the Go that |
| 27 | // built it, and the pinned executables. Nothing in it is typed in from a |
| 28 | // live run; the one digest that moves with a build, the tool proxy's, is |
| 29 | // recorded by make release-runner (see toolProxySHA256). |
| 30 | type Manifest struct { |
| 31 | Schema int `json:"schema"` |
| 32 | RunnerTarget string `json:"runner_target"` |
| 33 | InstanceTarget string `json:"instance_target"` |
| 34 | Go string `json:"go"` |
| 35 | Claude string `json:"claude"` |
| 36 | Codex string `json:"codex"` |
| 37 | LauncherVersion string `json:"launcher_version"` |
| 38 | BwrapVersion string `json:"bwrap_version"` |
| 39 | PolicyVersion string `json:"policy_version"` |
| 40 | Binaries []BinaryDigest `json:"binaries"` |
| 41 | } |
| 42 | |
| 43 | // toolProxySHA256 is the SHA-256 of the tool proxy built beside this |
| 44 | // binary, from the same commit. make release-runner builds the proxy |
| 45 | // first and links its digest into the runner and the instance: |
| 46 | // |
| 47 | // -X amadan.net/oficina/equipo/internal/release.toolProxySHA256=<hex> |
| 48 | // |
| 49 | // A binary built any other way has none, so it can neither install a |
| 50 | // runner nor serve one. |
| 51 | var toolProxySHA256 string |
| 52 | |
| 53 | // BuildManifest is manifest.json plus the tool proxy this build recorded. |
| 54 | func BuildManifest() Manifest { |
| 55 | var value Manifest |
| 56 | if err := json.Unmarshal([]byte(manifest), &value); err != nil { |
| 57 | panic(err) |
| 58 | } |
| 59 | if sha256Hex(toolProxySHA256) { |
| 60 | value.Binaries = append(value.Binaries, BinaryDigest{Name: "equipo-tool-proxy", Version: value.LauncherVersion, SHA256: toolProxySHA256}) |
| 61 | } |
| 62 | return value |
| 63 | } |
| 64 | |
| 65 | // VerifyInstalled binds each separately installed executable to the release. |
| 66 | // A build with no recorded tool proxy cannot authorize provider execution. |
| 67 | func (m Manifest) VerifyInstalled(paths map[string]string) error { |
| 68 | if m.Schema != 3 || m.LauncherVersion == "" || m.PolicyVersion == "" || len(m.Binaries) != 5 { |
| 69 | return errManifest |
| 70 | } |
| 71 | versions := map[string]string{"claude": m.Claude, "codex": m.Codex, "codex-code-mode-host": m.Codex, "bwrap": m.BwrapVersion, "equipo-tool-proxy": m.LauncherVersion} |
| 72 | for _, binary := range m.Binaries { |
| 73 | version, ok := versions[binary.Name] |
| 74 | if !ok || version == "" || binary.Version != version || len(binary.SHA256) != 64 { |
| 75 | return errManifest |
| 76 | } |
| 77 | path := paths[binary.Name] |
| 78 | if !filepath.IsAbs(path) { |
| 79 | return errManifest |
| 80 | } |
| 81 | f, err := os.Open(path) |
| 82 | if err != nil { |
| 83 | return errManifest |
| 84 | } |
| 85 | info, err := f.Stat() |
| 86 | if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 { |
| 87 | f.Close() |
| 88 | return errManifest |
| 89 | } |
| 90 | hash := sha256.New() |
| 91 | _, err = io.Copy(hash, f) |
| 92 | closeErr := f.Close() |
| 93 | if err != nil || closeErr != nil || hex.EncodeToString(hash.Sum(nil)) != binary.SHA256 { |
| 94 | return errManifest |
| 95 | } |
| 96 | delete(versions, binary.Name) |
| 97 | } |
| 98 | if len(versions) != 0 { |
| 99 | return errManifest |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | var errManifest = errors.New("release_manifest_unverified") |
| 105 | |