Skip to content

Commit

Permalink
Use active tree on server startup
Browse files Browse the repository at this point in the history
Signed-off-by: Lily Sturmann <lsturman@redhat.com>
  • Loading branch information
lkatalin committed Mar 28, 2022
1 parent befbcc0 commit 2b7a2d6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion cmd/rekor-server/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ var serveCmd = &cobra.Command{
log.Logger.Fatalf("unable get sharding details from sharding config: %v", err)
}

api.ConfigureAPI(ranges)
api.ConfigureAPI(ranges, treeID)
server.ConfigureAPI()

http.Handle("/metrics", promhttp.Handler())
Expand Down
19 changes: 10 additions & 9 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type API struct {
certChainPem string // PEM encoded timestamping cert chain
}

func NewAPI(ranges sharding.LogRanges) (*API, error) {
func NewAPI(ranges sharding.LogRanges, treeID uint) (*API, error) {
logRPCServer := fmt.Sprintf("%s:%d",
viper.GetString("trillian_log_server.address"),
viper.GetUint("trillian_log_server.port"))
Expand All @@ -78,16 +78,17 @@ func NewAPI(ranges sharding.LogRanges) (*API, error) {
logAdminClient := trillian.NewTrillianAdminClient(tConn)
logClient := trillian.NewTrillianLogClient(tConn)

tLogID := viper.GetInt64("trillian_log_server.tlog_id")
if tLogID == 0 {
log.Logger.Info("No tree ID specified, attempting to intitialize one")
tid := int64(treeID)
if tid == 0 {
log.Logger.Info("No tree ID specified, attempting to create a new tree")
t, err := createAndInitTree(ctx, logAdminClient, logClient)
if err != nil {
return nil, errors.Wrap(err, "create and init tree")
}
tLogID = t.TreeId
tid = t.TreeId
}
ranges.SetActive(tLogID)
log.Logger.Infof("Starting Rekor server with active tree %v", tid)
ranges.SetActive(tid)

rekorSigner, err := signer.New(ctx, viper.GetString("rekor_server.signer"))
if err != nil {
Expand Down Expand Up @@ -140,7 +141,7 @@ func NewAPI(ranges sharding.LogRanges) (*API, error) {
return &API{
// Transparency Log Stuff
logClient: logClient,
logID: tLogID,
logID: tid,
logRanges: ranges,
// Signing/verifying fields
pubkey: string(pubkey),
Expand All @@ -159,11 +160,11 @@ var (
storageClient storage.AttestationStorage
)

func ConfigureAPI(ranges sharding.LogRanges) {
func ConfigureAPI(ranges sharding.LogRanges, treeID uint) {
cfg := radix.PoolConfig{}
var err error

api, err = NewAPI(ranges)
api, err = NewAPI(ranges, treeID)
if err != nil {
log.Logger.Panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware
var tid int64
tidString, err := sharding.GetTreeIDFromIDString(params.EntryUUID)
if err != nil {
// If EntryID is plain UUID, assume no sharding and use ActiveIndex. The ActiveIndex
// If EntryID is plain UUID, assume no sharding and use ActiveTreeID. The ActiveTreeID
// will == the tlog_id if a tlog_id is passed in at server startup.
if err.Error() == "cannot get treeID from plain UUID" {
tid = api.logRanges.ActiveTreeID()
Expand Down
15 changes: 0 additions & 15 deletions pkg/api/trillian_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,21 +320,6 @@ func (t *TrillianClient) getConsistencyProof(firstSize, lastSize int64) *Respons
}

func createAndInitTree(ctx context.Context, adminClient trillian.TrillianAdminClient, logClient trillian.TrillianLogClient) (*trillian.Tree, error) {
// First look for and use an existing tree
trees, err := adminClient.ListTrees(ctx, &trillian.ListTreesRequest{})
if err != nil {
return nil, errors.Wrap(err, "list trees")
}

for _, t := range trees.Tree {
if t.TreeType == trillian.TreeType_LOG {
log.Logger.Infof("Found existing tree with ID: %v", t.TreeId)
return t, nil
}
}

log.Logger.Infof("No existing tree found, attempting to create a new one")
// Otherwise create and initialize one
t, err := adminClient.CreateTree(ctx, &trillian.CreateTreeRequest{
Tree: &trillian.Tree{
TreeType: trillian.TreeType_LOG,
Expand Down
4 changes: 3 additions & 1 deletion pkg/sharding/ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package sharding

import (
"errors"
"fmt"
"io/ioutil"
"strings"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
"github.com/sigstore/rekor/pkg/log"
)

type LogRanges struct {
Expand All @@ -38,6 +39,7 @@ type LogRange struct {

func NewLogRanges(path string, treeID uint) (LogRanges, error) {
if path == "" {
log.Logger.Info("No config file specified, skipping init of logRange map")
return LogRanges{}, nil
}
if treeID == 0 {
Expand Down

0 comments on commit 2b7a2d6

Please sign in to comment.