filerenewer_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package renewal
  14. import (
  15. "crypto/x509"
  16. "testing"
  17. certutil "k8s.io/client-go/util/cert"
  18. pkiutil "k8s.io/kubernetes/cmd/kubeadm/app/util/pkiutil"
  19. )
  20. func TestFileRenewer(t *testing.T) {
  21. // creates a File renewer using a test Certificate authority
  22. fr := NewFileRenewer(testCACert, testCAKey)
  23. // renews a certificate
  24. certCfg := &pkiutil.CertConfig{
  25. Config: certutil.Config{
  26. CommonName: "test-certs",
  27. AltNames: certutil.AltNames{
  28. DNSNames: []string{"test-domain.space"},
  29. },
  30. Usages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  31. },
  32. }
  33. cert, _, err := fr.Renew(certCfg)
  34. if err != nil {
  35. t.Fatalf("unexpected error renewing cert: %v", err)
  36. }
  37. // verify the renewed certificate
  38. pool := x509.NewCertPool()
  39. pool.AddCert(testCACert)
  40. _, err = cert.Verify(x509.VerifyOptions{
  41. DNSName: "test-domain.space",
  42. Roots: pool,
  43. KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
  44. })
  45. if err != nil {
  46. t.Errorf("couldn't verify new cert: %v", err)
  47. }
  48. }