config.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 CoreOS, Inc.
  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. "google.golang.org/grpc"
  23. )
  24. // EndpointDialer is a policy for choosing which endpoint to dial next
  25. type EndpointDialer func(*Client) (*grpc.ClientConn, error)
  26. type Config struct {
  27. // Endpoints is a list of URLs
  28. Endpoints []string
  29. // RetryDialer chooses the next endpoint to use
  30. RetryDialer EndpointDialer
  31. // DialTimeout is the timeout for failing to establish a connection.
  32. DialTimeout time.Duration
  33. // TLS holds the client secure credentials, if any.
  34. TLS *tls.Config
  35. // Logger is the logger used by client library.
  36. Logger Logger
  37. }
  38. type YamlConfig struct {
  39. Endpoints []string `json:"endpoints"`
  40. DialTimeout time.Duration `json:"dial-timeout"`
  41. InsecureTransport bool `json:"insecure-transport"`
  42. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify"`
  43. Certfile string `json:"cert-file"`
  44. Keyfile string `json:"key-file"`
  45. CAfile string `json:"ca-file"`
  46. }
  47. func configFromFile(fpath string) (*Config, error) {
  48. b, err := ioutil.ReadFile(fpath)
  49. if err != nil {
  50. return nil, err
  51. }
  52. yc := &YamlConfig{}
  53. err = yaml.Unmarshal(b, yc)
  54. if err != nil {
  55. return nil, err
  56. }
  57. cfg := &Config{
  58. Endpoints: yc.Endpoints,
  59. DialTimeout: yc.DialTimeout,
  60. }
  61. if yc.InsecureTransport {
  62. cfg.TLS = nil
  63. return cfg, nil
  64. }
  65. var (
  66. cert *tls.Certificate
  67. cp *x509.CertPool
  68. )
  69. if yc.Certfile != "" && yc.Keyfile != "" {
  70. cert, err = tlsutil.NewCert(yc.Certfile, yc.Keyfile, nil)
  71. if err != nil {
  72. return nil, err
  73. }
  74. }
  75. if yc.CAfile != "" {
  76. cp, err = tlsutil.NewCertPool([]string{yc.CAfile})
  77. if err != nil {
  78. return nil, err
  79. }
  80. }
  81. tlscfg := &tls.Config{
  82. MinVersion: tls.VersionTLS10,
  83. InsecureSkipVerify: yc.InsecureSkipTLSVerify,
  84. RootCAs: cp,
  85. }
  86. if cert != nil {
  87. tlscfg.Certificates = []tls.Certificate{*cert}
  88. }
  89. cfg.TLS = tlscfg
  90. return cfg, nil
  91. }