-
Notifications
You must be signed in to change notification settings - Fork 690
/
Copy pathsecret.go
256 lines (215 loc) · 6.73 KB
/
secret.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
// Copyright Project Contour 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 dag
import (
"bytes"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"strings"
core_v1 "k8s.io/api/core/v1"
)
const (
// CACertificateKey is the key name for accessing TLS CA certificate bundles in Kubernetes Secrets.
CACertificateKey = "ca.crt"
// CRLKey is the key name for accessing CRL bundles in Kubernetes Secrets.
CRLKey = "crl.pem"
)
// validTLSSecret returns an error if the Secret is not of type TLS or Opaque or
// if it doesn't contain valid certificate and private key material in
// the tls.crt and tls.key keys.
func validTLSSecret(secret *core_v1.Secret) error {
if secret.Type != core_v1.SecretTypeTLS && secret.Type != core_v1.SecretTypeOpaque {
return fmt.Errorf("secret type is not %q or %q", core_v1.SecretTypeTLS, core_v1.SecretTypeOpaque)
}
data, ok := secret.Data[core_v1.TLSCertKey]
if !ok {
return errors.New("missing TLS certificate")
}
if err := validateServingBundle(data); err != nil {
return fmt.Errorf("invalid TLS certificate: %v", err)
}
data, ok = secret.Data[core_v1.TLSPrivateKeyKey]
if !ok {
return errors.New("missing TLS private key")
}
if err := validatePrivateKey(data); err != nil {
return fmt.Errorf("invalid TLS private key: %v", err)
}
return nil
}
// validCASecret returns an error if the Secret is not of type TLS or Opaque or
// if it doesn't contain a valid CA bundle in the ca.crt key.
func validCASecret(secret *core_v1.Secret) error {
if secret.Type != core_v1.SecretTypeTLS && secret.Type != core_v1.SecretTypeOpaque {
return fmt.Errorf("secret type is not %q or %q", core_v1.SecretTypeTLS, core_v1.SecretTypeOpaque)
}
if len(secret.Data[CACertificateKey]) == 0 {
return fmt.Errorf("empty %q key", CACertificateKey)
}
if err := validateCABundle(secret.Data[CACertificateKey]); err != nil {
return fmt.Errorf("invalid CA certificate bundle: %v", err)
}
return nil
}
// validCRLSecret returns an error if the Secret is not of type TLS or Opaque or
// if it doesn't contain a valid CRL in the crl.pem key.
func validCRLSecret(secret *core_v1.Secret) error {
if secret.Type != core_v1.SecretTypeTLS && secret.Type != core_v1.SecretTypeOpaque {
return fmt.Errorf("secret type is not %q or %q", core_v1.SecretTypeTLS, core_v1.SecretTypeOpaque)
}
if len(secret.Data[CRLKey]) == 0 {
return fmt.Errorf("empty %q key", CRLKey)
}
if err := validateCRL(secret.Data[CRLKey]); err != nil {
return err
}
return nil
}
// containsPEMHeader returns true if the given slice contains a string
// that looks like a PEM header block. The problem is that pem.Decode
// does not give us a way to distinguish between a missing PEM block
// and an invalid PEM block. This means that if there is any non-PEM
// data at the end of a byte slice, we would normally detect it as an
// error. However, users of the OpenSSL API check for the
// `PEM_R_NO_START_LINE` error code and would accept files with
// trailing non-PEM data.
func containsPEMHeader(data []byte) bool {
// A PEM header starts with the begin token.
start := bytes.Index(data, []byte("-----BEGIN"))
if start == -1 {
return false
}
// And ends with the end token.
end := bytes.Index(data[start+10:], []byte("-----"))
if end == -1 {
return false
}
// And must be on a single line.
if bytes.Contains(data[start:start+end], []byte("\n")) {
return false
}
return true
}
// validateServingBundle validates that a PEM bundle contains at least one
// certificate, and that the first certificate has a
// CN or SAN set.
func validateServingBundle(data []byte) error {
var exists bool
// The first PEM in a bundle should always have a CN set.
i := 0
for containsPEMHeader(data) {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
return errors.New("failed to parse PEM block")
}
if block.Type != "CERTIFICATE" {
return fmt.Errorf("unexpected block type '%s'", block.Type)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
// Only run the CN and SAN checks on the first cert in a bundle
if i == 0 && !hasCommonName(cert) && !hasSubjectAltNames(cert) {
return errors.New("certificate has no common name or subject alt name")
}
exists = true
i++
}
if !exists {
return errors.New("failed to locate certificate")
}
return nil
}
// validateCABundle validates that a PEM bundle contains at least
// one valid certificate.
func validateCABundle(data []byte) error {
var exists bool
for containsPEMHeader(data) {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
return errors.New("failed to parse PEM block")
}
if block.Type != "CERTIFICATE" {
return fmt.Errorf("unexpected block type '%s'", block.Type)
}
exists = true
}
if !exists {
return errors.New("failed to locate certificate")
}
return nil
}
func hasCommonName(c *x509.Certificate) bool {
return strings.TrimSpace(c.Subject.CommonName) != ""
}
func hasSubjectAltNames(c *x509.Certificate) bool {
return len(c.DNSNames) > 0 || len(c.IPAddresses) > 0
}
func validatePrivateKey(data []byte) error {
var keys int
for containsPEMHeader(data) {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
return errors.New("failed to parse PEM block")
}
switch block.Type {
case "PRIVATE KEY":
if _, err := x509.ParsePKCS8PrivateKey(block.Bytes); err != nil {
return err
}
keys++
case "RSA PRIVATE KEY":
if _, err := x509.ParsePKCS1PrivateKey(block.Bytes); err != nil {
return err
}
keys++
case "EC PRIVATE KEY":
if _, err := x509.ParseECPrivateKey(block.Bytes); err != nil {
return err
}
keys++
case "EC PARAMETERS":
// ignored
default:
return fmt.Errorf("unexpected block type '%s'", block.Type)
}
}
switch keys {
case 0:
return errors.New("failed to locate private key")
case 1:
return nil
default:
return errors.New("multiple private keys")
}
}
// validateCRL checks that PEM file contains at least one CRL.
func validateCRL(data []byte) error {
for containsPEMHeader(data) {
var block *pem.Block
block, data = pem.Decode(data)
if block == nil {
return errors.New("failed to parse PEM block")
}
if block.Type == "X509 CRL" {
return nil
}
}
return errors.New("failed to locate CRL")
}