| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "crypto/rand" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "the-valley/note" |
| 11 | ) |
| 12 | |
| 13 | // An attestation ref takes added notes, and nothing else: every note it |
| 14 | // holds stays at its path, byte for byte, the tree it moves to is |
| 15 | // everything a created ref must be, and no push deletes it. Each update |
| 16 | // here is read from real git objects, as the hook reads a push. |
| 17 | func TestAnAttestationRefOnlyTakesAddedNotes(t *testing.T) { |
| 18 | dir := t.TempDir() |
| 19 | gitIn(t, dir, "", "init", "--quiet", "--bare", dir) |
| 20 | t.Setenv("GIT_DIR", dir) |
| 21 | |
| 22 | type signer struct { |
| 23 | name string |
| 24 | priv ed25519.PrivateKey |
| 25 | key note.VerifierKey |
| 26 | } |
| 27 | newSigner := func(name string) signer { |
| 28 | pub, priv, err := ed25519.GenerateKey(rand.Reader) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | return signer{name, priv, note.NewVerifierKey(name, pub)} |
| 33 | } |
| 34 | worker, other := newSigner("worker.example.org/attestations"), newSigner("other.example.org/attestations") |
| 35 | digest := strings.Repeat("d", 64) |
| 36 | ref := fmt.Sprintf("refs/the-valley/attestations/%s/%08x", digest, worker.key.Hash) |
| 37 | signed := func(s signer, check, result string) string { |
| 38 | text := note.StatementType + "\nsubject.primary valley-tree-v1\nsubject.digest.valley-tree-v1 " + digest + |
| 39 | "\npredicate.check.name " + check + "\npredicate.result " + result + "\n" |
| 40 | n, err := note.Sign([]byte(text), s.name, s.priv) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | return gitIn(t, dir, string(n), "hash-object", "-w", "--stdin") |
| 45 | } |
| 46 | // tree writes an attestation's tree: one directory per check holding |
| 47 | // its statement.note, and any further entries given as git mktree |
| 48 | // reads them. |
| 49 | tree := func(notes map[string]string, extra ...string) string { |
| 50 | var lines []string |
| 51 | for check, blob := range notes { |
| 52 | leaf := mktree(t, dir, 1, "100644 blob "+blob, "statement.note") |
| 53 | lines = append(lines, "040000 tree "+leaf+"\t"+check) |
| 54 | } |
| 55 | return gitIn(t, dir, strings.Join(append(lines, extra...), "\n")+"\n", "mktree") |
| 56 | } |
| 57 | one, two := signed(worker, "one", "passed"), signed(worker, "two", "passed") |
| 58 | held := tree(map[string]string{"one": one}) |
| 59 | empty := gitIn(t, dir, "", "mktree") |
| 60 | |
| 61 | p := policy{project: "p", repo: gitRepository{}, verifiers: []note.VerifierKey{worker.key, other.key}} |
| 62 | const zero = "0000000000000000000000000000000000000000" |
| 63 | for _, c := range []struct { |
| 64 | what, new string |
| 65 | says string |
| 66 | }{ |
| 67 | {"a note added for another check", tree(map[string]string{"one": one, "two": two}), ""}, |
| 68 | {"a note changed", tree(map[string]string{"one": signed(worker, "one", "failed"), "two": two}), "changes the note at one/statement.note"}, |
| 69 | {"a note dropped", tree(map[string]string{"two": two}), "drops the note at one/statement.note"}, |
| 70 | {"a note added under another key", tree(map[string]string{"one": one, "two": signed(other, "two", "passed")}), "the note at two/statement.note carries no signature under the key hash"}, |
| 71 | {"another tree adding no note", tree(map[string]string{"one": one}, "040000 tree "+empty+"\tnothing"), "adds no note"}, |
| 72 | {"a note added past the bounds", tree(map[string]string{"one": one}, "040000 tree "+mktree(t, dir, maxEntriesInTree+1, "100644 blob "+two, "n")+"\tmany"), "more than 64 entries"}, |
| 73 | {"a delete", zero, "never removed"}, |
| 74 | } { |
| 75 | got := p.decide("operator", update{old: held, new: c.new, ref: ref}) |
| 76 | if (got == "") != (c.says == "") || !strings.Contains(got, c.says) { |
| 77 | t.Errorf("%s: got %q, want one saying %q", c.what, got, c.says) |
| 78 | } |
| 79 | } |
| 80 | } |