package workspace import ( "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runnerproto" "context" "encoding/json" "os" "path/filepath" ) func (m *Manager) createRead(ctx context.Context, a runnerproto.Assignment, b runnerproto.RunnerBrief) (Workspace, error) { if a.ID == "" || a.TaskID == "" || a.WorkspaceKey == "" || digest(a.Selection) != digest(b.Selection) { return Workspace{}, provider.ErrIdentity } policy, err := m.preflight(ctx, a) if err != nil { return Workspace{}, err } path := filepath.Join(m.cfg.WorkspaceRoot, "read-"+digest(a.WorkspaceKey)) dir := filepath.Join(m.cfg.PrivateRoot, "attempt-"+digest(a.WorkspaceKey)) if err = os.Mkdir(dir, 0700); err != nil { return Workspace{}, provider.ErrUncertain } if err = os.Mkdir(path, 0700); err != nil { return Workspace{}, err } if b.Target.Kind == team.TargetProjectRead { if b.Target.Ref != "" || len(b.Sources) == 0 { return Workspace{}, provider.ErrIdentity } for _, src := range b.Sources { if team.ContentDigest(src.Payload) != src.Ref.Hash { return Workspace{}, provider.ErrIdentity } } raw, err := json.Marshal(b.Sources) if err != nil { return Workspace{}, err } if err = os.WriteFile(filepath.Join(path, "sources.json"), raw, 0400); err != nil { return Workspace{}, err } } else { if !m.validTarget(ctx, b.Target, true) { return Workspace{}, provider.ErrIdentity } repo, _, err := m.fetch(ctx, b.Target) if err != nil { return Workspace{}, err } if err = m.init(ctx, path, objectFormat(b.Target.BaseRevision), false); err != nil { return Workspace{}, err } if err = copyObjects(filepath.Join(repo, "objects"), filepath.Join(path, ".git", "objects")); err != nil { return Workspace{}, err } if _, err = m.run(ctx, path, "checkout", "--detach", b.Target.BaseRevision); err != nil { return Workspace{}, err } if err = os.RemoveAll(filepath.Join(path, ".git")); err != nil { return Workspace{}, err } } hash, err := readHash(path) if err != nil { return Workspace{}, err } 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} if err = save(filepath.Join(dir, "attempt.json"), at); err != nil { return Workspace{}, err } return localWorkspace(at, a.WorkspaceKey), nil } func readHash(root string) (string, error) { files := map[string]string{} err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { if err != nil { return err } if d.IsDir() { return nil } if !d.Type().IsRegular() { return provider.ErrIdentity } raw, err := os.ReadFile(path) if err != nil { return err } rel, err := filepath.Rel(root, path) if err != nil { return err } files[rel] = team.ContentDigest(raw) return nil }) return digest(files), err } func validateRead(at attempt) error { if !allowed(at.Workspace.Path, at.Request.ReadPolicy) { return provider.ErrPolicy } h, err := readHash(at.Workspace.Path) if err != nil || h != at.ContentHash { return provider.ErrIdentity } return nil }