certs.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2019 Yunion
  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. package certs
  15. import (
  16. "crypto"
  17. "crypto/x509"
  18. "fmt"
  19. "yunion.io/x/pkg/errors"
  20. certutil "yunion.io/x/onecloud/pkg/util/tls/cert"
  21. pkiutil "yunion.io/x/onecloud/pkg/util/tls/pki"
  22. )
  23. type certKeyLocation struct {
  24. pkiDir string
  25. caBaseName string
  26. baseName string
  27. uxName string
  28. }
  29. // validateSignedCert tries to load a x509 certificate and private key from pkiDir and validates
  30. // that the cert is signed by a given CA
  31. func validateSignedCert(l certKeyLocation) error {
  32. // Try to load CA
  33. caCert, err := pkiutil.TryLoadCertFromDisk(l.pkiDir, l.caBaseName)
  34. if err != nil {
  35. return errors.Wrapf(err, "failure loading certificate authority for %s", l.uxName)
  36. }
  37. return validateSignedCertWithCA(l, caCert)
  38. }
  39. // validateSignedCertWithCA tries to load a certificate and validate it with the given caCert
  40. func validateSignedCertWithCA(l certKeyLocation, caCert *x509.Certificate) error {
  41. // Try to load key and signed certificate
  42. signedCert, _, err := pkiutil.TryLoadCertAndKeyFromDisk(l.pkiDir, l.baseName)
  43. if err != nil {
  44. return errors.Wrapf(err, "failure loading certificate for %s", l.uxName)
  45. }
  46. // Check if the cert is signed by the CA
  47. if err := signedCert.CheckSignatureFrom(caCert); err != nil {
  48. return errors.Wrapf(err, "certificate %s is not signed by corresponding CA", l.uxName)
  49. }
  50. return nil
  51. }
  52. // validatePrivatePublicKey tries to load a private key from pkiDir
  53. func validatePrivatePublicKey(l certKeyLocation) error {
  54. // Try to load key
  55. _, _, err := pkiutil.TryLoadPrivatePublicKeyFromDisk(l.pkiDir, l.baseName)
  56. if err != nil {
  57. return errors.Wrapf(err, "failure loading key for %s", l.uxName)
  58. }
  59. return nil
  60. }
  61. // writeCertificateAuthorithyFilesIfNotExist write a new certificate Authority to the given path.
  62. // If there already is a certificate file at the given path; tries to load it and check if the values in the
  63. // existing and the expected certificate equals. If they do; will just skip writing the file as it's up-to-date,
  64. // otherwise this function returns an error.
  65. func writeCertificateAuthorithyFilesIfNotExist(pkiDir string, baseName string, caCert *x509.Certificate, caKey crypto.Signer) error {
  66. // If cert or key exists, we should try to load them
  67. if pkiutil.CertOrKeyExist(pkiDir, baseName) {
  68. // Try to load .crt and .key from the PKI directory
  69. caCert, _, err := pkiutil.TryLoadCertAndKeyFromDisk(pkiDir, baseName)
  70. if err != nil {
  71. return errors.Wrapf(err, "failure loading %s certificate", baseName)
  72. }
  73. // Check if the existing cert is a CA
  74. if !caCert.IsCA {
  75. return errors.Errorf("certificate %s is not a CA", baseName)
  76. }
  77. // kubeadm doesn't validate the existing certificate Authority more than this;
  78. // Basically, if we find a certificate file with the same path; and it is a CA
  79. // kubeadm thinks those files are equal and doesn't bother writing a new file
  80. fmt.Printf("[certs] Using the existing %q certificate and key\n", baseName)
  81. } else {
  82. // Write .crt and .key files to disk
  83. fmt.Printf("[certs] Generating %q certificate and key\n", baseName)
  84. if err := pkiutil.WriteCertAndKey(pkiDir, baseName, caCert, caKey); err != nil {
  85. return errors.Wrapf(err, "failure while saving %s certificate and key", baseName)
  86. }
  87. }
  88. return nil
  89. }
  90. // writeCertificateFilesIfNotExist write a new certificate to the given path.
  91. // If there already is a certificate file at the given path; kubeadm tries to load it and check if the values in the
  92. // existing and the expected certificate equals. If they do; kubeadm will just skip writing the file as it's up-to-date,
  93. // otherwise this function returns an error.
  94. func writeCertificateFilesIfNotExist(pkiDir string, baseName string, signingCert *x509.Certificate, cert *x509.Certificate, key crypto.Signer, cfg *certutil.Config) error {
  95. // Checks if the signed certificate exists in the PKI directory
  96. if pkiutil.CertOrKeyExist(pkiDir, baseName) {
  97. // Try to load signed certificate .crt and .key from the PKI directory
  98. signedCert, _, err := pkiutil.TryLoadCertAndKeyFromDisk(pkiDir, baseName)
  99. if err != nil {
  100. return errors.Wrapf(err, "failure loading %s certificate", baseName)
  101. }
  102. // Check if the existing cert is signed by the given CA
  103. if err := signedCert.CheckSignatureFrom(signingCert); err != nil {
  104. return errors.Errorf("certificate %s is not signed by corresponding CA", baseName)
  105. }
  106. // Check if the certificate has the correct attributes
  107. if err := validateCertificateWithConfig(signedCert, baseName, cfg); err != nil {
  108. return err
  109. }
  110. fmt.Printf("[certs] Using the existing %q certificate and key\n", baseName)
  111. } else {
  112. // Write .crt and .key files to disk
  113. fmt.Printf("[certs] Generating %q certificate and key\n", baseName)
  114. if err := pkiutil.WriteCertAndKey(pkiDir, baseName, cert, key); err != nil {
  115. return errors.Wrapf(err, "failure while saving %s certificate and key", baseName)
  116. }
  117. if pkiutil.HasServerAuth(cert) {
  118. fmt.Printf("[certs] %s serving cert is signed for DNS names %v and IPs %v\n", baseName, cert.DNSNames, cert.IPAddresses)
  119. }
  120. }
  121. return nil
  122. }
  123. // validateCertificateWithConfig makes sure that a given certificate is valid at
  124. // least for the SANs defined in the configuration.
  125. func validateCertificateWithConfig(cert *x509.Certificate, baseName string, cfg *certutil.Config) error {
  126. for _, dnsName := range cfg.AltNames.DNSNames {
  127. if err := cert.VerifyHostname(dnsName); err != nil {
  128. return errors.Wrapf(err, "certificate %s is invalid", baseName)
  129. }
  130. }
  131. for _, ipAddress := range cfg.AltNames.IPs {
  132. if err := cert.VerifyHostname(ipAddress.String()); err != nil {
  133. return errors.Wrapf(err, "certificate %s is invalid", baseName)
  134. }
  135. }
  136. return nil
  137. }