-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
149 lines (131 loc) · 4.29 KB
/
main.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
// Copyright 2024 The Tessera authors. 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.
// gcp is a simple personality allowing to run conformance/compliance/performance tests and showing how to use the Tessera GCP storage implmentation.
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"net/http"
"time"
tessera "github.com/transparency-dev/trillian-tessera"
"github.com/transparency-dev/trillian-tessera/storage/gcp"
"golang.org/x/mod/sumdb/note"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"k8s.io/klog/v2"
)
var (
bucket = flag.String("bucket", "", "Bucket to use for storing log")
listen = flag.String("listen", ":2024", "Address:port to listen on")
spanner = flag.String("spanner", "", "Spanner resource URI ('projects/.../...')")
signer = flag.String("signer", "", "Note signer to use to sign checkpoints")
persistentDedup = flag.Bool("gcp_dedup", false, "EXPERIMENTAL: Set to true to enable persistent dedupe storage")
additionalSigners = []string{}
)
func init() {
flag.Func("additional_signer", "Additional note signer for checkpoints, may be specified multiple times", func(s string) error {
additionalSigners = append(additionalSigners, s)
return nil
})
}
func main() {
klog.InitFlags(nil)
flag.Parse()
ctx := context.Background()
s, a := signerFromFlags()
// Create our Tessera storage backend:
gcpCfg := storageConfigFromFlags()
storage, err := gcp.New(ctx, gcpCfg,
tessera.WithCheckpointSigner(s, a...),
tessera.WithCheckpointInterval(10*time.Second),
tessera.WithBatching(1024, time.Second),
tessera.WithPushback(10*4096),
)
if err != nil {
klog.Exitf("Failed to create new GCP storage: %v", err)
}
// Handle dedup configuration
addDelegate := storage.Add
// PersistentDedup is currently experimental, so there's no terraform or documentation yet!
if *persistentDedup {
addDelegate, err = gcp.NewDedupe(ctx, fmt.Sprintf("%s_dedup", *spanner), addDelegate)
if err != nil {
klog.Exitf("Failed to create new GCP dedupe: %v", err)
}
}
dedupeAdd := tessera.InMemoryDedupe(addDelegate, 256)
// Expose a HTTP handler for the conformance test writes.
// This should accept arbitrary bytes POSTed to /add, and return an ascii
// decimal representation of the index assigned to the entry.
http.HandleFunc("POST /add", func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
idx, err := dedupeAdd(r.Context(), tessera.NewEntry(b))()
if err != nil {
if errors.Is(err, tessera.ErrPushback) {
w.Header().Add("Retry-After", "1")
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
// Write out the assigned index
_, _ = w.Write([]byte(fmt.Sprintf("%d", idx)))
})
h2s := &http2.Server{}
h1s := &http.Server{
Addr: *listen,
Handler: h2c.NewHandler(http.DefaultServeMux, h2s),
}
if err := h1s.ListenAndServe(); err != nil {
klog.Exitf("ListenAndServe: %v", err)
}
}
// storageConfigFromFlags returns a gcp.Config struct populated with values
// provided via flags.
func storageConfigFromFlags() gcp.Config {
if *bucket == "" {
klog.Exit("--bucket must be set")
}
if *spanner == "" {
klog.Exit("--spanner must be set")
}
return gcp.Config{
Bucket: *bucket,
Spanner: *spanner,
}
}
func signerFromFlags() (note.Signer, []note.Signer) {
s, err := note.NewSigner(*signer)
if err != nil {
klog.Exitf("Failed to create new signer: %v", err)
}
var a []note.Signer
for _, as := range additionalSigners {
s, err := note.NewSigner(as)
if err != nil {
klog.Exitf("Failed to create additional signer: %v", err)
}
a = append(a, s)
}
return s, a
}