| 1 |
/* |
| 2 |
Copyright 2024 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 webhook |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
"k8s.io/apimachinery/pkg/util/validation/field" |
| 23 |
) |
| 24 |
|
| 25 |
// exampleCert was generated from crypto/tls/generate_cert.go with the following command: |
| 26 |
// |
| 27 |
// go run generate_cert.go --rsa-bits 2048 --host example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h |
| 28 |
var exampleCert = []byte(`-----BEGIN CERTIFICATE----- |
| 29 |
MIIDADCCAeigAwIBAgIQVHG3Fn9SdWayyLOZKCW1vzANBgkqhkiG9w0BAQsFADAS |
| 30 |
MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw |
| 31 |
MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A |
| 32 |
MIIBCgKCAQEArTCu9fiIclNgDdWHphewM+JW55dCb5yYGlJgCBvwbOx547M9p+tn |
| 33 |
zm9QOhsdZDHDZsG9tqnWxE2Nc1HpIJyOlfYsOoonpEoG/Ep6nnK91ngj0bn/JlNy |
| 34 |
+i/bwU4r97MOukvnOIQez9/D9jAJaOX2+b8/d4lRz9BsqiwJyg+ynZ5tVVYj7aMi |
| 35 |
vXnd6HOnJmtqutOtr3beucJnkd6XbwRkLUcAYATT+ZihOWRbTuKqhCg6zGkJOoUG |
| 36 |
f8sX61JjoilxiURA//ftGVbdTCU3DrmGmardp5NNOHbumMYU8Vhmqgx1Bqxb+9he |
| 37 |
7G42uW5YWYK/GqJzgVPjjlB2dOGj9KrEWQIDAQABo1AwTjAOBgNVHQ8BAf8EBAMC |
| 38 |
AqQwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDwYDVR0TAQH/BAUwAwEB/zAWBgNVHREE |
| 39 |
DzANggtleGFtcGxlLmNvbTANBgkqhkiG9w0BAQsFAAOCAQEAig4AIi9xWs1+pLES |
| 40 |
eeGGdSDoclplFpcbXANnsYYFyLf+8pcWgVi2bOmb2gXMbHFkB07MA82wRJAUTaA+ |
| 41 |
2iNXVQMhPCoA7J6ADUbww9doJX2S9HGyArhiV/MhHtE8txzMn2EKNLdhhk3N9rmV |
| 42 |
x/qRbWAY1U2z4BpdrAR87Fe81Nlj7h45csW9K+eS+NgXipiNTIfEShKgCFM8EdxL |
| 43 |
1WXg7r9AvYV3TNDPWTjLsm1rQzzZQ7Uvcf6deWiNodZd8MOT/BFLclDPTK6cF2Hr |
| 44 |
UU4dq6G4kCwMSxWE4cM3HlZ4u1dyIt47VbkP0rtvkBCXx36y+NXYA5lzntchNFZP |
| 45 |
uvEQdw== |
| 46 |
-----END CERTIFICATE-----`) |
| 47 |
|
| 48 |
func TestValidateCABundle(t *testing.T) { |
| 49 |
tests := []struct { |
| 50 |
name string |
| 51 |
caBundle []byte |
| 52 |
expectErr bool |
| 53 |
}{ |
| 54 |
{ |
| 55 |
name: "nil caBundle is valid", |
| 56 |
caBundle: nil, |
| 57 |
expectErr: false, |
| 58 |
}, { |
| 59 |
name: "empty caBundle is valid", |
| 60 |
caBundle: []byte(""), |
| 61 |
expectErr: false, |
| 62 |
}, { |
| 63 |
name: "non empty caBundle with invalid certificate should not validate", |
| 64 |
caBundle: []byte("bogus"), |
| 65 |
expectErr: true, |
| 66 |
}, { |
| 67 |
name: "non empty caBundle with no certificate should not validate", |
| 68 |
caBundle: []byte("Cg=="), |
| 69 |
expectErr: true, |
| 70 |
}, { |
| 71 |
name: "non empty caBundle with valid certificate should validate", |
| 72 |
caBundle: exampleCert, |
| 73 |
expectErr: false, |
| 74 |
}, |
| 75 |
} |
| 76 |
|
| 77 |
for _, tc := range tests { |
| 78 |
errList := ValidateCABundle(field.NewPath(""), tc.caBundle) |
| 79 |
if len(errList) > 0 && !tc.expectErr { |
| 80 |
t.Errorf("Expected no error for test %s, got %v", tc.name, errList) |
| 81 |
} else if len(errList) == 0 && tc.expectErr { |
| 82 |
t.Errorf("Expected error for test %s, received no error", tc.name) |
| 83 |
} |
| 84 |
} |
| 85 |
} |