Validate CABundle when writing CRD
Kubernetes-commit: a5791b344c04ded4f443c7e134242a29bd0e2bac
This commit is contained in:
parent
2b2a4b0fa8
commit
130a469e09
|
@ -23,8 +23,18 @@ import (
|
|||
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
"k8s.io/client-go/transport"
|
||||
)
|
||||
|
||||
func ValidateCABundle(fldPath *field.Path, caBundle []byte) field.ErrorList {
|
||||
var allErrors field.ErrorList
|
||||
_, err := transport.TLSConfigFor(&transport.Config{TLS: transport.TLSConfig{CAData: caBundle}})
|
||||
if err != nil {
|
||||
allErrors = append(allErrors, field.Invalid(fldPath, caBundle, err.Error()))
|
||||
}
|
||||
return allErrors
|
||||
}
|
||||
|
||||
// ValidateWebhookURL validates webhook's URL.
|
||||
func ValidateWebhookURL(fldPath *field.Path, URL string, forceHttps bool) field.ErrorList {
|
||||
var allErrors field.ErrorList
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
Copyright 2024 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 webhook
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
)
|
||||
|
||||
// exampleCert was generated from crypto/tls/generate_cert.go with the following command:
|
||||
//
|
||||
// go run generate_cert.go --rsa-bits 2048 --host example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h
|
||||
var exampleCert = []byte(`-----BEGIN CERTIFICATE-----
|
||||
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4u1dyIt47VbkP0rtvkBCXx36y+NXYA5lzntchNFZP
|
||||
uvEQdw==
|
||||
-----END CERTIFICATE-----`)
|
||||
|
||||
func TestValidateCABundle(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
caBundle []byte
|
||||
expectErr bool
|
||||
}{
|
||||
{
|
||||
name: "nil caBundle is valid",
|
||||
caBundle: nil,
|
||||
expectErr: false,
|
||||
}, {
|
||||
name: "empty caBundle is valid",
|
||||
caBundle: []byte(""),
|
||||
expectErr: false,
|
||||
}, {
|
||||
name: "non empty caBundle with invalid certificate should not validate",
|
||||
caBundle: []byte("bogus"),
|
||||
expectErr: true,
|
||||
}, {
|
||||
name: "non empty caBundle with no certificate should not validate",
|
||||
caBundle: []byte("Cg=="),
|
||||
expectErr: true,
|
||||
}, {
|
||||
name: "non empty caBundle with valid certificate should validate",
|
||||
caBundle: exampleCert,
|
||||
expectErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
errList := ValidateCABundle(field.NewPath(""), tc.caBundle)
|
||||
if len(errList) > 0 && !tc.expectErr {
|
||||
t.Errorf("Expected no error for test %s, got %v", tc.name, errList)
|
||||
} else if len(errList) == 0 && tc.expectErr {
|
||||
t.Errorf("Expected error for test %s, received no error", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue