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
|
/*
Copyright 2020 The Kubernetes 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 csr
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"testing"
certificates "k8s.io/api/certificates/v1"
)
func TestEnsureCompatible(t *testing.T) {
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
req := pemWithPrivateKey(privateKey)
tests := map[string]struct {
new, orig *certificates.CertificateSigningRequest
privateKey interface{}
err string
}{
"nil signerName on 'new' matches any signerName on 'orig'": {
new: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
},
},
orig: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
SignerName: "example.com/test",
},
},
privateKey: privateKey,
},
"nil signerName on 'orig' matches any signerName on 'new'": {
new: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
SignerName: "example.com/test",
},
},
orig: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
},
},
privateKey: privateKey,
},
"signerName on 'orig' matches signerName on 'new'": {
new: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
SignerName: "example.com/test",
},
},
orig: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
SignerName: "example.com/test",
},
},
privateKey: privateKey,
},
"signerName on 'orig' does not match signerName on 'new'": {
new: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
SignerName: "example.com/test",
},
},
orig: &certificates.CertificateSigningRequest{
Spec: certificates.CertificateSigningRequestSpec{
Request: req,
SignerName: "example.com/not-test",
},
},
privateKey: privateKey,
err: `csr signerNames differ: new "example.com/test", orig: "example.com/not-test"`,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := ensureCompatible(test.new, test.orig, test.privateKey)
if err != nil && test.err == "" {
t.Errorf("expected no error, but got: %v", err)
} else if err != nil && test.err != err.Error() {
t.Errorf("error did not match as expected, got=%v, exp=%s", err, test.err)
}
if err == nil && test.err != "" {
t.Errorf("expected to get an error but got none")
}
})
}
}
func pemWithPrivateKey(pk crypto.PrivateKey) []byte {
template := &x509.CertificateRequest{
Subject: pkix.Name{
CommonName: "something",
Organization: []string{"test"},
},
}
return pemWithTemplate(template, pk)
}
func pemWithTemplate(template *x509.CertificateRequest, key crypto.PrivateKey) []byte {
csrDER, err := x509.CreateCertificateRequest(rand.Reader, template, key)
if err != nil {
panic(err)
}
csrPemBlock := &pem.Block{
Type: "CERTIFICATE REQUEST",
Bytes: csrDER,
}
p := pem.EncodeToMemory(csrPemBlock)
if p == nil {
panic("invalid pem block")
}
return p
}
|