-
Notifications
You must be signed in to change notification settings - Fork 164
/
entries.go
499 lines (436 loc) · 17.4 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
//
// 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"
"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"
"golang.org/x/sync/errgroup"
"google.golang.org/genproto/googleapis/rpc/code"
"google.golang.org/grpc/codes"
"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/sharding"
"github.com/sigstore/rekor/pkg/types"
"github.com/sigstore/sigstore/pkg/signature"
"github.com/sigstore/sigstore/pkg/signature/options"
)
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: %v", err)
}
canonicalized, err := jsoncanonicalizer.Transform(payload)
if err != nil {
return nil, fmt.Errorf("canonicalizing error: %v", err)
}
signature, err := signer.SignMessage(bytes.NewReader(canonicalized), options.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("signing error: %v", err)
}
return signature, nil
}
// logEntryFromLeaf creates a signed LogEntry struct from trillian structs
func logEntryFromLeaf(ctx context.Context, signer signature.Signer, tc TrillianClient, leaf *trillian.LogLeaf,
signedLogRoot *trillian.SignedLogRoot, proof *trillian.Proof, tid int64, ranges sharding.LogRanges) (models.LogEntry, error) {
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)
}
inclusionProof := models.InclusionProof{
TreeSize: swag.Int64(int64(root.TreeSize)),
RootHash: swag.String(hex.EncodeToString(root.RootHash)),
LogIndex: swag.Int64(proof.GetLeafIndex()),
Hashes: hashes,
}
uuid := hex.EncodeToString(leaf.MerkleLeafHash)
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.NewEntry(pe)
if err != nil {
return nil, err
}
attKey := eimpl.AttestationKey()
if attKey != "" {
att, err := storageClient.FetchAttestation(ctx, attKey)
if err != nil {
log.Logger.Errorf("error fetching attestation by key, trying by UUID: %s %s", attKey, err)
// the original attestation implementation stored this by uuid instead of by digest
activeTree := fmt.Sprintf("%x", tc.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, err
}
att, err = storageClient.FetchAttestation(ctx, entryIDstruct.UUID)
if err != nil {
log.Logger.Errorf("error fetching attestation by uuid: %s %s", entryIDstruct.UUID, err)
}
}
if err == nil {
logEntryAnon.Attestation = &models.LogEntryAnonAttestation{
Data: att,
}
}
}
}
logEntryAnon.Verification = &models.LogEntryAnonVerification{
InclusionProof: &inclusionProof,
SignedEntryTimestamp: strfmt.Base64(signature),
}
return models.LogEntry{
uuid: 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()
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(resolvedIndex)
switch resp.status {
case codes.OK:
case codes.NotFound, codes.OutOfRange, codes.InvalidArgument:
return handleRekorAPIError(params, http.StatusNotFound, fmt.Errorf("grpc error: %w", resp.err), "")
default:
return handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("grpc err: %w", resp.err), trillianCommunicationError)
}
result := resp.getLeafAndProofResult
leaf := result.Leaf
if leaf == nil {
return handleRekorAPIError(params, http.StatusNotFound, errors.New("grpc returned 0 leaves with success code"), "")
}
logEntry, err := logEntryFromLeaf(ctx, api.signer, tc, leaf, result.SignedLogRoot, result.Proof, tid, api.logRanges)
if err != nil {
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.NewEntry(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 := NewTrillianClient(ctx)
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))
err := fmt.Errorf("grpc error: %v", insertionStatus.String())
return nil, handleRekorAPIError(params, http.StatusConflict, err, fmt.Sprintf(entryAlreadyExists, existingUUID), "entryURL", getEntryURL(*params.HTTPRequest.URL, existingUUID))
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", tc.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 viper.GetBool("enable_retrieve_api") {
go func() {
keys, err := entry.IndexKeys()
if err != nil {
log.RequestIDLogger(params.HTTPRequest).Error(err)
return
}
for _, key := range keys {
if err := addToIndex(context.Background(), key, entryID); err != nil {
log.RequestIDLogger(params.HTTPRequest).Error(err)
}
}
}()
}
if viper.GetBool("enable_attestation_storage") {
go func() {
attKey, attVal := entry.AttestationKeyValue()
if attVal == nil {
log.RequestIDLogger(params.HTTPRequest).Infof("no attestation for %s", uuid)
return
}
if err := storeAttestation(context.Background(), attKey, attVal); err != nil {
// entryIDstruct.UUID
log.RequestIDLogger(params.HTTPRequest).Errorf("error storing attestation: %s", err)
} else {
log.RequestIDLogger(params.HTTPRequest).Infof("stored attestation for uuid %s with filename %s", entryIDstruct.UUID, attKey)
}
}()
}
signature, err := signEntry(ctx, api.signer, logEntryAnon)
if err != nil {
return nil, handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("signing entry error: %v", err), signingError)
}
logEntryAnon.Verification = &models.LogEntryAnonVerification{
SignedEntryTimestamp: strfmt.Base64(signature),
}
logEntry := models.LogEntry{
uuid: logEntryAnon,
}
return logEntry, nil
}
// 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 {
uuid, err := sharding.GetUUIDFromIDString(params.EntryUUID)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf("could not get UUID from ID string %v", params.EntryUUID))
}
tidString, err := sharding.GetTreeIDFromIDString(params.EntryUUID)
// If treeID is found in EntryID, route to correct tree
if err == nil {
tid, err := strconv.ParseInt(tidString, 16, 64)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf("could not convert treeID %v to int", tidString))
}
logEntry, err := RetrieveUUID(params, uuid, tid)
if err != nil {
if errors.Is(err, ErrNotFound) {
return handleRekorAPIError(params, http.StatusNotFound, err, "")
}
return handleRekorAPIError(params, http.StatusInternalServerError, err, "")
}
return entries.NewGetLogEntryByUUIDOK().WithPayload(logEntry)
}
// If EntryID is plain UUID (ex. from client v0.5), check all trees
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 := RetrieveUUID(params, uuid, t.TreeID)
if err != nil {
continue
}
return entries.NewGetLogEntryByUUIDOK().WithPayload(logEntry)
}
return handleRekorAPIError(params, http.StatusNotFound, err, "UUID not found in any known trees")
}
return handleRekorAPIError(params, http.StatusBadRequest, err, fmt.Sprintf("could not get treeID from ID string %v", params.EntryUUID))
}
// 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{}
tc := NewTrillianClient(httpReqCtx)
if len(params.Entry.EntryUUIDs) > 0 || len(params.Entry.Entries()) > 0 {
g, _ := errgroup.WithContext(httpReqCtx)
searchHashes := make([][]byte, len(params.Entry.EntryUUIDs)+len(params.Entry.Entries()))
for i, uuid := range params.Entry.EntryUUIDs {
hash, err := hex.DecodeString(uuid)
if err != nil {
return handleRekorAPIError(params, http.StatusBadRequest, err, malformedUUID)
}
searchHashes[i] = hash
}
code := http.StatusBadRequest
for i, e := range params.Entry.Entries() {
i, e := i, e // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
entry, err := types.NewEntry(e)
if err != nil {
return err
}
leaf, err := types.CanonicalizeEntry(httpReqCtx, entry)
if err != nil {
code = http.StatusInternalServerError
return err
}
hasher := rfc6962.DefaultHasher
leafHash := hasher.HashLeaf(leaf)
searchHashes[i+len(params.Entry.EntryUUIDs)] = leafHash
return nil
})
}
if err := g.Wait(); err != nil {
return handleRekorAPIError(params, code, err, err.Error())
}
searchByHashResults := make([]*trillian.GetEntryAndProofResponse, len(searchHashes))
g, _ = errgroup.WithContext(httpReqCtx)
for i, hash := range searchHashes {
i, hash := i, hash // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
resp := tc.getLeafAndProofByHash(hash)
switch resp.status {
case codes.OK, codes.NotFound:
default:
return resp.err
}
leafResult := resp.getLeafAndProofResult
if leafResult != nil && leafResult.Leaf != nil {
searchByHashResults[i] = leafResult
}
return nil
})
}
if err := g.Wait(); err != nil {
return handleRekorAPIError(params, code, err, err.Error())
}
for _, leafResp := range searchByHashResults {
if leafResp != nil {
logEntry, err := logEntryFromLeaf(httpReqCtx, api.signer, tc, leafResp.Leaf, leafResp.SignedLogRoot, leafResp.Proof, api.logRanges.ActiveTreeID(), api.logRanges)
if err != nil {
return handleRekorAPIError(params, code, err, err.Error())
}
resultPayload = append(resultPayload, logEntry)
}
}
}
if len(params.Entry.LogIndexes) > 0 {
g, _ := errgroup.WithContext(httpReqCtx)
leafResults := make([]*trillian.GetEntryAndProofResponse, len(params.Entry.LogIndexes))
for i, logIndex := range params.Entry.LogIndexes {
i, logIndex := i, logIndex // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
resp := tc.getLeafAndProofByIndex(swag.Int64Value(logIndex))
switch resp.status {
case codes.OK, codes.NotFound:
default:
return resp.err
}
leafResult := resp.getLeafAndProofResult
if leafResult != nil && leafResult.Leaf != nil {
leafResults[i] = leafResult
}
return nil
})
}
if err := g.Wait(); err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("grpc error: %w", err), trillianUnexpectedResult)
}
for _, result := range leafResults {
if result != nil {
logEntry, err := logEntryFromLeaf(httpReqCtx, api.signer, tc, result.Leaf, result.SignedLogRoot, result.Proof, api.logRanges.ActiveTreeID(), api.logRanges)
if err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, err, trillianUnexpectedResult)
}
resultPayload = append(resultPayload, logEntry)
}
}
}
return entries.NewSearchLogQueryOK().WithPayload(resultPayload)
}
var ErrNotFound = errors.New("grpc returned 0 leaves with success code")
// Attempt to retrieve a UUID from a backend tree
func RetrieveUUID(params entries.GetLogEntryByUUIDParams, uuid string, tid int64) (models.LogEntry, error) {
ctx := params.HTTPRequest.Context()
hashValue, err := hex.DecodeString(uuid)
if err != nil {
return models.LogEntry{}, err
}
tc := NewTrillianClientFromTreeID(params.HTTPRequest.Context(), tid)
log.RequestIDLogger(params.HTTPRequest).Debugf("Attempting to retrieve UUID %v from TreeID %v", uuid, tid)
resp := tc.getLeafAndProofByHash(hashValue)
switch resp.status {
case codes.OK:
result := resp.getLeafAndProofResult
leaf := result.Leaf
if leaf == nil {
return models.LogEntry{}, ErrNotFound
}
logEntry, err := logEntryFromLeaf(ctx, api.signer, tc, leaf, result.SignedLogRoot, result.Proof, tid, api.logRanges)
if err != nil {
return models.LogEntry{}, errors.New("could not create log entry from leaf")
}
return logEntry, nil
case codes.NotFound:
return models.LogEntry{}, ErrNotFound
default:
return models.LogEntry{}, errors.New("unexpected error")
}
}