-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmain.go
More file actions
283 lines (257 loc) · 9.11 KB
/
Copy pathmain.go
File metadata and controls
283 lines (257 loc) · 9.11 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
// 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.
// aws is a simple personality allowing to run conformance/compliance/performance tests and showing how to use the Tessera AWS storage implementation.
package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
"log/slog"
aaws "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/go-sql-driver/mysql"
fnote "github.com/transparency-dev/formats/note"
"github.com/transparency-dev/tessera"
"github.com/transparency-dev/tessera/storage/aws"
aws_as "github.com/transparency-dev/tessera/storage/aws/antispam"
"golang.org/x/mod/sumdb/note"
)
var (
bucket = flag.String("bucket", "", "Bucket to use for storing log")
dbName = flag.String("db_name", "", "AuroraDB name for the log DB")
dbHost = flag.String("db_host", "", "AuroraDB host")
dbPort = flag.Int("db_port", 3306, "AuroraDB port")
dbUser = flag.String("db_user", "", "AuroraDB user")
dbPassword = flag.String("db_password", "", "AuroraDB user")
dbMaxConns = flag.Int("db_max_conns", 0, "Maximum connections to the database, defaults to 0, i.e unlimited")
dbMaxIdle = flag.Int("db_max_idle_conns", 2, "Maximum idle database connections in the connection pool, defaults to 2")
s3Endpoint = flag.String("s3_endpoint", "", "Endpoint for custom non-AWS S3 service")
s3AccessKeyID = flag.String("s3_access_key", "", "Access key ID for custom non-AWS S3 service")
s3SecretAccessKey = flag.String("s3_secret", "", "Secret access key for custom non-AWS S3 service")
listen = flag.String("listen", ":2024", "Address:port to listen on")
signer = flag.String("signer", "", "Note signer to use to sign checkpoints")
publishInterval = flag.Duration("publish_interval", 3*time.Second, "How frequently to publish updated checkpoints")
traceFraction = flag.Float64("trace_fraction", 0, "Fraction of open-telemetry span traces to sample")
slogLevel = flag.Int("slog_level", 0, "The cut-off threshold for structured logging. Default is 0 (INFO). See https://pkg.go.dev/log/slog#Level for other levels.")
logFormat = flag.String("log_format", "text", "The format of the logs: text or json.")
additionalSigners = []string{}
antispamEnable = flag.Bool("antispam", false, "EXPERIMENTAL: Set to true to enable persistent antispam storage")
antispamDb = flag.String("antispam_db_name", "", "AuroraDB name for the antispam DB")
)
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() {
flag.Parse()
ctx := context.Background()
var handler slog.Handler
switch *logFormat {
case "json":
handler = slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.Level(*slogLevel)})
default:
handler = slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.Level(*slogLevel)})
}
slog.SetDefault(slog.New(handler))
shutdownOTel := initOTel(ctx, *traceFraction)
defer shutdownOTel(ctx)
s, a := signerFromFlags()
// Create our Tessera storage backend:
awsCfg := storageConfigFromFlags()
driver, err := aws.New(ctx, awsCfg)
if err != nil {
slog.ErrorContext(ctx, "Failed to create new AWS storage", slog.Any("error", err))
os.Exit(1)
}
var antispam tessera.Antispam
// Persistent antispam is currently experimental, so there's no documentation yet!
if *antispamEnable {
asOpts := aws_as.AntispamOpts{} // Use defaults
antispam, err = aws_as.NewAntispam(ctx, antispamMysqlConfig().FormatDSN(), asOpts)
if err != nil {
slog.ErrorContext(ctx, "Failed to create new AWS antispam storage", slog.Any("error", err))
os.Exit(1)
}
}
appender, shutdown, _, err := tessera.NewAppender(ctx, driver, tessera.NewAppendOptions().
WithCheckpointSigner(s, a...).
WithCheckpointInterval(*publishInterval).
WithBatching(512, 300*time.Millisecond).
WithPushback(10*4096).
WithAntispam(tessera.DefaultAntispamInMemorySize, antispam))
if err != nil {
slog.ErrorContext(ctx, "Failed to create new appender", slog.Any("error", err))
os.Exit(1)
}
// 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 := appender.Add(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
_, _ = fmt.Fprintf(w, "%d", idx.Index)
})
var protocols http.Protocols
protocols.SetHTTP1(true)
protocols.SetUnencryptedHTTP2(true)
server := &http.Server{
Addr: *listen,
Handler: http.DefaultServeMux,
Protocols: &protocols,
ReadHeaderTimeout: 5 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
if err := shutdown(ctx); err != nil {
slog.ErrorContext(ctx, "Failed to cleanly shutdown after ListenAndServe", slog.Any("error", err))
os.Exit(1)
}
slog.ErrorContext(ctx, "ListenAndServe", slog.Any("error", err))
os.Exit(1)
}
}
// storageConfigFromFlags returns an aws.Config struct populated with values
// provided via flags.
func storageConfigFromFlags() aws.Config {
ctx := context.Background()
if *bucket == "" {
slog.ErrorContext(ctx, "--bucket must be set")
os.Exit(1)
}
if *dbName == "" {
slog.ErrorContext(ctx, "--db_name must be set")
os.Exit(1)
}
if *dbHost == "" {
slog.ErrorContext(ctx, "--db_host must be set")
os.Exit(1)
}
if *dbPort == 0 {
slog.ErrorContext(ctx, "--db_port must be set")
os.Exit(1)
}
if *dbUser == "" {
slog.ErrorContext(ctx, "--db_user must be set")
os.Exit(1)
}
// Empty password isn't an option with AuroraDB MySQL.
if *dbPassword == "" {
slog.ErrorContext(ctx, "--db_password must be set")
os.Exit(1)
}
c := mysql.Config{
User: *dbUser,
Passwd: *dbPassword,
Net: "tcp",
Addr: fmt.Sprintf("%s:%d", *dbHost, *dbPort),
DBName: *dbName,
AllowCleartextPasswords: true,
AllowNativePasswords: true,
}
// Configure to use MinIO Server
var awsConfig *aaws.Config
var s3Opts func(o *s3.Options)
if *s3Endpoint != "" {
const defaultRegion = "us-east-1"
s3Opts = func(o *s3.Options) {
o.BaseEndpoint = aaws.String(*s3Endpoint)
o.Credentials = credentials.NewStaticCredentialsProvider(*s3AccessKeyID, *s3SecretAccessKey, "")
o.Region = defaultRegion
o.UsePathStyle = true
}
awsConfig = &aaws.Config{
Region: defaultRegion,
}
}
return aws.Config{
Bucket: *bucket,
SDKConfig: awsConfig,
S3Options: s3Opts,
DSN: c.FormatDSN(),
MaxOpenConns: *dbMaxConns,
MaxIdleConns: *dbMaxIdle,
}
}
func antispamMysqlConfig() *mysql.Config {
ctx := context.Background()
if *antispamDb == "" {
slog.ErrorContext(ctx, "--antispam_db_name must be set")
os.Exit(1)
}
if *dbHost == "" {
slog.ErrorContext(ctx, "--db_host must be set")
os.Exit(1)
}
if *dbPort == 0 {
slog.ErrorContext(ctx, "--db_port must be set")
os.Exit(1)
}
if *dbUser == "" {
slog.ErrorContext(ctx, "--db_user must be set")
os.Exit(1)
}
// Empty password isn't an option with AuroraDB MySQL.
if *dbPassword == "" {
slog.ErrorContext(ctx, "--db_password must be set")
os.Exit(1)
}
return &mysql.Config{
User: *dbUser,
Passwd: *dbPassword,
Net: "tcp",
Addr: fmt.Sprintf("%s:%d", *dbHost, *dbPort),
DBName: *antispamDb,
AllowCleartextPasswords: true,
AllowNativePasswords: true,
}
}
func signerFromFlags() (note.Signer, []note.Signer) {
s, err := fnote.NewSigner(*signer)
if err != nil {
slog.ErrorContext(context.Background(), "Failed to create new signer", slog.Any("error", err))
os.Exit(1)
}
var a []note.Signer
for _, as := range additionalSigners {
s, err := fnote.NewSigner(as)
if err != nil {
slog.ErrorContext(context.Background(), "Failed to create additional signer", slog.Any("error", err))
os.Exit(1)
}
a = append(a, s)
}
return s, a
}