98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package worktree
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"encoding/pem"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-git/go-billy/v6/memfs"
|
|
"github.com/go-git/go-git/v6"
|
|
"github.com/go-git/go-git/v6/plumbing"
|
|
"github.com/go-git/go-git/v6/plumbing/object"
|
|
"github.com/go-git/go-git/v6/storage/memory"
|
|
"github.com/hiddeco/sshsig"
|
|
"golang.org/x/crypto/ssh"
|
|
)
|
|
|
|
// Commits made in the task container carry no identity and no signature; the
|
|
// push must publish them as the connection's bot, signed, without touching
|
|
// what is already on the forge.
|
|
func TestAttestSignsOnlyUnpublishedCommits(t *testing.T) {
|
|
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
block, err := ssh.MarshalPrivateKey(priv, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
signer, err := NewSSHSigner(pem.EncodeToMemory(block))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pub, err := ssh.NewPublicKey(priv.Public())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
r, err := git.Init(memory.NewStorage(), git.WithWorkTree(memfs.New()))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wt, _ := r.Worktree()
|
|
commit := func(name string) plumbing.Hash {
|
|
f, _ := wt.Filesystem().Create(name)
|
|
_, _ = f.Write([]byte(name))
|
|
_ = f.Close()
|
|
_, _ = wt.Add(name)
|
|
// The explicit signer stands in for whatever the machine's git config asks for.
|
|
h, err := wt.Commit(name, &git.CommitOptions{Author: &object.Signature{Name: "someone", Email: "x@y", When: time.Now()}, Signer: signer})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return h
|
|
}
|
|
base := commit("base")
|
|
if err := r.Storer.SetReference(plumbing.NewHashReference(plumbing.NewRemoteReferenceName("origin", "main"), base)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commit("one")
|
|
commit("two")
|
|
head, _ := r.Head()
|
|
ref := head.Name()
|
|
|
|
m := &Manager{BotUser: "archie", BotEmail: "archie@example.com", Signer: signer}
|
|
for range 2 { // a second attest must change nothing
|
|
if err := m.attest(context.Background(), r, ref); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
tip, _ := r.Reference(ref, true)
|
|
c, _ := r.CommitObject(tip.Hash())
|
|
for _, want := range []string{"two", "one"} {
|
|
if c.Message != want || c.Author.Name != "archie" || c.Committer.Email != "archie@example.com" {
|
|
t.Fatalf("commit %q: author %q committer %q", c.Message, c.Author.Name, c.Committer.Email)
|
|
}
|
|
unsigned := &plumbing.MemoryObject{}
|
|
_ = c.EncodeWithoutSignature(unsigned)
|
|
body, _ := unsigned.Reader()
|
|
sig, err := sshsig.Unarmor([]byte(c.Signature))
|
|
if err != nil {
|
|
t.Fatalf("commit %q signature: %v", want, err)
|
|
}
|
|
if err := sshsig.Verify(body, sig, pub, sshsig.HashSHA512, "git"); err != nil {
|
|
t.Fatalf("commit %q does not verify: %v", want, err)
|
|
}
|
|
if c, err = c.Parent(0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if c.Hash != base || c.Author.Name != "someone" {
|
|
t.Fatalf("published commit was rewritten: %s by %s", c.Hash, c.Author.Name)
|
|
}
|
|
}
|