summaryrefslogtreecommitdiff
path: root/edge/pkg/edgehub/certificate/certmanager.go
blob: 5a32a6938557ea8f4e60c314dab88bcbafc40058 (about) (plain)
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
package certificate

import (
	"bytes"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"fmt"
	"io"
	nethttp "net/http"
	"os"
	"time"

	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/util/cert"
	certutil "k8s.io/client-go/util/cert"
	"k8s.io/client-go/util/keyutil"
	"k8s.io/klog/v2"

	"github.com/kubeedge/api/apis/componentconfig/edgecore/v1alpha2"
	"github.com/kubeedge/kubeedge/common/constants"
	"github.com/kubeedge/kubeedge/edge/pkg/edgehub/common/http"
	"github.com/kubeedge/kubeedge/pkg/security/certs"
	"github.com/kubeedge/kubeedge/pkg/security/token"
)

// jitteryDuration uses some jitter to set the rotation threshold so each node
// will rotate at approximately 70-90% of the total lifetime of the
// certificate.  With jitter, if a number of nodes are added to a cluster at
// approximately the same time (such as cluster creation time), they won't all
// try to rotate certificates at the same time for the rest of the life of the
// cluster.
//
// This function is represented as a variable to allow replacement during testing.
var jitteryDuration = func(totalDuration float64) time.Duration {
	return wait.Jitter(time.Duration(totalDuration), 0.2) - time.Duration(totalDuration*0.3)
}

var CleanupTokenChan = make(chan struct{}, 1)

type CertManager struct {
	RotateCertificates bool
	NodeName           string

	caFile   string
	certFile string
	keyFile  string

	token string
	// Set to time.Now but can be stubbed out for testing
	now func() time.Time

	caURL   string
	certURL string
	Done    chan struct{}
}

// NewCertManager creates a CertManager for edge certificate management according to EdgeHub config
func NewCertManager(edgehub v1alpha2.EdgeHub, nodename string) CertManager {
	return CertManager{
		RotateCertificates: edgehub.RotateCertificates,
		NodeName:           nodename,
		token:              edgehub.Token,
		caFile:             edgehub.TLSCAFile,
		certFile:           edgehub.TLSCertFile,
		keyFile:            edgehub.TLSPrivateKeyFile,
		now:                time.Now,
		caURL:              edgehub.HTTPServer + constants.DefaultCAURL,
		certURL:            edgehub.HTTPServer + constants.DefaultCertURL,
		Done:               make(chan struct{}),
	}
}

// Start starts the CertManager
func (cm *CertManager) Start() {
	if _, err := cm.getCurrent(); err != nil {
		klog.Infof("unable to get the current edge certs, reason: %v", err)
		if err = cm.applyCerts(); err != nil {
			klog.Exitf("failed to apply the edge certs, err: %v", err)
		}
		// inform to cleanup token in configuration edgecore.yaml
		CleanupTokenChan <- struct{}{}
	}
	if cm.RotateCertificates {
		cm.rotate()
	}
}

// getCurrent returns current edge certificate
func (cm *CertManager) getCurrent() (*tls.Certificate, error) {
	cert, err := tls.LoadX509KeyPair(cm.certFile, cm.keyFile)
	if err != nil {
		return nil, err
	}
	certs, err := x509.ParseCertificates(cert.Certificate[0])
	if err != nil {
		return nil, fmt.Errorf("unable to parse certificate data: %v", err)
	}
	cert.Leaf = certs[0]
	return &cert, nil
}

// applyCerts realizes the certificate application by token
func (cm *CertManager) applyCerts() error {
	cacert, err := GetCACert(cm.caURL)
	if err != nil {
		return fmt.Errorf("failed to get CA certificate, err: %v", err)
	}

	// validate the CA certificate by hashcode
	realToken, err := token.VerifyCAAndGetRealToken(cm.token, cacert)
	if err != nil {
		return err
	}

	// save the ca.crt to file
	caPem, err := certs.WriteDERToPEMFile(cm.caFile, cert.CertificateBlockType, cacert)
	if err != nil {
		return fmt.Errorf("failed to save the CA certificate to file: %s, error: %v", cm.caFile, err)
	}
	certDER, keyDER, err := cm.GetEdgeCert(cm.certURL, pem.EncodeToMemory(caPem), tls.Certificate{}, realToken)
	if err != nil {
		return fmt.Errorf("failed to get edge certificate from the cloudcore, error: %v", err)
	}
	// save the edge.crt to the file
	if _, err := certs.WriteDERToPEMFile(cm.certFile,
		certutil.CertificateBlockType, certDER); err != nil {
		return fmt.Errorf("failed to save the certificate file %s, err: %v", cm.certFile, err)
	}
	if _, err := certs.WriteDERToPEMFile(cm.keyFile,
		keyutil.ECPrivateKeyBlockType, keyDER); err != nil {
		return fmt.Errorf("failed to save the certificate key file %s, err: %v", cm.keyFile, err)
	}
	return nil
}

