config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // AutoSyncInterval is the interval to update endpoints with its latest members.
  27. // 0 disables auto-sync. By default auto-sync is disabled.
  28. AutoSyncInterval time.Duration
  29. // DialTimeout is the timeout for failing to establish a connection.
  30. DialTimeout time.Duration
  31. // TLS holds the client secure credentials, if any.
  32. TLS *tls.Config
  33. // Username is a username for authentication
  34. Username string
  35. // Password is a password for authentication
  36. Password string
  37. }
  38. type yamlConfig struct {
  39. Endpoints []string `json:"endpoints"`
  40. AutoSyncInterval time.Duration `json:"auto-sync-interval"`
  41. DialTimeout time.Duration `json:"dial-timeout"`
  42. InsecureTransport bool `json:"insecure-transport"`
  43. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify"`
  44. Certfile string `json:"cert-file"`
  45. Keyfile string `json:"key-file"`
  46. CAfile string `json:"ca-file"`
  47. }
  48. func configFromFile(fpath string) (*Config, error) {
  49. b, err := ioutil.ReadFile(fpath)
  50. if err != nil {
  51. return nil, err
  52. }
  53. yc := &yamlConfig{}
  54. err = yaml.Unmarshal(b, yc)
  55. if err != nil {
  56. return nil, err
  57. }
  58. cfg := &Config{
  59. Endpoints: yc.Endpoints,
  60. AutoSyncInterval: yc.AutoSyncInterval,
  61. DialTimeout: yc.DialTimeout,
  62. }
  63. if yc.InsecureTransport {
  64. cfg.TLS = nil
  65. return cfg, nil
  66. }
  67. var (
  68. cert *tls.Certificate
  69. cp *x509.CertPool
  70. )
  71. if yc.Certfile != "" && yc.Keyfile != "" {
  72. cert, err = tlsutil.NewCert(yc.Certfile, yc.Keyfile, nil)
  73. if err != nil {
  74. return nil, err
  75. }
  76. }
  77. if yc.CAfile != "" {
  78. cp, err = tlsutil.NewCertPool([]string{yc.CAfile})
  79. if err != nil {
  80. return nil, err
  81. }
  82. }
  83. tlscfg := &tls.Config{
  84. MinVersion: tls.VersionTLS10,
  85. InsecureSkipVerify: yc.InsecureSkipTLSVerify,
  86. RootCAs: cp,
  87. }
  88. if cert != nil {
  89. tlscfg.Certificates = []tls.Certificate{*cert}
  90. }
  91. cfg.TLS = tlscfg
  92. return cfg, nil
  93. }