| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "reflect" |
| 8 | "regexp" |
| 9 | ) |
| 10 | |
| 11 | var publicationAgent = regexp.MustCompile(`^[A-Za-z0-9_-]+~[^\s~@]+@[^\s~@]+$`) |
| 12 | |
| 13 | // CanonicalJSON shares the core payload identity with runner adapters. |
| 14 | func CanonicalJSON(raw []byte) ([]byte, string, error) { return canonical(raw) } |
| 15 | |
| 16 | // ContentDigest hashes immutable artifact and description bytes. |
| 17 | func ContentDigest(raw []byte) string { return hashBytes(raw) } |
| 18 | |
| 19 | // ValidPublicationAgent checks the disclosed agent spelling used in receipts. |
| 20 | func ValidPublicationAgent(agent string) bool { return publicationAgent.MatchString(agent) } |
| 21 | |
| 22 | type PublicationEvidence struct { |
| 23 | Step StepID `json:"step"` |
| 24 | Attempt AttemptID `json:"attempt"` |
| 25 | Role Role `json:"role"` |
| 26 | Hash string `json:"hash"` |
| 27 | Label string `json:"label"` |
| 28 | Provenance Provenance `json:"provenance"` |
| 29 | ExecutorBinary string `json:"executor_binary,omitempty"` |
| 30 | ExecutorVersion string `json:"executor_version,omitempty"` |
| 31 | } |
| 32 | |
| 33 | type PublicationDescription struct { |
| 34 | RenderedApprovals []RenderedApproval `json:"rendered_approvals,omitempty"` |
| 35 | Version int `json:"version"` |
| 36 | Target TargetBinding `json:"target"` |
| 37 | Label string `json:"label"` |
| 38 | Isolation IsolationSnapshot `json:"isolation"` |
| 39 | Evidence []PublicationEvidence `json:"evidence"` |
| 40 | } |
| 41 | |
| 42 | func publicationDescription(ctx context.Context, tx *sql.Tx, j jobRow, isolation IsolationSnapshot) (string, error) { |
| 43 | if !isolation.valid() { |
| 44 | return "", ErrEvidence |
| 45 | } |
| 46 | desc := PublicationDescription{RenderedApprovals: j.Evidence.RenderedApprovals, Version: 1, Target: j.Target, Label: isolation.Status, Isolation: isolation} |
| 47 | if isolation.Mode != "" { |
| 48 | desc.Label = isolation.Mode |
| 49 | } |
| 50 | if isolation.RunnerTainted { |
| 51 | desc.Label = "unconfined" |
| 52 | } |
| 53 | rows, err := tx.QueryContext(ctx, `SELECT `+artifactColumns+` FROM artifacts WHERE job_id=? ORDER BY id`, j.ID) |
| 54 | if err != nil { |
| 55 | return "", err |
| 56 | } |
| 57 | defer rows.Close() |
| 58 | for rows.Next() { |
| 59 | a, err := scanArtifact(rows) |
| 60 | if err != nil { |
| 61 | return "", err |
| 62 | } |
| 63 | if a.Provenance.ConnectionID == "" || a.Provenance.validate() != nil || !a.Provenance.Isolation.valid() { |
| 64 | return "", ErrEvidence |
| 65 | } |
| 66 | e := PublicationEvidence{Step: a.Step, Attempt: a.Attempt, Role: a.Role, Hash: a.Hash, Provenance: a.Provenance, Label: a.Provenance.Isolation.Status} |
| 67 | if a.Provenance.Isolation.Mode != "" { |
| 68 | e.Label = a.Provenance.Isolation.Mode |
| 69 | } |
| 70 | if isolation.RunnerTainted || a.Provenance.Isolation.RunnerTainted { |
| 71 | e.Label = "unconfined" |
| 72 | } |
| 73 | if a.Role == RoleBaseResult || a.Role == RoleCandidateResult || a.Role == RoleRepositoryChecks || a.Role == RoleRender { |
| 74 | var executor struct { |
| 75 | Binary string `json:"executor_binary"` |
| 76 | Version string `json:"executor_version"` |
| 77 | } |
| 78 | if json.Unmarshal(a.Payload, &executor) != nil { |
| 79 | return "", ErrEvidence |
| 80 | } |
| 81 | e.ExecutorBinary = executor.Binary |
| 82 | e.ExecutorVersion = executor.Version |
| 83 | } |
| 84 | desc.Evidence = append(desc.Evidence, e) |
| 85 | } |
| 86 | if err = rows.Err(); err != nil { |
| 87 | return "", err |
| 88 | } |
| 89 | raw, err := json.Marshal(desc) |
| 90 | if err != nil { |
| 91 | return "", err |
| 92 | } |
| 93 | return string(raw), nil |
| 94 | } |
| 95 | |
| 96 | func (s *Store) Publication(ctx context.Context, scope Scope, h Handle) (Publication, error) { |
| 97 | var p Publication |
| 98 | err := s.read(ctx, func(tx *sql.Tx) error { |
| 99 | hd, err := current(ctx, tx, scope, h, s.clock.Now()) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | if hd.job.Policy.Version != 2 || hd.step.Capability != CapPublish { |
| 104 | return ErrScope |
| 105 | } |
| 106 | rep, err := s.evaluateEvidence(ctx, tx, hd.job, true) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | if !rep.Satisfied { |
| 111 | return ErrEvidence |
| 112 | } |
| 113 | a, err := latestArtifact(ctx, tx, scope.Job, RoleCandidate) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | c, err := gitCandidate(a, hd.job) |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | observed, err := s.observedProvenance(ctx, tx, scope, h) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | description, err := publicationDescription(ctx, tx, hd.job, observed.Isolation) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | p = Publication{Repository: hd.job.Target.Repository, Ref: hd.job.Target.Ref, Head: c.Commit, CandidateHash: a.Hash, Isolation: observed.Isolation, Description: description, DescriptionHash: hashBytes([]byte(description))} |
| 130 | return s.checkPublication(ctx, tx, hd.job, mustPublicationJSON(p)) |
| 131 | }) |
| 132 | return p, err |
| 133 | } |
| 134 | func mustPublicationJSON(p Publication) []byte { raw, _ := json.Marshal(p); return raw } |
| 135 | |
| 136 | func ValidatePublicationReceipt(payload []byte, receipt Receipt) error { |
| 137 | if receipt.Outcome != OutcomeSuccess { |
| 138 | return nil |
| 139 | } |
| 140 | var p Publication |
| 141 | if strictJSON(payload, &p) != nil || p.Description == "" || p.DescriptionHash != hashBytes([]byte(p.Description)) { |
| 142 | return ErrEvidence |
| 143 | } |
| 144 | for key, want := range map[string]string{"repository": p.Repository, "ref": p.Ref, "qualified_ref": "refs/heads/" + p.Ref, "head": p.Head, "candidate_hash": p.CandidateHash, "description_hash": p.DescriptionHash} { |
| 145 | if want == "" || receipt.Fields[key] != want { |
| 146 | return ErrEvidence |
| 147 | } |
| 148 | } |
| 149 | if !ValidPublicationAgent(receipt.Fields["agent"]) { |
| 150 | return ErrEvidence |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | type PublicationAuthorization struct { |
| 156 | Ticket AdmissionTicket `json:"ticket"` |
| 157 | Target TargetBinding `json:"target"` |
| 158 | Candidate GitCandidate `json:"candidate"` |
| 159 | Publication Publication `json:"publication"` |
| 160 | } |
| 161 | |
| 162 | func (s *Store) AuthorizePublication(ctx context.Context, scope Scope, h Handle, id ActionID, requestID string) (PublicationAuthorization, error) { |
| 163 | var a PublicationAuthorization |
| 164 | err := s.Atomic(ctx, func(ctx context.Context, tx *sql.Tx) error { |
| 165 | p, err := s.Publication(ctx, scope, h) |
| 166 | if err != nil { |
| 167 | return err |
| 168 | } |
| 169 | ticket, err := s.AdmitOnce(ctx, scope, h, id, requestID) |
| 170 | if err != nil { |
| 171 | return err |
| 172 | } |
| 173 | var saved Publication |
| 174 | if ticket.Dispatch.Operation != OpForgePublish || strictJSON(ticket.Dispatch.Payload, &saved) != nil || string(mustPublicationJSON(saved)) != string(mustPublicationJSON(p)) { |
| 175 | return ErrEvidence |
| 176 | } |
| 177 | j, err := loadJob(ctx, tx, scope) |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | art, err := latestArtifact(ctx, tx, scope.Job, RoleCandidate) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | candidate, err := gitCandidate(art, j) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | a = PublicationAuthorization{Ticket: ticket, Target: j.Target, Candidate: candidate, Publication: p} |
| 190 | return nil |
| 191 | }) |
| 192 | return a, err |
| 193 | } |
| 194 | |
| 195 | func publicationDescriptionContains(have, need string) bool { |
| 196 | var a, b PublicationDescription |
| 197 | if strictJSON([]byte(have), &a) != nil || strictJSON([]byte(need), &b) != nil || len(a.Evidence) != len(b.Evidence) { |
| 198 | return false |
| 199 | } |
| 200 | if !isolationContains(a.Isolation, b.Isolation) || !publicationLabelContains(a.Label, b.Label) { |
| 201 | return false |
| 202 | } |
| 203 | a.Isolation, a.Label = b.Isolation, b.Label |
| 204 | for i := range a.Evidence { |
| 205 | if !isolationContains(a.Evidence[i].Provenance.Isolation, b.Evidence[i].Provenance.Isolation) || !publicationLabelContains(a.Evidence[i].Label, b.Evidence[i].Label) { |
| 206 | return false |
| 207 | } |
| 208 | a.Evidence[i].Provenance.Isolation = b.Evidence[i].Provenance.Isolation |
| 209 | a.Evidence[i].Label = b.Evidence[i].Label |
| 210 | } |
| 211 | return reflect.DeepEqual(a, b) |
| 212 | } |
| 213 | |
| 214 | func publicationLabelContains(have, need string) bool { |
| 215 | switch need { |
| 216 | case "unconfined": |
| 217 | return have == need |
| 218 | case "owner mode": |
| 219 | return have == need || have == "unconfined" |
| 220 | default: |
| 221 | return true |
| 222 | } |
| 223 | } |
| 224 | |