-
Notifications
You must be signed in to change notification settings - Fork 81
/
csrs.go
422 lines (376 loc) · 14 KB
/
csrs.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
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
// Package csrs implements tester for CertificateSigningRequest.
package csrs
import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-k8s-tester/eksconfig"
k8s_client "github.com/aws/aws-k8s-tester/pkg/k8s-client"
"go.uber.org/zap"
"golang.org/x/time/rate"
certificatesv1beta1 "k8s.io/api/certificates/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Config defines "CertificateSigningRequest" configuration.
type Config struct {
Logger *zap.Logger
Stopc chan struct{}
Sig chan os.Signal
EKSConfig *eksconfig.Config
K8SClient k8s_client.EKS
}
// Tester defines CertificateSigningRequest tester.
type Tester interface {
// Create creates "CertificateSigningRequest" objects to test CertificateSigningRequest.
Create() error
// Delete deletes "CertificateSigningRequest" and Pods.
Delete() error
}
// New creates a new CSR tester.
func New(cfg Config) (Tester, error) {
return &tester{cfg: cfg, cancel: make(chan struct{})}, nil
}
type tester struct {
cfg Config
deploymentCreated time.Time
cancel chan struct{}
}
func (ts *tester) Create() error {
if ts.cfg.EKSConfig.AddOnCSRs.Created {
ts.cfg.Logger.Info("skipping create AddOnCSRs")
return nil
}
ts.cfg.EKSConfig.AddOnCSRs.Created = true
ts.cfg.EKSConfig.Sync()
createStart := time.Now()
defer func() {
ts.cfg.EKSConfig.AddOnCSRs.CreateTook = time.Since(createStart)
ts.cfg.EKSConfig.AddOnCSRs.CreateTookString = ts.cfg.EKSConfig.AddOnCSRs.CreateTook.String()
ts.cfg.EKSConfig.Sync()
}()
if err := k8s_client.CreateNamespace(ts.cfg.Logger, ts.cfg.K8SClient.KubernetesClientSet(), ts.cfg.EKSConfig.AddOnCSRs.Namespace); err != nil {
return err
}
if err := ts.createCSRs(); err != nil {
return err
}
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) Delete() error {
if !ts.cfg.EKSConfig.AddOnCSRs.Created {
ts.cfg.Logger.Info("skipping delete AddOnCSRs")
return nil
}
deleteStart := time.Now()
defer func() {
ts.cfg.EKSConfig.AddOnCSRs.DeleteTook = time.Since(deleteStart)
ts.cfg.EKSConfig.AddOnCSRs.DeleteTookString = ts.cfg.EKSConfig.AddOnCSRs.DeleteTook.String()
ts.cfg.EKSConfig.Sync()
}()
if err := k8s_client.DeleteNamespaceAndWait(ts.cfg.Logger,
ts.cfg.K8SClient.KubernetesClientSet(),
ts.cfg.EKSConfig.AddOnCSRs.Namespace,
k8s_client.DefaultNamespaceDeletionInterval,
k8s_client.DefaultNamespaceDeletionTimeout); err != nil {
return fmt.Errorf("failed to delete CSRs namespace (%v)", err)
}
ts.cfg.EKSConfig.AddOnCSRs.Created = false
return ts.cfg.EKSConfig.Sync()
}
// only letters and numbers for CSR key names
var regex = regexp.MustCompile("[^a-zA-Z0-9]+")
func (ts *tester) createCSRs() (err error) {
ts.cfg.Logger.Info("creating CSRs",
zap.Int("objects", ts.cfg.EKSConfig.AddOnCSRs.Objects),
)
// valid config key must consist of alphanumeric characters
pfx := strings.ToLower(regex.ReplaceAllString(ts.cfg.EKSConfig.Name, ""))
// overwrite if any
ts.cfg.EKSConfig.AddOnCSRs.CreatedNames = make([]string, 0, ts.cfg.EKSConfig.AddOnCSRs.Objects)
ts.cfg.EKSConfig.Sync()
if ts.cfg.EKSConfig.ClientQPS <= 1 {
err = ts.createCSRsSequential(pfx, ts.cfg.EKSConfig.AddOnCSRs.FailThreshold)
} else {
err = ts.createCSRsParallel(pfx, ts.cfg.EKSConfig.AddOnCSRs.FailThreshold)
}
ts.cfg.EKSConfig.Sync()
return err
}
/*
https://kubernetes.io/docs/tasks/tls/managing-tls-in-a-cluster/
$ cat <<EOF | cfssl genkey - | cfssljson -bare server
{
"hosts": [
"ec2-54-185-246-128.us-west-2.compute.amazonaws.com",
"system:node:ip-172-20-32-89.us-west-2.compute.internal",
"172.20.32.89",
"54.185.246.128"
],
"names": [
{
"O": "system:nodes"
}
],
"CN": "system:node:ip-172-20-32-89.us-west-2.compute.internal",
"key": {
"algo": "ecdsa",
"size": 256
}
}
EOF
$ openssl req -text -noout -in server.csr
Certificate Request:
Data:
Version: 1 (0x0)
Subject: O = system:nodes, CN = system:node:ip-172-20-32-89.us-west-2.compute.internal
Subject Public Key Info:
Public Key Algorithm: id-ecPublicKey
Public-Key: (256 bit)
pub:
04:45:2b:6e:ff:97:c4:9c:9b:5a:ae:fa:55:a5:2b:
d5:d9:40:a5:37:1f:e2:6a:0f:9e:a7:1c:05:fd:a5:
30:a7:5d:47:4f:88:98:b3:76:cb:0c:3e:2f:ba:ce:
40:52:47:ad:6e:22:a4:e5:be:c1:eb:f7:49:52:ad:
29:0f:c7:bb:a2
ASN1 OID: prime256v1
NIST CURVE: P-256
Attributes:
Requested Extensions:
X509v3 Subject Alternative Name:
DNS:ec2-54-185-246-128.us-west-2.compute.amazonaws.com, IP Address:172.20.32.89, IP Address:54.185.246.128, URI:system:node:ip-172-20-32-89.us-west-2.compute.internal
Signature Algorithm: ecdsa-with-SHA256
30:44:02:20:53:9f:b8:01:d6:16:fc:5a:1d:b7:5d:76:87:f1:
79:c4:3a:8f:40:82:8f:23:38:bc:30:12:c8:d0:08:90:ea:99:
02:20:68:fc:bc:ee:3d:48:15:88:55:9e:89:c4:8f:d8:0f:a5:
a4:11:75:af:54:39:59:c6:bd:5e:7d:e2:f7:08:7c:47
$ cat server.csr | base64 --wrap=0
$ echo "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURSBSRVFVRVNULS0tLS0KTUlJQnJEQ0NBVk1DQVFBd1dERVZNQk1HQTFVRUNoTU1jM2x6ZEdWdE9tNXZaR1Z6TVQ4d1BRWURWUVFERXpaegplWE4wWlcwNmJtOWtaVHBwY0MweE56SXRNakF0TXpJdE9Ea3VkWE10ZDJWemRDMHlMbU52YlhCMWRHVXVhVzUwClpYSnVZV3d3V1RBVEJnY3Foa2pPUFFJQkJnZ3Foa2pPUFFNQkJ3TkNBQVJGSzI3L2w4U2NtMXF1K2xXbEs5WFoKUUtVM0grSnFENTZuSEFYOXBUQ25YVWRQaUppemRzc01QaSs2emtCU1I2MXVJcVRsdnNIcjkwbFNyU2tQeDd1aQpvSUdZTUlHVkJna3Foa2lHOXcwQkNRNHhnWWN3Z1lRd2dZRUdBMVVkRVFSNk1IaUNNbVZqTWkwMU5DMHhP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plWE4wWlcwNmJtOWtaVHBwY0MweE56SXRNakF0TXpJdE9Ea3VkWE10ZDJWemRDMHlMbU52YlhCMQpkR1V1YVc1MFpYSnVZV3d3Q2dZSUtvWkl6ajBFQXdJRFJ3QXdSQUlnVTUrNEFkWVcvRm9kdDExMmgvRjV4RHFQClFJS1BJemk4TUJMSTBBaVE2cGtDSUdqOHZPNDlTQldJVlo2SnhJL1lENldrRVhXdlZEbFp4cjFlZmVMM0NIeEgKLS0tLS1FTkQgQ0VSVElGSUNBVEUgUkVRVUVTVC0tLS0tCg==")
func (ts *tester) createCSR(idx int, name string) *certificatesv1beta1.CertificateSigningRequest {
return &certificatesv1beta1.CertificateSigningRequest{
TypeMeta: metav1.TypeMeta{
APIVersion: "certificates.k8s.io/v1beta1",
Kind: "CertificateSigningRequest",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ts.cfg.EKSConfig.AddOnCSRs.Namespace,
GenerateName: name,
CreationTimestamp: metav1.Time{Time: time.Now().Add(-20 * time.Minute)},
},
Spec: certificatesv1beta1.CertificateSigningRequestSpec{
Groups: []string{"system:bootstrappers", "system:nodes", "system:authenticated"},
Request: reqData,
UID: "heptio-authenticator-aws:280347406217:AROAUCRQB56EUYTYXXJKV",
Usages: []certificatesv1beta1.KeyUsage{
certificatesv1beta1.UsageDigitalSignature,
certificatesv1beta1.UsageKeyEncipherment,
certificatesv1beta1.UsageServerAuth,
},
Username: "system:node:ip-172-20-32-89.us-west-2.compute.internal",
},
Status: certificatesv1beta1.CertificateSigningRequestStatus{
Certificate: nil,
Conditions: pickCond(idx, "Testing via "+ts.cfg.EKSConfig.Name, ts.cfg.EKSConfig.AddOnCSRs.InitialRequestConditionType),
},
}
}
func (ts *tester) createCSRsSequential(pfx string, failThreshold int) error {
qps := float64(ts.cfg.EKSConfig.ClientQPS)
burst := int(ts.cfg.EKSConfig.ClientBurst)
ts.cfg.Logger.Info("creating CSRs sequential",
zap.Float64("qps", qps),
zap.Int("burst", burst),
)
fails := 0
for i := 0; i < ts.cfg.EKSConfig.AddOnCSRs.Objects; i++ {
key := fmt.Sprintf("%s%06d", pfx, i)
req := ts.createCSR(i, key)
t1 := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
csr, err := ts.cfg.K8SClient.KubernetesClientSet().
CertificatesV1beta1().
CertificateSigningRequests().
Create(ctx, req, metav1.CreateOptions{})
cancel()
t2 := time.Now()
if err != nil {
select {
case <-ts.cancel:
return errors.New("CSR creation aborted")
case <-ts.cfg.Stopc:
return errors.New("CSR creation aborted")
default:
fails++
ts.cfg.Logger.Warn("create CSR failed",
zap.Int("fails", fails),
zap.Int("threshold", failThreshold),
zap.Error(err),
)
if fails >= failThreshold {
close(ts.cancel)
return fmt.Errorf("exceeded CSR writes fail threshold %d (%v)", failThreshold, err)
}
}
continue
}
fails = 0
csrName := csr.GetObjectMeta().GetName()
ts.cfg.EKSConfig.AddOnCSRs.CreatedNames = append(ts.cfg.EKSConfig.AddOnCSRs.CreatedNames, csrName)
ts.cfg.EKSConfig.Sync()
if ts.cfg.EKSConfig.LogLevel == "debug" || i%50 == 0 {
ts.cfg.Logger.Info(
"created CSR",
zap.String("key", csrName),
zap.Int("index", i),
zap.Int("created", len(ts.cfg.EKSConfig.AddOnCSRs.CreatedNames)),
zap.Int("target-total", ts.cfg.EKSConfig.AddOnCSRs.Objects),
zap.Duration("took", t2.Sub(t1)),
)
}
}
ts.cfg.Logger.Info("created CSRs sequential",
zap.Int("objects", ts.cfg.EKSConfig.AddOnCSRs.Objects),
zap.Int("success", len(ts.cfg.EKSConfig.AddOnCSRs.CreatedNames)),
)
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) createCSRsParallel(pfx string, failThreshold int) error {
qps := float64(ts.cfg.EKSConfig.ClientQPS)
burst := int(ts.cfg.EKSConfig.ClientBurst)
rateLimiter := rate.NewLimiter(rate.Limit(qps), burst)
ts.cfg.Logger.Info("creating CSRs parallel",
zap.Float64("qps", qps),
zap.Int("burst", burst),
)
rch := make(chan result, int(qps))
for i := 0; i < ts.cfg.EKSConfig.AddOnCSRs.Objects; i++ {
go func(i int) {
if !rateLimiter.Allow() {
ts.cfg.Logger.Debug("waiting for rate limiter creating CSR", zap.Int("index", i))
werr := rateLimiter.Wait(context.Background())
ts.cfg.Logger.Debug("waited for rate limiter", zap.Int("index", i), zap.Error(werr))
}
select {
case <-ts.cancel:
return
case <-ts.cfg.Stopc:
return
default:
}
key := fmt.Sprintf("%s%06d", pfx, i)
req := ts.createCSR(i, key)
t1 := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
csr, err := ts.cfg.K8SClient.KubernetesClientSet().
CertificatesV1beta1().
CertificateSigningRequests().
Create(ctx, req, metav1.CreateOptions{})
cancel()
t2 := time.Now()
if err != nil {
select {
case <-ts.cancel:
ts.cfg.Logger.Warn("exiting")
return
case <-ts.cfg.Stopc:
ts.cfg.Logger.Warn("exiting")
return
case rch <- result{csr: csr, err: err, took: t2.Sub(t1), start: t1, end: t2}:
}
return
}
select {
case <-ts.cancel:
ts.cfg.Logger.Warn("exiting")
return
case <-ts.cfg.Stopc:
ts.cfg.Logger.Warn("exiting")
return
case rch <- result{csr: csr, err: nil, took: t2.Sub(t1), start: t1, end: t2}:
}
if ts.cfg.EKSConfig.LogLevel == "debug" || i%50 == 0 {
ts.cfg.Logger.Info("created CSR",
zap.String("key", csr.GetObjectMeta().GetName()),
zap.Int("index", i),
zap.Int("created", len(ts.cfg.EKSConfig.AddOnCSRs.CreatedNames)),
zap.Int("target-total", ts.cfg.EKSConfig.AddOnCSRs.Objects),
zap.Duration("took", t2.Sub(t1)),
)
}
}(i)
}
fails := 0
for i := 0; i < ts.cfg.EKSConfig.AddOnCSRs.Objects; i++ {
var rv result
select {
case rv = <-rch:
case <-ts.cancel:
ts.cfg.Logger.Warn("exiting")
return errors.New("aborted")
case <-ts.cfg.Stopc:
ts.cfg.Logger.Warn("exiting")
return errors.New("aborted")
}
if rv.err != nil {
fails++
ts.cfg.Logger.Warn("create CSR failed",
zap.Int("fails", fails),
zap.Int("threshold", failThreshold),
zap.Error(rv.err),
)
if fails >= failThreshold {
close(ts.cancel)
return fmt.Errorf("exceeded CSR writes fail threshold %d (%v)", failThreshold, rv.err)
}
continue
}
fails = 0
csrName := rv.csr.GetObjectMeta().GetName()
ts.cfg.EKSConfig.AddOnCSRs.CreatedNames = append(ts.cfg.EKSConfig.AddOnCSRs.CreatedNames, csrName)
ts.cfg.EKSConfig.Sync()
}
ts.cfg.Logger.Info("created CSRs parallel",
zap.Int("objects", ts.cfg.EKSConfig.AddOnCSRs.Objects),
zap.Int("success", len(ts.cfg.EKSConfig.AddOnCSRs.CreatedNames)),
)
return ts.cfg.EKSConfig.Sync()
}
type result struct {
csr *certificatesv1beta1.CertificateSigningRequest
err error
took time.Duration
start time.Time
end time.Time
}
var conds = []certificatesv1beta1.RequestConditionType{
certificatesv1beta1.CertificateApproved,
certificatesv1beta1.CertificateDenied,
certificatesv1beta1.RequestConditionType(""),
}
func pickCond(idx int, msg string, tp string) (cs []certificatesv1beta1.CertificateSigningRequestCondition) {
cs = []certificatesv1beta1.CertificateSigningRequestCondition{
{
Reason: "Test",
Message: msg,
LastUpdateTime: metav1.NewTime(time.Now().Add(-time.Hour)),
},
}
switch tp {
case string(certificatesv1beta1.CertificateApproved):
cs[0].Type = certificatesv1beta1.CertificateApproved
case string(certificatesv1beta1.CertificateDenied):
cs[0].Type = certificatesv1beta1.CertificateDenied
case "Pending", "":
cs = make([]certificatesv1beta1.CertificateSigningRequestCondition, 0)
case "Random":
cs[0].Type = conds[idx%3]
}
return cs
}