...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert/cert_test.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cert_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert/generator"
    21  	"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/webhook/cert/writer"
    22  
    23  	"k8s.io/client-go/util/cert"
    24  )
    25  
    26  const COMMON_NAME = "foo.example.com"
    27  
    28  // TestDoesCertificateWorkWithKubernetes verifies that certificates
    29  // that are created without a SAN are considered invalid, and
    30  // thereby triggering overwriting the certificate with one that does
    31  // have one.
    32  //
    33  // This verifies a fix for https://github.com/GoogleCloudPlatform/k8s-config-connector/issues/406.
    34  func TestDoesCertificateWorkWithKubernetes(t *testing.T) {
    35  	certConfig := cert.Config{CommonName: COMMON_NAME}
    36  	privateKey, err := generator.NewPrivateKey()
    37  	if err != nil {
    38  		t.Fatalf("error when creating a private key: %s", err)
    39  	}
    40  	cert, err := generator.NewSelfSignedCACert(certConfig, privateKey)
    41  	if err != nil {
    42  		t.Fatalf("error when creating a new-style certificate: %s", err)
    43  	}
    44  	if !writer.DoesCertificateWorkWithK8sAPIClient(cert) {
    45  		t.Fatalf("writer detected a new-style certificate is invalid. This implies" +
    46  			"that newly generated certificates may not be compatible with all versions" +
    47  			"of the Kubernetes HTTP client")
    48  	}
    49  	cert.DNSNames = nil
    50  	if writer.DoesCertificateWorkWithK8sAPIClient(cert) {
    51  		t.Fatalf("DoesCertificateWorkWithK8sAPIClient recognized an invalid cert as valid.")
    52  	}
    53  }
    54  

View as plain text