This repository has been archived by the owner on Aug 13, 2020. It is now read-only.
/
tls.go
162 lines (141 loc) · 4.67 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
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
/*
Copyright 2017 Codem8s.
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.
*/
package generate
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"log"
"math/big"
"net"
"os"
"time"
)
const (
// CertificatesDestinationFolder point to the directory where certificates will be created
CertificatesDestinationFolder = "resources/"
cnBase = "codem8s_autograph"
certificateBits = 2048
twoYears = 2
caFile = CertificatesDestinationFolder + "ca.pem"
caKeyFile = CertificatesDestinationFolder + "ca.key"
fileMode = 0600
)
// TLSCertificates generate TLS certificates and put them in resources directory
func TLSCertificates() {
os.MkdirAll(CertificatesDestinationFolder, 0700)
generateCA()
caPair, err := tls.LoadX509KeyPair(caFile, caKeyFile)
if err != nil {
log.Fatal(err)
}
// The subjectAltName/IP address in the certificate MUST match the one configured on the Kubernetes Service.
kubernetesServiceIP := []net.IP{net.ParseIP("10.0.0.231")}
generateCertificate("client", caPair, kubernetesServiceIP)
generateCertificate("server", caPair, kubernetesServiceIP)
}
func generateSerialNumber() *big.Int {
serialNumber, err := rand.Int(rand.Reader, (&big.Int{}).Exp(big.NewInt(2), big.NewInt(159), nil))
if err != nil {
log.Fatal(err)
}
return serialNumber
}
func generateCA() {
serialNumber := generateSerialNumber()
ca := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: cnBase + "_ca",
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(twoYears, 0, 0),
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
privateKey, err := rsa.GenerateKey(rand.Reader, certificateBits)
if err != nil {
log.Fatal(err)
}
publicKey := &privateKey.PublicKey
caBinary, err := x509.CreateCertificate(rand.Reader, ca, ca, publicKey, privateKey)
if err != nil {
log.Fatal(err)
}
certOut, err := os.Create(caFile)
if err != nil {
log.Fatal(err)
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: caBinary})
err = certOut.Close()
if err != nil {
log.Fatal(err)
}
log.Print(caFile + " created \n")
// Private key
keyOut, err := os.OpenFile(caKeyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileMode)
if err != nil {
log.Fatal(err)
}
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
err = keyOut.Close()
if err != nil {
log.Fatal(err)
}
log.Print(caKeyFile + " created \n")
}
func generateCertificate(name string, caPair tls.Certificate, kubernetesServiceIP []net.IP) {
ca, err := x509.ParseCertificate(caPair.Certificate[0])
if err != nil {
log.Fatal(err)
}
serialNumber := generateSerialNumber()
// Prepare certificate
cert := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: cnBase + "_" + name,
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(twoYears, 0, 0),
SubjectKeyId: []byte{1, 2, 3, 4, 6},
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
IPAddresses: kubernetesServiceIP,
IsCA: false,
}
priv, err := rsa.GenerateKey(rand.Reader, certificateBits)
if err != nil {
log.Fatal(err)
}
pub := &priv.PublicKey
// Sign the certificate
certBinary, err := x509.CreateCertificate(rand.Reader, cert, ca, pub, caPair.PrivateKey)
// Public key
pemFile := CertificatesDestinationFolder + name + ".pem"
certOut, err := os.Create(pemFile)
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certBinary})
certOut.Close()
log.Print(pemFile + " created \n")
// Private key
keyFile := CertificatesDestinationFolder + name + ".key"
keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileMode)
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
keyOut.Close()
log.Print(keyFile + " created \n")
}