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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/*
Copyright 2020 The KubeEdge Authors.
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 httpserver
import (
"crypto"
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"strings"
"github.com/emicklei/go-restful"
"github.com/golang-jwt/jwt"
certutil "k8s.io/client-go/util/cert"
"k8s.io/klog/v2"
hubconfig "github.com/kubeedge/kubeedge/cloud/pkg/cloudhub/config"
"github.com/kubeedge/kubeedge/common/constants"
)
// StartHTTPServer starts the http service
func StartHTTPServer() {
serverContainer := restful.NewContainer()
ws := new(restful.WebService)
ws.Path("/")
ws.Route(ws.GET(constants.DefaultCertURL).To(edgeCoreClientCert))
ws.Route(ws.GET(constants.DefaultCAURL).To(getCA))
ws.Route(ws.POST(constants.DefaultNodeUpgradeURL).To(upgradeEdge))
serverContainer.Add(ws)
addr := fmt.Sprintf("%s:%d", hubconfig.Config.HTTPS.Address, hubconfig.Config.HTTPS.Port)
cert, err := tls.X509KeyPair(pem.EncodeToMemory(&pem.Block{Type: certutil.CertificateBlockType, Bytes: hubconfig.Config.Cert}), pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: hubconfig.Config.Key}))
if err != nil {
klog.Exit(err)
}
server := &http.Server{
Addr: addr,
Handler: serverContainer,
TLSConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.RequestClientCert,
},
}
klog.Exit(server.ListenAndServeTLS("", ""))
}
// getCA returns the caCertDER
func getCA(request *restful.Request, response *restful.Response) {
caCertDER := hubconfig.Config.Ca
if _, err := response.Write(caCertDER); err != nil {
klog.Errorf("failed to write caCertDER, err: %v", err)
}
}
// EncodeCertPEM returns PEM-encoded certificate data
func EncodeCertPEM(cert *x509.Certificate) []byte {
block := pem.Block{
Type: certutil.CertificateBlockType,
Bytes: cert.Raw,
}
return pem.EncodeToMemory(&block)
}
// edgeCoreClientCert will verify the certificate of EdgeCore or token then create EdgeCoreCert and return it
func edgeCoreClientCert(request *restful.Request, response *restful.Response) {
if cert := request.Request.TLS.PeerCertificates; len(cert) > 0 {
if err := verifyCert(cert[0]); err != nil {
klog.Errorf("failed to sign the certificate for edgenode: %s, failed to verify the certificate", request.Request.Header.Get(constants.NodeName))
response.WriteHeader(http.StatusUnauthorized)
if _, err := response.Write([]byte(err.Error())); err != nil {
klog.Errorf("failed to write response, err: %v", err)
}
} else {
signEdgeCert(response, request.Request)
}
return
}
if verifyAuthorization(response, request.Request) {
signEdgeCert(response, request.Request)
} else {
klog.Errorf("failed to sign the certificate for edgenode: %s, invalid token", request.Request.Header.Get(constants.NodeName))
}
}
// verifyCert verifies the edge certificate by CA certificate when edge certificates rotate.
func verifyCert(cert *x509.Certificate) error {
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM(pem.EncodeToMemory(&pem.Block{Type: certutil.CertificateBlockType, Bytes: hubconfig.Config.Ca}))
if !ok {
return fmt.Errorf("failed to parse root certificate")
}
opts := x509.VerifyOptions{
Roots: roots,
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
if _, err := cert.Verify(opts); err != nil {
return fmt.Errorf("failed to verify edge certificate: %v", err)
}
return nil
}
// verifyAuthorization verifies the token from EdgeCore CSR
func verifyAuthorization(w http.ResponseWriter, r *http.Request) bool {
authorizationHeader := r.Header.Get("authorization")
if authorizationHeader == "" {
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte("Invalid authorization token")); err != nil {
klog.Errorf("failed to write http response, err: %v", err)
}
return false
}
bearerToken := strings.Split(authorizationHeader, " ")
if len(bearerToken) != 2 {
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte("Invalid authorization token")); err != nil {
klog.Errorf("failed to write http response, err: %v", err)
}
return false
}
token, err := jwt.Parse(bearerToken[1], func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("there was an error")
}
caKey := hubconfig.Config.CaKey
return caKey, nil
})
if err != nil {
if err == jwt.ErrSignatureInvalid {
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte("Invalid authorization token")); err != nil {
klog.Errorf("Write body error %v", err)
}
return false
}
w.WriteHeader(http.StatusBadRequest)
if _, err := w.Write([]byte("Invalid authorization token")); err != nil {
klog.Errorf("Write body error %v", err)
}
return false
}
if !token.Valid {
w.WriteHeader(http.StatusUnauthorized)
if _, err := w.Write([]byte("Invalid authorization token")); err != nil {
klog.Errorf("Write body error %v", err)
}
return false
}
return true
}
// signEdgeCert signs the CSR from EdgeCore
func signEdgeCert(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, constants.MaxRespBodyLength)
csrContent, err := io.ReadAll(r.Body)
if err != nil {
klog.Errorf("fail to read file when signing the cert for edgenode:%s! error:%v", r.Header.Get(constants.NodeName), err)
return
}
csr, err := x509.ParseCertificateRequest(csrContent)
if err != nil {
klog.Errorf("fail to ParseCertificateRequest of edgenode: %s! error:%v", r.Header.Get(constants.NodeName), err)
return
}
usagesStr := r.Header.Get("ExtKeyUsages")
var usages []x509.ExtKeyUsage
if usagesStr == "" {
usages = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
} else {
err := json.Unmarshal([]byte(usagesStr), &usages)
if err != nil {
klog.Errorf("unmarshal http header ExtKeyUsages fail, err: %v", err)
return
}
}
klog.V(4).Infof("receive sign crt request, ExtKeyUsages: %v", usages)
clientCertDER, err := signCerts(csr.Subject, csr.PublicKey, usages)
if err != nil {
klog.Errorf("fail to signCerts for edgenode:%s! error:%v", r.Header.Get(constants.NodeName), err)
return
}
if _, err := w.Write(clientCertDER); err != nil {
klog.Errorf("write error %v", err)
}
}
// signCerts will create a certificate for EdgeCore
func signCerts(subInfo pkix.Name, pbKey crypto.PublicKey, usages []x509.ExtKeyUsage) ([]byte, error) {
cfgs := &certutil.Config{
CommonName: subInfo.CommonName,
Organization: subInfo.Organization,
Usages: usages,
}
clientKey := pbKey
ca := hubconfig.Config.Ca
caCert, err := x509.ParseCertificate(ca)
if err != nil {
return nil, fmt.Errorf("unable to ParseCertificate: %v", err)
}
var caKey crypto.Signer
caKeyDER := hubconfig.Config.CaKey
caKey, err = x509.ParseECPrivateKey(caKeyDER)
if err != nil {
caKey, err = x509.ParsePKCS1PrivateKey(caKeyDER)
if err != nil {
return nil, fmt.Errorf("unable to Parse PrivateKey: %v", err)
}
}
edgeCertSigningDuration := hubconfig.Config.CloudHub.EdgeCertSigningDuration
certDER, err := NewCertFromCa(cfgs, caCert, clientKey, caKey, edgeCertSigningDuration) //crypto.Signer(caKey)
if err != nil {
return nil, fmt.Errorf("unable to NewCertFromCa: %v", err)
}
return certDER, err
}
// PrepareAllCerts check whether the certificates exist in the local directory,
// and then check whether certificates exist in the secret, generate if they don't exist
func PrepareAllCerts() error {
// Check whether the ca exists in the local directory
if hubconfig.Config.Ca == nil && hubconfig.Config.CaKey == nil {
var caDER, caKeyDER []byte
klog.Info("Ca and CaKey don't exist in local directory, and will read from the secret")
// Check whether the ca exists in the secret
caSecret, err := GetSecret(CaSecretName, constants.SystemNamespace)
if err != nil {
klog.Info("Ca and CaKey don't exist in the secret, and will be created by CloudCore")
var caKey crypto.Signer
caDER, caKey, err = NewCertificateAuthorityDer()
if err != nil {
klog.Errorf("failed to create Certificate Authority, error: %v", err)
return err
}
caKeyDER, err = x509.MarshalECPrivateKey(caKey.(*ecdsa.PrivateKey))
if err != nil {
klog.Errorf("failed to convert an EC private key to SEC 1, ASN.1 DER form, error: %v", err)
return err
}
err = CreateCaSecret(caDER, caKeyDER)
if err != nil {
klog.Errorf("failed to create ca to secrets, error: %v", err)
return err
}
} else {
caDER = caSecret.Data[CaDataName]
caKeyDER = caSecret.Data[CaKeyDataName]
}
UpdateConfig(caDER, caKeyDER, nil, nil)
} else {
// HubConfig has been initialized
if err := CreateCaSecret(hubconfig.Config.Ca, hubconfig.Config.CaKey); err != nil {
klog.Errorf("failed to save ca and key to the secret, error: %v", err)
return err
}
}
// Check whether the CloudCore certificates exist in the local directory
if hubconfig.Config.Key == nil && hubconfig.Config.Cert == nil {
klog.Infof("CloudCoreCert and key don't exist in local directory, and will read from the secret")
// Check whether the CloudCore certificates exist in the secret
var certDER, keyDER []byte
cloudSecret, err := GetSecret(CloudCoreSecretName, constants.SystemNamespace)
if err != nil {
klog.Info("CloudCoreCert and key don't exist in the secret, and will be signed by CA")
certDER, keyDER, err = SignCerts()
if err != nil {
klog.Errorf("failed to sign a certificate, error: %v", err)
return err
}
err = CreateCloudCoreSecret(certDER, keyDER)
if err != nil {
klog.Errorf("failed to save CloudCore cert and key to secret, error: %v", err)
return err
}
} else {
certDER = cloudSecret.Data[CloudCoreCertName]
keyDER = cloudSecret.Data[CloudCoreKeyDataName]
}
UpdateConfig(nil, nil, certDER, keyDER)
} else {
// HubConfig has been initialized
if err := CreateCloudCoreSecret(hubconfig.Config.Cert, hubconfig.Config.Key); err != nil {
klog.Errorf("failed to save CloudCore cert to secret, error: %v", err)
return err
}
}
return nil
}
|