-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
205 lines (176 loc) · 8.19 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
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
// Copyright 2016 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.
// The ct_server binary runs the CT personality.
package main
import (
"context"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
tesseract "github.com/transparency-dev/static-ct"
"github.com/transparency-dev/static-ct/storage"
"github.com/transparency-dev/static-ct/storage/gcp"
tessera "github.com/transparency-dev/trillian-tessera"
tgcp "github.com/transparency-dev/trillian-tessera/storage/gcp"
"golang.org/x/mod/sumdb/note"
"k8s.io/klog/v2"
)
func init() {
flag.Var(¬AfterStart, "not_after_start", "Start of the range of acceptable NotAfter values, inclusive. Leaving this unset implies no lower bound to the range. RFC3339 UTC format, e.g: 2024-01-02T15:04:05Z.")
flag.Var(¬AfterLimit, "not_after_limit", "Cut off point of notAfter dates - only notAfter dates strictly *before* notAfterLimit will be accepted. Leaving this unset means no upper bound on the accepted range. RFC3339 UTC format, e.g: 2024-01-02T15:04:05Z.")
}
// Global flags that affect all log instances.
var (
notAfterStart timestampFlag
notAfterLimit timestampFlag
httpEndpoint = flag.String("http_endpoint", "localhost:6962", "Endpoint for HTTP (host:port).")
httpDeadline = flag.Duration("http_deadline", time.Second*10, "Deadline for HTTP requests.")
maskInternalErrors = flag.Bool("mask_internal_errors", false, "Don't return error strings with Internal Server Error HTTP responses.")
origin = flag.String("origin", "", "Origin of the log, for checkpoints and the monitoring prefix.")
bucket = flag.String("bucket", "", "Name of the bucket to store the log in.")
spannerDB = flag.String("spanner_db_path", "", "Spanner database path: projects/{projectId}/instances/{instanceId}/databases/{databaseId}.")
spannerDedupDB = flag.String("spanner_dedup_db_path", "", "Spanner deduplication database path: projects/{projectId}/instances/{instanceId}/databases/{databaseId}.")
rootsPemFile = flag.String("roots_pem_file", "", "Path to the file containing root certificates that are acceptable to the log. The certs are served through get-roots endpoint.")
rejectExpired = flag.Bool("reject_expired", false, "If true then the certificate validity period will be checked against the current time during the validation of submissions. This will cause expired certificates to be rejected.")
rejectUnexpired = flag.Bool("reject_unexpired", false, "If true then CTFE rejects certificates that are either currently valid or not yet valid.")
extKeyUsages = flag.String("ext_key_usages", "", "If set, will restrict the set of such usages that the server will accept. By default all are accepted. The values specified must be ones known to the x509 package.")
rejectExtensions = flag.String("reject_extension", "", "A list of X.509 extension OIDs, in dotted string form (e.g. '2.3.4.5') which, if present, should cause submissions to be rejected.")
signerPublicKeySecretName = flag.String("signer_public_key_secret_name", "", "Public key secret name for checkpoints and SCTs signer. Format: projects/{projectId}/secrets/{secretName}/versions/{secretVersion}.")
signerPrivateKeySecretName = flag.String("signer_private_key_secret_name", "", "Private key secret name for checkpoints and SCTs signer. Format: projects/{projectId}/secrets/{secretName}/versions/{secretVersion}.")
traceFraction = flag.Float64("trace_fraction", 0, "Fraction of open-telemetry span traces to sample")
)
// nolint:staticcheck
func main() {
klog.InitFlags(nil)
flag.Parse()
ctx := context.Background()
shutdownOTel := initOTel(ctx, *traceFraction)
defer shutdownOTel(ctx)
signer, err := NewSecretManagerSigner(ctx, *signerPublicKeySecretName, *signerPrivateKeySecretName)
if err != nil {
klog.Exitf("Can't create secret manager signer: %v", err)
}
chainValidationConfig := tesseract.ChainValidationConfig{
RootsPEMFile: *rootsPemFile,
RejectExpired: *rejectExpired,
RejectUnexpired: *rejectUnexpired,
ExtKeyUsages: *extKeyUsages,
RejectExtensions: *rejectExtensions,
NotAfterStart: notAfterStart.t,
NotAfterLimit: notAfterLimit.t,
}
logHandler, err := tesseract.NewLogHandler(ctx, *origin, signer, chainValidationConfig, newGCPStorage, *httpDeadline, *maskInternalErrors)
if err != nil {
klog.Exitf("Can't initialize CT HTTP Server: %v", err)
}
klog.CopyStandardLogTo("WARNING")
klog.Info("**** CT HTTP Server Starting ****")
http.Handle("/", logHandler)
// Bring up the HTTP server and serve until we get a signal not to.
srv := http.Server{Addr: *httpEndpoint}
shutdownWG := new(sync.WaitGroup)
go awaitSignal(func() {
shutdownWG.Add(1)
defer shutdownWG.Done()
// Allow 60s for any pending requests to finish then terminate any stragglers
// TODO(phboneff): maybe wait for the sequencer queue to be empty?
ctx, cancel := context.WithTimeout(context.Background(), time.Second*60)
defer cancel()
klog.Info("Shutting down HTTP server...")
if err := srv.Shutdown(ctx); err != nil {
klog.Errorf("srv.Shutdown(): %v", err)
}
klog.Info("HTTP server shutdown")
})
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
klog.Warningf("Server exited: %v", err)
}
// Wait will only block if the function passed to awaitSignal was called,
// in which case it'll block until the HTTP server has gracefully shutdown
shutdownWG.Wait()
klog.Flush()
}
// awaitSignal waits for standard termination signals, then runs the given
// function; it should be run as a separate goroutine.
func awaitSignal(doneFn func()) {
// Arrange notification for the standard set of signals used to terminate a server
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// Now block main and wait for a signal
sig := <-sigs
klog.Warningf("Signal received: %v", sig)
klog.Flush()
doneFn()
}
func newGCPStorage(ctx context.Context, signer note.Signer) (*storage.CTStorage, error) {
if *bucket == "" {
return nil, errors.New("missing bucket")
}
if *spannerDB == "" {
return nil, errors.New("missing spannerDB")
}
gcpCfg := tgcp.Config{
Bucket: *bucket,
Spanner: *spannerDB,
}
driver, err := tgcp.New(ctx, gcpCfg)
if err != nil {
return nil, fmt.Errorf("failed to initialize GCP Tessera storage driver: %v", err)
}
opts := tessera.NewAppendOptions().
WithCheckpointSigner(signer).
WithCTLayout()
// TODO(phbnf): figure out the best way to thread the `shutdown` func NewAppends returns back out to main so we can cleanly close Tessera down
// when it's time to exit.
appender, _, _, err := tessera.NewAppender(ctx, driver, opts)
if err != nil {
return nil, fmt.Errorf("failed to initialize GCP Tessera appender: %v", err)
}
issuerStorage, err := gcp.NewIssuerStorage(ctx, *bucket, "fingerprints/", "application/pkix-cert")
if err != nil {
return nil, fmt.Errorf("failed to initialize GCP issuer storage: %v", err)
}
beDedupStorage, err := gcp.NewDedupeStorage(ctx, *spannerDedupDB)
if err != nil {
return nil, fmt.Errorf("failed to initialize GCP Spanner deduplication database: %v", err)
}
return storage.NewCTStorage(appender, issuerStorage, beDedupStorage)
}
type timestampFlag struct {
t *time.Time
}
func (t *timestampFlag) String() string {
if t.t != nil {
return t.t.Format(time.RFC3339)
}
return ""
}
func (t *timestampFlag) Set(w string) error {
if !strings.HasSuffix(w, "Z") {
return fmt.Errorf("timestamps MUST be in UTC, got %v", w)
}
tt, err := time.Parse(time.RFC3339, w)
if err != nil {
return fmt.Errorf("can't parse %q as RFC3339 timestamp: %v", w, err)
}
t.t = &tt
return nil
}