forked from istio/istio
/
ca.go
330 lines (291 loc) · 11.7 KB
/
ca.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
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
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2017 Istio 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 ca
import (
"context"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"strings"
"time"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/probe"
"istio.io/istio/security/pkg/k8s/configmap"
"istio.io/istio/security/pkg/pki/util"
)
const (
// istioCASecretType is the Istio secret annotation type.
istioCASecretType = "istio.io/ca-root"
// caCertID is the CA certificate chain file.
caCertID = "ca-cert.pem"
// caPrivateKeyID is the private key file of CA.
caPrivateKeyID = "ca-key.pem"
// CASecret stores the key/cert of self-signed CA for persistency purpose.
CASecret = "istio-ca-secret"
// CertChainID is the ID/name for the certificate chain file.
CertChainID = "cert-chain.pem"
// PrivateKeyID is the ID/name for the private key file.
PrivateKeyID = "key.pem"
// RootCertID is the ID/name for the CA root certificate file.
RootCertID = "root-cert.pem"
// ServiceAccountNameAnnotationKey is the key to specify corresponding service account in the annotation of K8s secrets.
ServiceAccountNameAnnotationKey = "istio.io/service-account.name"
// ReadSigningCertCheckInterval specifies the time to wait between retries on reading the signing key and cert.
ReadSigningCertCheckInterval = time.Second * 5
// The size of a private key for a self-signed Istio CA.
caKeySize = 2048
)
// caTypes is the enum for the CA type.
type caTypes int
const (
// selfSignedCA means the Istio CA uses a self signed certificate.
selfSignedCA caTypes = iota
// pluggedCertCA means the Istio CA uses a operator-specified key/cert.
pluggedCertCA
)
// CertificateAuthority contains methods to be supported by a CA.
type CertificateAuthority interface {
// Sign generates a certificate for a workload or CA, from the given CSR and TTL.
// TODO(myidpt): simplify this interface and pass a struct with cert field values instead.
Sign(csrPEM []byte, subjectIDs []string, ttl time.Duration, forCA bool) ([]byte, error)
// GetCAKeyCertBundle returns the KeyCertBundle used by CA.
GetCAKeyCertBundle() util.KeyCertBundle
}
// IstioCAOptions holds the configurations for creating an Istio CA.
// TODO(myidpt): remove IstioCAOptions.
type IstioCAOptions struct {
CAType caTypes
CertTTL time.Duration
MaxCertTTL time.Duration
KeyCertBundle util.KeyCertBundle
LivenessProbeOptions *probe.Options
ProbeCheckInterval time.Duration
}
// IstioCA generates keys and certificates for Istio identities.
type IstioCA struct {
certTTL time.Duration
maxCertTTL time.Duration
keyCertBundle util.KeyCertBundle
livenessProbe *probe.Probe
}
// Append root certificates in rootCertFile to the input certificate.
func appendRootCerts(pemCert []byte, rootCertFile string) ([]byte, error) {
var rootCerts []byte
if len(pemCert) > 0 {
// Copy the input certificate
rootCerts = make([]byte, len(pemCert))
copy(rootCerts, pemCert)
}
if len(rootCertFile) > 0 {
log.Debugf("append root certificates from %v", rootCertFile)
certBytes, err := ioutil.ReadFile(rootCertFile)
if err != nil {
return rootCerts, fmt.Errorf("failed to read root certificates (%v)", err)
}
log.Debugf("The root certificates to be appended is: %v", rootCertFile)
if len(rootCerts) > 0 {
// Append a newline after the last cert
rootCerts = []byte(strings.TrimSuffix(string(rootCerts), "\n") + "\n")
}
rootCerts = append(rootCerts, certBytes...)
}
return rootCerts, nil
}
// NewSelfSignedIstioCAOptions returns a new IstioCAOptions instance using self-signed certificate.
func NewSelfSignedIstioCAOptions(ctx context.Context, caCertTTL, certTTL, maxCertTTL time.Duration, org string, dualUse bool,
namespace string, readCertRetryInterval time.Duration, client corev1.CoreV1Interface, rootCertFile string) (caOpts *IstioCAOptions, err error) {
// For the first time the CA is up, if readSigningCertOnly is unset,
// it generates a self-signed key/cert pair and write it to CASecret.
// For subsequent restart, CA will reads key/cert from CASecret.
caSecret, scrtErr := client.Secrets(namespace).Get(CASecret, metav1.GetOptions{})
if scrtErr != nil && readCertRetryInterval > 0 {
log.Infof("Citadel in signing key/cert read only mode. Wait until secret %s:%s can be loaded...", namespace, CASecret)
ticker := time.NewTicker(readCertRetryInterval)
for scrtErr != nil {
select {
case <-ticker.C:
if caSecret, scrtErr = client.Secrets(namespace).Get(CASecret, metav1.GetOptions{}); scrtErr == nil {
log.Infof("Citadel successfully loaded the secret.")
break
}
case <-ctx.Done():
log.Errorf("Secret waiting thread is terminated.")
return nil, fmt.Errorf("secret waiting thread is terminated")
}
}
}
caOpts = &IstioCAOptions{
CAType: selfSignedCA,
CertTTL: certTTL,
MaxCertTTL: maxCertTTL,
}
if scrtErr != nil {
log.Infof("Failed to get secret (error: %s), will create one", scrtErr)
options := util.CertOptions{
TTL: caCertTTL,
Org: org,
IsCA: true,
IsSelfSigned: true,
RSAKeySize: caKeySize,
IsDualUse: dualUse,
}
pemCert, pemKey, ckErr := util.GenCertKeyFromOptions(options)
if ckErr != nil {
return nil, fmt.Errorf("unable to generate CA cert and key for self-signed CA (%v)", ckErr)
}
rootCerts, err := appendRootCerts(pemCert, rootCertFile)
if err != nil {
return nil, fmt.Errorf("failed to append root certificates (%v)", err)
}
if caOpts.KeyCertBundle, err = util.NewVerifiedKeyCertBundleFromPem(pemCert, pemKey, nil, rootCerts); err != nil {
return nil, fmt.Errorf("failed to create CA KeyCertBundle (%v)", err)
}
// Rewrite the key/cert back to secret so they will be persistent when CA restarts.
secret := BuildSecret("", CASecret, namespace, nil, nil, nil, pemCert, pemKey, istioCASecretType)
if _, err = client.Secrets(namespace).Create(secret); err != nil {
log.Errorf("Failed to write secret to CA (error: %s). This CA will not persist when restart.", err)
}
} else {
log.Infof("Load signing key and cert from existing secret %s:%s", caSecret.Namespace, caSecret.Name)
rootCerts, err := appendRootCerts(caSecret.Data[caCertID], rootCertFile)
if err != nil {
return nil, fmt.Errorf("failed to append root certificates (%v)", err)
}
if caOpts.KeyCertBundle, err = util.NewVerifiedKeyCertBundleFromPem(caSecret.Data[caCertID],
caSecret.Data[caPrivateKeyID], nil, rootCerts); err != nil {
return nil, fmt.Errorf("failed to create CA KeyCertBundle (%v)", err)
}
}
if err = updateCertInConfigmap(namespace, client, caOpts.KeyCertBundle); err != nil {
log.Errorf("Failed to write Citadel cert to configmap (%v). Node agents will not be able to connect.", err)
}
return caOpts, nil
}
// NewPluggedCertIstioCAOptions returns a new IstioCAOptions instance using given certificate.
func NewPluggedCertIstioCAOptions(certChainFile, signingCertFile, signingKeyFile, rootCertFile string,
certTTL, maxCertTTL time.Duration, namespace string, client corev1.CoreV1Interface) (caOpts *IstioCAOptions, err error) {
caOpts = &IstioCAOptions{
CAType: pluggedCertCA,
CertTTL: certTTL,
MaxCertTTL: maxCertTTL,
}
if caOpts.KeyCertBundle, err = util.NewVerifiedKeyCertBundleFromFile(
signingCertFile, signingKeyFile, certChainFile, rootCertFile); err != nil {
return nil, fmt.Errorf("failed to create CA KeyCertBundle (%v)", err)
}
// Validate that the passed in signing cert can be used as CA.
// The check can't be done inside `KeyCertBundle`, since bundle could also be used to
// validate workload certificates (i.e., where the leaf certificate is not a CA).
b, err := ioutil.ReadFile(signingCertFile)
if err != nil {
return nil, err
}
block, _ := pem.Decode(b)
if block == nil {
return nil, fmt.Errorf("invalid PEM encoded certificate")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse X.509 certificate")
}
if !cert.IsCA {
return nil, fmt.Errorf("certificate is not authorized to sign other certificates")
}
if err = updateCertInConfigmap(namespace, client, caOpts.KeyCertBundle); err != nil {
log.Errorf("Failed to write Citadel cert to configmap (%v). Node agents will not be able to connect.", err)
}
return caOpts, nil
}
// NewIstioCA returns a new IstioCA instance.
func NewIstioCA(opts *IstioCAOptions) (*IstioCA, error) {
ca := &IstioCA{
certTTL: opts.CertTTL,
maxCertTTL: opts.MaxCertTTL,
keyCertBundle: opts.KeyCertBundle,
livenessProbe: probe.NewProbe(),
}
return ca, nil
}
// Sign takes a PEM-encoded CSR, subject IDs and lifetime, and returns a signed certificate. If forCA is true,
// the signed certificate is a CA certificate, otherwise, it is a workload certificate.
// TODO(myidpt): Add error code to identify the Sign error types.
func (ca *IstioCA) Sign(csrPEM []byte, subjectIDs []string, requestedLifetime time.Duration, forCA bool) ([]byte, error) {
signingCert, signingKey, _, _ := ca.keyCertBundle.GetAll()
if signingCert == nil {
return nil, NewError(CANotReady, fmt.Errorf("Istio CA is not ready")) // nolint
}
csr, err := util.ParsePemEncodedCSR(csrPEM)
if err != nil {
return nil, NewError(CSRError, err)
}
lifetime := requestedLifetime
// If the requested requestedLifetime is non-positive, apply the default TTL.
if requestedLifetime.Seconds() <= 0 {
lifetime = ca.certTTL
}
// If the requested TTL is greater than maxCertTTL, return an error
if requestedLifetime.Seconds() > ca.maxCertTTL.Seconds() {
return nil, NewError(TTLError, fmt.Errorf(
"requested TTL %s is greater than the max allowed TTL %s", requestedLifetime, ca.maxCertTTL))
}
certBytes, err := util.GenCertFromCSR(csr, signingCert, csr.PublicKey, *signingKey, subjectIDs, lifetime, forCA)
if err != nil {
return nil, NewError(CertGenError, err)
}
block := &pem.Block{
Type: "CERTIFICATE",
Bytes: certBytes,
}
cert := pem.EncodeToMemory(block)
return cert, nil
}
// GetCAKeyCertBundle returns the KeyCertBundle for the CA.
func (ca *IstioCA) GetCAKeyCertBundle() util.KeyCertBundle {
return ca.keyCertBundle
}
// BuildSecret returns a secret struct, contents of which are filled with parameters passed in.
func BuildSecret(saName, scrtName, namespace string, certChain, privateKey, rootCert, caCert, caPrivateKey []byte, secretType v1.SecretType) *v1.Secret {
var ServiceAccountNameAnnotation map[string]string
if saName == "" {
ServiceAccountNameAnnotation = nil
} else {
ServiceAccountNameAnnotation = map[string]string{ServiceAccountNameAnnotationKey: saName}
}
return &v1.Secret{
Data: map[string][]byte{
CertChainID: certChain,
PrivateKeyID: privateKey,
RootCertID: rootCert,
caCertID: caCert,
caPrivateKeyID: caPrivateKey,
},
ObjectMeta: metav1.ObjectMeta{
Annotations: ServiceAccountNameAnnotation,
Name: scrtName,
Namespace: namespace,
},
Type: secretType,
}
}
func updateCertInConfigmap(namespace string, client corev1.CoreV1Interface, keyCertBundle util.KeyCertBundle) error {
_, _, _, cert := keyCertBundle.GetAllPem()
certEncoded := base64.StdEncoding.EncodeToString(cert)
cmc := configmap.NewController(namespace, client)
return cmc.InsertCATLSRootCert(certEncoded)
}