io.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /*
  15. Copyright 2014 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package cert
  27. import (
  28. "crypto/x509"
  29. "fmt"
  30. "io/ioutil"
  31. "os"
  32. "path/filepath"
  33. )
  34. // CanReadCertAndKey returns true if the certificate and key files already exists,
  35. // otherwise returns false. If lost one of cert and key, returns error.
  36. func CanReadCertAndKey(certPath, keyPath string) (bool, error) {
  37. certReadable := canReadFile(certPath)
  38. keyReadable := canReadFile(keyPath)
  39. if certReadable == false && keyReadable == false {
  40. return false, nil
  41. }
  42. if certReadable == false {
  43. return false, fmt.Errorf("error reading %s, certificate and key must be supplied as a pair", certPath)
  44. }
  45. if keyReadable == false {
  46. return false, fmt.Errorf("error reading %s, certificate and key must be supplied as a pair", keyPath)
  47. }
  48. return true, nil
  49. }
  50. // If the file represented by path exists and
  51. // readable, returns true otherwise returns false.
  52. func canReadFile(path string) bool {
  53. f, err := os.Open(path)
  54. if err != nil {
  55. return false
  56. }
  57. defer f.Close()
  58. return true
  59. }
  60. // WriteCert writes the pem-encoded certificate data to certPath.
  61. // The certificate file will be created with file mode 0644.
  62. // If the certificate file already exists, it will be overwritten.
  63. // The parent directory of the certPath will be created as needed with file mode 0755.
  64. func WriteCert(certPath string, data []byte) error {
  65. if err := os.MkdirAll(filepath.Dir(certPath), os.FileMode(0755)); err != nil {
  66. return err
  67. }
  68. return ioutil.WriteFile(certPath, data, os.FileMode(0644))
  69. }
  70. // NewPool returns an x509.CertPool containing the certificates in the given PEM-encoded file.
  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 NewPool(filename string) (*x509.CertPool, error) {
  73. pemBlock, err := ioutil.ReadFile(filename)
  74. if err != nil {
  75. return nil, err
  76. }
  77. pool, err := NewPoolFromBytes(pemBlock)
  78. if err != nil {
  79. return nil, fmt.Errorf("error creating pool from %s: %s", filename, err)
  80. }
  81. return pool, nil
  82. }
  83. // NewPoolFromBytes returns an x509.CertPool containing the certificates in the given PEM-encoded bytes.
  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 NewPoolFromBytes(pemBlock []byte) (*x509.CertPool, error) {
  86. certs, err := ParseCertsPEM(pemBlock)
  87. if err != nil {
  88. return nil, err
  89. }
  90. pool := x509.NewCertPool()
  91. for _, cert := range certs {
  92. pool.AddCert(cert)
  93. }
  94. return pool, nil
  95. }
  96. // CertsFromFile returns the x509.Certificates contained in the given PEM-encoded file.
  97. // 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
  98. func CertsFromFile(file string) ([]*x509.Certificate, error) {
  99. pemBlock, err := ioutil.ReadFile(file)
  100. if err != nil {
  101. return nil, err
  102. }
  103. certs, err := ParseCertsPEM(pemBlock)
  104. if err != nil {
  105. return nil, fmt.Errorf("error reading %s: %s", file, err)
  106. }
  107. return certs, nil
  108. }