Skip to content

Commit

Permalink
Wire up into main
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Aug 20, 2024
1 parent 2879710 commit da83c59
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
4 changes: 2 additions & 2 deletions cmd/example-gcp/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ type Signer struct {
kmsKeyName string
}

// New creates a signer which uses an Ed25519 key in GCP KMS.
// NewKMSSigner creates a signer which uses an Ed25519 key in GCP KMS.
// See https://cloud.google.com/kms/docs/algorithms#elliptic_curve_signing_algorithms
//
// kmsKeyName is the GCP KMS name of the key to be used.
// noteKeyName is the value used as the signer name in the note signature.
func New(ctx context.Context, c *kms.KeyManagementClient, kmsKeyName, noteKeyName string) (*Signer, error) {
func NewKMSSigner(ctx context.Context, c *kms.KeyManagementClient, kmsKeyName, noteKeyName string) (*Signer, error) {
s := &Signer{}

s.client = c
Expand Down
40 changes: 29 additions & 11 deletions cmd/example-gcp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,40 @@ import (
"strings"
"time"

kms "cloud.google.com/go/kms/apiv1"
tessera "github.com/transparency-dev/trillian-tessera"
"github.com/transparency-dev/trillian-tessera/storage/gcp"
"golang.org/x/mod/sumdb/note"
"k8s.io/klog/v2"
)

var (
bucket = flag.String("bucket", "", "Bucket to use for storing log")
listen = flag.String("listen", ":2024", "Address:port to listen on")
project = flag.String("project", os.Getenv("GOOGLE_CLOUD_PROJECT"), "GCP Project, take from env if unset")
spanner = flag.String("spanner", "", "Spanner resource URI ('projects/.../...')")
signer = flag.String("signer", "", "Path to file containing log private key")
bucket = flag.String("bucket", "", "Bucket to use for storing log")
listen = flag.String("listen", ":2024", "Address:port to listen on")
project = flag.String("project", os.Getenv("GOOGLE_CLOUD_PROJECT"), "GCP Project, take from env if unset")
spanner = flag.String("spanner", "", "Spanner resource URI ('projects/.../...')")
kmsKeyName = flag.String("kms_key", "", "GCP KMS key name for signing checkpoints")
origin = flag.String("origin", "", "Log origin string")
)

func main() {
klog.InitFlags(nil)
flag.Parse()
ctx := context.Background()

if *origin == "" {
klog.Exit("Must supply --origin")
}

gcpCfg := gcp.Config{
ProjectID: *project,
Bucket: *bucket,
Spanner: *spanner,
}
signer, verifier, kmsClose := signerFromFlags(ctx)
defer kmsClose()

Check failure on line 62 in cmd/example-gcp/main.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
storage, err := gcp.New(ctx, gcpCfg,
tessera.WithCheckpointSignerVerifier(signerFromFlags(), nil),
tessera.WithCheckpointSignerVerifier(signer, verifier),
tessera.WithBatching(1024, time.Second),
tessera.WithPushback(10*4096),
)
Expand Down Expand Up @@ -104,14 +112,24 @@ func main() {
}
}

func signerFromFlags() note.Signer {
raw, err := os.ReadFile(*signer)
// signerFromFlags creates and returns a new KMSSigner from the flags, along with a close func.
func signerFromFlags(ctx context.Context) (note.Signer, note.Verifier, func() error) {
kmClient, err := kms.NewKeyManagementClient(ctx)
if err != nil {
klog.Exitf("Failed to read secret key file %q: %v", *signer, err)
klog.Fatalf("Failed to create KeyManagementClient: %v", err)
}
signer, err := note.NewSigner(string(raw))
signer, err := NewKMSSigner(ctx, kmClient, *kmsKeyName, *origin)
if err != nil {
klog.Exitf("Failed to create new signer: %v", err)
}
return signer
vRaw, err := VerifierKeyString(ctx, kmClient, *kmsKeyName, *origin)
if err != nil {
klog.Exitf("Failed to create verifier string: %v", err)
}
verifier, err := note.NewVerifier(vRaw)
if err != nil {
klog.Exitf("Failed to create verifier from %q: %v", vRaw, err)
}

return signer, verifier, kmClient.Close
}

0 comments on commit da83c59

Please sign in to comment.