-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmain.go
More file actions
226 lines (205 loc) · 7.83 KB
/
Copy pathmain.go
File metadata and controls
226 lines (205 loc) · 7.83 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
// 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.
// posix runs a web server that allows new entries to be POSTed to
// a tlog-tiles log stored on a posix filesystem. It allows to run
// conformance/compliance/performance tests and showing how to use
// the Tessera POSIX storage implementation.
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"golang.org/x/mod/sumdb/note"
"log/slog"
fnote "github.com/transparency-dev/formats/note"
"github.com/transparency-dev/tessera"
"github.com/transparency-dev/tessera/storage/posix"
badger_as "github.com/transparency-dev/tessera/storage/posix/antispam"
)
var (
storageDir = flag.String("storage_dir", "", "Root directory to store log data.")
listen = flag.String("listen", ":2025", "Address:port to listen on")
privKeyFile = flag.String("private_key", "", "Location of private key file. If unset, uses the contents of the LOG_PRIVATE_KEY environment variable.")
persistentAntispam = flag.Bool("antispam", false, "EXPERIMENTAL: Set to true to enable Badger-based persistent antispam storage")
additionalPrivateKeyFiles = []string{}
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.")
mirrorPolicyFile = flag.String("mirror_policy", "", "File containing the mirror policy in tlog-policy format. If unset, no mirroring will be performed.")
)
func init() {
flag.Func("additional_private_key", "Location of additional private key, may be specified multiple times", func(s string) error {
additionalPrivateKeyFiles = append(additionalPrivateKeyFiles, s)
return nil
})
}
func addCacheHeaders(value string, fs http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", value)
fs.ServeHTTP(w, r)
}
}
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))
// Gather the info needed for reading/writing checkpoints
s, a := getSignersOrDie()
// Create the Tessera POSIX storage, using the directory from the --storage_dir flag
driver, err := posix.New(ctx, posix.Config{Path: *storageDir})
if err != nil {
slog.ErrorContext(ctx, "Failed to construct storage", slog.Any("error", err))
os.Exit(1)
}
var antispam tessera.Antispam
// Persistent antispam is currently experimental, so there's no terraform or documentation yet!
if *persistentAntispam {
asOpts := badger_as.AntispamOpts{}
antispam, err = badger_as.NewAntispam(ctx, filepath.Join(*storageDir, ".state", "antispam"), asOpts)
if err != nil {
slog.ErrorContext(ctx, "Failed to create new Badger antispam storage", slog.Any("error", err))
os.Exit(1)
}
}
opts := tessera.NewAppendOptions().
WithCheckpointSigner(s, a...).
WithCheckpointInterval(time.Second).
WithCheckpointRepublishInterval(time.Minute).
WithBatching(256, time.Second).
WithAntispam(tessera.DefaultAntispamInMemorySize, antispam)
if *mirrorPolicyFile != "" {
b, err := os.ReadFile(*mirrorPolicyFile)
if err != nil {
slog.ErrorContext(ctx, "Failed to read mirror policy", slog.Any("error", err))
os.Exit(1)
}
policy, err := tessera.NewWitnessGroupFromPolicy(b)
if err != nil {
slog.ErrorContext(ctx, "Failed to parse mirror policy", slog.Any("error", err))
os.Exit(1)
}
opts = opts.WithMirrors(policy, nil)
slog.InfoContext(ctx, "Mirroring enabled", slog.Any("policy", policy))
}
appender, shutdown, _, err := tessera.NewAppender(ctx, driver, opts)
if err != nil {
slog.ErrorContext(ctx, "Failed to create new appender", slog.Any("error", err))
os.Exit(1)
}
// Define a handler for /add that accepts POST requests and adds the POST body to the log
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 {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(err.Error()))
return
}
if _, err := fmt.Fprintf(w, "%d", idx.Index); err != nil {
slog.ErrorContext(ctx, "/add", slog.Any("error", err))
return
}
})
// Proxy all GET requests to the filesystem as a lightweight file server.
// This makes it easier to test this implementation from another machine.
fs := http.FileServer(http.Dir(*storageDir))
http.Handle("GET /checkpoint", addCacheHeaders("no-cache", fs))
http.Handle("GET /tile/", addCacheHeaders("max-age=31536000, immutable", fs))
http.Handle("GET /entries/", fs)
fmt.Printf("Environment variables useful for accessing this log:\n"+
"export WRITE_URL=http://localhost%s/ \n"+
"export READ_URL=http://localhost%s/ \n", *listen, *listen)
// Run the HTTP server with the single handler and block until this is terminated
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)
}
}
func getSignersOrDie() (note.Signer, []note.Signer) {
s := getSignerOrDie()
a := []note.Signer{}
for _, p := range additionalPrivateKeyFiles {
kr, err := getKeyFile(p)
if err != nil {
slog.ErrorContext(context.Background(), "Unable to get additional private key", slog.String("file", p), slog.Any("error", err))
os.Exit(1)
}
k, err := fnote.NewSigner(kr)
if err != nil {
slog.ErrorContext(context.Background(), "Failed to instantiate signer", slog.String("file", p), slog.Any("error", err))
os.Exit(1)
}
a = append(a, k)
}
return s, a
}
// Read log private key from file or environment variable
func getSignerOrDie() note.Signer {
var privKey string
var err error
if len(*privKeyFile) > 0 {
privKey, err = getKeyFile(*privKeyFile)
if err != nil {
slog.ErrorContext(context.Background(), "Unable to get private key", slog.Any("error", err))
os.Exit(1)
}
} else {
privKey = os.Getenv("LOG_PRIVATE_KEY")
if len(privKey) == 0 {
slog.ErrorContext(context.Background(), "Supply private key file path using --private_key or set LOG_PRIVATE_KEY environment variable")
os.Exit(1)
}
}
var s note.Signer
if s, err = fnote.NewSigner(privKey); err != nil {
slog.ErrorContext(context.Background(), "Failed to instantiate signer", slog.Any("error", err))
os.Exit(1)
}
return s
}
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
}