Skip to content

Commit 4331ec4

Browse files
authored
Move more logic into ctlog.go (#114)
* Move more logic into ctlog.go * return http.Handler, delete cros and co * remove TODO
1 parent dc158c3 commit 4331ec4

File tree

4 files changed

+39
-47
lines changed

4 files changed

+39
-47
lines changed

cmd/gcp/main.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/google/trillian/monitoring/opencensus"
32-
"github.com/google/trillian/monitoring/prometheus"
3332
"github.com/prometheus/client_golang/prometheus/promhttp"
34-
"github.com/rs/cors"
3533
sctfe "github.com/transparency-dev/static-ct"
3634
gcpSCTFE "github.com/transparency-dev/static-ct/storage/gcp"
3735
tessera "github.com/transparency-dev/trillian-tessera"
@@ -76,7 +74,6 @@ func main() {
7674
flag.Parse()
7775
ctx := context.Background()
7876

79-
timeSource := sctfe.SystemTimeSource{}
8077
signer, err := NewSecretManagerSigner(ctx, *signerPublicKeySecretName, *signerPrivateKeySecretName)
8178
if err != nil {
8279
klog.Exitf("Can't create secret manager signer: %v", err)
@@ -92,58 +89,20 @@ func main() {
9289
NotAfterLimit: notAfterLimit.t,
9390
}
9491

95-
log, err := sctfe.NewLog(ctx, *origin, signer, chainValidationConfig, timeSource, newGCPStorage)
92+
logHandler, err := sctfe.NewLogHandler(ctx, *origin, signer, chainValidationConfig, newGCPStorage, *httpDeadline, *maskInternalErrors)
9693
if err != nil {
97-
klog.Exitf("Invalid log config: %v", err)
94+
klog.Exitf("Can't initialize CT HTTP Server: %v", err)
9895
}
9996

100-
opts := &sctfe.HandlerOptions{
101-
Deadline: *httpDeadline,
102-
MetricFactory: prometheus.MetricFactory{},
103-
RequestLog: &sctfe.DefaultRequestLog{},
104-
MaskInternalErrors: *maskInternalErrors,
105-
TimeSource: timeSource,
106-
}
107-
108-
handlers := sctfe.NewPathHandlers(opts, log)
109-
11097
klog.CopyStandardLogTo("WARNING")
11198
klog.Info("**** CT HTTP Server Starting ****")
99+
http.Handle("/", logHandler)
112100

113101
metricsAt := *metricsEndpoint
114102
if metricsAt == "" {
115103
metricsAt = *httpEndpoint
116104
}
117105

118-
// Allow cross-origin requests to all handlers registered on corsMux.
119-
// This is safe for CT log handlers because the log is public and
120-
// unauthenticated so cross-site scripting attacks are not a concern.
121-
corsMux := http.NewServeMux()
122-
corsHandler := cors.AllowAll().Handler(corsMux)
123-
http.Handle("/", corsHandler)
124-
125-
// Register handlers for all the configured logs.
126-
for path, handler := range handlers {
127-
corsMux.Handle(path, handler)
128-
}
129-
130-
// Return a 200 on the root, for GCE default health checking :/
131-
corsMux.HandleFunc("/", func(resp http.ResponseWriter, req *http.Request) {
132-
if req.URL.Path == "/" {
133-
resp.WriteHeader(http.StatusOK)
134-
} else {
135-
resp.WriteHeader(http.StatusNotFound)
136-
}
137-
})
138-
139-
// Export a healthz target.
140-
corsMux.HandleFunc("/healthz", func(resp http.ResponseWriter, req *http.Request) {
141-
// TODO(al): Wire this up to tell the truth.
142-
if _, err := resp.Write([]byte("ok")); err != nil {
143-
klog.Errorf("resp.Write(): %v", err)
144-
}
145-
})
146-
147106
if metricsAt != *httpEndpoint {
148107
// Run a separate handler for metrics.
149108
go func() {

ctlog.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/ecdsa"
2121
"errors"
2222
"fmt"
23+
"net/http"
2324
"strconv"
2425
"strings"
2526
"time"
@@ -82,7 +83,12 @@ type log struct {
8283
storage Storage
8384
}
8485

85-
func NewLog(ctx context.Context, origin string, signer crypto.Signer, cfg ChainValidationConfig, ts timeSource, cs CreateStorage) (*log, error) {
86+
var sysTimeSource = SystemTimeSource{}
87+
88+
// newLog instantiates a new log instance, with write endpoints.
89+
// It initiates chain validation to validate writes, and storage to persist
90+
// chains.
91+
func newLog(ctx context.Context, origin string, signer crypto.Signer, cfg ChainValidationConfig, cs CreateStorage) (*log, error) {
8692
log := &log{}
8793

8894
if origin == "" {
@@ -110,7 +116,7 @@ func NewLog(ctx context.Context, origin string, signer crypto.Signer, cfg ChainV
110116
}
111117
log.chainValidationOpts = *vlc
112118

113-
cpSigner, err := newCpSigner(signer, origin, ts)
119+
cpSigner, err := newCpSigner(signer, origin, sysTimeSource)
114120
if err != nil {
115121
klog.Exitf("failed to create checkpoint Signer: %v", err)
116122
}
@@ -217,3 +223,29 @@ var stringToKeyUsage = map[string]x509.ExtKeyUsage{
217223
"MicrosoftServerGatedCrypto": x509.ExtKeyUsageMicrosoftServerGatedCrypto,
218224
"NetscapeServerGatedCrypto": x509.ExtKeyUsageNetscapeServerGatedCrypto,
219225
}
226+
227+
// NewLogHandler creates a Tessera based CT log pluged into HTTP handlers.
228+
// The HTTP server handlers implement https://c2sp.org/static-ct-api write
229+
// endpoints.
230+
func NewLogHandler(ctx context.Context, origin string, signer crypto.Signer, cfg ChainValidationConfig, cs CreateStorage, httpDeadline time.Duration, maskInternalErrors bool) (http.Handler, error) {
231+
log, err := newLog(ctx, origin, signer, cfg, cs)
232+
if err != nil {
233+
return nil, fmt.Errorf("newLog(): %v", err)
234+
}
235+
236+
opts := &HandlerOptions{
237+
Deadline: httpDeadline,
238+
RequestLog: &DefaultRequestLog{},
239+
MaskInternalErrors: maskInternalErrors,
240+
TimeSource: sysTimeSource,
241+
}
242+
243+
handlers := NewPathHandlers(opts, log)
244+
mux := http.NewServeMux()
245+
// Register handlers for all the configured logs.
246+
for path, handler := range handlers {
247+
mux.Handle(path, handler)
248+
}
249+
250+
return mux, nil
251+
}

ctlog_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func TestNewLog(t *testing.T) {
180180
},
181181
} {
182182
t.Run(tc.desc, func(t *testing.T) {
183-
log, err := NewLog(ctx, tc.origin, tc.signer, tc.cvcfg, SystemTimeSource{},
183+
log, err := newLog(ctx, tc.origin, tc.signer, tc.cvcfg,
184184
func(_ context.Context, _ note.Signer) (*CTStorage, error) {
185185
return &CTStorage{}, nil
186186
})

handlers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ func NewPathHandlers(opts *HandlerOptions, log *log) pathHandlers {
172172
prefix := strings.TrimRight(log.origin, "/")
173173

174174
// Bind each endpoint to an appHandler instance.
175+
// TODO(phboneff): try and get rid of PathHandlers and appHandler
175176
ph := pathHandlers{
176177
prefix + ct.AddChainPath: appHandler{opts: opts, log: log, handler: addChain, name: addChainName, method: http.MethodPost},
177178
prefix + ct.AddPreChainPath: appHandler{opts: opts, log: log, handler: addPreChain, name: addPreChainName, method: http.MethodPost},

0 commit comments

Comments
 (0)