forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.go
More file actions
605 lines (545 loc) · 14.6 KB
/
state.go
File metadata and controls
605 lines (545 loc) · 14.6 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/signal"
"reflect"
"runtime"
"sort"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"gopkg.in/square/go-jose.v2"
"github.com/letsencrypt/boulder/test/load-generator/acme"
"github.com/letsencrypt/challtestsrv"
)
// account is an ACME v2 account resource. It does not have a `jose.Signer`
// because we need to set the Signer options per-request with the URL being
// POSTed and must construct it on the fly from the `key`. Accounts are
// protected by a `sync.Mutex` that must be held for updates (see
// `account.Update`).
type account struct {
key *ecdsa.PrivateKey
id string
finalizedOrders []string
certs []string
mu sync.Mutex
}
// update locks an account resource's mutex and sets the `finalizedOrders` and
// `certs` fields to the provided values.
func (acct *account) update(finalizedOrders, certs []string) {
acct.mu.Lock()
defer acct.mu.Unlock()
acct.finalizedOrders = append(acct.finalizedOrders, finalizedOrders...)
acct.certs = append(acct.certs, certs...)
}
type context struct {
// The current V2 account (may be nil for legacy load generation)
acct *account
// Pending orders waiting for authorization challenge validation
pendingOrders []*OrderJSON
// Fulfilled orders in a valid status waiting for finalization
fulfilledOrders []string
// Finalized orders that have certificates
finalizedOrders []string
// A list of URLs for issued certificates
certs []string
// The nonce source for JWS signature nonce headers
ns *nonceSource
}
// signEmbeddedV2Request signs the provided request data using the context's
// account's private key. The provided URL is set as a protected header per ACME
// v2 JWS standards. The resulting JWS contains an **embedded** JWK - this makes
// this function primarily applicable to new account requests where no key ID is
// known.
func (c *context) signEmbeddedV2Request(data []byte, url string) (*jose.JSONWebSignature, error) {
// Create a signing key for the account's private key
signingKey := jose.SigningKey{
Key: c.acct.key,
Algorithm: jose.ES256,
}
// Create a signer, setting the URL protected header
signer, err := jose.NewSigner(signingKey, &jose.SignerOptions{
NonceSource: c.ns,
EmbedJWK: true,
ExtraHeaders: map[jose.HeaderKey]interface{}{
"url": url,
},
})
if err != nil {
return nil, err
}
// Sign the data with the signer
signed, err := signer.Sign(data)
if err != nil {
return nil, err
}
return signed, nil
}
// signKeyIDV2Request signs the provided request data using the context's
// account's private key. The provided URL is set as a protected header per ACME
// v2 JWS standards. The resulting JWS contains a Key ID header that is
// populated using the context's account's ID. This is the default JWS signing
// style for ACME v2 requests and should be used everywhere but where the key ID
// is unknown (e.g. new-account requests where an account doesn't exist yet).
func (c *context) signKeyIDV2Request(data []byte, url string) (*jose.JSONWebSignature, error) {
// Create a JWK with the account's private key and key ID
jwk := &jose.JSONWebKey{
Key: c.acct.key,
Algorithm: "ECDSA",
KeyID: c.acct.id,
}
// Create a signing key with the JWK
signerKey := jose.SigningKey{
Key: jwk,
Algorithm: jose.ES256,
}
// Ensure the signer's nonce source and URL header will be set
opts := &jose.SignerOptions{
NonceSource: c.ns,
ExtraHeaders: map[jose.HeaderKey]interface{}{
"url": url,
},
}
// Construct the signer with the configured options
signer, err := jose.NewSigner(signerKey, opts)
if err != nil {
return nil, err
}
// Sign the data with the signer
signed, err := signer.Sign(data)
if err != nil {
return nil, err
}
return signed, nil
}
type RateDelta struct {
Inc int64
Period time.Duration
}
type Plan struct {
Runtime time.Duration
Rate int64
Delta *RateDelta
}
type respCode struct {
code int
num int
}
// State holds *all* the stuff
type State struct {
domainBase string
email string
maxRegs int
maxNamesPerCert int
realIP string
certKey *ecdsa.PrivateKey
operations []func(*State, *context) error
rMu sync.RWMutex
// accts holds V2 account objects
accts []*account
challSrv *challtestsrv.ChallSrv
callLatency latencyWriter
directory *acme.Directory
challStrat acme.ChallengeStrategy
httpClient *http.Client
revokeChance float32
reqTotal int64
respCodes map[int]*respCode
cMu sync.Mutex
wg *sync.WaitGroup
}
type rawAccount struct {
FinalizedOrders []string `json:"finalizedOrders"`
Certs []string `json:"certs"`
ID string `json:"id"`
RawKey []byte `json:"rawKey"`
}
type snapshot struct {
Accounts []rawAccount
}
func (s *State) numAccts() int {
s.rMu.RLock()
defer s.rMu.RUnlock()
return len(s.accts)
}
// Snapshot will save out generated accounts
func (s *State) Snapshot(filename string) error {
fmt.Printf("[+] Saving accounts to %s\n", filename)
snap := snapshot{}
for _, acct := range s.accts {
k, err := x509.MarshalECPrivateKey(acct.key)
if err != nil {
return err
}
snap.Accounts = append(snap.Accounts, rawAccount{
Certs: acct.certs,
FinalizedOrders: acct.finalizedOrders,
ID: acct.id,
RawKey: k,
})
}
cont, err := json.Marshal(snap)
if err != nil {
return err
}
return ioutil.WriteFile(filename, cont, os.ModePerm)
}
// Restore previously generated accounts
func (s *State) Restore(filename string) error {
fmt.Printf("[+] Loading accounts from %q\n", filename)
// NOTE(@cpu): Using os.O_CREATE here explicitly to create the file if it does
// not exist.
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
content, err := ioutil.ReadAll(f)
if err != nil {
return err
}
// If the file's content is the empty string it was probably just created.
// Avoid an unmarshaling error by assuming an empty file is an empty snapshot.
if string(content) == "" {
content = []byte("{}")
}
snap := snapshot{}
err = json.Unmarshal(content, &snap)
if err != nil {
return err
}
for _, a := range snap.Accounts {
key, err := x509.ParseECPrivateKey(a.RawKey)
if err != nil {
continue
}
if err != nil {
continue
}
s.accts = append(s.accts, &account{
key: key,
id: a.ID,
finalizedOrders: a.FinalizedOrders,
certs: a.Certs,
})
}
return nil
}
// New returns a pointer to a new State struct or an error
func New(
directoryURL string,
keySize int,
domainBase string,
realIP string,
maxRegs, maxNamesPerCert int,
latencyPath string,
userEmail string,
operations []string,
challStrat string,
revokeChance float32) (*State, error) {
certKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, err
}
directory, err := acme.NewDirectory(directoryURL)
if err != nil {
return nil, err
}
strategy, err := acme.NewChallengeStrategy(challStrat)
if err != nil {
return nil, err
}
if revokeChance > 1 {
return nil, errors.New("revokeChance must be between 0.0 and 1.0")
}
httpClient := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 5 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // CDN bypass can cause validation failures
},
MaxIdleConns: 500,
IdleConnTimeout: 90 * time.Second,
},
Timeout: 10 * time.Second,
}
latencyFile, err := newLatencyFile(latencyPath)
if err != nil {
return nil, err
}
s := &State{
httpClient: httpClient,
directory: directory,
challStrat: strategy,
certKey: certKey,
domainBase: domainBase,
callLatency: latencyFile,
wg: new(sync.WaitGroup),
realIP: realIP,
maxRegs: maxRegs,
maxNamesPerCert: maxNamesPerCert,
email: userEmail,
respCodes: make(map[int]*respCode),
revokeChance: revokeChance,
}
// convert operations strings to methods
for _, opName := range operations {
op, present := stringToOperation[opName]
if !present {
return nil, fmt.Errorf("unknown operation %q", opName)
}
s.operations = append(s.operations, op)
}
return s, nil
}
// Run runs the WFE load-generator
func (s *State) Run(
httpOneAddrs []string,
tlsALPNOneAddrs []string,
dnsAddrs []string,
fakeDNS string,
p Plan) error {
// Create a new challenge server binding the requested addrs.
challSrv, err := challtestsrv.New(challtestsrv.Config{
HTTPOneAddrs: httpOneAddrs,
TLSALPNOneAddrs: tlsALPNOneAddrs,
DNSOneAddrs: dnsAddrs,
// Use a logger that has a load-generator prefix
Log: log.New(os.Stdout, "load-generator challsrv - ", log.LstdFlags),
})
// Setup the challenge server to return the mock "fake DNS" IP address
challSrv.SetDefaultDNSIPv4(fakeDNS)
// Disable returning any AAAA records.
challSrv.SetDefaultDNSIPv6("")
if err != nil {
return err
}
// Save the challenge server in the state
s.challSrv = challSrv
// Start the Challenge server in its own Go routine
go s.challSrv.Run()
if p.Delta != nil {
go func() {
for {
time.Sleep(p.Delta.Period)
atomic.AddInt64(&p.Rate, p.Delta.Inc)
}
}()
}
// Run sending loop
stop := make(chan bool, 1)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
fmt.Println("[+] Beginning execution plan")
i := int64(0)
go func() {
for {
start := time.Now()
select {
case <-stop:
return
default:
s.wg.Add(1)
go s.sendCall()
atomic.AddInt64(&i, 1)
}
sf := time.Duration(time.Second.Nanoseconds()/atomic.LoadInt64(&p.Rate)) - time.Since(start)
time.Sleep(sf)
}
}()
go func() {
lastTotal := int64(0)
lastReqTotal := int64(0)
for {
time.Sleep(time.Second)
curTotal := atomic.LoadInt64(&i)
curReqTotal := atomic.LoadInt64(&s.reqTotal)
fmt.Printf(
"%s Action rate: %d/s [expected: %d/s], Request rate: %d/s, Responses: [%s]\n",
time.Now().Format("2006-01-02 15:04:05"),
curTotal-lastTotal,
atomic.LoadInt64(&p.Rate),
curReqTotal-lastReqTotal,
s.respCodeString(),
)
lastTotal = curTotal
lastReqTotal = curReqTotal
}
}()
select {
case <-time.After(p.Runtime):
fmt.Println("[+] Execution plan finished")
case sig := <-sigs:
fmt.Printf("[!] Execution plan interrupted: %s caught\n", sig.String())
}
stop <- true
fmt.Println("[+] Waiting for pending flows to finish before killing challenge server")
s.wg.Wait()
fmt.Println("[+] Shutting down challenge server")
s.challSrv.Shutdown()
return nil
}
// HTTP utils
func (s *State) addRespCode(code int) {
s.cMu.Lock()
defer s.cMu.Unlock()
code = code / 100
if e, ok := s.respCodes[code]; ok {
e.num++
} else if !ok {
s.respCodes[code] = &respCode{code, 1}
}
}
// codes is a convenience type for holding copies of the state object's
// `respCodes` field of `map[int]*respCode`. Unlike the state object the
// respCodes are copied by value and not held as pointers. The codes type allows
// sorting the response codes for output.
type codes []respCode
func (c codes) Len() int {
return len(c)
}
func (c codes) Less(i, j int) bool {
return c[i].code < c[j].code
}
func (c codes) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (s *State) respCodeString() string {
s.cMu.Lock()
list := codes{}
for _, v := range s.respCodes {
list = append(list, *v)
}
s.cMu.Unlock()
sort.Sort(list)
counts := []string{}
for _, v := range list {
counts = append(counts, fmt.Sprintf("%dxx: %d", v.code, v.num))
}
return strings.Join(counts, ", ")
}
var userAgent = "boulder load-generator -- heyo ^_^"
func (s *State) post(
url string,
payload []byte,
ns *nonceSource,
latencyTag string,
expectedCode int) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Add("X-Real-IP", s.realIP)
req.Header.Add("User-Agent", userAgent)
req.Header.Add("Content-Type", "application/jose+json")
atomic.AddInt64(&s.reqTotal, 1)
started := time.Now()
resp, err := s.httpClient.Do(req)
finished := time.Now()
state := "error"
// Defer logging the latency and result
defer func() {
s.callLatency.Add(latencyTag, started, finished, state)
}()
if err != nil {
return nil, err
}
go s.addRespCode(resp.StatusCode)
if newNonce := resp.Header.Get("Replay-Nonce"); newNonce != "" {
ns.addNonce(newNonce)
}
if resp.StatusCode != expectedCode {
return nil, fmt.Errorf("POST %q returned HTTP status %d, expected %d",
url, resp.StatusCode, expectedCode)
}
state = "good"
return resp, nil
}
type nonceSource struct {
mu sync.Mutex
noncePool []string
s *State
}
func (ns *nonceSource) getNonce() (string, error) {
nonceURL := ns.s.directory.EndpointURL(acme.NewNonceEndpoint)
latencyTag := string(acme.NewNonceEndpoint)
started := time.Now()
resp, err := ns.s.httpClient.Head(nonceURL)
finished := time.Now()
state := "error"
defer func() {
ns.s.callLatency.Add(fmt.Sprintf("HEAD %s", latencyTag),
started, finished, state)
}()
if err != nil {
return "", err
}
defer resp.Body.Close()
if nonce := resp.Header.Get("Replay-Nonce"); nonce != "" {
state = "good"
return nonce, nil
}
return "", errors.New("'Replay-Nonce' header not supplied")
}
// Nonce satisfies the interface jose.NonceSource, should probably actually be per context but ¯\_(ツ)_/¯ for now
func (ns *nonceSource) Nonce() (string, error) {
ns.mu.Lock()
if len(ns.noncePool) == 0 {
ns.mu.Unlock()
return ns.getNonce()
}
defer ns.mu.Unlock()
nonce := ns.noncePool[0]
if len(ns.noncePool) > 1 {
ns.noncePool = ns.noncePool[1:]
} else {
ns.noncePool = []string{}
}
return nonce, nil
}
func (ns *nonceSource) addNonce(nonce string) {
ns.mu.Lock()
defer ns.mu.Unlock()
ns.noncePool = append(ns.noncePool, nonce)
}
// addAccount adds the provided account to the state's list of accts
func (s *State) addAccount(acct *account) {
s.rMu.Lock()
defer s.rMu.Unlock()
s.accts = append(s.accts, acct)
}
func (s *State) sendCall() {
defer s.wg.Done()
ctx := &context{}
for _, op := range s.operations {
err := op(s, ctx)
if err != nil {
method := runtime.FuncForPC(reflect.ValueOf(op).Pointer()).Name()
fmt.Printf("[FAILED] %s: %s\n", method, err)
break
}
}
// If the context's V2 account isn't nil, update it based on the context's
// finalizedOrders and certs.
if ctx.acct != nil {
ctx.acct.update(ctx.finalizedOrders, ctx.certs)
}
}