io.go 3.5 KB

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