-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathe2e_test.go
More file actions
530 lines (483 loc) · 17.3 KB
/
Copy pathe2e_test.go
File metadata and controls
530 lines (483 loc) · 17.3 KB
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
// Copyright 2025 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.
//go:build e2e
package main
import (
"bytes"
"context"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"fmt"
"net/http"
"os/exec"
"strings"
"testing"
"time"
"github.com/secure-systems-lab/go-securesystemslib/dsse"
v1 "github.com/sigstore/protobuf-specs/gen/pb-go/common/v1"
pbdsse "github.com/sigstore/protobuf-specs/gen/pb-go/dsse"
pbs "github.com/sigstore/protobuf-specs/gen/pb-go/rekor/v1"
"github.com/sigstore/rekor-tiles/v2/internal/safeint"
"github.com/sigstore/rekor-tiles/v2/pkg/client/read"
"github.com/sigstore/rekor-tiles/v2/pkg/client/write"
pb "github.com/sigstore/rekor-tiles/v2/pkg/generated/protobuf"
"github.com/sigstore/rekor-tiles/v2/pkg/note"
dsset "github.com/sigstore/rekor-tiles/v2/pkg/types/dsse"
"github.com/sigstore/rekor-tiles/v2/pkg/verify"
"github.com/sigstore/sigstore/pkg/signature"
sigdsse "github.com/sigstore/sigstore/pkg/signature/dsse"
"github.com/stretchr/testify/assert"
f_note "github.com/transparency-dev/formats/note"
"github.com/transparency-dev/merkle/proof"
"github.com/transparency-dev/merkle/rfc6962"
"github.com/transparency-dev/tessera/api"
"github.com/transparency-dev/tessera/api/layout"
"go.step.sm/crypto/pemutil"
signednote "golang.org/x/mod/sumdb/note"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/encoding/protojson"
)
const (
defaultRekorHostname = "rekor-local"
defaultWitnessVKey = "rekor-witness-test+a478f5cd+AUtKAvrTeY7srtAMfP5JCUOkZoU+A7F5VA094y5LGr89"
defaultServerPublicKey = "./testdata/pki/ed25519-pub-key.pem"
)
// backendConfig contains per-storage-backend configuration
type backendConfig struct {
// ServerURL is the write path for uploading entries
ServerURL string
// StorageURL is the read path for the tiles and entry bundles
StorageURL string
// ComposePath is the path to the docker compose file
ComposePath string
}
var gcpConfig = backendConfig{
ServerURL: "http://localhost:3003",
StorageURL: "http://localhost:7080/tiles",
ComposePath: "compose.yml",
}
func TestGCP(t *testing.T) {
t.Run("ReadWrite", func(t *testing.T) {
testReadWrite(t, gcpConfig)
})
t.Run("UnimplementedReadMethods", func(t *testing.T) {
testUnimplementedReadMethods(t, gcpConfig)
})
t.Run("PersistentDeduplication", func(t *testing.T) {
testPersistentDeduplication(t, gcpConfig)
})
}
func testReadWrite(t *testing.T, config backendConfig) {
ctx := context.Background()
// get verifier needed for both read and write
serverPubKey, err := pemutil.Read(defaultServerPublicKey)
if err != nil {
t.Fatal(err)
}
verifier, err := signature.LoadDefaultVerifier(serverPubKey)
if err != nil {
t.Fatal(err)
}
noteVerifier, err := note.NewNoteVerifier(defaultRekorHostname, verifier)
if err != nil {
t.Fatal(err)
}
// load witness verifier
witnessNoteVerifier, err := f_note.NewVerifierForCosignatureV1(defaultWitnessVKey)
if err != nil {
t.Fatal(err)
}
noteVerifiers := []signednote.Verifier{noteVerifier, witnessNoteVerifier}
// reader client
reader, err := read.NewReader(config.StorageURL, defaultRekorHostname, verifier)
if err != nil {
t.Fatal(err)
}
// writer client
writer, err := write.NewWriter(config.ServerURL)
if err != nil {
t.Fatal(err)
}
// log ID
_, logID, err := note.KeyHash(defaultRekorHostname, serverPubKey)
if err != nil {
t.Fatal(err)
}
// Get the current checkpoint
checkpoint, note, err := reader.ReadCheckpoint(ctx)
assert.NoError(t, err)
assert.NotNil(t, checkpoint)
assert.NotNil(t, note)
initialTreeSize := checkpoint.Size
clientPrivKey, clientPubKey, err := genKeys()
if err != nil {
t.Fatal(err)
}
// Add new entries - more than one tile's worth
numNewEntries := uint64(260)
group := new(errgroup.Group)
// Limit number of concurrent requests, as e2e tests fail on macOS
// without a reasonable limit set
group.SetLimit(50)
for i := uint64(1); i <= numNewEntries; i++ {
i := i
group.Go(func() error {
hr, err := newHashedRekordRequest(clientPrivKey, clientPubKey, i)
if err != nil {
return err
}
tle, err := writer.Add(ctx, hr)
assert.NoError(t, err)
assertHashedRekordTLE(t, tle, initialTreeSize, numNewEntries, logID, noteVerifiers, hr)
return nil
})
}
if err := group.Wait(); err != nil {
t.Fatal(err)
}
// Add one more entry outside of the errgroup so we know it's the last one.
numNewEntries++
hr, err := newHashedRekordRequest(clientPrivKey, clientPubKey, numNewEntries)
if err != nil {
t.Fatal(err)
}
tle, err := writer.Add(ctx, hr)
assert.NoError(t, err)
assertHashedRekordTLE(t, tle, initialTreeSize, numNewEntries, logID, noteVerifiers, hr)
// Check the checkpoint again
checkpoint, note, err = reader.ReadCheckpoint(ctx)
assert.NoError(t, err)
assert.NotNil(t, checkpoint)
assert.NotNil(t, note)
latestTreeSize := checkpoint.Size
assert.GreaterOrEqual(t, latestTreeSize, initialTreeSize+numNewEntries)
// Get the first tile, this should be full as long as more than 256 entries have been added.
tileLevel := uint64(0)
tileIndex := uint64(0)
tilePart := uint8(0)
firstTileBytes, err := reader.ReadTile(ctx, tileLevel, tileIndex, tilePart)
assert.NoError(t, err)
assert.NotEmpty(t, firstTileBytes)
entryBundle, err := reader.ReadEntryBundle(ctx, tileIndex, tilePart)
assert.NoError(t, err)
assert.NotEmpty(t, entryBundle)
// Get the latest tile on the lowest level. Since we added >256 entries, this index should be at least 1.
tileIndex = latestTreeSize / layout.TileWidth
assert.GreaterOrEqual(t, tileIndex, uint64(1))
tilePart = layout.PartialTileSize(tileLevel, latestTreeSize-1, latestTreeSize)
lastTileBytes, err := reader.ReadTile(ctx, tileLevel, tileIndex, tilePart)
assert.NoError(t, err)
assert.NotEmpty(t, lastTileBytes)
assert.NotEqual(t, firstTileBytes, lastTileBytes)
entryBundle, err = reader.ReadEntryBundle(ctx, tileIndex, tilePart)
assert.NoError(t, err)
assert.Contains(t, string(entryBundle), base64.StdEncoding.EncodeToString(artifactDigest(numNewEntries)))
// Parse a HashedRekord entry from the latest entry bundle
bundle := api.EntryBundle{}
err = bundle.UnmarshalText(entryBundle)
assert.NoError(t, err)
assert.NotEmpty(t, bundle.Entries)
e := &pb.Entry{}
err = protojson.Unmarshal(bundle.Entries[0], e)
assert.NoError(t, err)
assert.Equal(t, "hashedrekord", e.Kind)
assert.Equal(t, "0.0.2", e.ApiVersion)
hrEntry := e.Spec.GetHashedRekordV002()
assert.NotNil(t, hrEntry)
// Add a DSSE entry
numNewEntries++
dr, err := newDSSERequest(clientPrivKey, clientPubKey)
if err != nil {
t.Fatal(err)
}
tle, err = writer.Add(ctx, dr)
assert.NoError(t, err)
assertDSSETLE(t, tle, initialTreeSize+numNewEntries-1, logID, noteVerifiers, dr)
safeLogSize, err := safeint.NewSafeInt64(tle.InclusionProof.TreeSize)
if err != nil {
t.Fatal(err)
}
latestTreeSize = safeLogSize.U()
tileIndex = latestTreeSize / layout.TileWidth
tilePart = layout.PartialTileSize(tileLevel, latestTreeSize-1, latestTreeSize)
entryBundle, err = reader.ReadEntryBundle(ctx, tileIndex, tilePart)
assert.NoError(t, err)
expectedPayloadHash := sha256.Sum256([]byte("payload"))
expectedB64PayloadHash := base64.StdEncoding.EncodeToString(expectedPayloadHash[:])
assert.Contains(t, string(entryBundle), expectedB64PayloadHash)
// Parse a DSSE entry from the latest entry bundle
bundle = api.EntryBundle{}
err = bundle.UnmarshalText(entryBundle)
assert.NoError(t, err)
assert.NotEmpty(t, bundle.Entries)
e = &pb.Entry{}
// last entry in the bundle should be a DSSE entry
err = protojson.Unmarshal(bundle.Entries[len(bundle.Entries)-1], e)
assert.NoError(t, err)
assert.Equal(t, "dsse", e.Kind)
assert.Equal(t, "0.0.2", e.ApiVersion)
dsseEntry := e.Spec.GetDsseV002()
assert.NotNil(t, dsseEntry)
}
func testUnimplementedReadMethods(t *testing.T, config backendConfig) {
ctx := context.Background()
serverPubKey, err := pemutil.Read(defaultServerPublicKey)
if err != nil {
t.Fatal(err)
}
verifier, err := signature.LoadDefaultVerifier(serverPubKey)
if err != nil {
t.Fatal(err)
}
reader, err := read.NewReader(config.ServerURL+"/api/v2", defaultRekorHostname, verifier)
if err != nil {
t.Fatal(err)
}
_, _, err = reader.ReadCheckpoint(ctx)
assert.ErrorContains(t, err, "501") // the reader client drops the request body, hence why we only check the status code
_, err = reader.ReadTile(ctx, 0, 0, 0)
assert.ErrorContains(t, err, "501")
_, err = reader.ReadEntryBundle(ctx, 0, 0)
assert.ErrorContains(t, err, "501")
}
func testPersistentDeduplication(t *testing.T, config backendConfig) {
ctx := context.Background()
path, err := exec.LookPath("docker")
if err != nil {
t.Skip("skipping persistent deduplication test because docker is not installed")
}
output, err := exec.Command(path, "compose", "ps", "rekor").Output()
if err != nil || !strings.Contains(string(output), "rekor-tiles-rekor-1") {
t.Skip("skipping persistent deduplication test because rekor-tiles is not running as a local docker container")
}
// writer client
writer, err := write.NewWriter(config.ServerURL)
if err != nil {
t.Fatal(err)
}
clientPrivKey, clientPubKey, err := genKeys()
if err != nil {
t.Fatal(err)
}
// add one entry
hr, err := newHashedRekordRequest(clientPrivKey, clientPubKey, 0)
if err != nil {
t.Fatal(err)
}
_, err = writer.Add(ctx, hr)
assert.NoError(t, err)
// add the same entry and check for in-memory deduplication
_, err = writer.Add(ctx, hr)
assert.Error(t, err)
assert.ErrorContains(t, err, "unexpected response: 409")
assert.ErrorContains(t, err, "an equivalent entry already exists in the transparency log with index")
// verify custom response header is set
payload, err := protojson.Marshal(&pb.CreateEntryRequest{
Spec: &pb.CreateEntryRequest_HashedRekordRequestV002{
HashedRekordRequestV002: hr,
},
})
resp, err := http.Post(config.ServerURL+"/api/v2/log/entries", "application/json", bytes.NewBuffer(payload))
assert.NoError(t, err)
defer resp.Body.Close()
assert.Equal(t, resp.StatusCode, 409)
assert.NotEmpty(t, resp.Header.Get("x-log-index"))
// restart rekor-tiles and check for persistent deduplication
err = exec.Command(path, "compose", "restart", "rekor").Run()
if err != nil {
t.Fatal(err)
}
for i := 0; i <= 3; i++ {
out, err := exec.Command(path, "compose", "ps", "rekor", "--format='{{print .Status}}'").Output()
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(out), "(healthy)") {
break
}
if i == 3 {
t.Fatal("docker container took too long to restart")
}
time.Sleep(1 * time.Second)
}
_, err = writer.Add(ctx, hr)
assert.Error(t, err)
assert.ErrorContains(t, err, "unexpected response: 409")
assert.ErrorContains(t, err, "an equivalent entry already exists in the transparency log with index")
}
func artifactDigest(idx uint64) []byte {
baseArtifact := "testartifact"
artifact := []byte(fmt.Sprintf("%s%d", baseArtifact, idx))
digest := sha256.Sum256(artifact)
return digest[:]
}
func genKeys() (*ecdsa.PrivateKey, []byte, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, err
}
pubKey, err := x509.MarshalPKIXPublicKey(privKey.Public())
if err != nil {
return nil, nil, err
}
return privKey, pubKey, nil
}
func newHashedRekordRequest(privKey *ecdsa.PrivateKey, pubKey []byte, idx uint64) (*pb.HashedRekordRequestV002, error) {
digest := artifactDigest(idx)
sig, err := ecdsa.SignASN1(rand.Reader, privKey, digest)
if err != nil {
return nil, err
}
return &pb.HashedRekordRequestV002{
Signature: &pb.Signature{
Content: sig,
Verifier: &pb.Verifier{
Verifier: &pb.Verifier_PublicKey{
PublicKey: &pb.PublicKey{
RawBytes: pubKey,
},
},
KeyDetails: v1.PublicKeyDetails_PKIX_ECDSA_P256_SHA_256,
},
},
Digest: digest,
}, nil
}
func newDSSEEnvelope(privKey *ecdsa.PrivateKey) (*pbdsse.Envelope, error) {
ecdsaSigner, err := signature.LoadECDSASigner(privKey, crypto.SHA256)
if err != nil {
return nil, err
}
envelopeSigner, err := dsse.NewEnvelopeSigner(&sigdsse.SignerAdapter{
SignatureSigner: ecdsaSigner,
})
if err != nil {
return nil, err
}
payload := "payload"
payloadType := "application/vnd.in-toto+json"
envelope, err := envelopeSigner.SignPayload(context.Background(), payloadType, []byte(payload))
if err != nil {
return nil, err
}
return dsset.ToProto(envelope)
}
func newDSSERequest(privKey *ecdsa.PrivateKey, pubKey []byte) (*pb.DSSERequestV002, error) {
envelope, err := newDSSEEnvelope(privKey)
if err != nil {
return nil, err
}
return &pb.DSSERequestV002{
Envelope: envelope,
Verifiers: []*pb.Verifier{
{
Verifier: &pb.Verifier_PublicKey{
PublicKey: &pb.PublicKey{
RawBytes: pubKey,
},
},
KeyDetails: v1.PublicKeyDetails_PKIX_ECDSA_P256_SHA_256,
},
},
}, nil
}
func assertHashedRekordTLE(t *testing.T, tle *pbs.TransparencyLogEntry, initialTreeSize, numNewEntries uint64, logID []byte, verifiers []signednote.Verifier, hr *pb.HashedRekordRequestV002) {
assert.NotNil(t, tle)
// Check server does not set deprecated fields
assert.Zero(t, tle.IntegratedTime)
assert.Nil(t, tle.InclusionPromise)
// Check populated fields
// Assert log index is [initialTreeSize, initialTreeSize+numNewEntries). We can't know the precise index since
// entry upload is done in parallel.
assert.GreaterOrEqual(t, tle.LogIndex, int64(initialTreeSize))
assert.Less(t, tle.LogIndex, int64(initialTreeSize+numNewEntries))
// Assert log IDs are equivalent
assert.Equal(t, tle.LogId.KeyId, logID)
// Assert kind and version match expected values
assert.Equal(t, tle.KindVersion.Kind, "hashedrekord")
assert.Equal(t, tle.KindVersion.Version, "0.0.2")
// Verify checkpoint and inclusion proof
verifyInclusionProof(t, tle, verifiers)
// Parse canonicalized body and assert entry matches request
e := &pb.Entry{}
assert.NotNil(t, tle.CanonicalizedBody)
err := protojson.Unmarshal(tle.CanonicalizedBody, e)
assert.NoError(t, err)
assert.Equal(t, "hashedrekord", e.Kind)
assert.Equal(t, "0.0.2", e.ApiVersion)
hrEntry := e.Spec.GetHashedRekordV002()
assert.NotNil(t, hrEntry)
assert.Equal(t, hrEntry.Signature, hr.Signature)
assert.Equal(t, hrEntry.Data.Algorithm, v1.HashAlgorithm_SHA2_256)
assert.Equal(t, hrEntry.Data.Digest, hr.Digest)
}
func assertDSSETLE(t *testing.T, tle *pbs.TransparencyLogEntry, index uint64, logID []byte, verifiers []signednote.Verifier, dr *pb.DSSERequestV002) {
assert.NotNil(t, tle)
// Check server does not set deprecated fields
assert.Zero(t, tle.IntegratedTime)
assert.Nil(t, tle.InclusionPromise)
// Check populated fields
// Assert log index. There is a single DSSE upload so the exact index is known
assert.Equal(t, tle.LogIndex, int64(index))
// Assert log IDs are equivalent
assert.Equal(t, tle.LogId.KeyId, logID)
// Assert kind and version match expected values
assert.Equal(t, tle.KindVersion.Kind, "dsse")
assert.Equal(t, tle.KindVersion.Version, "0.0.2")
// Verify checkpoint and inclusion proof
verifyInclusionProof(t, tle, verifiers)
// Parse canonicalized body and assert entry matches request
e := &pb.Entry{}
assert.NotNil(t, tle.CanonicalizedBody)
err := protojson.Unmarshal(tle.CanonicalizedBody, e)
assert.NoError(t, err)
assert.Equal(t, "dsse", e.Kind)
assert.Equal(t, "0.0.2", e.ApiVersion)
dsseEntry := e.Spec.GetDsseV002()
assert.NotNil(t, dsseEntry)
// Assert payload hash is as expected
assert.Equal(t, dsseEntry.PayloadHash.Algorithm, v1.HashAlgorithm_SHA2_256)
expectedPayloadHash := sha256.Sum256(dr.Envelope.Payload)
assert.Equal(t, dsseEntry.PayloadHash.Digest, expectedPayloadHash[:])
// Assert signature matches envelope's signature
assert.Len(t, dsseEntry.Signatures, 1)
assert.Equal(t, dsseEntry.Signatures[0].Content, dr.Envelope.Signatures[0].Sig)
assert.Equal(t, dsseEntry.Signatures[0].Verifier, dr.Verifiers[0])
}
func verifyInclusionProof(t *testing.T, tle *pbs.TransparencyLogEntry, verifiers []signednote.Verifier) {
// Server also verifies inclusion proof before returning response
assert.NotNil(t, tle.InclusionProof)
// Verify checkpoint signature, assuming the first verifier in the list is for the log and the remaining verifiers are for witnesses
checkpoint, note, err := verify.VerifyWitnessedCheckpoint(tle.InclusionProof.Checkpoint.Envelope, verifiers[0], verifiers[1:]...)
assert.NoError(t, err)
// Expect 2 valid signatures, from the log and witness
assert.Len(t, note.Sigs, 2)
// Verify duplicated tle.inclusion_proof fields match bundle and parsed checkpoint values
assert.Equal(t, tle.InclusionProof.LogIndex, tle.LogIndex)
assert.Equal(t, tle.InclusionProof.TreeSize, int64(checkpoint.Size))
assert.Equal(t, tle.InclusionProof.RootHash, checkpoint.Hash)
// Verify inclusion proof
leafHash := rfc6962.DefaultHasher.HashLeaf(tle.CanonicalizedBody)
assert.NoError(t, proof.VerifyInclusion(rfc6962.DefaultHasher,
uint64(tle.LogIndex),
checkpoint.Size,
leafHash,
tle.InclusionProof.Hashes,
checkpoint.Hash))
}