Skip to content

Commit

Permalink
Use API's logRanges to retrieve artifacts
Browse files Browse the repository at this point in the history
Signed-off-by: Lily Sturmann <lsturman@redhat.com>
  • Loading branch information
lkatalin committed Feb 22, 2022
1 parent 25b2ac8 commit 9356473
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
14 changes: 10 additions & 4 deletions cmd/rekor-cli/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,28 @@ var getCmd = &cobra.Command{
}
}

// Note: this UUID may be an EntryID
if uuid != "" {
params := entries.NewGetLogEntryByUUIDParams()
params.SetTimeout(viper.GetDuration("timeout"))

params.EntryUUID, err = sharding.GetUUIDFromIDString(uuid)
// NOTE: This undoes the change that let people pass in longer UUIDs without
// trouble even if their client is old, a.k.a. it will be able to use the TreeID
// (if present) for routing in the GetLogEntryByUUIDHandler
params.EntryUUID = uuid

resp, err := rekorClient.Entries.GetLogEntryByUUID(params)
if err != nil {
return nil, fmt.Errorf("unable to parse uuid: %w", err)
return nil, err
}

resp, err := rekorClient.Entries.GetLogEntryByUUID(params)
u, err := sharding.GetUUIDFromIDString(params.EntryUUID)
if err != nil {
return nil, err
}

for k, entry := range resp.Payload {
if k != params.EntryUUID {
if k != u {
continue
}

Expand Down
31 changes: 26 additions & 5 deletions pkg/api/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
"github.com/go-openapi/runtime/middleware"
Expand Down Expand Up @@ -117,9 +118,11 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
// GetLogEntryAndProofByIndexHandler returns the entry and inclusion proof for a specified log index
func GetLogEntryByIndexHandler(params entries.GetLogEntryByIndexParams) middleware.Responder {
ctx := params.HTTPRequest.Context()
tc := NewTrillianClient(ctx)
tid, resolvedIndex := api.logRanges.ResolveVirtualIndex(int(params.LogIndex))
tc := NewTrillianClientFromTreeID(ctx, tid)
log.RequestIDLogger(params.HTTPRequest).Debugf("Retrieving resolved index %v from TreeID %v", resolvedIndex, tid)

resp := tc.getLeafAndProofByIndex(params.LogIndex)
resp := tc.getLeafAndProofByIndex(resolvedIndex)
switch resp.status {
case codes.OK:
case codes.NotFound, codes.OutOfRange, codes.InvalidArgument:
Expand Down Expand Up @@ -268,12 +271,30 @@ func getEntryURL(locationURL url.URL, uuid string) strfmt.URI {
func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware.Responder {
ctx := params.HTTPRequest.Context()

entryUUID, err := sharding.GetUUIDFromIDString(params.EntryUUID)
uuid, err := sharding.GetUUIDFromIDString(params.EntryUUID)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, "")
}
hashValue, _ := hex.DecodeString(entryUUID)
tc := NewTrillianClient(params.HTTPRequest.Context())
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
// 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.ActiveIndex()
} else {
return handleRekorAPIError(params, http.StatusBadRequest, err, "")
}
} else {
tid, err = strconv.ParseInt(tidString, 16, 64)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, "")
}
}
hashValue, _ := hex.DecodeString(uuid)

tc := NewTrillianClientFromTreeID(params.HTTPRequest.Context(), tid)
log.RequestIDLogger(params.HTTPRequest).Debugf("Retrieving UUID %v from TreeID %v", uuid, tid)

resp := tc.getLeafAndProofByHash(hashValue)
switch resp.status {
Expand Down
8 changes: 8 additions & 0 deletions pkg/api/trillian_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func NewTrillianClient(ctx context.Context) TrillianClient {
}
}

func NewTrillianClientFromTreeID(ctx context.Context, tid int64) TrillianClient {
return TrillianClient{
client: api.logClient,
logID: tid,
context: ctx,
}
}

type Response struct {
status codes.Code
err error
Expand Down

0 comments on commit 9356473

Please sign in to comment.