-
Notifications
You must be signed in to change notification settings - Fork 312
/
cacert.go
160 lines (138 loc) · 4.65 KB
/
cacert.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
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package manager
import (
"crypto/x509"
"encoding/pem"
"fmt"
"path/filepath"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/crypto"
"github.com/pingcap/tiup/pkg/utils"
)
func genAndSaveClusterCA(name, tlsPath string) (*crypto.CertificateAuthority, error) {
ca, err := crypto.NewCA(name)
if err != nil {
return nil, err
}
// save CA private key
if err := utils.SaveFileWithBackup(filepath.Join(tlsPath, spec.TLSCAKey), ca.Key.Pem(), ""); err != nil {
return nil, perrs.Annotatef(err, "cannot save CA private key for %s", name)
}
// save CA certificate
if err := utils.SaveFileWithBackup(
filepath.Join(tlsPath, spec.TLSCACert),
pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: ca.Cert.Raw,
}), ""); err != nil {
return nil, perrs.Annotatef(err, "cannot save CA certificate for %s", name)
}
return ca, nil
}
func genAndSaveClientCert(ca *crypto.CertificateAuthority, name, tlsPath string) error {
privKey, err := crypto.NewKeyPair(crypto.KeyTypeRSA, crypto.KeySchemeRSASSAPSSSHA256)
if err != nil {
return err
}
// save client private key
if err := utils.SaveFileWithBackup(filepath.Join(tlsPath, spec.TLSClientKey), privKey.Pem(), ""); err != nil {
return perrs.Annotatef(err, "cannot save client private key for %s", name)
}
csr, err := privKey.CSR(
"tiup-cluster-client",
fmt.Sprintf("%s-client", name),
[]string{}, []string{},
)
if err != nil {
return perrs.Annotatef(err, "cannot generate CSR of client certificate for %s", name)
}
cert, err := ca.Sign(csr)
if err != nil {
return perrs.Annotatef(err, "cannot sign client certificate for %s", name)
}
// save client certificate
if err := utils.SaveFileWithBackup(
filepath.Join(tlsPath, spec.TLSClientCert),
pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
}), ""); err != nil {
return perrs.Annotatef(err, "cannot save client PEM certificate for %s", name)
}
// save pfx format certificate
clientCert, err := x509.ParseCertificate(cert)
if err != nil {
return perrs.Annotatef(err, "cannot decode signed client certificate for %s", name)
}
pfxData, err := privKey.PKCS12(clientCert, ca)
if err != nil {
return perrs.Annotatef(err, "cannot encode client certificate to PKCS#12 format for %s", name)
}
if err := utils.SaveFileWithBackup(
filepath.Join(tlsPath, spec.PFXClientCert),
pfxData,
""); err != nil {
return perrs.Annotatef(err, "cannot save client PKCS#12 certificate for %s", name)
}
return nil
}
// genAndSaveCertificate generate CA and client cert for TLS enabled cluster
func (m *Manager) genAndSaveCertificate(clusterName string, globalOptions *spec.GlobalOptions) (*crypto.CertificateAuthority, error) {
var ca *crypto.CertificateAuthority
if globalOptions.TLSEnabled {
// generate CA
tlsPath := m.specManager.Path(clusterName, spec.TLSCertKeyDir)
if err := utils.CreateDir(tlsPath); err != nil {
return nil, err
}
ca, err := genAndSaveClusterCA(clusterName, tlsPath)
if err != nil {
return nil, err
}
// generate client cert
if err = genAndSaveClientCert(ca, clusterName, tlsPath); err != nil {
return nil, err
}
}
return ca, nil
}
// checkCertificate check if the certificate file exists
// no need to determine whether to enable tls
func (m *Manager) checkCertificate(clusterName string) error {
tlsFiles := []string{
m.specManager.Path(clusterName, spec.TLSCertKeyDir, spec.TLSCACert),
m.specManager.Path(clusterName, spec.TLSCertKeyDir, spec.TLSClientKey),
m.specManager.Path(clusterName, spec.TLSCertKeyDir, spec.TLSClientCert),
}
// check if the file exists
for _, file := range tlsFiles {
if !utils.IsExist(file) {
return perrs.Errorf("TLS file: %s does not exist", file)
}
}
return nil
}
// loadCertificate
// certificate file exists and reload is true
// will reload certificate file
func (m *Manager) loadCertificate(clusterName string, globalOptions *spec.GlobalOptions, reload bool) error {
err := m.checkCertificate(clusterName)
// no need to reload and the file already exists
if !reload && err == nil {
return nil
}
_, err = m.genAndSaveCertificate(clusterName, globalOptions)
return err
}