tlsconfig.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package storage
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "fmt"
  6. "os"
  7. )
  8. // NewTLSConfig creates a new tls.Config from the given TLSConfig.
  9. func NewTLSConfig(cfg *TLSConfig) (*tls.Config, error) {
  10. tlsConfig := &tls.Config{InsecureSkipVerify: cfg.InsecureSkipVerify}
  11. // If a CA cert is provided then let's read it in.
  12. if len(cfg.CAFile) > 0 {
  13. b, err := readCAFile(cfg.CAFile)
  14. if err != nil {
  15. return nil, err
  16. }
  17. if !updateRootCA(tlsConfig, b) {
  18. return nil, fmt.Errorf("unable to use specified CA cert %s", cfg.CAFile)
  19. }
  20. }
  21. if len(cfg.ServerName) > 0 {
  22. tlsConfig.ServerName = cfg.ServerName
  23. }
  24. // If a client cert & key is provided then configure TLS config accordingly.
  25. if len(cfg.CertFile) > 0 && len(cfg.KeyFile) == 0 {
  26. return nil, fmt.Errorf("client cert file %q specified without client key file", cfg.CertFile)
  27. } else if len(cfg.KeyFile) > 0 && len(cfg.CertFile) == 0 {
  28. return nil, fmt.Errorf("client key file %q specified without client cert file", cfg.KeyFile)
  29. } else if len(cfg.CertFile) > 0 && len(cfg.KeyFile) > 0 {
  30. // Verify that client cert and key are valid.
  31. if _, err := cfg.getClientCertificate(nil); err != nil {
  32. return nil, err
  33. }
  34. tlsConfig.GetClientCertificate = cfg.getClientCertificate
  35. }
  36. return tlsConfig, nil
  37. }
  38. // readCAFile reads the CA cert file from disk.
  39. func readCAFile(f string) ([]byte, error) {
  40. data, err := os.ReadFile(f)
  41. if err != nil {
  42. return nil, fmt.Errorf("unable to load specified CA cert %s: %s", f, err)
  43. }
  44. return data, nil
  45. }
  46. // updateRootCA parses the given byte slice as a series of PEM encoded certificates and updates tls.Config.RootCAs.
  47. func updateRootCA(cfg *tls.Config, b []byte) bool {
  48. caCertPool := x509.NewCertPool()
  49. if !caCertPool.AppendCertsFromPEM(b) {
  50. return false
  51. }
  52. cfg.RootCAs = caCertPool
  53. return true
  54. }
  55. // getClientCertificate reads the pair of client cert and key from disk and returns a tls.Certificate.
  56. func (c *TLSConfig) getClientCertificate(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
  57. cert, err := tls.LoadX509KeyPair(c.CertFile, c.KeyFile)
  58. if err != nil {
  59. return nil, fmt.Errorf("unable to use specified client cert (%s) & key (%s): %s", c.CertFile, c.KeyFile, err)
  60. }
  61. return &cert, nil
  62. }
  63. // TLSConfig configures the options for TLS connections.
  64. type TLSConfig struct {
  65. // The CA cert to use for the targets.
  66. CAFile string `yaml:"ca_file"`
  67. // The client cert file for the targets.
  68. CertFile string `yaml:"cert_file"`
  69. // The client key file for the targets.
  70. KeyFile string `yaml:"key_file"`
  71. // Used to verify the hostname for the targets.
  72. ServerName string `yaml:"server_name"`
  73. // Disable target certificate validation.
  74. InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
  75. }