-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathentries.go
674 lines (592 loc) · 24.5 KB
/
entries.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//
// Copyright 2021 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/cyberphone/json-canonicalization/go/src/webpki.org/jsoncanonicalizer"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/google/trillian"
ttypes "github.com/google/trillian/types"
"github.com/spf13/viper"
"github.com/transparency-dev/merkle/rfc6962"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc/codes"
"github.com/sigstore/rekor/pkg/events"
"github.com/sigstore/rekor/pkg/events/newentry"
"github.com/sigstore/rekor/pkg/generated/models"
"github.com/sigstore/rekor/pkg/generated/restapi/operations/entries"
"github.com/sigstore/rekor/pkg/log"
"github.com/sigstore/rekor/pkg/pubsub"
"github.com/sigstore/rekor/pkg/sharding"
"github.com/sigstore/rekor/pkg/tle"
"github.com/sigstore/rekor/pkg/trillianclient"
"github.com/sigstore/rekor/pkg/types"
"github.com/sigstore/rekor/pkg/util"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/options"
)
const (
maxSearchQueries = 10
)
func signEntry(ctx context.Context, signer signature.Signer, entry models.LogEntryAnon) ([]byte, error) {
payload, err := entry.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("marshalling error: %w", err)
}
canonicalized, err := jsoncanonicalizer.Transform(payload)
if err != nil {
return nil, fmt.Errorf("canonicalizing error: %w", err)
}
signature, err := signer.SignMessage(bytes.NewReader(canonicalized), options.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("signing error: %w", err)
}
return signature, nil
}
// logEntryFromLeaf creates a signed LogEntry struct from trillian structs
func logEntryFromLeaf(ctx context.Context, signer signature.Signer, _ trillianclient.TrillianClient, leaf *trillian.LogLeaf,
signedLogRoot *trillian.SignedLogRoot, proof *trillian.Proof, tid int64, ranges sharding.LogRanges) (models.LogEntry, error) {
log.ContextLogger(ctx).Debugf("log entry from leaf %d", leaf.GetLeafIndex())
root := &ttypes.LogRootV1{}
if err := root.UnmarshalBinary(signedLogRoot.LogRoot); err != nil {
return nil, err
}
hashes := []string{}
for _, hash := range proof.Hashes {
hashes = append(hashes, hex.EncodeToString(hash))
}
virtualIndex := sharding.VirtualLogIndex(leaf.GetLeafIndex(), tid, ranges)
logEntryAnon := models.LogEntryAnon{
LogID: swag.String(api.pubkeyHash),
LogIndex: &virtualIndex,
Body: leaf.LeafValue,
IntegratedTime: swag.Int64(leaf.IntegrateTimestamp.AsTime().Unix()),
}
signature, err := signEntry(ctx, signer, logEntryAnon)
if err != nil {
return nil, fmt.Errorf("signing entry error: %w", err)
}
scBytes, err := util.CreateAndSignCheckpoint(ctx, viper.GetString("rekor_server.hostname"), tid, root.TreeSize, root.RootHash, api.signer)
if err != nil {
return nil, err
}
inclusionProof := models.InclusionProof{
TreeSize: swag.Int64(int64(root.TreeSize)),
RootHash: swag.String(hex.EncodeToString(root.RootHash)),
LogIndex: swag.Int64(proof.GetLeafIndex()),
Hashes: hashes,
Checkpoint: stringPointer(string(scBytes)),
}
uuid := hex.EncodeToString(leaf.MerkleLeafHash)
treeID := fmt.Sprintf("%x", tid)
entryIDstruct, err := sharding.CreateEntryIDFromParts(treeID, uuid)
if err != nil {
return nil, fmt.Errorf("error creating EntryID from active treeID %v and uuid %v: %w", treeID, uuid, err)
}
entryID := entryIDstruct.ReturnEntryIDString()
if viper.GetBool("enable_attestation_storage") {
pe, err := models.UnmarshalProposedEntry(bytes.NewReader(leaf.LeafValue), runtime.JSONConsumer())
if err != nil {
return nil, err
}
eimpl, err := types.UnmarshalEntry(pe)
if err != nil {
return nil, err
}
if entryWithAtt, ok := eimpl.(types.EntryWithAttestationImpl); ok {
var att []byte
var fetchErr error
attKey := entryWithAtt.AttestationKey()
// if we're given a key by the type logic, let's try that first
if attKey != "" {
att, fetchErr = attestationStorageClient.FetchAttestation(ctx, attKey)
if fetchErr != nil {
log.ContextLogger(ctx).Debugf("error fetching attestation by key, trying by UUID: %s %v", attKey, fetchErr)
}
}
// if looking up by key failed or we weren't able to generate a key, try looking up by uuid
if attKey == "" || fetchErr != nil {
att, fetchErr = attestationStorageClient.FetchAttestation(ctx, entryIDstruct.UUID)
if fetchErr != nil {
log.ContextLogger(ctx).Debugf("error fetching attestation by uuid: %s %v", entryIDstruct.UUID, fetchErr)
}
}
if fetchErr == nil {
logEntryAnon.Attestation = &models.LogEntryAnonAttestation{
Data: att,
}
}
}
}
logEntryAnon.Verification = &models.LogEntryAnonVerification{
InclusionProof: &inclusionProof,
SignedEntryTimestamp: strfmt.Base64(signature),
}
return models.LogEntry{
entryID: logEntryAnon}, nil
}
// GetLogEntryAndProofByIndexHandler returns the entry and inclusion proof for a specified log index
func GetLogEntryByIndexHandler(params entries.GetLogEntryByIndexParams) middleware.Responder {
ctx := params.HTTPRequest.Context()
logEntry, err := retrieveLogEntryByIndex(ctx, int(params.LogIndex))
if err != nil {
if errors.Is(err, ErrNotFound) {
return handleRekorAPIError(params, http.StatusNotFound, fmt.Errorf("grpc error: %w", err), "")
}
return handleRekorAPIError(params, http.StatusInternalServerError, err, err.Error())
}
return entries.NewGetLogEntryByIndexOK().WithPayload(logEntry)
}
func createLogEntry(params entries.CreateLogEntryParams) (models.LogEntry, middleware.Responder) {
ctx := params.HTTPRequest.Context()
entry, err := types.CreateVersionedEntry(params.ProposedEntry)
if err != nil {
return nil, handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf(validationError, err))
}
leaf, err := types.CanonicalizeEntry(ctx, entry)
if err != nil {
if _, ok := (err).(types.ValidationError); ok {
return nil, handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf(validationError, err))
}
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, failedToGenerateCanonicalEntry)
}
tc := trillianclient.NewTrillianClient(ctx, api.logClient, api.logID)
resp := tc.AddLeaf(leaf)
// this represents overall GRPC response state (not the results of insertion into the log)
if resp.Status != codes.OK {
return nil, handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("grpc error: %w", resp.Err), trillianUnexpectedResult)
}
// this represents the results of inserting the proposed leaf into the log; status is nil in success path
insertionStatus := resp.GetAddResult.QueuedLeaf.Status
if insertionStatus != nil {
switch insertionStatus.Code {
case int32(code.Code_OK):
case int32(code.Code_ALREADY_EXISTS), int32(code.Code_FAILED_PRECONDITION):
existingUUID := hex.EncodeToString(rfc6962.DefaultHasher.HashLeaf(leaf))
activeTree := fmt.Sprintf("%x", api.logID)
entryIDstruct, err := sharding.CreateEntryIDFromParts(activeTree, existingUUID)
if err != nil {
err := fmt.Errorf("error creating EntryID from active treeID %v and uuid %v: %w", activeTree, existingUUID, err)
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, fmt.Sprintf(validationError, err))
}
existingEntryID := entryIDstruct.ReturnEntryIDString()
err = fmt.Errorf("grpc error: %v", insertionStatus.String())
return nil, handleRekorAPIError(params, http.StatusConflict, err, fmt.Sprintf(entryAlreadyExists, existingEntryID), "entryURL", getEntryURL(*params.HTTPRequest.URL, existingEntryID))
default:
err := fmt.Errorf("grpc error: %v", insertionStatus.String())
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, trillianUnexpectedResult)
}
}
// We made it this far, that means the entry was successfully added.
metricNewEntries.Inc()
queuedLeaf := resp.GetAddResult.QueuedLeaf.Leaf
uuid := hex.EncodeToString(queuedLeaf.GetMerkleLeafHash())
activeTree := fmt.Sprintf("%x", api.logID)
entryIDstruct, err := sharding.CreateEntryIDFromParts(activeTree, uuid)
if err != nil {
err := fmt.Errorf("error creating EntryID from active treeID %v and uuid %v: %w", activeTree, uuid, err)
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, fmt.Sprintf(validationError, err))
}
entryID := entryIDstruct.ReturnEntryIDString()
// The log index should be the virtual log index across all shards
virtualIndex := sharding.VirtualLogIndex(queuedLeaf.LeafIndex, api.logRanges.ActiveTreeID(), api.logRanges)
logEntryAnon := models.LogEntryAnon{
LogID: swag.String(api.pubkeyHash),
LogIndex: swag.Int64(virtualIndex),
Body: queuedLeaf.GetLeafValue(),
IntegratedTime: swag.Int64(queuedLeaf.IntegrateTimestamp.AsTime().Unix()),
}
if indexStorageClient != nil {
go func() {
start := time.Now()
var err error
defer func() {
labels := map[string]string{
"success": strconv.FormatBool(err == nil),
}
metricIndexStorageLatency.With(labels).Observe(float64(time.Since(start)))
}()
keys, err := entry.IndexKeys()
if err != nil {
log.ContextLogger(ctx).Errorf("getting entry index keys: %v", err)
return
}
if err := addToIndex(context.Background(), keys, entryID); err != nil {
log.ContextLogger(ctx).Errorf("adding keys to index: %v", err)
}
}()
}
if viper.GetBool("enable_attestation_storage") {
if entryWithAtt, ok := entry.(types.EntryWithAttestationImpl); ok {
attKey, attVal := entryWithAtt.AttestationKeyValue()
if attVal != nil {
go func() {
if err := storeAttestation(context.Background(), attKey, attVal); err != nil {
// entryIDstruct.UUID
log.ContextLogger(ctx).Debugf("error storing attestation: %s", err)
} else {
log.ContextLogger(ctx).Debugf("stored attestation for uuid %s with filename %s", entryIDstruct.UUID, attKey)
}
}()
} else {
log.ContextLogger(ctx).Infof("no attestation returned for %s", uuid)
}
}
}
signature, err := signEntry(ctx, api.signer, logEntryAnon)
if err != nil {
return nil, handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("signing entry error: %w", err), signingError)
}
root := &ttypes.LogRootV1{}
if err := root.UnmarshalBinary(resp.GetLeafAndProofResult.SignedLogRoot.LogRoot); err != nil {
return nil, handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("error unmarshalling log root: %w", err), sthGenerateError)
}
hashes := []string{}
for _, hash := range resp.GetLeafAndProofResult.Proof.Hashes {
hashes = append(hashes, hex.EncodeToString(hash))
}
scBytes, err := util.CreateAndSignCheckpoint(ctx, viper.GetString("rekor_server.hostname"), api.logID, root.TreeSize, root.RootHash, api.signer)
if err != nil {
return nil, handleRekorAPIError(params, http.StatusInternalServerError, err, sthGenerateError)
}
inclusionProof := models.InclusionProof{
TreeSize: swag.Int64(int64(root.TreeSize)),
RootHash: swag.String(hex.EncodeToString(root.RootHash)),
LogIndex: swag.Int64(queuedLeaf.LeafIndex),
Hashes: hashes,
Checkpoint: swag.String(string(scBytes)),
}
logEntryAnon.Verification = &models.LogEntryAnonVerification{
InclusionProof: &inclusionProof,
SignedEntryTimestamp: strfmt.Base64(signature),
}
logEntry := models.LogEntry{
entryID: logEntryAnon,
}
if api.newEntryPublisher != nil {
// Publishing notifications should not block the API response.
go func() {
verifiers, err := entry.Verifiers()
if err != nil {
incPublishEvent(newentry.Name, "", false)
log.ContextLogger(ctx).Errorf("Could not get verifiers for log entry %s: %v", entryID, err)
return
}
var subjects []string
for _, v := range verifiers {
subjects = append(subjects, v.Subjects()...)
}
pbEntry, err := tle.GenerateTransparencyLogEntry(logEntryAnon)
if err != nil {
incPublishEvent(newentry.Name, "", false)
log.ContextLogger(ctx).Error(err)
return
}
event, err := newentry.New(entryID, pbEntry, subjects)
if err != nil {
incPublishEvent(newentry.Name, "", false)
log.ContextLogger(ctx).Error(err)
return
}
if viper.GetBool("rekor_server.publish_events_protobuf") {
go publishEvent(ctx, api.newEntryPublisher, event, events.ContentTypeProtobuf)
}
if viper.GetBool("rekor_server.publish_events_json") {
go publishEvent(ctx, api.newEntryPublisher, event, events.ContentTypeJSON)
}
}()
}
return logEntry, nil
}
func publishEvent(ctx context.Context, publisher pubsub.Publisher, event *events.Event, contentType events.EventContentType) {
err := publisher.Publish(context.WithoutCancel(ctx), event, contentType)
incPublishEvent(event.Type().Name(), contentType, err == nil)
if err != nil {
log.ContextLogger(ctx).Error(err)
}
}
func incPublishEvent(event string, contentType events.EventContentType, ok bool) {
status := "SUCCESS"
if !ok {
status = "ERROR"
}
labels := map[string]string{
"event": event,
"status": status,
"content_type": string(contentType),
}
metricPublishEvents.With(labels).Inc()
}
// CreateLogEntryHandler creates new entry into log
func CreateLogEntryHandler(params entries.CreateLogEntryParams) middleware.Responder {
httpReq := params.HTTPRequest
logEntry, err := createLogEntry(params)
if err != nil {
return err
}
var uuid string
for location := range logEntry {
uuid = location
}
return entries.NewCreateLogEntryCreated().WithPayload(logEntry).WithLocation(getEntryURL(*httpReq.URL, uuid)).WithETag(uuid)
}
// getEntryURL returns the absolute path to the log entry in a RESTful style
func getEntryURL(locationURL url.URL, uuid string) strfmt.URI {
// remove API key from output
query := locationURL.Query()
query.Del("apiKey")
locationURL.RawQuery = query.Encode()
locationURL.Path = fmt.Sprintf("%v/%v", locationURL.Path, uuid)
return strfmt.URI(locationURL.String())
}
// GetLogEntryByUUIDHandler gets log entry and inclusion proof for specified UUID aka merkle leaf hash
func GetLogEntryByUUIDHandler(params entries.GetLogEntryByUUIDParams) middleware.Responder {
logEntry, err := retrieveLogEntry(params.HTTPRequest.Context(), params.EntryUUID)
if err != nil {
if errors.Is(err, ErrNotFound) {
return handleRekorAPIError(params, http.StatusNotFound, err, "")
}
if _, ok := (err).(types.ValidationError); ok {
return handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf("incorrectly formatted uuid %s", params.EntryUUID), params.EntryUUID)
}
return handleRekorAPIError(params, http.StatusInternalServerError, err, trillianCommunicationError)
}
return entries.NewGetLogEntryByUUIDOK().WithPayload(logEntry)
}
// SearchLogQueryHandler searches log by index, UUID, or proposed entry and returns array of entries found with inclusion proofs
func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Responder {
httpReqCtx := params.HTTPRequest.Context()
resultPayload := []models.LogEntry{}
totalQueries := len(params.Entry.EntryUUIDs) + len(params.Entry.Entries()) + len(params.Entry.LogIndexes)
if totalQueries > maxSearchQueries {
return handleRekorAPIError(params, http.StatusUnprocessableEntity, fmt.Errorf(maxSearchQueryLimit, maxSearchQueries), fmt.Sprintf(maxSearchQueryLimit, maxSearchQueries))
}
if len(params.Entry.EntryUUIDs) > 0 || len(params.Entry.Entries()) > 0 {
var searchHashes [][]byte
for _, entryID := range params.Entry.EntryUUIDs {
// if we got this far, then entryID is either a 64 or 80 character hex string
err := sharding.ValidateEntryID(entryID)
if err == nil {
logEntry, err := retrieveLogEntry(httpReqCtx, entryID)
if err != nil && !errors.Is(err, ErrNotFound) {
return handleRekorAPIError(params, http.StatusInternalServerError, err, fmt.Sprintf("error getting log entry for %s", entryID))
} else if err == nil {
resultPayload = append(resultPayload, logEntry)
}
continue
} else if len(entryID) == sharding.EntryIDHexStringLen {
// if ValidateEntryID failed and this is a full length entryID, then we can't search for it
return handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf("invalid entryID %s", entryID))
}
// At this point, check if we got a uuid instead of an EntryID, so search for the hash later
uuid := entryID
if err := sharding.ValidateUUID(uuid); err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf("invalid uuid %s", uuid))
}
hash, err := hex.DecodeString(uuid)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, malformedUUID)
}
searchHashes = append(searchHashes, hash)
}
entries := params.Entry.Entries()
for _, e := range entries {
entry, err := types.UnmarshalEntry(e)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, fmt.Errorf("unmarshalling entry: %w", err), err.Error())
}
leaf, err := types.CanonicalizeEntry(httpReqCtx, entry)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, fmt.Errorf("canonicalizing entry: %w", err), err.Error())
}
hasher := rfc6962.DefaultHasher
leafHash := hasher.HashLeaf(leaf)
searchHashes = append(searchHashes, leafHash)
}
searchByHashResults := make([]map[int64]*trillian.GetEntryAndProofResponse, len(searchHashes))
for i, hash := range searchHashes {
var results map[int64]*trillian.GetEntryAndProofResponse
for _, shard := range api.logRanges.AllShards() {
tcs := trillianclient.NewTrillianClient(httpReqCtx, api.logClient, shard)
resp := tcs.GetLeafAndProofByHash(hash)
switch resp.Status {
case codes.OK:
leafResult := resp.GetLeafAndProofResult
if leafResult != nil && leafResult.Leaf != nil {
if results == nil {
results = map[int64]*trillian.GetEntryAndProofResponse{}
}
results[shard] = resp.GetLeafAndProofResult
}
case codes.NotFound:
// do nothing here, do not throw 404 error
continue
default:
return handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("error getLeafAndProofByHash(%s): code: %v, msg %v", hex.EncodeToString(hash), resp.Status, resp.Err), trillianCommunicationError)
}
}
searchByHashResults[i] = results
}
for _, hashMap := range searchByHashResults {
for shard, leafResp := range hashMap {
if leafResp == nil {
continue
}
tcs := trillianclient.NewTrillianClient(httpReqCtx, api.logClient, shard)
logEntry, err := logEntryFromLeaf(httpReqCtx, api.signer, tcs, leafResp.Leaf, leafResp.SignedLogRoot, leafResp.Proof, shard, api.logRanges)
if err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, err, err.Error())
}
resultPayload = append(resultPayload, logEntry)
}
}
}
if len(params.Entry.LogIndexes) > 0 {
for _, logIndex := range params.Entry.LogIndexes {
logEntry, err := retrieveLogEntryByIndex(httpReqCtx, int(swag.Int64Value(logIndex)))
if err != nil && !errors.Is(err, ErrNotFound) {
return handleRekorAPIError(params, http.StatusInternalServerError, err, err.Error())
} else if err == nil {
resultPayload = append(resultPayload, logEntry)
}
}
}
return entries.NewSearchLogQueryOK().WithPayload(resultPayload)
}
var ErrNotFound = errors.New("grpc returned 0 leaves with success code")
func retrieveLogEntryByIndex(ctx context.Context, logIndex int) (models.LogEntry, error) {
log.ContextLogger(ctx).Infof("Retrieving log entry by index %d", logIndex)
tid, resolvedIndex := api.logRanges.ResolveVirtualIndex(logIndex)
tc := trillianclient.NewTrillianClient(ctx, api.logClient, tid)
log.ContextLogger(ctx).Debugf("Retrieving resolved index %v from TreeID %v", resolvedIndex, tid)
resp := tc.GetLeafAndProofByIndex(resolvedIndex)
switch resp.Status {
case codes.OK:
case codes.NotFound, codes.OutOfRange, codes.InvalidArgument:
return models.LogEntry{}, ErrNotFound
default:
return models.LogEntry{}, fmt.Errorf("grpc err: %w: %s", resp.Err, trillianCommunicationError)
}
result := resp.GetLeafAndProofResult
leaf := result.Leaf
if leaf == nil {
return models.LogEntry{}, ErrNotFound
}
return logEntryFromLeaf(ctx, api.signer, tc, leaf, result.SignedLogRoot, result.Proof, tid, api.logRanges)
}
// Retrieve a Log Entry
// If a tree ID is specified, look in that tree
// Otherwise, look through all inactive and active shards
func retrieveLogEntry(ctx context.Context, entryUUID string) (models.LogEntry, error) {
log.ContextLogger(ctx).Debugf("Retrieving log entry %v", entryUUID)
uuid, err := sharding.GetUUIDFromIDString(entryUUID)
if err != nil {
return nil, sharding.ErrPlainUUID
}
// Get the tree ID and check that shard for the entry
tid, err := sharding.TreeID(entryUUID)
if err == nil {
return retrieveUUIDFromTree(ctx, uuid, tid)
}
// If we got a UUID instead of an EntryID, search all shards
if errors.Is(err, sharding.ErrPlainUUID) {
trees := []sharding.LogRange{{TreeID: api.logRanges.ActiveTreeID()}}
trees = append(trees, api.logRanges.GetInactive()...)
for _, t := range trees {
logEntry, err := retrieveUUIDFromTree(ctx, uuid, t.TreeID)
if err != nil {
if errors.Is(err, ErrNotFound) {
continue
}
return nil, err
}
return logEntry, nil
}
return nil, ErrNotFound
}
return nil, err
}
func retrieveUUIDFromTree(ctx context.Context, uuid string, tid int64) (models.LogEntry, error) {
log.ContextLogger(ctx).Debugf("Retrieving log entry %v from tree %d", uuid, tid)
hashValue, err := hex.DecodeString(uuid)
if err != nil {
return models.LogEntry{}, types.ValidationError(err)
}
tc := trillianclient.NewTrillianClient(ctx, api.logClient, tid)
log.ContextLogger(ctx).Debugf("Attempting to retrieve UUID %v from TreeID %v", uuid, tid)
resp := tc.GetLeafAndProofByHash(hashValue)
switch resp.Status {
case codes.OK:
result := resp.GetLeafAndProofResult
if resp.Err != nil {
// this shouldn't be possible since GetLeafAndProofByHash verifies the inclusion proof using a computed leaf hash
// so this is just a defensive check
if result.Leaf == nil {
return models.LogEntry{}, ErrNotFound
}
return models.LogEntry{}, err
}
logEntry, err := logEntryFromLeaf(ctx, api.signer, tc, result.Leaf, result.SignedLogRoot, result.Proof, tid, api.logRanges)
if err != nil {
return models.LogEntry{}, fmt.Errorf("could not create log entry from leaf: %w", err)
}
return logEntry, nil
case codes.NotFound:
return models.LogEntry{}, ErrNotFound
default:
log.ContextLogger(ctx).Errorf("Unexpected response code while attempting to retrieve UUID %v from TreeID %v: %v", uuid, tid, resp.Status)
return models.LogEntry{}, errors.New("unexpected error")
}
}
// handlers for APIs that may be disabled in a given instance
func CreateLogEntryNotImplementedHandler(_ entries.CreateLogEntryParams) middleware.Responder {
err := &models.Error{
Code: http.StatusNotImplemented,
Message: "Create Entry API not enabled in this Rekor instance",
}
return entries.NewCreateLogEntryDefault(http.StatusNotImplemented).WithPayload(err)
}
func GetLogEntryByIndexNotImplementedHandler(_ entries.GetLogEntryByIndexParams) middleware.Responder {
err := &models.Error{
Code: http.StatusNotImplemented,
Message: "Get Log Entry by Index API not enabled in this Rekor instance",
}
return entries.NewGetLogEntryByIndexDefault(http.StatusNotImplemented).WithPayload(err)
}
func GetLogEntryByUUIDNotImplementedHandler(_ entries.GetLogEntryByUUIDParams) middleware.Responder {
err := &models.Error{
Code: http.StatusNotImplemented,
Message: "Get Log Entry by UUID API not enabled in this Rekor instance",
}
return entries.NewGetLogEntryByUUIDDefault(http.StatusNotImplemented).WithPayload(err)
}
func SearchLogQueryNotImplementedHandler(_ entries.SearchLogQueryParams) middleware.Responder {
err := &models.Error{
Code: http.StatusNotImplemented,
Message: "Search Log Query API not enabled in this Rekor instance",
}
return entries.NewSearchLogQueryDefault(http.StatusNotImplemented).WithPayload(err)
}