This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logenv.go
115 lines (106 loc) · 3.5 KB
/
logenv.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
// Copyright 2017 Google Inc. 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.
package integration
import (
"context"
"fmt"
"net"
"net/http"
"sync"
"time"
"github.com/golang/glog"
"github.com/google/certificate-transparency-go/trillian/ctfe"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
"github.com/google/trillian"
"github.com/google/trillian/monitoring/prometheus"
"github.com/google/trillian/testonly/integration"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// CTLogEnv is a test environment that contains both a log server and a CT personality
// connected to it.
type CTLogEnv struct {
logEnv *integration.LogEnv
wg *sync.WaitGroup
ctListener net.Listener
ctHTTPServer *http.Server
CTAddr string
}
// NewCTLogEnv creates a fresh DB, log server, and CT personality.
// testID should be unique to each unittest package so as to allow parallel tests.
// Created logIDs will be set to cfgs.
func NewCTLogEnv(ctx context.Context, cfgs []*configpb.LogConfig, numSequencers int, testID string) (*CTLogEnv, error) {
// Start log server and signer.
logEnv, err := integration.NewLogEnv(ctx, numSequencers, testID)
if err != nil {
return nil, fmt.Errorf("failed to create LogEnv: %v", err)
}
// Provision the logs.
for _, cfg := range cfgs {
logID, err := logEnv.CreateLog()
if err != nil {
return nil, fmt.Errorf("failed to provision log %d: %v", cfg.LogId, err)
}
cfg.LogId = logID
}
// Start the CT personality.
addr, listener, err := listen()
if err != nil {
return nil, fmt.Errorf("failed to find an unused port for CT personality: %v", err)
}
server := http.Server{Addr: addr, Handler: nil}
var wg sync.WaitGroup
wg.Add(1)
go func(env *integration.LogEnv, server *http.Server, listener net.Listener, cfgs []*configpb.LogConfig) {
defer wg.Done()
client := trillian.NewTrillianLogClient(env.ClientConn)
opts := ctfe.InstanceOptions{Deadline: 10 * time.Second, MetricFactory: prometheus.MetricFactory{}, RequestLog: new(ctfe.DefaultRequestLog)}
for _, cfg := range cfgs {
handlers, err := ctfe.SetUpInstance(ctx, client, cfg, opts)
if err != nil {
glog.Fatalf("Failed to set up log instance for %+v: %v", cfg, err)
}
for path, handler := range *handlers {
http.Handle(path, handler)
}
}
http.Handle("/metrics", promhttp.Handler())
server.Serve(listener)
}(logEnv, &server, listener, cfgs)
return &CTLogEnv{
logEnv: logEnv,
wg: &wg,
ctListener: listener,
ctHTTPServer: &server,
CTAddr: addr,
}, nil
}
// Close shuts down the servers.
func (env *CTLogEnv) Close() {
env.ctListener.Close()
env.wg.Wait()
env.logEnv.Close()
}
// listen opens a random high numbered port for listening.
func listen() (string, net.Listener, error) {
lis, err := net.Listen("tcp", ":0")
if err != nil {
return "", nil, err
}
_, port, err := net.SplitHostPort(lis.Addr().String())
if err != nil {
return "", nil, err
}
addr := "localhost:" + port
return addr, lis, nil
}