| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "io/fs" |
| 9 | "net/http" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "reflect" |
| 13 | "slices" |
| 14 | "strings" |
| 15 | "sync" |
| 16 | ) |
| 17 | |
| 18 | // InstallAssets is configured before use and must not be copied after use. |
| 19 | type InstallAssets struct { |
| 20 | Root, Version string |
| 21 | Manifest Manifest |
| 22 | mu sync.Mutex |
| 23 | files map[string]os.FileInfo |
| 24 | available bool |
| 25 | // darwin: the release lists a Mac worker and its files verified. |
| 26 | darwin bool |
| 27 | embedded fs.FS |
| 28 | digest func(io.Reader) (string, error) |
| 29 | } |
| 30 | |
| 31 | func NewInstallAssets(root, version string, manifest Manifest) *InstallAssets { |
| 32 | a := &InstallAssets{Root: root, Version: version, Manifest: manifest, embedded: embeddedInstallFiles()} |
| 33 | a.Available() |
| 34 | return a |
| 35 | } |
| 36 | |
| 37 | var installAssetNames = []string{"install-manifest.json", "equipo-runner", "equipo-tool-proxy", "install-v1.sh"} |
| 38 | |
| 39 | // darwinAssetNames are the Mac worker's files, served when the release |
| 40 | // manifest lists the darwin/arm64 target and they match its digests. |
| 41 | var darwinAssetNames = []string{DarwinDir + "/equipo-runner", DarwinDir + "/gitleaks"} |
| 42 | |
| 43 | type installFile interface { |
| 44 | fs.File |
| 45 | io.ReadSeeker |
| 46 | } |
| 47 | |
| 48 | func (a *InstallAssets) open(name string) (installFile, error) { |
| 49 | if a.Root == "" { |
| 50 | if a.embedded == nil { |
| 51 | return nil, errManifest |
| 52 | } |
| 53 | f, err := a.embedded.Open(name) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | seekable, ok := f.(installFile) |
| 58 | if !ok { |
| 59 | f.Close() |
| 60 | return nil, errManifest |
| 61 | } |
| 62 | return seekable, nil |
| 63 | } |
| 64 | if !filepath.IsAbs(a.Root) || filepath.Clean(a.Root) != a.Root { |
| 65 | return nil, errManifest |
| 66 | } |
| 67 | path := filepath.Join(a.Root, name) |
| 68 | real, e := filepath.EvalSymlinks(path) |
| 69 | if e != nil || real != path { |
| 70 | return nil, errManifest |
| 71 | } |
| 72 | f, e := os.Open(path) |
| 73 | if e != nil { |
| 74 | return nil, e |
| 75 | } |
| 76 | i, e := f.Stat() |
| 77 | limit := int64(128 << 20) |
| 78 | switch name { |
| 79 | case "install-manifest.json", "install-v1.sh": |
| 80 | limit = 1 << 20 |
| 81 | } |
| 82 | if e != nil || !i.Mode().IsRegular() || i.Size() > limit { |
| 83 | f.Close() |
| 84 | return nil, errManifest |
| 85 | } |
| 86 | return f, nil |
| 87 | } |
| 88 | func sameInstallFile(a, b os.FileInfo) bool { |
| 89 | return a != nil && b != nil && os.SameFile(a, b) && a.Size() == b.Size() && a.ModTime() == b.ModTime() && a.Mode() == b.Mode() |
| 90 | } |
| 91 | func (a *InstallAssets) unchanged() bool { |
| 92 | if a.Root == "" { |
| 93 | return a.files != nil |
| 94 | } |
| 95 | for name := range a.files { |
| 96 | f, e := a.open(name) |
| 97 | if e != nil { |
| 98 | return false |
| 99 | } |
| 100 | i, e := f.Stat() |
| 101 | f.Close() |
| 102 | if e != nil || !sameInstallFile(a.files[name], i) { |
| 103 | return false |
| 104 | } |
| 105 | } |
| 106 | return true |
| 107 | } |
| 108 | |
| 109 | // CachedAvailable only checks metadata. Page rendering never hashes a release. |
| 110 | func (a *InstallAssets) CachedAvailable() bool { |
| 111 | a.mu.Lock() |
| 112 | defer a.mu.Unlock() |
| 113 | return a.available && a.unchanged() |
| 114 | } |
| 115 | func (a *InstallAssets) Available() bool { |
| 116 | a.mu.Lock() |
| 117 | defer a.mu.Unlock() |
| 118 | if !ValidInstallVersion(a.Version) || !a.Manifest.Installable() { |
| 119 | return false |
| 120 | } |
| 121 | if a.files != nil && a.unchanged() { |
| 122 | return a.available |
| 123 | } |
| 124 | a.available, a.darwin = false, false |
| 125 | files := map[string]installFile{} |
| 126 | infos := map[string]os.FileInfo{} |
| 127 | defer func() { |
| 128 | for _, f := range files { |
| 129 | f.Close() |
| 130 | } |
| 131 | }() |
| 132 | for _, name := range installAssetNames { |
| 133 | f, e := a.open(name) |
| 134 | if e != nil { |
| 135 | a.files = nil |
| 136 | return false |
| 137 | } |
| 138 | files[name] = f |
| 139 | infos[name], e = f.Stat() |
| 140 | if e != nil { |
| 141 | a.files = nil |
| 142 | return false |
| 143 | } |
| 144 | } |
| 145 | a.files = infos |
| 146 | var m InstallManifest |
| 147 | decoder := json.NewDecoder(files["install-manifest.json"]) |
| 148 | if decoder.Decode(&m) != nil || m.Validate(a.Version) != nil || !reflect.DeepEqual(m.Release, a.Manifest) { |
| 149 | return false |
| 150 | } |
| 151 | if decoder.Decode(new(any)) != io.EOF { |
| 152 | return false |
| 153 | } |
| 154 | digest := a.digest |
| 155 | if digest == nil { |
| 156 | digest = func(r io.Reader) (string, error) { |
| 157 | h := sha256.New() |
| 158 | _, e := io.Copy(h, r) |
| 159 | return hex.EncodeToString(h.Sum(nil)), e |
| 160 | } |
| 161 | } |
| 162 | for name, want := range map[string]string{"equipo-runner": m.RunnerSHA256, "equipo-tool-proxy": a.Manifest.ToolProxySHA256()} { |
| 163 | got, e := digest(files[name]) |
| 164 | if e != nil || want == "" || got != want { |
| 165 | return false |
| 166 | } |
| 167 | } |
| 168 | if want := m.Targets[TargetLinux].GitleaksSHA256; want != "" { |
| 169 | f, err := a.open("gitleaks") |
| 170 | if err != nil { |
| 171 | return false |
| 172 | } |
| 173 | files["gitleaks"] = f |
| 174 | got, err := digest(f) |
| 175 | if err != nil || got != want { |
| 176 | return false |
| 177 | } |
| 178 | infos["gitleaks"], err = f.Stat() |
| 179 | if err != nil { |
| 180 | return false |
| 181 | } |
| 182 | } |
| 183 | // The page renderer is not served here: the Linux runner fetches it |
| 184 | // from the forge (RendererForge), checked against what is linked into |
| 185 | // it. An instance carrying it would pass the platform's ship cap. |
| 186 | // The Mac worker: listed, present and matching, or not served at all. |
| 187 | if t, e := m.DarwinTarget(a.Version); e == nil { |
| 188 | mac := map[string]installFile{} |
| 189 | for _, name := range darwinAssetNames { |
| 190 | if f, e := a.open(name); e == nil { |
| 191 | mac[name] = f |
| 192 | files[name] = f |
| 193 | } |
| 194 | } |
| 195 | ok := len(mac) == len(darwinAssetNames) |
| 196 | if t.GitleaksSHA256 == "" { |
| 197 | ok = mac[darwinAssetNames[0]] != nil |
| 198 | } |
| 199 | wants := map[string]string{darwinAssetNames[0]: t.RunnerSHA256} |
| 200 | if t.GitleaksSHA256 != "" { |
| 201 | wants[darwinAssetNames[1]] = t.GitleaksSHA256 |
| 202 | } |
| 203 | for name, want := range wants { |
| 204 | if !ok { |
| 205 | break |
| 206 | } |
| 207 | info, e := mac[name].Stat() |
| 208 | got, de := digest(mac[name]) |
| 209 | ok = e == nil && de == nil && got == want |
| 210 | if ok { |
| 211 | infos[name] = info |
| 212 | } |
| 213 | } |
| 214 | a.darwin = ok |
| 215 | if ok { |
| 216 | a.files = infos |
| 217 | } |
| 218 | } |
| 219 | // A replacement or write during verification must not enter the cache. |
| 220 | if !a.unchanged() { |
| 221 | a.files = nil |
| 222 | return false |
| 223 | } |
| 224 | a.available = true |
| 225 | return true |
| 226 | } |
| 227 | func (a *InstallAssets) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 228 | prefix := "/releases/" + a.Version + "/" |
| 229 | name := strings.TrimPrefix(r.URL.Path, prefix) |
| 230 | mac := slices.Contains(darwinAssetNames, name) |
| 231 | if !strings.HasPrefix(r.URL.Path, prefix) || (r.Method != "GET" && r.Method != "HEAD") || (name != "install-v1.sh" && name != "install-manifest.json" && name != "equipo-runner" && name != "equipo-tool-proxy" && name != "gitleaks" && !mac) { |
| 232 | w.WriteHeader(http.StatusNotFound) |
| 233 | return |
| 234 | } |
| 235 | if !a.Available() { |
| 236 | w.WriteHeader(http.StatusServiceUnavailable) |
| 237 | return |
| 238 | } |
| 239 | a.mu.Lock() |
| 240 | served := (!mac || a.darwin) && a.files[name] != nil |
| 241 | a.mu.Unlock() |
| 242 | if !served { |
| 243 | w.WriteHeader(http.StatusNotFound) |
| 244 | return |
| 245 | } |
| 246 | f, e := a.open(name) |
| 247 | if e != nil { |
| 248 | w.WriteHeader(http.StatusServiceUnavailable) |
| 249 | return |
| 250 | } |
| 251 | defer f.Close() |
| 252 | i, e := f.Stat() |
| 253 | a.mu.Lock() |
| 254 | valid := e == nil && a.available && (a.Root == "" || sameInstallFile(a.files[name], i)) |
| 255 | a.mu.Unlock() |
| 256 | if !valid { |
| 257 | w.WriteHeader(http.StatusServiceUnavailable) |
| 258 | return |
| 259 | } |
| 260 | w.Header().Set("Content-Type", "application/octet-stream") |
| 261 | w.Header().Set("X-Content-Type-Options", "nosniff") |
| 262 | w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") |
| 263 | http.ServeContent(w, r, name, i.ModTime(), f) |
| 264 | } |
| 265 | |