forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
333 lines (311 loc) · 13.6 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2017 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.
// ct_hammer is a stress/load test for a CT log.
package main
import (
"bytes"
"compress/gzip"
"context"
"crypto/tls"
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/golang/glog"
"github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/fixchain/ratelimiter"
"github.com/google/certificate-transparency-go/jsonclient"
"github.com/google/certificate-transparency-go/loglist"
"github.com/google/certificate-transparency-go/trillian/ctfe"
"github.com/google/certificate-transparency-go/trillian/ctfe/configpb"
"github.com/google/certificate-transparency-go/trillian/integration"
"github.com/google/certificate-transparency-go/x509util"
"github.com/google/trillian/monitoring"
"github.com/google/trillian/monitoring/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
banner = flag.Bool("banner", true, "Display intro")
httpServers = flag.String("ct_http_servers", "localhost:8092", "Comma-separated list of (assumed interchangeable) servers, each as address:port")
// Options for synthetic cert generation.
testDir = flag.String("testdata_dir", "testdata", "Name of directory with test data")
leafNotAfter = flag.String("leaf_not_after", "", "Not-After date to use for leaf certs, RFC3339/ISO-8601 format (e.g. 2017-11-26T12:29:19Z)")
// Options for copied-cert generation.
srcLogURI = flag.String("src_log_uri", "", "URI for source log to copy certificates from")
srcPubKey = flag.String("src_pub_key", "", "Name of file containing source log's public key")
srcLogName = flag.String("src_log_name", "", "Name of source log to copy certificate from (from --log_list)")
logList = flag.String("log_list", loglist.AllLogListURL, "Location of master log list (URL or filename)")
skipHTTPSVerify = flag.Bool("skip_https_verify", false, "Skip verification of HTTPS transport connection to source log")
chainBufSize = flag.Int("buffered_chains", 100, "Number of buffered certificate chains to hold")
startIndex = flag.Int64("start_index", 0, "Index of start point in source log to scan from (-1 for random start index)")
batchSize = flag.Int("batch_size", 500, "Max number of entries to request at per call to get-entries")
parallelFetch = flag.Int("parallel_fetch", 2, "Number of concurrent GetEntries fetches")
metricsEndpoint = flag.String("metrics_endpoint", "", "Endpoint for serving metrics; if left empty, metrics will not be exposed")
seed = flag.Int64("seed", -1, "Seed for random number generation")
logConfig = flag.String("log_config", "", "File holding log config in JSON")
mmd = flag.Duration("mmd", 2*time.Minute, "Default MMD for logs")
operations = flag.Uint64("operations", ^uint64(0), "Number of operations to perform")
minGetEntries = flag.Int("min_get_entries", 1, "Minimum get-entries request size")
maxGetEntries = flag.Int("max_get_entries", 500, "Maximum get-entries request size")
oversizedGetEntries = flag.Bool("oversized_get_entries", false, "Whether get-entries requests can go beyond log size")
maxParallelChains = flag.Int("max_parallel_chains", 2, "Maximum number of chains to add in parallel (will always add at least 1 chain)")
limit = flag.Int("rate_limit", 0, "Maximum rate of requests to an individual log; 0 for no rate limit")
ignoreErrors = flag.Bool("ignore_errors", false, "Whether to ignore errors and retry the operation")
maxRetry = flag.Duration("max_retry", 60*time.Second, "How long to keep retrying when ignore_errors is set")
reqDeadline = flag.Duration("req_deadline", 10*time.Second, "Deadline to set on individual requests")
)
var (
addChainBias = flag.Int("add_chain", 20, "Bias for add-chain operations")
addPreChainBias = flag.Int("add_pre_chain", 20, "Bias for add-pre-chain operations")
getSTHBias = flag.Int("get_sth", 2, "Bias for get-sth operations")
getSTHConsistencyBias = flag.Int("get_sth_consistency", 2, "Bias for get-sth-consistency operations")
getProofByHashBias = flag.Int("get_proof_by_hash", 2, "Bias for get-proof-by-hash operations")
getEntriesBias = flag.Int("get_entries", 2, "Bias for get-entries operations")
getRootsBias = flag.Int("get_roots", 1, "Bias for get-roots operations")
getEntryAndProofBias = flag.Int("get_entry_and_proof", 0, "Bias for get-entry-and-proof operations")
invalidChance = flag.Int("invalid_chance", 10, "Chance of generating an invalid operation, as the N in 1-in-N (0 for never)")
dupeChance = flag.Int("duplicate_chance", 10, "Chance of generating a duplicate submission, as the N in 1-in-N (0 for never)")
strictSTHConsistencySize = flag.Bool("strict_sth_consistency_size", true, "If set to true, hammer will use only tree sizes from STHs it's seen for consistency proofs, otherwise it'll choose a random size for the smaller tree")
)
func newLimiter(rate int) integration.Limiter {
if rate <= 0 {
return nil
}
return ratelimiter.NewLimiter(rate)
}
// copierGeneratorFactory returns a function that creates per-Log ChainGenerator instances
// that are based off a source CT log specified by the command line arguments.
func copierGeneratorFactory(ctx context.Context) integration.GeneratorFactory {
var tlsCfg *tls.Config
if *skipHTTPSVerify {
glog.Warning("Skipping HTTPS connection verification")
tlsCfg = &tls.Config{InsecureSkipVerify: *skipHTTPSVerify}
}
httpClient := &http.Client{
Timeout: 60 * time.Second,
Transport: &http.Transport{
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
MaxIdleConnsPerHost: 10,
DisableKeepAlives: false,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsCfg,
},
}
uri := *srcLogURI
var opts jsonclient.Options
if *srcPubKey != "" {
pubkey, err := ioutil.ReadFile(*srcPubKey)
if err != nil {
glog.Exit(err)
}
opts.PublicKey = string(pubkey)
}
if len(*srcLogName) > 0 {
llData, err := x509util.ReadFileOrURL(*logList, httpClient)
if err != nil {
glog.Exitf("Failed to read log list: %v", err)
}
ll, err := loglist.NewFromJSON(llData)
if err != nil {
glog.Exitf("Failed to build log list: %v", err)
}
logs := ll.FindLogByName(*srcLogName)
if len(logs) == 0 {
glog.Exitf("No log with name like %q found in loglist %q", *srcLogName, *logList)
}
if len(logs) > 1 {
logNames := make([]string, len(logs))
for i, log := range logs {
logNames[i] = fmt.Sprintf("%q", log.Description)
}
glog.Exitf("Multiple logs with name like %q found in loglist: %s", *srcLogName, strings.Join(logNames, ","))
}
uri = "https://" + logs[0].URL
if opts.PublicKey == "" {
opts.PublicKeyDER = logs[0].Key
}
}
logClient, err := client.New(uri, httpClient, opts)
if err != nil {
glog.Exitf("Failed to create client for %q: %v", uri, err)
}
glog.Infof("Testing with certs copied from log at %s starting at index %d", uri, *startIndex)
genOpts := integration.CopyChainOptions{
StartIndex: *startIndex,
BufSize: *chainBufSize,
BatchSize: *batchSize,
ParallelFetch: *parallelFetch,
}
return func(c *configpb.LogConfig) (integration.ChainGenerator, error) {
return integration.NewCopyChainGeneratorFromOpts(ctx, logClient, c, genOpts)
}
}
func main() {
flag.Parse()
if *logConfig == "" {
glog.Exit("Test aborted as no log config provided (via --log_config)")
}
if *seed == -1 {
*seed = time.Now().UTC().UnixNano() & 0xFFFFFFFF
}
fmt.Printf("Today's test has been brought to you by the letters C and T and the number %#x\n", *seed)
rand.Seed(*seed)
cfg, err := ctfe.LogConfigFromFile(*logConfig)
if err != nil {
glog.Exitf("Failed to read log config: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var generatorFactory integration.GeneratorFactory
if len(*srcLogURI) > 0 || len(*srcLogName) > 0 {
// Test cert chains will be generated by copying from a source log.
generatorFactory = copierGeneratorFactory(ctx)
} else if *testDir != "" {
// Test cert chains will be generated as synthetic certs from a template.
// Retrieve the test data holding the template and key.
glog.Infof("Testing with synthetic certs based on data from %s", *testDir)
generatorFactory, err = integration.SyntheticGeneratorFactory(*testDir, *leafNotAfter)
if err != nil {
glog.Exitf("Failed to make cert generator: %v", err)
}
}
if generatorFactory == nil {
glog.Warningf("Warning: add-[pre-]chain operations disabled as no cert generation method available")
*addChainBias = 0
*addPreChainBias = 0
generatorFactory = func(c *configpb.LogConfig) (integration.ChainGenerator, error) {
return nil, nil
}
}
bias := integration.HammerBias{
Bias: map[ctfe.EntrypointName]int{
ctfe.AddChainName: *addChainBias,
ctfe.AddPreChainName: *addPreChainBias,
ctfe.GetSTHName: *getSTHBias,
ctfe.GetSTHConsistencyName: *getSTHConsistencyBias,
ctfe.GetProofByHashName: *getProofByHashBias,
ctfe.GetEntriesName: *getEntriesBias,
ctfe.GetRootsName: *getRootsBias,
ctfe.GetEntryAndProofName: *getEntryAndProofBias,
},
InvalidChance: map[ctfe.EntrypointName]int{
ctfe.AddChainName: *invalidChance,
ctfe.AddPreChainName: *invalidChance,
ctfe.GetSTHName: 0,
ctfe.GetSTHConsistencyName: *invalidChance,
ctfe.GetProofByHashName: *invalidChance,
ctfe.GetEntriesName: *invalidChance,
ctfe.GetRootsName: 0,
ctfe.GetEntryAndProofName: 0,
},
}
var mf monitoring.MetricFactory
if *metricsEndpoint != "" {
mf = prometheus.MetricFactory{}
http.Handle("/metrics", promhttp.Handler())
server := http.Server{Addr: *metricsEndpoint, Handler: nil}
glog.Infof("Serving metrics at %v", *metricsEndpoint)
go func() {
err := server.ListenAndServe()
glog.Warningf("Metrics server exited: %v", err)
}()
} else {
mf = monitoring.InertMetricFactory{}
}
if *banner {
fmt.Print("\n\nStop")
for i := 0; i < 8; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Print(".")
}
mc := "H4sIAAAAAAAA/4xVPbLzMAjsv1OkU8FI9LqDOAUFDUNBxe2/QXYSS/HLe5SeXZYfsf73+D1KB8D2B2RxZpGw8gcsSoQYeH1ya0fof1BpnhpuUR+P8ijorESq8Yto6WYWqsrMGh4qSkdI/YFZWu8d3AAAkklEHBGTNAYxbpKltWRgRzQ3A3CImDIjVSVCicThbLK0VjsiAGAGIIKbmUcIq/KkqYo4BNZDqtgZMAPNPSJCRISZZ36d5OiTUbqJZAOYIoCHUreImJsCPMobQ20SqjBbLWWbBGRREhHQU2MMUu9TwB12cC7X3SNrs1yPKvv5gD4yn+kzshOfMg69fVknJNbdcsjuDvgNXWPmTXCuEnuvP4NdlSWymIQjfsFWzbERZ5sz730NpbvoOGMOzu7eeBUaW3w8r4z2iRuD4uY6W9wgZ96+YZvpHW7SabvlH7CviKWQyp81EL2zj7Fcbee7MpSuNHzj2z18LdAvAkAr8pr/3cGFUO+apa2n64TK3XouTBpEch2Rf8GnzajAFY438+SzgURfV7sXT+q1FNTJYdLF9WxJzFheAyNmXfKuiel5/mW2QqSx2umlQ+L2GpTPWZBu5tvpXW5/fy4xTYd2ly+vR052dZbjTIh0u4vzyRDF6kPzoRLRfhp2pqnr5wce5eAGP6onaRv8EYdl7gfd5zIId/gxYvr4pWW7KnbjoU6kRL62e25b44ZQz7Oaf4GrTovnqemNsyOdL40Dls11ocMPn29nYeUvmt3S1v8DAAD//wEAAP//TRo+KHEIAAA="
mcData, _ := base64.StdEncoding.DecodeString(mc)
b := bytes.NewReader(mcData)
r, _ := gzip.NewReader(b)
io.Copy(os.Stdout, r)
r.Close()
fmt.Print("\n\nHammer Time\n\n")
}
type result struct {
prefix string
err error
}
results := make(chan result, len(cfg))
var wg sync.WaitGroup
for _, c := range cfg {
wg.Add(1)
pool, err := integration.NewRandomPool(*httpServers, c.PublicKey, c.Prefix)
if err != nil {
glog.Exitf("Failed to create client pool: %v", err)
}
mmd := *mmd
// Note: Although the (usually lower than MMD) expected merge delay is not
// a guarantee, it should be OK for testing.
if emd := c.ExpectedMergeDelaySec; emd != 0 {
mmd = time.Second * time.Duration(emd)
}
generator, err := generatorFactory(c)
if err != nil {
glog.Exitf("Failed to build chain generator: %v", err)
}
cfg := integration.HammerConfig{
LogCfg: c,
MetricFactory: mf,
MMD: mmd,
ChainGenerator: generator,
ClientPool: pool,
EPBias: bias,
MinGetEntries: *minGetEntries,
MaxGetEntries: *maxGetEntries,
OversizedGetEntries: *oversizedGetEntries,
Operations: *operations,
Limiter: newLimiter(*limit),
MaxParallelChains: *maxParallelChains,
IgnoreErrors: *ignoreErrors,
MaxRetryDuration: *maxRetry,
RequestDeadline: *reqDeadline,
DuplicateChance: *dupeChance,
StrictSTHConsistencySize: *strictSTHConsistencySize,
}
go func(cfg integration.HammerConfig) {
defer wg.Done()
err := integration.HammerCTLog(cfg)
results <- result{prefix: cfg.LogCfg.Prefix, err: err}
}(cfg)
}
wg.Wait()
glog.Infof("completed tests on all %d logs:", len(cfg))
close(results)
errCount := 0
for e := range results {
if e.err != nil {
errCount++
glog.Errorf(" %s: failed with %v", e.prefix, e.err)
}
}
if errCount > 0 {
glog.Exitf("non-zero error count (%d), exiting", errCount)
}
glog.Info(" no errors; done")
}