io.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright 2014 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 cert
  14. import (
  15. "crypto/x509"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. )
  20. // CanReadCertAndKey returns true if the certificate and key files already exists,
  21. // otherwise returns false. If lost one of cert and key, returns error.
  22. func CanReadCertAndKey(certPath, keyPath string) (bool, error) {
  23. certReadable := canReadFile(certPath)
  24. keyReadable := canReadFile(keyPath)
  25. if certReadable == false && keyReadable == false {
  26. return false, nil
  27. }
  28. if certReadable == false {
  29. return false, fmt.Errorf("error reading %s, certificate and key must be supplied as a pair", certPath)
  30. }
  31. if keyReadable == false {
  32. return false, fmt.Errorf("error reading %s, certificate and key must be supplied as a pair", keyPath)
  33. }
  34. return true, nil
  35. }
  36. // If the file represented by path exists and
  37. // readable, returns true otherwise returns false.
  38. func canReadFile(path string) bool {
  39. f, err := os.Open(path)
  40. if err != nil {
  41. return false
  42. }
  43. defer f.Close()
  44. return true
  45. }
  46. // WriteCert writes the pem-encoded certificate data to certPath.
  47. // The certificate file will be created with file mode 0644.
  48. // If the certificate file already exists, it will be overwritten.
  49. // The parent directory of the certPath will be created as needed with file mode 0755.
  50. func WriteCert(certPath string, data []byte) error {
  51. if err := os.MkdirAll(filepath.Dir(certPath), os.FileMode(0755)); err != nil {
  52. return err
  53. }
  54. return os.WriteFile(certPath, data, os.FileMode(0644))
  55. }
  56. // NewPool returns an x509.CertPool containing the certificates in the given PEM-encoded file.
  57. // Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates
  58. func NewPool(filename string) (*x509.CertPool, error) {
  59. pemBlock, err := os.ReadFile(filename)
  60. if err != nil {
  61. return nil, err
  62. }
  63. pool, err := NewPoolFromBytes(pemBlock)
  64. if err != nil {
  65. return nil, fmt.Errorf("error creating pool from %s: %s", filename, err)
  66. }
  67. return pool, nil
  68. }
  69. // NewPoolFromBytes returns an x509.CertPool containing the certificates in the given PEM-encoded bytes.
  70. // Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates
  71. func NewPoolFromBytes(pemBlock []byte) (*x509.CertPool, error) {
  72. certs, err := ParseCertsPEM(pemBlock)
  73. if err != nil {
  74. return nil, err
  75. }
  76. pool := x509.NewCertPool()
  77. for _, cert := range certs {
  78. pool.AddCert(cert)
  79. }
  80. return pool, nil
  81. }
  82. // CertsFromFile returns the x509.Certificates contained in the given PEM-encoded file.
  83. // Returns an error if the file could not be read, a certificate could not be parsed, or if the file does not contain any certificates
  84. func CertsFromFile(file string) ([]*x509.Certificate, error) {
  85. pemBlock, err := os.ReadFile(file)
  86. if err != nil {
  87. return nil, err
  88. }
  89. certs, err := ParseCertsPEM(pemBlock)
  90. if err != nil {
  91. return nil, fmt.Errorf("error reading %s: %s", file, err)
  92. }
  93. return certs, nil
  94. }