Skip to content

Commit 7db7879

Browse files
authored
fix: use entry uuid uniformly (sigstore#1012)
Signed-off-by: Asra Ali <asraa@google.com>
1 parent a0c78e7 commit 7db7879

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

cmd/rekor-cli/app/get.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,26 +133,17 @@ var getCmd = &cobra.Command{
133133
if uuid != "" {
134134
params := entries.NewGetLogEntryByUUIDParams()
135135
params.SetTimeout(viper.GetDuration("timeout"))
136-
137-
// NOTE: This undoes the change that let people pass in longer UUIDs without
138-
// trouble even if their client is old, a.k.a. it will be able to use the TreeID
139-
// (if present) for routing in the GetLogEntryByUUIDHandler
140136
params.EntryUUID = uuid
141137

142138
resp, err := rekorClient.Entries.GetLogEntryByUUID(params)
143139
if err != nil {
144140
return nil, err
145141
}
146142

147-
u, err := sharding.GetUUIDFromIDString(params.EntryUUID)
148-
if err != nil {
149-
return nil, err
150-
}
151-
152143
var e models.LogEntryAnon
153144
for k, entry := range resp.Payload {
154-
if k != u {
155-
continue
145+
if err := compareEntryUUIDs(params.EntryUUID, k); err != nil {
146+
return nil, err
156147
}
157148

158149
// verify log entry
@@ -169,6 +160,48 @@ var getCmd = &cobra.Command{
169160
}),
170161
}
171162

163+
// TODO: Move this to the verify pkg, but only after sharding cleans
164+
// up it's Trillian client dependency.
165+
func compareEntryUUIDs(requestEntryUUID string, responseEntryUUID string) error {
166+
requestUUID, err := sharding.GetUUIDFromIDString(requestEntryUUID)
167+
if err != nil {
168+
return err
169+
}
170+
responseUUID, err := sharding.GetUUIDFromIDString(responseEntryUUID)
171+
if err != nil {
172+
return err
173+
}
174+
// Compare UUIDs.
175+
if requestUUID != responseUUID {
176+
return fmt.Errorf("unexpected entry returned from rekor server: expected %s, got %s", requestEntryUUID, responseEntryUUID)
177+
}
178+
// If the request contains a Tree ID, then compare that.
179+
requestTreeID, err := sharding.GetTreeIDFromIDString(requestEntryUUID)
180+
if err != nil {
181+
if errors.Is(err, sharding.ErrPlainUUID) {
182+
// The request did not contain a Tree ID, we're good.
183+
return nil
184+
}
185+
// The request had a bad Tree ID, error out.
186+
return err
187+
}
188+
// We requested an entry from a given Tree ID.
189+
responseTreeID, err := sharding.GetTreeIDFromIDString(responseEntryUUID)
190+
if err != nil {
191+
if errors.Is(err, sharding.ErrPlainUUID) {
192+
// The response does not contain a Tree ID, we can only do so much.
193+
// Old rekor instances may not have returned one.
194+
return nil
195+
}
196+
return err
197+
}
198+
// We have Tree IDs. Compare.
199+
if requestTreeID != responseTreeID {
200+
return fmt.Errorf("unexpected entry returned from rekor server: expected %s, got %s", requestEntryUUID, responseEntryUUID)
201+
}
202+
return nil
203+
}
204+
172205
func parseEntry(uuid string, e models.LogEntryAnon) (interface{}, error) {
173206
b, err := base64.StdEncoding.DecodeString(e.Body.(string))
174207
if err != nil {

cmd/rekor-cli/app/verify.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/sigstore/rekor/pkg/generated/client/entries"
3232
"github.com/sigstore/rekor/pkg/generated/models"
3333
"github.com/sigstore/rekor/pkg/log"
34-
"github.com/sigstore/rekor/pkg/sharding"
3534
"github.com/sigstore/rekor/pkg/types"
3635
"github.com/sigstore/rekor/pkg/verify"
3736
)
@@ -160,13 +159,9 @@ var verifyCmd = &cobra.Command{
160159
}
161160

162161
if viper.IsSet("uuid") {
163-
uuid, err := sharding.GetUUIDFromIDString(viper.GetString("uuid"))
164-
if err != nil {
162+
if err := compareEntryUUIDs(viper.GetString("uuid"), o.EntryUUID); err != nil {
165163
return nil, err
166164
}
167-
if uuid != o.EntryUUID {
168-
return nil, fmt.Errorf("unexpected entry returned from rekor server")
169-
}
170165
}
171166

172167
// Get Rekor Pub

pkg/api/entries.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
107107
}
108108

109109
uuid := hex.EncodeToString(leaf.MerkleLeafHash)
110+
treeID := fmt.Sprintf("%x", tid)
111+
entryIDstruct, err := sharding.CreateEntryIDFromParts(treeID, uuid)
112+
if err != nil {
113+
return nil, fmt.Errorf("error creating EntryID from active treeID %v and uuid %v: %w", treeID, uuid, err)
114+
}
115+
entryID := entryIDstruct.ReturnEntryIDString()
116+
110117
if viper.GetBool("enable_attestation_storage") {
111118
pe, err := models.UnmarshalProposedEntry(bytes.NewReader(leaf.LeafValue), runtime.JSONConsumer())
112119
if err != nil {
@@ -130,11 +137,6 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
130137
}
131138
// if looking up by key failed or we weren't able to generate a key, try looking up by uuid
132139
if attKey == "" || fetchErr != nil {
133-
activeTree := fmt.Sprintf("%x", tc.logID)
134-
entryIDstruct, err := sharding.CreateEntryIDFromParts(activeTree, uuid)
135-
if err != nil {
136-
return nil, fmt.Errorf("error creating EntryID from active treeID %v and uuid %v: %w", activeTree, uuid, err)
137-
}
138140
att, fetchErr = storageClient.FetchAttestation(ctx, entryIDstruct.UUID)
139141
if fetchErr != nil {
140142
log.ContextLogger(ctx).Errorf("error fetching attestation by uuid: %s %v", entryIDstruct.UUID, fetchErr)
@@ -154,7 +156,7 @@ func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianC
154156
}
155157

156158
return models.LogEntry{
157-
uuid: logEntryAnon}, nil
159+
entryID: logEntryAnon}, nil
158160
}
159161

160162
// GetLogEntryAndProofByIndexHandler returns the entry and inclusion proof for a specified log index
@@ -296,7 +298,7 @@ func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middl
296298
}
297299

298300
logEntry := models.LogEntry{
299-
uuid: logEntryAnon,
301+
entryID: logEntryAnon,
300302
}
301303
return logEntry, nil
302304
}

tests/sharding-e2e-test.sh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,14 @@ fi
243243
echo
244244
echo "Testing /api/v1/log/entries/retrieve endpoint..."
245245

246-
UUID1=$($REKOR_CLI get --log-index 1 --rekor_server http://localhost:3000 --format json | jq -r .UUID)
247-
UUID2=$($REKOR_CLI get --log-index 3 --rekor_server http://localhost:3000 --format json | jq -r .UUID)
246+
ENTRY_ID_1=$($REKOR_CLI get --log-index 1 --rekor_server http://localhost:3000 --format json | jq -r .UUID)
247+
ENTRY_ID_2=$($REKOR_CLI get --log-index 3 --rekor_server http://localhost:3000 --format json | jq -r .UUID)
248248

249249

250250
# Make sure retrieve by UUID in the inactive shard works
251-
NUM_ELEMENTS=$(curl -f http://localhost:3000/api/v1/log/entries/retrieve -H "Content-Type: application/json" -H "Accept: application/json" -d "{ \"entryUUIDs\": [\"$UUID1\"]}" | jq '. | length')
251+
NUM_ELEMENTS=$(curl -f http://localhost:3000/api/v1/log/entries/retrieve -H "Content-Type: application/json" -H "Accept: application/json" -d "{ \"entryUUIDs\": [\"$ENTRY_ID_1\"]}" | jq '. | length')
252252
stringsMatch $NUM_ELEMENTS "1"
253253

254-
HEX_INITIAL_TREE_ID=$(printf "%x" $INITIAL_TREE_ID | awk '{ for(c = 0; c < 16 ; c++) s = s"0"; s = s$1; print substr(s, 1 + length(s) - 16);}')
255-
HEX_INITIAL_SHARD_ID=$(printf "%x" $SHARD_TREE_ID | awk '{ for(c = 0; c < 16 ; c++) s = s"0"; s = s$1; print substr(s, 1 + length(s) - 16);}')
256-
257-
ENTRY_ID_1=$(echo -n "$HEX_INITIAL_TREE_ID$UUID1" | xargs echo -n)
258-
ENTRY_ID_2=$(echo -n "$HEX_INITIAL_SHARD_ID$UUID2" | xargs echo -n)
259-
260254
# -f makes sure we exit on failure
261255
NUM_ELEMENTS=$(curl -f http://localhost:3000/api/v1/log/entries/retrieve -H "Content-Type: application/json" -H "Accept: application/json" -d "{ \"entryUUIDs\": [\"$ENTRY_ID_1\", \"$ENTRY_ID_2\"]}" | jq '. | length')
262256
stringsMatch $NUM_ELEMENTS "2"

0 commit comments

Comments
 (0)