| 1 | package checks |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/taskplan" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "crypto/sha256" |
| 8 | "encoding/hex" |
| 9 | "image/png" |
| 10 | "io" |
| 11 | "io/fs" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "slices" |
| 15 | "time" |
| 16 | |
| 17 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 18 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 19 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 20 | ) |
| 21 | |
| 22 | type RenderDiffFile = taskplan.RenderDiffFile |
| 23 | |
| 24 | type renderBuffer struct{ bytes.Buffer } |
| 25 | |
| 26 | func (b *renderBuffer) Write(p []byte) (int, error) { |
| 27 | if b.Len()+len(p) > team.MaxArtifactBytes { |
| 28 | return 0, team.ErrSize |
| 29 | } |
| 30 | return b.Buffer.Write(p) |
| 31 | } |
| 32 | |
| 33 | type snapshotFile struct { |
| 34 | Mode uint32 |
| 35 | Hash string |
| 36 | } |
| 37 | |
| 38 | func snapshotFiles(root string) (map[string]snapshotFile, error) { |
| 39 | files := map[string]snapshotFile{} |
| 40 | err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error { |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | if d.IsDir() { |
| 45 | return nil |
| 46 | } |
| 47 | if d.Type()&os.ModeSymlink != 0 || !d.Type().IsRegular() { |
| 48 | return provider.ErrPolicy |
| 49 | } |
| 50 | if len(files) >= 100000 { |
| 51 | return team.ErrSize |
| 52 | } |
| 53 | info, err := d.Info() |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | file, err := os.Open(path) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | hash := sha256.New() |
| 62 | _, copyErr := io.Copy(hash, file) |
| 63 | closeErr := file.Close() |
| 64 | if copyErr != nil { |
| 65 | return copyErr |
| 66 | } |
| 67 | if closeErr != nil { |
| 68 | return closeErr |
| 69 | } |
| 70 | rel, err := filepath.Rel(root, path) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | files[rel] = snapshotFile{uint32(info.Mode().Perm()), hex.EncodeToString(hash.Sum(nil))} |
| 75 | return nil |
| 76 | }) |
| 77 | return files, err |
| 78 | } |
| 79 | func diffBytes(root, path string, exists bool) ([]byte, error) { |
| 80 | if !exists { |
| 81 | return nil, nil |
| 82 | } |
| 83 | f, err := os.Open(filepath.Join(root, path)) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | defer f.Close() |
| 88 | b, err := io.ReadAll(io.LimitReader(f, team.MaxArtifactBytes+1)) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | if len(b) > team.MaxArtifactBytes { |
| 93 | return nil, team.ErrSize |
| 94 | } |
| 95 | return b, nil |
| 96 | } |
| 97 | func renderDiff(base, work string) ([]byte, error) { |
| 98 | before, err := snapshotFiles(base) |
| 99 | if err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | after, err := snapshotFiles(work) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | paths := map[string]bool{} |
| 107 | for k := range before { |
| 108 | paths[k] = true |
| 109 | } |
| 110 | for k := range after { |
| 111 | paths[k] = true |
| 112 | } |
| 113 | names := make([]string, 0, len(paths)) |
| 114 | for k := range paths { |
| 115 | names = append(names, k) |
| 116 | } |
| 117 | slices.Sort(names) |
| 118 | changes := []RenderDiffFile{} |
| 119 | total := 0 |
| 120 | for _, p := range names { |
| 121 | a, aok := before[p] |
| 122 | b, bok := after[p] |
| 123 | if aok == bok && a == b { |
| 124 | continue |
| 125 | } |
| 126 | old, err := diffBytes(base, p, aok) |
| 127 | if err != nil { |
| 128 | return nil, err |
| 129 | } |
| 130 | next, err := diffBytes(work, p, bok) |
| 131 | if err != nil { |
| 132 | return nil, err |
| 133 | } |
| 134 | total += len(old) + len(next) |
| 135 | if total > team.MaxArtifactBytes { |
| 136 | return nil, team.ErrSize |
| 137 | } |
| 138 | changes = append(changes, RenderDiffFile{Path: filepath.ToSlash(p), Before: old, After: next, BeforeMode: a.Mode, AfterMode: b.Mode}) |
| 139 | } |
| 140 | raw := encode(changes) |
| 141 | if len(raw) > team.MaxArtifactBytes { |
| 142 | return nil, team.ErrSize |
| 143 | } |
| 144 | return raw, nil |
| 145 | } |
| 146 | func (e *Executor) Render(ctx context.Context, recipe runnerproto.RenderRecipe, candidateHash string) (team.RenderResult, error) { |
| 147 | return e.render(ctx, recipe, candidateHash, nil) |
| 148 | } |
| 149 | func (e *Executor) RenderDefinition(ctx context.Context, d Definition, hash string) (team.RenderResult, error) { |
| 150 | if d.Render == nil { |
| 151 | return team.RenderResult{}, provider.ErrIdentity |
| 152 | } |
| 153 | if err := e.definition(ctx, d, true); err != nil { |
| 154 | return team.RenderResult{}, err |
| 155 | } |
| 156 | return e.render(ctx, *d.Render, hash, &d) |
| 157 | } |
| 158 | func (e *Executor) render(ctx context.Context, recipe runnerproto.RenderRecipe, candidateHash string, d *Definition) (team.RenderResult, error) { |
| 159 | var result team.RenderResult |
| 160 | if recipe.Version != 1 || !commandValid(recipe.Command, false) || recipe.Command.Parser != "exit" || !hexID(e.cfg.RenderRecipeHash, 64) || Digest(encode(recipe)) != e.cfg.RenderRecipeHash { |
| 161 | return result, provider.ErrIdentity |
| 162 | } |
| 163 | c, err := e.cfg.ResolveCandidate(ctx, candidateHash) |
| 164 | if err != nil || CandidateHash(c) != candidateHash || !objectID(c.BaseCommit) || !objectID(c.Commit) || !objectID(c.Tree) || c.Repository == "" { |
| 165 | return result, provider.ErrIdentity |
| 166 | } |
| 167 | work, cleanup, err := e.scratch(ctx, c.Commit) |
| 168 | if err != nil { |
| 169 | return result, err |
| 170 | } |
| 171 | defer cleanup() |
| 172 | base, cleanupBase, err := e.scratch(ctx, c.BaseCommit) |
| 173 | if err != nil { |
| 174 | return result, err |
| 175 | } |
| 176 | defer cleanupBase() |
| 177 | diff, err := renderDiff(base, work) |
| 178 | if err != nil { |
| 179 | return result, err |
| 180 | } |
| 181 | if d != nil { |
| 182 | if err = e.material(*d, work); err != nil { |
| 183 | return result, err |
| 184 | } |
| 185 | } |
| 186 | if err = safeDir(work, recipe.Command.Dir); err != nil { |
| 187 | return result, err |
| 188 | } |
| 189 | end := time.Now().Add(time.Duration(recipe.Command.TimeoutSeconds) * time.Second) |
| 190 | if e.cfg.Deadline.Before(end) { |
| 191 | end = e.cfg.Deadline |
| 192 | } |
| 193 | ctx, cancel := context.WithDeadline(ctx, end) |
| 194 | defer cancel() |
| 195 | var pixels, stderr renderBuffer |
| 196 | code, err := e.sandbox.Run(ctx, recipe.Command, work, e.cfg.Policy, &pixels, &stderr) |
| 197 | if err != nil { |
| 198 | return result, err |
| 199 | } |
| 200 | if ctx.Err() != nil { |
| 201 | return result, ctx.Err() |
| 202 | } |
| 203 | if code != 0 || pixels.Len() == 0 { |
| 204 | return result, provider.ErrContract |
| 205 | } |
| 206 | dimensions, err := png.DecodeConfig(bytes.NewReader(pixels.Bytes())) |
| 207 | if err != nil || dimensions.Width > 8192 || dimensions.Height > 8192 { |
| 208 | return result, provider.ErrContract |
| 209 | } |
| 210 | iso, err := e.isolation(ctx) |
| 211 | if err != nil { |
| 212 | return result, err |
| 213 | } |
| 214 | environment := encode(struct { |
| 215 | Policy provider.ReadPolicy |
| 216 | Binary, Version string |
| 217 | }{e.cfg.Policy, e.binary, e.version}) |
| 218 | result = team.RenderResult{Repository: c.Repository, BaseCommit: c.BaseCommit, CandidateHash: candidateHash, DiffHash: e.saveLog(diff), RenderHash: e.saveLog(pixels.Bytes()), RecipeHash: e.cfg.RenderRecipeHash, EnvironmentHash: Digest(environment), Isolation: iso, ExecutorBinary: e.binary, ExecutorVersion: e.version} |
| 219 | return result, e.attest(ctx, team.RoleRender, result, iso) |
| 220 | } |
| 221 | |