-
Notifications
You must be signed in to change notification settings - Fork 589
/
certificate.go
144 lines (123 loc) · 4.36 KB
/
certificate.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
package certificate
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"time"
"github.com/kong/kubernetes-ingress-controller/v3/test/consts"
)
type SelfSignedCertificateOptions struct {
CommonName string
DNSNames []string
CATrue bool
Expired bool
}
type SelfSignedCertificateOptionsDecorator func(SelfSignedCertificateOptions) SelfSignedCertificateOptions
func WithCommonName(commonName string) SelfSignedCertificateOptionsDecorator {
return func(opts SelfSignedCertificateOptions) SelfSignedCertificateOptions {
opts.CommonName = commonName
return opts
}
}
func WithDNSNames(dnsNames ...string) SelfSignedCertificateOptionsDecorator {
return func(opts SelfSignedCertificateOptions) SelfSignedCertificateOptions {
opts.DNSNames = append(opts.DNSNames, dnsNames...)
return opts
}
}
// WithCATrue allows to use returned certificate to sign other certificates (uses BasicConstraints extension).
func WithCATrue() SelfSignedCertificateOptionsDecorator {
return func(opts SelfSignedCertificateOptions) SelfSignedCertificateOptions {
opts.CATrue = true
return opts
}
}
func WithAlreadyExpired() SelfSignedCertificateOptionsDecorator {
return func(opts SelfSignedCertificateOptions) SelfSignedCertificateOptions {
opts.Expired = true
return opts
}
}
// MustGenerateSelfSignedCert generates a tls.Certificate struct to be used in TLS client/listener configurations.
// Certificate is self-signed thus returned cert can be used as CA for it.
func MustGenerateSelfSignedCert(decorators ...SelfSignedCertificateOptionsDecorator) tls.Certificate {
// Generate a new RSA private key.
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
panic(fmt.Sprintf("Failed to generate RSA key: %s", err))
}
options := SelfSignedCertificateOptions{
CommonName: "",
DNSNames: []string{},
}
for _, decorator := range decorators {
options = decorator(options)
}
notBefore := time.Now()
notAfter := notBefore.AddDate(1, 0, 0)
if options.Expired {
notBefore = notBefore.AddDate(-2, 0, 0)
notAfter = notAfter.AddDate(-2, 0, 0)
}
// Create a self-signed X.509 certificate.
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
Organization: []string{"Kong HQ"},
Country: []string{"US"},
Province: []string{"California"},
Locality: []string{"San Francisco"},
StreetAddress: []string{"150 Spear Street, Suite 1600"},
PostalCode: []string{"94105"},
CommonName: options.CommonName,
},
NotBefore: notBefore,
NotAfter: notAfter,
DNSNames: options.DNSNames,
BasicConstraintsValid: true,
IsCA: options.CATrue,
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, &privateKey.PublicKey, privateKey)
if err != nil {
panic(fmt.Sprintf("Failed to create x509 certificate: %s", err))
}
// Create a tls.Certificate from the generated private key and certificate.
certificate := tls.Certificate{
Certificate: [][]byte{derBytes},
PrivateKey: privateKey,
}
return certificate
}
// MustGenerateSelfSignedCertPEMFormat generates self-signed certificate
// and returns certificate and key in PEM format. Certificate is self-signed
// thus returned cert can be used as CA for it.
func MustGenerateSelfSignedCertPEMFormat(decorators ...SelfSignedCertificateOptionsDecorator) (cert []byte, key []byte) {
tlsCert := MustGenerateSelfSignedCert(decorators...)
certBlock := &pem.Block{
Type: "CERTIFICATE",
Bytes: tlsCert.Certificate[0],
}
privateKey, ok := tlsCert.PrivateKey.(*rsa.PrivateKey)
if !ok {
panic("Private Key should be convertible to *rsa.PrivateKey")
}
keyBlock := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
}
return pem.EncodeToMemory(certBlock), pem.EncodeToMemory(keyBlock)
}
var kongSystemServiceCert, kongSystemServiceKey = MustGenerateSelfSignedCertPEMFormat(
WithCommonName(fmt.Sprintf("*.%s.svc", consts.ControllerNamespace)),
WithDNSNames(fmt.Sprintf("*.%s.svc", consts.ControllerNamespace)),
)
// GetKongSystemSelfSignedCerts returns the self-signed certificate and key
// with CN=*.<controllerNamespace>.svc and subjectAltName=DNS:*.<controllerNamespace>.svc.
func GetKongSystemSelfSignedCerts() (cert []byte, key []byte) {
return kongSystemServiceCert, kongSystemServiceKey
}