oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package workspace
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/team"
5 "amadan.net/oficina/equipo/internal/runner/provider"
6 "amadan.net/oficina/equipo/internal/runnerproto"
7 "context"
8 "encoding/json"
9 "os"
10 "path/filepath"
11)
12
13func (m *Manager) createRead(ctx context.Context, a runnerproto.Assignment, b runnerproto.RunnerBrief) (Workspace, error) {
14 if a.ID == "" || a.TaskID == "" || a.WorkspaceKey == "" || digest(a.Selection) != digest(b.Selection) {
15 return Workspace{}, provider.ErrIdentity
16 }
17 policy, err := m.preflight(ctx, a)
18 if err != nil {
19 return Workspace{}, err
20 }
21 path := filepath.Join(m.cfg.WorkspaceRoot, "read-"+digest(a.WorkspaceKey))
22 dir := filepath.Join(m.cfg.PrivateRoot, "attempt-"+digest(a.WorkspaceKey))
23 if err = os.Mkdir(dir, 0700); err != nil {
24 return Workspace{}, provider.ErrUncertain
25 }
26 if err = os.Mkdir(path, 0700); err != nil {
27 return Workspace{}, err
28 }
29 if b.Target.Kind == team.TargetProjectRead {
30 if b.Target.Ref != "" || len(b.Sources) == 0 {
31 return Workspace{}, provider.ErrIdentity
32 }
33 for _, src := range b.Sources {
34 if team.ContentDigest(src.Payload) != src.Ref.Hash {
35 return Workspace{}, provider.ErrIdentity
36 }
37 }
38 raw, err := json.Marshal(b.Sources)
39 if err != nil {
40 return Workspace{}, err
41 }
42 if err = os.WriteFile(filepath.Join(path, "sources.json"), raw, 0400); err != nil {
43 return Workspace{}, err
44 }
45 } else {
46 if !m.validTarget(ctx, b.Target, true) {
47 return Workspace{}, provider.ErrIdentity
48 }
49 repo, _, err := m.fetch(ctx, b.Target)
50 if err != nil {
51 return Workspace{}, err
52 }
53 if err = m.init(ctx, path, objectFormat(b.Target.BaseRevision), false); err != nil {
54 return Workspace{}, err
55 }
56 if err = copyObjects(filepath.Join(repo, "objects"), filepath.Join(path, ".git", "objects")); err != nil {
57 return Workspace{}, err
58 }
59 if _, err = m.run(ctx, path, "checkout", "--detach", b.Target.BaseRevision); err != nil {
60 return Workspace{}, err
61 }
62 if err = os.RemoveAll(filepath.Join(path, ".git")); err != nil {
63 return Workspace{}, err
64 }
65 }
66 hash, err := readHash(path)
67 if err != nil {
68 return Workspace{}, err
69 }
70 at := attempt{Local: true, ReadOnly: true, ContentHash: hash, AssignmentHash: digest(a), Hash: digest(b), Request: provider.WorkspaceRequest{Path: path, ReadPolicy: policy}, Workspace: provider.Workspace{WorkspaceID: a.WorkspaceKey, Path: path}, Target: b.Target, ArtifactHashes: b.ArtifactHashes, Isolation: a.Selection.Isolation}
71 if err = save(filepath.Join(dir, "attempt.json"), at); err != nil {
72 return Workspace{}, err
73 }
74 return localWorkspace(at, a.WorkspaceKey), nil
75}
76func readHash(root string) (string, error) {
77 files := map[string]string{}
78 err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
79 if err != nil {
80 return err
81 }
82 if d.IsDir() {
83 return nil
84 }
85 if !d.Type().IsRegular() {
86 return provider.ErrIdentity
87 }
88 raw, err := os.ReadFile(path)
89 if err != nil {
90 return err
91 }
92 rel, err := filepath.Rel(root, path)
93 if err != nil {
94 return err
95 }
96 files[rel] = team.ContentDigest(raw)
97 return nil
98 })
99 return digest(files), err
100}
101func validateRead(at attempt) error {
102 if !allowed(at.Workspace.Path, at.Request.ReadPolicy) {
103 return provider.ErrPolicy
104 }
105 h, err := readHash(at.Workspace.Path)
106 if err != nil || h != at.ContentHash {
107 return provider.ErrIdentity
108 }
109 return nil
110}
111