-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.go
More file actions
407 lines (362 loc) · 12.8 KB
/
Copy pathmain.go
File metadata and controls
407 lines (362 loc) · 12.8 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
// Copyright 2025 Google LLC. All Rights Reserved.
//
// 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.
// logandmap is a binary that serves as a demo of how to run a log and a map in the
// same process.
// The log is a Tessera POSIX log, and the map is an in-memory verifiable index.
// A web server is hosted that allows lookups in the map to be performed.
// The log is updated periodically with entries of type LogEntry, and the map keys
// each of the module names from that struct to each of the indices in the log where
// an entry for that module is stored.
package main
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"flag"
"fmt"
"iter"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"time"
"github.com/gorilla/mux"
"github.com/transparency-dev/formats/log"
fnote "github.com/transparency-dev/formats/note"
"github.com/transparency-dev/incubator/vindex"
"github.com/transparency-dev/incubator/vindex/internal/web"
"github.com/transparency-dev/tessera"
"github.com/transparency-dev/tessera/api"
"github.com/transparency-dev/tessera/client"
"github.com/transparency-dev/tessera/storage/posix"
"go.opentelemetry.io/otel/exporters/prometheus"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"golang.org/x/mod/sumdb/note"
"k8s.io/klog/v2"
)
var (
inputLogPrivKeyFile = flag.String("input_log_private_key_path", "", "Location of private key file. If unset, uses the contents of the INPUT_LOG_PRIVATE_KEY environment variable.")
outputLogPrivKeyFile = flag.String("output_log_private_key_path", "", "Location of private key file. If unset, uses the contents of the OUTPUT_LOG_PRIVATE_KEY environment variable.")
storageDir = flag.String("storage_dir", "", "Root directory in which to store the data for the demo. This will create subdirectories for the Input Log, Output Log, and allocate space to store the verifiable map persistence.")
persistIndex = flag.Bool("persist_index", true, "Set to false to use a memory-based implementation of the verifiable index.")
listen = flag.String("listen", ":8088", "Address to set up HTTP server listening on")
inputLogReaders = flag.Uint("input_log_readers", 4, "Number of parallel readers for the input log")
)
func main() {
klog.InitFlags(nil)
flag.Parse()
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
if err := run(ctx); err != nil {
klog.Exitf("Run failed: %v", err)
}
}
type LogEntry struct {
Module string `json:"module"`
Version string `json:"version"`
Hash []byte `json:"hash"`
}
func run(ctx context.Context) error {
// Set up storage for the input log, index, and output log.
if *storageDir == "" {
return errors.New("storage_dir must be set")
}
exporter, err := prometheus.New()
if err != nil {
return fmt.Errorf("failed to create prometheus exporter: %v", err)
}
provider := sdkmetric.NewMeterProvider(sdkmetric.WithReader(exporter))
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := provider.Shutdown(shutdownCtx); err != nil {
klog.Errorf("failed to shutdown meter provider: %v", err)
}
}()
inputLogDir := path.Join(*storageDir, "inputlog")
outputLogDir := path.Join(*storageDir, "outputlog")
mapRoot := path.Join(*storageDir, "vindex")
if err := os.MkdirAll(inputLogDir, 0o755); err != nil {
return fmt.Errorf("failed to create input log directory: %v", err)
}
if err := os.MkdirAll(outputLogDir, 0o755); err != nil {
return fmt.Errorf("failed to create output log directory: %v", err)
}
if err := os.MkdirAll(mapRoot, 0o755); err != nil {
return fmt.Errorf("failed to create vindex directory: %v", err)
}
// Create the input log, output log, and verifiable index.
// The input log is continuously getting new leaves written to it.
inputLog, inputCloser := inputLogOrDie(ctx, inputLogDir)
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
inputCloser(shutdownCtx)
}()
outputLog, outputCloser := outputLogOrDie(ctx, outputLogDir)
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
outputCloser(shutdownCtx)
}()
vi, err := vindex.NewVerifiableIndex(ctx, inputLog, mapFnFromFlags(), outputLog, mapRoot, vindex.Options{
PersistIndex: *persistIndex,
MeterProvider: provider,
})
if err != nil {
return fmt.Errorf("failed to create vindex: %v", err)
}
defer func() {
if err := vi.Close(); err != nil {
klog.Errorf("failed to close vindex: %v", err)
}
}()
// Keeps the map synced with the latest published input log state.
go maintainMap(ctx, vi)
// Run a web server to serve the input log, index, and output log.
webShutdown, err := runWebServer(vi, inputLogDir, outputLogDir)
if err != nil {
return fmt.Errorf("failed to start web server: %v", err)
}
defer func() {
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := webShutdown(shutdownCtx); err != nil {
klog.Errorf("failed to shutdown web server: %v", err)
}
}()
<-ctx.Done()
return nil
}
// inputLogOrDie returns an input log that is being updated periodically.
func inputLogOrDie(ctx context.Context, inputLogDir string) (log logReaderSource, closer func(context.Context)) {
// Gather the info needed for reading/writing checkpoints
ils, ilv := getInputLogSignerVerifierOrDie()
// Set up a Tessera POSIX log
ild, err := posix.New(ctx, posix.Config{Path: inputLogDir})
if err != nil {
klog.Exit(fmt.Errorf("failed to create input log: %v", err))
}
inputAppender, inputShutdown, inputReader, err := tessera.NewAppender(ctx, ild, tessera.NewAppendOptions().
WithCheckpointSigner(ils).
WithCheckpointInterval(5*time.Second).
WithBatching(256, time.Second))
if err != nil {
klog.Exit(fmt.Errorf("failed to get appender: %v", err))
}
inputLog := logReaderSource{
r: inputReader,
v: ilv,
numReaders: *inputLogReaders,
}
// Submits new entries to the log in the background.
go submitEntries(ctx, inputAppender)
return inputLog, func(ctx context.Context) {
if err := inputShutdown(ctx); err != nil {
klog.Warningf("Error shutting down Input Log appender: %v", err)
}
}
}
// logReaderSource adapts a tessera.LogReader to a vindex.InputLog.
type logReaderSource struct {
r tessera.LogReader
v note.Verifier
numReaders uint
}
func (s logReaderSource) Checkpoint(ctx context.Context) (checkpoint []byte, err error) {
return s.r.ReadCheckpoint(ctx)
}
func (s logReaderSource) Parse(cpRaw []byte) (*log.Checkpoint, error) {
cp, _, _, err := log.ParseCheckpoint(cpRaw, s.v.Name(), s.v)
return cp, err
}
func (s logReaderSource) Leaves(ctx context.Context, start, end uint64) iter.Seq2[[]byte, error] {
tsf := func(ctx context.Context) (uint64, error) {
return end, nil
}
bi := client.EntryBundles(ctx, s.numReaders, tsf, s.r.ReadEntryBundle, start, end-start)
unbundleFn := func(bundle []byte) ([][]byte, error) {
eb := &api.EntryBundle{}
if err := eb.UnmarshalText(bundle); err != nil {
return nil, err
}
return eb.Entries, nil
}
return func(yield func([]byte, error) bool) {
// Unwrap the client.Entry type to return an iterator of []byte only.
for entry, err := range client.Entries(bi, unbundleFn) {
if err != nil {
if !yield(nil, err) {
return
}
continue
}
if !yield(entry.Entry, nil) {
return
}
}
}
}
// outputLogOrDie returns an output log using a POSIX log in the given directory.
func outputLogOrDie(ctx context.Context, outputLogDir string) (log vindex.OutputLog, closer func(context.Context)) {
s, v := getOutputLogSignerVerifierOrDie()
l, c, err := vindex.NewOutputLog(ctx, outputLogDir, s, v, vindex.OutputLogOpts{})
if err != nil {
klog.Exit(err)
}
return l, c
}
// maintainMap reads entries from the log and sync them to the vindex.
func maintainMap(ctx context.Context, vi *vindex.VerifiableIndex) {
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
if err := vi.Update(ctx); err != nil {
klog.Warning(err)
}
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
// submitEntries continually creates new log entries and submits them to the log.
// Entries are json-encoded LogEntry structs. The module are randomly pulled from a
// list of [foo, bar, baz, splat]. The version is the current timestamp, as a string.
// The hash is set to the sha256 of the module+version.
func submitEntries(ctx context.Context, appender *tessera.Appender) {
modules := []string{"foo", "bar", "baz", "splat"}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
klog.Info("Context cancelled, stopping log appender")
return
case <-ticker.C:
module := modules[r.Intn(len(modules))]
version := time.Now().Format(time.RFC3339Nano)
h := sha256.Sum256([]byte(module + version))
entry := LogEntry{
Module: module,
Version: version,
Hash: h[:],
}
data, err := json.Marshal(entry)
if err != nil {
klog.Errorf("Failed to marshal log entry: %v", err)
continue
}
if idx, err := appender.Add(ctx, tessera.NewEntry(data))(); err != nil {
klog.Errorf("Failed to append to log: %v", err)
} else {
klog.V(2).Infof("Appended entry for %s@%s at index %d", module, version, idx.Index)
}
}
}
}
func runWebServer(vi *vindex.VerifiableIndex, inLogDir, outLogDir string) (func(context.Context) error, error) {
srv := web.NewServer(vi.Lookup)
ilfs := http.FileServer(http.Dir(inLogDir))
olfs := http.FileServer(http.Dir(outLogDir))
r := mux.NewRouter()
r.PathPrefix("/inputlog/").Handler(http.StripPrefix("/inputlog/", ilfs))
r.PathPrefix("/outputlog/").Handler(http.StripPrefix("/outputlog/", olfs))
srv.RegisterHandlers(r)
listener, err := net.Listen("tcp", *listen)
if err != nil {
return nil, err
}
hServer := &http.Server{
Handler: r,
}
go func() {
if err := hServer.Serve(listener); err != http.ErrServerClosed {
klog.Errorf("HTTP server Serve: %v", err)
}
}()
klog.Infof("Started HTTP server listening on %s", *listen)
return hServer.Shutdown, nil
}
// Read input log private key from file or environment variable and generate the
// note Signer and Verifier pair for it.
func getInputLogSignerVerifierOrDie() (note.Signer, note.Verifier) {
var privKey string
var err error
if len(*inputLogPrivKeyFile) > 0 {
privKey, err = getKeyFile(*inputLogPrivKeyFile)
if err != nil {
klog.Exitf("Unable to get private key: %v", err)
}
} else {
privKey = os.Getenv("INPUT_LOG_PRIVATE_KEY")
if len(privKey) == 0 {
klog.Exit("Supply private key file path using --input_log_private_key_path or set INPUT_LOG_PRIVATE_KEY environment variable")
}
}
s, v, err := fnote.NewEd25519SignerVerifier(privKey)
if err != nil {
klog.Exitf("Failed to get signer/verifier: %v", err)
}
return s, v
}
// Read output log private key from file or environment variable and generate the
// note Signer and Verifier pair for it.
func getOutputLogSignerVerifierOrDie() (note.Signer, note.Verifier) {
var privKey string
var err error
if len(*outputLogPrivKeyFile) > 0 {
privKey, err = getKeyFile(*outputLogPrivKeyFile)
if err != nil {
klog.Exitf("Unable to get private key: %v", err)
}
} else {
privKey = os.Getenv("OUTPUT_LOG_PRIVATE_KEY")
if len(privKey) == 0 {
klog.Exit("Supply private key file path using --output_log_private_key_path or set OUTPUT_LOG_PRIVATE_KEY environment variable")
}
}
s, v, err := fnote.NewEd25519SignerVerifier(privKey)
if err != nil {
klog.Exitf("Failed to get signer/verifier: %v", err)
}
return s, v
}
func getKeyFile(path string) (string, error) {
k, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read key file: %w", err)
}
return string(k), nil
}
func mapFnFromFlags() vindex.MapFn {
mapFn := func(data []byte) [][sha256.Size]byte {
var entry LogEntry
if err := json.Unmarshal(data, &entry); err != nil {
panic(fmt.Errorf("failed to unmarshal entry: %v", err))
}
// This returns a key which is simply the hash of the module name.
// This could be changed to return something more complex, e.g. include
// a static prefix of "module=", which would allow the same map to host
// multiple queries in parallel.
return [][sha256.Size]byte{sha256.Sum256([]byte(entry.Module))}
}
return mapFn
}