| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | |
| 8 | "gorm.io/gorm" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/equipo/conversation" |
| 11 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 12 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 13 | ) |
| 14 | |
| 15 | func instanceValidators() RunnerValidators { |
| 16 | return runnerproto.ReportValidators(runnerproto.ReportErrors{Invalid: ErrInvalid, Forbidden: ErrForbidden, Conflict: ErrConflict}, func(ctx context.Context, assignment string, kinds []string) ([]json.RawMessage, error) { |
| 17 | tx, ok := ctx.Value(runnerTxKey{}).(*gorm.DB) |
| 18 | if !ok { |
| 19 | return nil, ErrUnavailable |
| 20 | } |
| 21 | bodies, err := conversation.ReportEvents(tx, assignment, kinds) |
| 22 | return bodies, storeError(err) |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | func (s *Store) supervisedAssignment(ctx context.Context, raw *sql.Tx, scope team.Scope, h team.Handle) (runnerAssignment, Assignment, error) { |
| 27 | var a runnerAssignment |
| 28 | if s.execution == nil { |
| 29 | return a, Assignment{}, ErrUnavailable |
| 30 | } |
| 31 | tx := s.execution.gormTx(ctx, raw) |
| 32 | if err := tx.First(&a, "team_id=? AND job_id=? AND attempt_id=? AND fence=? AND runner_id=?", scope.Account, scope.Job, h.Attempt, h.Fence, h.Runner).Error; err != nil { |
| 33 | return a, Assignment{}, storeError(err) |
| 34 | } |
| 35 | value, err := assignmentValue(a) |
| 36 | return a, value, err |
| 37 | } |
| 38 | func (s *Store) supervisedProvenance(ctx context.Context, raw *sql.Tx, scope team.Scope, h team.Handle) (team.Provenance, error) { |
| 39 | a, value, err := s.supervisedAssignment(ctx, raw, scope, h) |
| 40 | if err != nil { |
| 41 | return team.Provenance{}, err |
| 42 | } |
| 43 | p := team.Provenance{Producer: "equipo-runner", Provider: value.Selection.Provider, ModelFamily: value.Selection.Family, Context: runnerproto.EventHash([]any{scope, h.Attempt, h.Fence, h.Runner})} |
| 44 | if a.StepKey == "verify" || a.StepKey == "publish" { |
| 45 | return p, nil |
| 46 | } |
| 47 | var rows []runnerEvent |
| 48 | if err = s.execution.gormTx(ctx, raw).Where("assignment_id=? AND kind=?", a.ID, "timeline_ready").Find(&rows).Error; err != nil { |
| 49 | return p, storeError(err) |
| 50 | } |
| 51 | bodies := make([]json.RawMessage, 0, len(rows)) |
| 52 | for _, row := range rows { |
| 53 | bodies = append(bodies, json.RawMessage(row.Body)) |
| 54 | } |
| 55 | return runnerproto.ObservedProvenance(bodies, p, scope, h, runnerproto.ReportErrors{Forbidden: ErrForbidden, Conflict: ErrConflict}) |
| 56 | } |
| 57 | |
| 58 | func (s *Store) supervisedExecutorEvidence(ctx context.Context, raw *sql.Tx, scope team.Scope, h team.Handle, spec team.ArtifactSpec) (team.Provenance, error) { |
| 59 | a, value, err := s.supervisedAssignment(ctx, raw, scope, h) |
| 60 | if err != nil { |
| 61 | return team.Provenance{}, err |
| 62 | } |
| 63 | var brief RunnerBrief |
| 64 | if json.Unmarshal([]byte(a.Brief), &brief) != nil { |
| 65 | return team.Provenance{}, ErrInvalid |
| 66 | } |
| 67 | action := brief.Step.Action |
| 68 | if action == "" { |
| 69 | action = a.StepKey |
| 70 | if action == "verify" { |
| 71 | action = "checks" |
| 72 | } |
| 73 | } |
| 74 | var iso team.IsolationSnapshot |
| 75 | switch spec.Role { |
| 76 | case team.RoleBaseResult, team.RoleCandidateResult: |
| 77 | var r team.CheckResult |
| 78 | if json.Unmarshal(spec.Payload, &r) != nil { |
| 79 | return team.Provenance{}, ErrInvalid |
| 80 | } |
| 81 | iso = r.Isolation |
| 82 | if (spec.Role == team.RoleBaseResult && action != "reproduce" && action != "scope") || (spec.Role == team.RoleCandidateResult && action != "checks") { |
| 83 | return team.Provenance{}, ErrForbidden |
| 84 | } |
| 85 | case team.RoleRender: |
| 86 | var r team.RenderResult |
| 87 | if json.Unmarshal(spec.Payload, &r) != nil { |
| 88 | return team.Provenance{}, ErrInvalid |
| 89 | } |
| 90 | tx := s.execution.gormTx(ctx, raw) |
| 91 | var capability string |
| 92 | if err := tx.Raw("SELECT capability FROM steps WHERE id=(SELECT step_id FROM attempts WHERE id=?)", h.Attempt).Scan(&capability).Error; err != nil { |
| 93 | return team.Provenance{}, err |
| 94 | } |
| 95 | if capability != string(team.CapVerify) && capability != string(team.CapImplement) { |
| 96 | return team.Provenance{}, ErrForbidden |
| 97 | } |
| 98 | var brief RunnerBrief |
| 99 | if json.Unmarshal([]byte(a.Brief), &brief) != nil || brief.RenderRecipe == nil { |
| 100 | return team.Provenance{}, ErrForbidden |
| 101 | } |
| 102 | recipe, err := json.Marshal(brief.RenderRecipe) |
| 103 | if err != nil || team.ContentDigest(recipe) != r.RecipeHash { |
| 104 | return team.Provenance{}, ErrForbidden |
| 105 | } |
| 106 | for _, hash := range []string{r.DiffHash, r.RenderHash} { |
| 107 | var blob artifactBlob |
| 108 | if err := tx.First(&blob, "team_id=? AND job_id=? AND digest=?", scope.Account, scope.Job, hash).Error; err != nil { |
| 109 | return team.Provenance{}, storeError(err) |
| 110 | } |
| 111 | if len(blob.Bytes) == 0 || blobDigest(blob.Bytes) != hash { |
| 112 | return team.Provenance{}, ErrForbidden |
| 113 | } |
| 114 | var upload artifactBlobUpload |
| 115 | if err := tx.First(&upload, "attempt_id=? AND digest=?", h.Attempt, hash).Error; err != nil { |
| 116 | return team.Provenance{}, storeError(err) |
| 117 | } |
| 118 | } |
| 119 | iso = r.Isolation |
| 120 | case team.RoleRepositoryChecks: |
| 121 | var r team.RepositoryCheckResult |
| 122 | if action != "checks" || json.Unmarshal(spec.Payload, &r) != nil { |
| 123 | return team.Provenance{}, ErrInvalid |
| 124 | } |
| 125 | iso = r.Isolation |
| 126 | default: |
| 127 | return team.Provenance{}, ErrForbidden |
| 128 | } |
| 129 | expectedIsolation := value.Selection.Isolation |
| 130 | if expectedIsolation.RunnerTainted { |
| 131 | expectedIsolation.Status = "unconfined" |
| 132 | } |
| 133 | if !executorReportIsolation(iso, expectedIsolation) { |
| 134 | return team.Provenance{}, ErrForbidden |
| 135 | } |
| 136 | return team.Provenance{Producer: "equipo-checks", Provider: "executor", Context: h.Runner, ConnectionID: value.Selection.ConnectionID, Isolation: value.Selection.Isolation}, nil |
| 137 | } |
| 138 | |
| 139 | func executorReportIsolation(have, need team.IsolationSnapshot) bool { |
| 140 | return team.IsolationContains(have, need) |
| 141 | } |
| 142 | |