-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls.go
89 lines (71 loc) · 2.23 KB
/
tls.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
// Package tls implements helpers for TLS certificates.
package tls
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/util/cert"
"github.com/kudobuilder/test-tools/pkg/client"
"github.com/kudobuilder/test-tools/pkg/kubernetes"
)
const rsaBits = 2048
// CertSecretBuilder tracks the options set for a cert secret.
type CertSecretBuilder struct {
Name string
Namespace string
CommonName string
}
// CreateCertSecret creates a secret containing a self-signed certificate and key.
// Additional parameters can be added to this call. The creation is started by
// calling 'Do'.
func CreateCertSecret(name string) CertSecretBuilder {
return CertSecretBuilder{
Name: name,
}
}
// WithNamespace sets the namespace in which the secret will be created.
func (builder CertSecretBuilder) WithNamespace(namespace string) CertSecretBuilder {
builder.Namespace = namespace
return builder
}
// WithCommonName sets the common name of the certificate.
func (builder CertSecretBuilder) WithCommonName(commonName string) CertSecretBuilder {
builder.CommonName = commonName
return builder
}
// Do creates the certificate secret.
func (builder CertSecretBuilder) Do(client client.Client) (kubernetes.Secret, error) {
signingPriv, err := rsa.GenerateKey(rand.Reader, rsaBits)
if err != nil {
return kubernetes.Secret{}, err
}
config := cert.Config{
CommonName: builder.CommonName,
}
cacert, err := cert.NewSelfSignedCACert(config, signingPriv)
if err != nil {
return kubernetes.Secret{}, err
}
var serverKey, serverCert bytes.Buffer
if err := pem.Encode(&serverCert, &pem.Block{Type: "CERTIFICATE", Bytes: cacert.Raw}); err != nil {
return kubernetes.Secret{}, fmt.Errorf("failed creating cert: %v", err)
}
if err := pem.Encode(
&serverKey,
&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(signingPriv)},
); err != nil {
return kubernetes.Secret{}, fmt.Errorf("failed creating key: %v", err)
}
data := map[string][]byte{
corev1.TLSCertKey: serverCert.Bytes(),
corev1.TLSPrivateKeyKey: serverKey.Bytes(),
}
return kubernetes.CreateSecret(builder.Name).
WithNamespace(builder.Namespace).
WithData(data).
Do(client)
}