-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca.go
252 lines (218 loc) · 8.91 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
// 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 (
"encoding/base64"
"encoding/pem"
"fmt"
"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"
// 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
}
// NewSelfSignedIstioCAOptions returns a new IstioCAOptions instance using self-signed certificate.
func NewSelfSignedIstioCAOptions(caCertTTL, certTTL, maxCertTTL time.Duration, org string, dualUse bool,
namespace string, client corev1.CoreV1Interface) (caOpts *IstioCAOptions, err error) {
// For the first time the CA is up, 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{})
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)
}
if caOpts.KeyCertBundle, err = util.NewVerifiedKeyCertBundleFromPem(pemCert, pemKey, nil, pemCert); 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 {
if caOpts.KeyCertBundle, err = util.NewVerifiedKeyCertBundleFromPem(caSecret.Data[caCertID],
caSecret.Data[caPrivateKeyID], nil, caSecret.Data[caCertID]); 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)
}
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)
}