| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func TestPublishDescriptionProvenance(t *testing.T) { |
| 11 | g := newGitEnv(t) |
| 12 | g.build() |
| 13 | g.review() |
| 14 | g.verify(true) |
| 15 | h := g.claimStep("publish") |
| 16 | p, err := g.store.Publication(g.ctx, g.scope, h) |
| 17 | must(t, err) |
| 18 | if p.DescriptionHash != hashBytes([]byte(p.Description)) || !strings.Contains(p.Description, "unconfined") || !strings.Contains(p.Description, "repository_checks") || !strings.Contains(p.Description, "provider_version") { |
| 19 | t.Fatal("description missing persisted evidence", p.Description) |
| 20 | } |
| 21 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(p), Reserve: Usage{Calls: 1}}) |
| 22 | must(t, err) |
| 23 | ticket, err := g.store.AdmitOnce(g.ctx, g.scope, h, id, "publish-admit") |
| 24 | must(t, err) |
| 25 | retry, err := g.store.AdmitOnce(g.ctx, g.scope, h, id, "publish-admit") |
| 26 | must(t, err) |
| 27 | if string(mustJSON(ticket)) != string(mustJSON(retry)) || ticket.Scope != g.scope || ticket.Handle != h { |
| 28 | t.Fatal("admission retry changed ticket") |
| 29 | } |
| 30 | var saved Publication |
| 31 | must(t, json.Unmarshal(ticket.Dispatch.Payload, &saved)) |
| 32 | if saved.Description != p.Description { |
| 33 | t.Fatal("description changed at admission") |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestPublishReceiptRequiresDescription(t *testing.T) { |
| 38 | g := newGitEnv(t) |
| 39 | g.build() |
| 40 | g.review() |
| 41 | g.verify(true) |
| 42 | h := g.claimStep("publish") |
| 43 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication()), Reserve: Usage{Calls: 1}}) |
| 44 | must(t, err) |
| 45 | d, err := g.store.Admit(g.ctx, g.scope, id) |
| 46 | must(t, err) |
| 47 | receipt, err := g.forge.Execute(g.ctx, d) |
| 48 | must(t, err) |
| 49 | for _, field := range []string{"description_hash", "agent", "head", "candidate_hash", "qualified_ref"} { |
| 50 | wrong := receipt |
| 51 | wrong.Fields = map[string]string{} |
| 52 | for k, v := range receipt.Fields { |
| 53 | wrong.Fields[k] = v |
| 54 | } |
| 55 | delete(wrong.Fields, field) |
| 56 | if g.store.Record(g.ctx, g.scope, id, wrong) == nil { |
| 57 | t.Fatal("incomplete publication receipt accepted", field) |
| 58 | } |
| 59 | } |
| 60 | must(t, g.store.Record(g.ctx, g.scope, id, receipt)) |
| 61 | receipt.Fields["description_hash"] = strings.Repeat("f", 64) |
| 62 | if err := g.store.Record(g.ctx, g.scope, id, receipt); err == nil { |
| 63 | t.Fatal("changed duplicate description receipt accepted") |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func TestPublishAuthorization(t *testing.T) { |
| 68 | g := newGitEnv(t) |
| 69 | g.build() |
| 70 | g.review() |
| 71 | g.verify(true) |
| 72 | h := g.claimStep("publish") |
| 73 | p, err := g.store.Publication(g.ctx, g.scope, h) |
| 74 | must(t, err) |
| 75 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(p), Reserve: Usage{Calls: 1}}) |
| 76 | must(t, err) |
| 77 | a, err := g.store.AuthorizePublication(g.ctx, g.scope, h, id, "authorize") |
| 78 | must(t, err) |
| 79 | if a.Candidate != g.candidate || a.Publication.Description != p.Description || a.Ticket.Handle != h { |
| 80 | t.Fatal("authorization lost immutable binding") |
| 81 | } |
| 82 | h.Runner = "wrong" |
| 83 | if _, err = g.store.AuthorizePublication(g.ctx, g.scope, h, id, "authorize"); err == nil { |
| 84 | t.Fatal("wrong runner authorized") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestPublishExactEvidence(t *testing.T) { |
| 89 | g := newGitEnv(t) |
| 90 | g.observed.Isolation.Status = "confined" |
| 91 | g.observed.ConnectionID = "confined-on-tainted-runner" |
| 92 | g.build() |
| 93 | g.review() |
| 94 | h := g.claimStep("verify") |
| 95 | _, err := g.register(h, RoleCandidateResult, mustJSON(CheckResult{ReproductionHash: hashBytes(FixtureReproduction), SubjectHash: g.artifact.Hash, CheckDefinitionHash: CheckDefinitionHash, Outcome: CheckPass, TestsRun: 3})) |
| 96 | must(t, err) |
| 97 | checks := g.checks() |
| 98 | checks.ExecutorBinary = "equipo-checks" |
| 99 | checks.ExecutorVersion = "1" |
| 100 | checks.Isolation = g.observed.Isolation |
| 101 | _, err = g.register(h, RoleRepositoryChecks, mustJSON(checks)) |
| 102 | must(t, err) |
| 103 | g.finish(h) |
| 104 | p, err := g.store.Publication(g.ctx, g.scope, g.claimStep("publish")) |
| 105 | must(t, err) |
| 106 | var description PublicationDescription |
| 107 | must(t, json.Unmarshal([]byte(p.Description), &description)) |
| 108 | if description.Label != "unconfined" || description.Isolation.Status != "confined" || !description.Isolation.RunnerTainted { |
| 109 | t.Fatal("runner exposure replaced capability", description) |
| 110 | } |
| 111 | found := false |
| 112 | for _, e := range description.Evidence { |
| 113 | if e.Label != "unconfined" || e.Provenance.ConnectionID != "confined-on-tainted-runner" || e.Provenance.Isolation.Status != "confined" { |
| 114 | t.Fatal("confined connection lost exposure", e) |
| 115 | } |
| 116 | if e.Role == RoleRepositoryChecks { |
| 117 | found = true |
| 118 | if e.ExecutorBinary != "equipo-checks" || e.ExecutorVersion != "1" { |
| 119 | t.Fatal("executor identity missing", e) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | if !found { |
| 124 | t.Fatal("executor evidence missing") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | func TestPublishMissingConnectionProvenance(t *testing.T) { |
| 129 | g := newGitEnv(t) |
| 130 | g.build() |
| 131 | g.review() |
| 132 | g.verify(true) |
| 133 | h := g.claimStep("publish") |
| 134 | _, err := g.store.Publication(g.ctx, g.scope, h) |
| 135 | must(t, err) |
| 136 | _, err = g.store.db.Exec(`UPDATE artifacts SET provenance=json_remove(provenance,'$.connection_id') WHERE id=?`, g.artifact.ID) |
| 137 | must(t, err) |
| 138 | if _, err = g.store.Publication(g.ctx, g.scope, h); err == nil { |
| 139 | t.Fatal("stripped connection provenance published") |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | func TestPublishDescriptionChangedAtAdmit(t *testing.T) { |
| 144 | g := newGitEnv(t) |
| 145 | g.build() |
| 146 | g.review() |
| 147 | g.verify(true) |
| 148 | h := g.claimStep("publish") |
| 149 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(g.publication()), Reserve: Usage{Calls: 1}}) |
| 150 | must(t, err) |
| 151 | _, err = g.store.db.Exec(`UPDATE artifacts SET provenance=json_set(provenance,'$.connection_id','changed') WHERE id=?`, g.artifact.ID) |
| 152 | must(t, err) |
| 153 | if _, err = g.store.AdmitOnce(g.ctx, g.scope, h, id, "publish-admit"); err == nil { |
| 154 | t.Fatal("changed description provenance admitted") |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func PublicationAuthorizationFixture(t *testing.T) (PublicationAuthorization, time.Time) { |
| 159 | g := newGitEnvRepository(t, 40, "amadan.net/example/repo") |
| 160 | g.build() |
| 161 | g.review() |
| 162 | g.verify(true) |
| 163 | h := g.claimStep("publish") |
| 164 | p, err := g.store.Publication(g.ctx, g.scope, h) |
| 165 | must(t, err) |
| 166 | id, err := g.store.Intend(g.ctx, g.scope, h, IntentSpec{Key: "publish", Operation: OpForgePublish, Payload: mustJSON(p), Reserve: Usage{Calls: 1}}) |
| 167 | must(t, err) |
| 168 | a, err := g.store.AuthorizePublication(g.ctx, g.scope, h, id, "cross-package") |
| 169 | must(t, err) |
| 170 | return a, g.clock.Now() |
| 171 | } |
| 172 | |