// rotate starts edge certificate rotation process
func (cm *CertManager) rotate() {
	klog.Infof("Certificate rotation is enabled.")
	go wait.Forever(func() {
		deadline, err := cm.nextRotationDeadline()
		if err != nil {
			klog.Errorf("failed to get next rotation deadline:%v", err)
		}
		if sleepInterval := deadline.Sub(cm.now()); sleepInterval > 0 {
			klog.V(2).Infof("Waiting %v for next certificate rotation", sleepInterval)

			timer := time.NewTimer(sleepInterval)
			defer timer.Stop()

			<-timer.C // unblock when deadline expires
		}

		backoff := wait.Backoff{
			Duration: 2 * time.Second,
			Factor:   2,
			Jitter:   0.1,
			Steps:    5,
		}
		if err := wait.ExponentialBackoff(backoff, cm.rotateCert); err != nil {
			utilruntime.HandleError(fmt.Errorf("reached backoff limit, still unable to rotate certs: %v", err))
			wait.PollInfinite(32*time.Second, cm.rotateCert)
		}
	}, time.Second)
}

// nextRotationDeadline returns the rotation deadline. It is different in every rotation.
func (cm *CertManager) nextRotationDeadline() (time.Time, error) {
	cert, err := cm.getCurrent()
	if err != nil {
		return time.Time{}, fmt.Errorf("faild to get current certificate")
	}
	notAfter := cert.Leaf.NotAfter
	totalDuration := float64(notAfter.Sub(cert.Leaf.NotBefore))
	deadline := cert.Leaf.NotBefore.Add(jitteryDuration(totalDuration))
	klog.V(2).Infof("Certificate expiration is %v, rotation deadline is %v", notAfter, deadline)

	return deadline, nil
}

// rotateCert realizes the specific process of edge certificate rotation.
func (cm *CertManager) rotateCert() (bool, error) {
	klog.Infof("Rotating certificates")

	tlsCert, err := cm.getCurrent()
	if err != nil {
		klog.Errorf("failed to get current certificate:%v", err)
		return false, nil
	}
	caPem, err := cm.getCA()
	if err != nil {
		klog.Errorf("failed to get CA certificate locally:%v", err)
		return false, nil
	}
	certDER, keyDER, err := cm.GetEdgeCert(cm.certURL, caPem, *tlsCert, "")
	if err != nil {
		klog.Errorf("failed to get edge certificate from CloudCore:%v", err)
		return false, nil
	}
	if _, err := certs.WriteDERToPEMFile(cm.certFile,
		certutil.CertificateBlockType, certDER); err != nil {
		klog.Errorf("failed to save the certificate file %s, err: %v", cm.certFile, err)
		return false, nil
	}
	if _, err := certs.WriteDERToPEMFile(cm.keyFile,
		keyutil.ECPrivateKeyBlockType, keyDER); err != nil {
		klog.Errorf("failed to save the certificate key file %s, err: %v", cm.keyFile, err)
		return false, nil
	}

	klog.Info("succeeded to rotate certificate")

	cm.Done <- struct{}{}

	return true, nil
}

// getCA returns the CA in pem format.
func (cm *CertManager) getCA() ([]byte, error) {
	return os.ReadFile(cm.caFile)
}

// GetCACert gets the cloudcore CA certificate
func GetCACert(url string) ([]byte, error) {
	client := http.NewHTTPClient()
	req, err := http.BuildRequest(nethttp.MethodGet, url, nil, "", "")
	if err != nil {
		return nil, err
	}
	res, err := http.SendRequest(req, client)
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	caCert, err := io.ReadAll(io.LimitReader(res.Body, constants.MaxRespBodyLength))
	if err != nil {
		return nil, err
	}

	return caCert, nil
}

// GetEdgeCert applies for the certificate from cloudcore
func (cm *CertManager) GetEdgeCert(url string, capem []byte, tlscert tls.Certificate, token string,
) ([]byte, []byte, error) {
	h := certs.GetHandler(certs.HandlerTypeX509)
	pkw, err := h.GenPrivateKey()
	if err != nil {
		return nil, nil, fmt.Errorf("failed to generate a private key of edge cert, err: %v", err)
	}
	csrPem, err := h.CreateCSR(pkix.Name{
		Country:      []string{"CN"},
		Organization: []string{"system:nodes"},
		Locality:     []string{"Hangzhou"},
		Province:     []string{"Zhejiang"},
		CommonName:   fmt.Sprintf("system:node:%s", cm.NodeName),
	}, pkw, nil)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to create a csr of edge cert, err %v", err)
	}

	client, err := http.NewHTTPClientWithCA(capem, tlscert)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to create a http client, err: %v", err)
	}

	req, err := http.BuildRequest(nethttp.MethodGet, url, bytes.NewReader(csrPem.Bytes), token, cm.NodeName)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to generate a http request, err: %v", err)
	}

	res, err := http.SendRequest(req, client)
	if err != nil {
		return nil, nil, fmt.Errorf("failed to request the cloudcore server, err: %v", err)
	}
	defer res.Body.Close()

	content, err := io.ReadAll(io.LimitReader(res.Body, constants.MaxRespBodyLength))
	if err != nil {
		return nil, nil, fmt.Errorf("failed to read response body, err: %v", err)
	}
	if res.StatusCode != nethttp.StatusOK {
		return nil, nil, fmt.Errorf("failed to call http, code: %d, message: %s", res.StatusCode, string(content))
	}
	return content, pkw.DER(), nil
}