config.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2016 The etcd Authors
  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 clientv3
  15. import (
  16. "crypto/tls"
  17. "crypto/x509"
  18. "io/ioutil"
  19. "time"
  20. "github.com/coreos/etcd/pkg/tlsutil"
  21. "github.com/ghodss/yaml"
  22. )
  23. type Config struct {
  24. // Endpoints is a list of URLs
  25. Endpoints []string
  26. // DialTimeout is the timeout for failing to establish a connection.
  27. DialTimeout time.Duration
  28. // TLS holds the client secure credentials, if any.
  29. TLS *tls.Config
  30. // Logger is the logger used by client library.
  31. Logger Logger
  32. // Username is a username for authentication
  33. Username string
  34. // Password is a password for authentication
  35. Password string
  36. }
  37. type yamlConfig struct {
  38. Endpoints []string `json:"endpoints"`
  39. DialTimeout time.Duration `json:"dial-timeout"`
  40. InsecureTransport bool `json:"insecure-transport"`
  41. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify"`
  42. Certfile string `json:"cert-file"`
  43. Keyfile string `json:"key-file"`
  44. CAfile string `json:"ca-file"`
  45. }
  46. func configFromFile(fpath string) (*Config, error) {
  47. b, err := ioutil.ReadFile(fpath)
  48. if err != nil {
  49. return nil, err
  50. }
  51. yc := &yamlConfig{}
  52. err = yaml.Unmarshal(b, yc)
  53. if err != nil {
  54. return nil, err
  55. }
  56. cfg := &Config{
  57. Endpoints: yc.Endpoints,
  58. DialTimeout: yc.DialTimeout,
  59. }
  60. if yc.InsecureTransport {
  61. cfg.TLS = nil
  62. return cfg, nil
  63. }
  64. var (
  65. cert *tls.Certificate
  66. cp *x509.CertPool
  67. )
  68. if yc.Certfile != "" && yc.Keyfile != "" {
  69. cert, err = tlsutil.NewCert(yc.Certfile, yc.Keyfile, nil)
  70. if err != nil {
  71. return nil, err
  72. }
  73. }
  74. if yc.CAfile != "" {
  75. cp, err = tlsutil.NewCertPool([]string{yc.CAfile})
  76. if err != nil {
  77. return nil, err
  78. }
  79. }
  80. tlscfg := &tls.Config{
  81. MinVersion: tls.VersionTLS10,
  82. InsecureSkipVerify: yc.InsecureSkipTLSVerify,
  83. RootCAs: cp,
  84. }
  85. if cert != nil {
  86. tlscfg.Certificates = []tls.Certificate{*cert}
  87. }
  88. cfg.TLS = tlscfg
  89. return cfg, nil
  90. }