| 1 |
/* |
| 2 |
Copyright 2019 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package dynamiccertificates |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"crypto/rand" |
| 22 |
"crypto/rsa" |
| 23 |
"crypto/tls" |
| 24 |
"crypto/x509" |
| 25 |
"crypto/x509/pkix" |
| 26 |
"encoding/base64" |
| 27 |
"encoding/pem" |
| 28 |
"fmt" |
| 29 |
"math/big" |
| 30 |
"net" |
| 31 |
"testing" |
| 32 |
"time" |
| 33 |
|
| 34 |
netutils "k8s.io/utils/net" |
| 35 |
|
| 36 |
"github.com/stretchr/testify/assert" |
| 37 |
) |
| 38 |
|
| 39 |
type testCertSpec struct { |
| 40 |
host string |
| 41 |
names, ips []string // in certificate |
| 42 |
} |
| 43 |
|
| 44 |
type namedtestCertSpec struct { |
| 45 |
testCertSpec |
| 46 |
explicitNames []string // as --tls-sni-cert-key explicit names |
| 47 |
} |
| 48 |
|
| 49 |
func TestBuiltNamedCertificates(t *testing.T) { |
| 50 |
tests := []struct { |
| 51 |
certs []namedtestCertSpec |
| 52 |
explicitNames []string |
| 53 |
expected map[string]int // name to certs[*] index |
| 54 |
errorString string |
| 55 |
}{ |
| 56 |
{ |
| 57 |
// empty certs |
| 58 |
expected: map[string]int{}, |
| 59 |
}, |
| 60 |
{ |
| 61 |
// only one cert |
| 62 |
certs: []namedtestCertSpec{ |
| 63 |
{ |
| 64 |
testCertSpec: testCertSpec{ |
| 65 |
host: "test.com", |
| 66 |
}, |
| 67 |
}, |
| 68 |
}, |
| 69 |
expected: map[string]int{ |
| 70 |
"test.com": 0, |
| 71 |
}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
// ip as cns are ignored |
| 75 |
certs: []namedtestCertSpec{ |
| 76 |
{ |
| 77 |
testCertSpec: testCertSpec{ |
| 78 |
host: "1.2.3.4", |
| 79 |
names: []string{"test.com"}, |
| 80 |
}, |
| 81 |
}, |
| 82 |
}, |
| 83 |
expected: map[string]int{ |
| 84 |
"test.com": 0, |
| 85 |
}, |
| 86 |
}, |
| 87 |
{ |
| 88 |
// ips are ignored |
| 89 |
certs: []namedtestCertSpec{ |
| 90 |
{ |
| 91 |
testCertSpec: testCertSpec{ |
| 92 |
host: "test.com", |
| 93 |
ips: []string{"1.2.3.4"}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
expected: map[string]int{ |
| 98 |
"test.com": 0, |
| 99 |
}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
// two certs with the same name |
| 103 |
certs: []namedtestCertSpec{ |
| 104 |
{ |
| 105 |
testCertSpec: testCertSpec{ |
| 106 |
host: "test.com", |
| 107 |
}, |
| 108 |
}, |
| 109 |
{ |
| 110 |
testCertSpec: testCertSpec{ |
| 111 |
host: "test.com", |
| 112 |
}, |
| 113 |
}, |
| 114 |
}, |
| 115 |
expected: map[string]int{ |
| 116 |
"test.com": 0, |
| 117 |
}, |
| 118 |
}, |
| 119 |
{ |
| 120 |
// two certs with different names |
| 121 |
certs: []namedtestCertSpec{ |
| 122 |
{ |
| 123 |
testCertSpec: testCertSpec{ |
| 124 |
host: "test2.com", |
| 125 |
}, |
| 126 |
}, |
| 127 |
{ |
| 128 |
testCertSpec: testCertSpec{ |
| 129 |
host: "test1.com", |
| 130 |
}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
expected: map[string]int{ |
| 134 |
"test1.com": 1, |
| 135 |
"test2.com": 0, |
| 136 |
}, |
| 137 |
}, |
| 138 |
{ |
| 139 |
// two certs with the same name, explicit trumps |
| 140 |
certs: []namedtestCertSpec{ |
| 141 |
{ |
| 142 |
testCertSpec: testCertSpec{ |
| 143 |
host: "test.com", |
| 144 |
}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
testCertSpec: testCertSpec{ |
| 148 |
host: "test.com", |
| 149 |
}, |
| 150 |
explicitNames: []string{"test.com"}, |
| 151 |
}, |
| 152 |
}, |
| 153 |
expected: map[string]int{ |
| 154 |
"test.com": 1, |
| 155 |
}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
// certs with partial overlap; ips are ignored |
| 159 |
certs: []namedtestCertSpec{ |
| 160 |
{ |
| 161 |
testCertSpec: testCertSpec{ |
| 162 |
host: "a", |
| 163 |
names: []string{"a.test.com", "test.com"}, |
| 164 |
}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
testCertSpec: testCertSpec{ |
| 168 |
host: "b", |
| 169 |
names: []string{"b.test.com", "test.com"}, |
| 170 |
}, |
| 171 |
}, |
| 172 |
}, |
| 173 |
expected: map[string]int{ |
| 174 |
"a": 0, "b": 1, |
| 175 |
"a.test.com": 0, "b.test.com": 1, |
| 176 |
"test.com": 0, |
| 177 |
}, |
| 178 |
}, |
| 179 |
{ |
| 180 |
// wildcards |
| 181 |
certs: []namedtestCertSpec{ |
| 182 |
{ |
| 183 |
testCertSpec: testCertSpec{ |
| 184 |
host: "a", |
| 185 |
names: []string{"a.test.com", "test.com"}, |
| 186 |
}, |
| 187 |
explicitNames: []string{"*.test.com", "test.com"}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
testCertSpec: testCertSpec{ |
| 191 |
host: "b", |
| 192 |
names: []string{"b.test.com", "test.com"}, |
| 193 |
}, |
| 194 |
explicitNames: []string{"dev.test.com", "test.com"}, |
| 195 |
}}, |
| 196 |
expected: map[string]int{ |
| 197 |
"test.com": 0, |
| 198 |
"*.test.com": 0, |
| 199 |
"dev.test.com": 1, |
| 200 |
}, |
| 201 |
}, |
| 202 |
} |
| 203 |
|
| 204 |
NextTest: |
| 205 |
for i, test := range tests { |
| 206 |
var sniCerts []SNICertKeyContentProvider |
| 207 |
bySignature := map[string]int{} // index in test.certs by cert signature |
| 208 |
for j, c := range test.certs { |
| 209 |
certProvider, err := createTestTLSCerts(c.testCertSpec, c.explicitNames) |
| 210 |
if err != nil { |
| 211 |
t.Errorf("%d - failed to create cert %d: %v", i, j, err) |
| 212 |
continue NextTest |
| 213 |
} |
| 214 |
|
| 215 |
sniCerts = append(sniCerts, certProvider) |
| 216 |
|
| 217 |
sig, err := certSignature(certProvider) |
| 218 |
if err != nil { |
| 219 |
t.Errorf("%d - failed to get signature for %d: %v", i, j, err) |
| 220 |
continue NextTest |
| 221 |
} |
| 222 |
bySignature[sig] = j |
| 223 |
} |
| 224 |
|
| 225 |
c := DynamicServingCertificateController{sniCerts: sniCerts} |
| 226 |
content, err := c.newTLSContent() |
| 227 |
assert.NoError(t, err) |
| 228 |
|
| 229 |
certMap, err := c.BuildNamedCertificates(content.sniCerts) |
| 230 |
if err == nil && len(test.errorString) != 0 { |
| 231 |
t.Errorf("%d - expected no error, got: %v", i, err) |
| 232 |
} else if err != nil && err.Error() != test.errorString { |
| 233 |
t.Errorf("%d - expected error %q, got: %v", i, test.errorString, err) |
| 234 |
} else { |
| 235 |
got := map[string]int{} |
| 236 |
for name, cert := range certMap { |
| 237 |
x509Certs, err := x509.ParseCertificates(cert.Certificate[0]) |
| 238 |
assert.NoError(t, err, "%d - invalid certificate for %q", i, name) |
| 239 |
assert.NotEmpty(t, x509Certs, "%d - expected at least one x509 cert in tls cert for %q", i, name) |
| 240 |
got[name] = bySignature[x509CertSignature(x509Certs[0])] |
| 241 |
} |
| 242 |
|
| 243 |
assert.EqualValues(t, test.expected, got, "%d - wrong certificate map", i) |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
func parseIPList(ips []string) []net.IP { |
| 249 |
var netIPs []net.IP |
| 250 |
for _, ip := range ips { |
| 251 |
netIPs = append(netIPs, netutils.ParseIPSloppy(ip)) |
| 252 |
} |
| 253 |
return netIPs |
| 254 |
} |
| 255 |
|
| 256 |
func createTestTLSCerts(spec testCertSpec, names []string) (certProvider SNICertKeyContentProvider, err error) { |
| 257 |
certPem, keyPem, err := generateSelfSignedCertKey(spec.host, parseIPList(spec.ips), spec.names) |
| 258 |
if err != nil { |
| 259 |
return nil, err |
| 260 |
} |
| 261 |
|
| 262 |
return NewStaticSNICertKeyContent("test-cert", certPem, keyPem, names...) |
| 263 |
} |
| 264 |
|
| 265 |
func x509CertSignature(cert *x509.Certificate) string { |
| 266 |
return base64.StdEncoding.EncodeToString(cert.Signature) |
| 267 |
} |
| 268 |
|
| 269 |
func certSignature(certProvider CertKeyContentProvider) (string, error) { |
| 270 |
currentCert, currentKey := certProvider.CurrentCertKeyContent() |
| 271 |
|
| 272 |
tlsCert, err := tls.X509KeyPair(currentCert, currentKey) |
| 273 |
if err != nil { |
| 274 |
return "", err |
| 275 |
} |
| 276 |
|
| 277 |
x509Certs, err := x509.ParseCertificates(tlsCert.Certificate[0]) |
| 278 |
if err != nil { |
| 279 |
return "", err |
| 280 |
} |
| 281 |
return x509CertSignature(x509Certs[0]), nil |
| 282 |
} |
| 283 |
|
| 284 |
// generateSelfSignedCertKey creates a self-signed certificate and key for the given host. |
| 285 |
// Host may be an IP or a DNS name |
| 286 |
// You may also specify additional subject alt names (either ip or dns names) for the certificate |
| 287 |
func generateSelfSignedCertKey(host string, alternateIPs []net.IP, alternateDNS []string) ([]byte, []byte, error) { |
| 288 |
priv, err := rsa.GenerateKey(rand.Reader, 2048) |
| 289 |
if err != nil { |
| 290 |
return nil, nil, err |
| 291 |
} |
| 292 |
|
| 293 |
template := x509.Certificate{ |
| 294 |
SerialNumber: big.NewInt(1), |
| 295 |
Subject: pkix.Name{ |
| 296 |
CommonName: fmt.Sprintf("%s@%d", host, time.Now().Unix()), |
| 297 |
}, |
| 298 |
NotBefore: time.Unix(0, 0), |
| 299 |
NotAfter: time.Now().Add(time.Hour * 24 * 365 * 100), |
| 300 |
|
| 301 |
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, |
| 302 |
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 303 |
BasicConstraintsValid: true, |
| 304 |
IsCA: true, |
| 305 |
} |
| 306 |
|
| 307 |
if ip := netutils.ParseIPSloppy(host); ip != nil { |
| 308 |
template.IPAddresses = append(template.IPAddresses, ip) |
| 309 |
} else { |
| 310 |
template.DNSNames = append(template.DNSNames, host) |
| 311 |
} |
| 312 |
|
| 313 |
template.IPAddresses = append(template.IPAddresses, alternateIPs...) |
| 314 |
template.DNSNames = append(template.DNSNames, alternateDNS...) |
| 315 |
|
| 316 |
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) |
| 317 |
if err != nil { |
| 318 |
return nil, nil, err |
| 319 |
} |
| 320 |
|
| 321 |
// Generate cert |
| 322 |
certBuffer := bytes.Buffer{} |
| 323 |
if err := pem.Encode(&certBuffer, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil { |
| 324 |
return nil, nil, err |
| 325 |
} |
| 326 |
|
| 327 |
// Generate key |
| 328 |
keyBuffer := bytes.Buffer{} |
| 329 |
if err := pem.Encode(&keyBuffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)}); err != nil { |
| 330 |
return nil, nil, err |
| 331 |
} |
| 332 |
|
| 333 |
return certBuffer.Bytes(), keyBuffer.Bytes(), nil |
| 334 |
} |