forked from sigstore/gitsign
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: go-git based signer implementation
- Loading branch information
Showing
8 changed files
with
193 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package e2e | ||
|
||
import ( | ||
"context" | ||
"crypto/x509" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-git/go-billy/v5/memfs" | ||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/go-git/go-git/v5/storage/memory" | ||
"github.com/sigstore/gitsign/internal/fulcio/fulcioroots" | ||
"github.com/sigstore/gitsign/internal/git/gittest" | ||
"github.com/sigstore/gitsign/pkg/fulcio" | ||
gsgit "github.com/sigstore/gitsign/pkg/git" | ||
"github.com/sigstore/gitsign/pkg/gitsign" | ||
"github.com/sigstore/gitsign/pkg/rekor" | ||
"github.com/sigstore/sigstore/pkg/oauth" | ||
"github.com/sigstore/sigstore/pkg/oauthflow" | ||
"github.com/sigstore/sigstore/pkg/tuf" | ||
) | ||
|
||
func TestSign(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
flow := &oauthflow.InteractiveIDTokenGetter{ | ||
HTMLPage: oauth.InteractiveSuccessHTML, | ||
} | ||
fulcio, err := fulcio.NewClient("https://fulcio.sigstore.dev", fulcio.OIDCOptions{ | ||
ClientID: "sigstore", | ||
Issuer: "https://oauth2.sigstore.dev/auth", | ||
TokenGetter: flow, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
rekor, err := rekor.NewWithOptions(ctx, "https://rekor.sigstore.dev") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
signer, err := gitsign.NewSigner(ctx, fulcio, rekor) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Make a commit + sign it | ||
storage := memory.NewStorage() | ||
repo, err := git.Init(storage, memfs.New()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
w, err := repo.Worktree() | ||
if err != nil { | ||
panic(err) | ||
} | ||
sha, err := w.Commit("example commit", &git.CommitOptions{ | ||
Author: &object.Signature{ | ||
Name: "John Doe", | ||
Email: "[email protected]", | ||
When: time.UnixMicro(1234567890).UTC(), | ||
}, | ||
Signer: signer, | ||
AllowEmptyCommits: true, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
commit, err := repo.CommitObject(sha) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
body := gittest.MarshalCommitBody(t, commit) | ||
sig := []byte(commit.PGPSignature) | ||
|
||
// Verify the commit | ||
// TODO: export verifier functions? | ||
tuf.Initialize(ctx, tuf.DefaultRemoteRoot, nil) | ||
root, intermediate, err := fulcioroots.New(x509.NewCertPool(), fulcioroots.FromTUF(ctx)) | ||
if err != nil { | ||
t.Fatalf("error getting certificate root: %v", err) | ||
} | ||
verifier, err := gsgit.NewCertVerifier(gsgit.WithRootPool(root), gsgit.WithIntermediatePool(intermediate)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
summary, err := gsgit.Verify(ctx, verifier, rekor, body, sig, true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Log(summary.LogEntry.LogID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,7 +120,7 @@ func TestGetCert(t *testing.T) { | |
key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
email := "[email protected]" | ||
|
||
client := &Client{ | ||
client := &ClientImpl{ | ||
// fakeFulcio is what will be doing the validation. | ||
LegacyClient: &fakeFulcio{ | ||
signer: key, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Package gitsign provides a signer for signing git commits and tags via Gitsign keyless flow. | ||
package gitsign | ||
|
||
import ( | ||
"context" | ||
"crypto" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"fmt" | ||
"io" | ||
|
||
fulciointernal "github.com/sigstore/gitsign/internal/fulcio" | ||
"github.com/sigstore/gitsign/internal/signature" | ||
"github.com/sigstore/gitsign/pkg/fulcio" | ||
"github.com/sigstore/gitsign/pkg/rekor" | ||
) | ||
|
||
type PrivateKeySigner interface { | ||
crypto.PrivateKey | ||
crypto.Signer | ||
} | ||
|
||
type fulcioSigner struct { | ||
ctx context.Context | ||
key PrivateKeySigner | ||
fulcio fulcio.Client | ||
rekor rekor.Writer | ||
} | ||
|
||
func NewSigner(ctx context.Context, fulcio fulcio.Client, rekor rekor.Writer) (*fulcioSigner, error) { | ||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return nil, fmt.Errorf("generating private key: %w", err) | ||
} | ||
return &fulcioSigner{ | ||
ctx: ctx, | ||
key: priv, | ||
fulcio: fulcio, | ||
rekor: rekor, | ||
}, nil | ||
} | ||
|
||
func (f *fulcioSigner) Sign(message io.Reader) ([]byte, error) { | ||
cert, err := f.fulcio.GetCert(f.key) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting fulcio cert: %w", err) | ||
} | ||
|
||
id := &fulciointernal.Identity{ | ||
PrivateKey: f.key, | ||
CertPEM: cert.CertPEM, | ||
ChainPEM: cert.ChainPEM, | ||
} | ||
|
||
body, err := io.ReadAll(message) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading message: %w", err) | ||
} | ||
|
||
resp, err := signature.Sign(f.ctx, id, body, signature.SignOptions{ | ||
Rekor: f.rekor, | ||
|
||
// TODO: make SignOptions configurable? | ||
Armor: true, | ||
Detached: true, | ||
IncludeCerts: -2, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp.Signature, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package gitsign |