1 /* 2 Copyright 2021 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 cert_test 18 19 import ( 20 cryptorand "crypto/rand" 21 "crypto/rsa" 22 "testing" 23 24 "k8s.io/client-go/util/cert" 25 ) 26 27 const COMMON_NAME = "foo.example.com" 28 29 // TestSelfSignedCertHasSAN verifies the existing of 30 // a SAN on the generated self-signed certificate. 31 // a SAN ensures that the certificate is considered 32 // valid by default in go 1.15 and above, which 33 // turns off fallback to Common Name by default. 34 func TestSelfSignedCertHasSAN(t *testing.T) { 35 key, err := rsa.GenerateKey(cryptorand.Reader, 2048) 36 if err != nil { 37 t.Fatalf("rsa key failed to generate: %v", err) 38 } 39 selfSignedCert, err := cert.NewSelfSignedCACert(cert.Config{CommonName: COMMON_NAME}, key) 40 if err != nil { 41 t.Fatalf("self signed certificate failed to generate: %v", err) 42 } 43 if len(selfSignedCert.DNSNames) == 0 { 44 t.Fatalf("self signed certificate has zero DNS names.") 45 } 46 } 47