config.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "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. // Username is a username of authentication
  38. Username string
  39. // Password is a password of authentication
  40. Password string
  41. }
  42. type yamlConfig struct {
  43. Endpoints []string `json:"endpoints"`
  44. DialTimeout time.Duration `json:"dial-timeout"`
  45. InsecureTransport bool `json:"insecure-transport"`
  46. InsecureSkipTLSVerify bool `json:"insecure-skip-tls-verify"`
  47. Certfile string `json:"cert-file"`
  48. Keyfile string `json:"key-file"`
  49. CAfile string `json:"ca-file"`
  50. }
  51. func configFromFile(fpath string) (*Config, error) {
  52. b, err := ioutil.ReadFile(fpath)
  53. if err != nil {
  54. return nil, err
  55. }
  56. yc := &yamlConfig{}
  57. err = yaml.Unmarshal(b, yc)
  58. if err != nil {
  59. return nil, err
  60. }
  61. cfg := &Config{
  62. Endpoints: yc.Endpoints,
  63. DialTimeout: yc.DialTimeout,
  64. }
  65. if yc.InsecureTransport {
  66. cfg.TLS = nil
  67. return cfg, nil
  68. }
  69. var (
  70. cert *tls.Certificate
  71. cp *x509.CertPool
  72. )
  73. if yc.Certfile != "" && yc.Keyfile != "" {
  74. cert, err = tlsutil.NewCert(yc.Certfile, yc.Keyfile, nil)
  75. if err != nil {
  76. return nil, err
  77. }
  78. }
  79. if yc.CAfile != "" {
  80. cp, err = tlsutil.NewCertPool([]string{yc.CAfile})
  81. if err != nil {
  82. return nil, err
  83. }
  84. }
  85. tlscfg := &tls.Config{
  86. MinVersion: tls.VersionTLS10,
  87. InsecureSkipVerify: yc.InsecureSkipTLSVerify,
  88. RootCAs: cp,
  89. }
  90. if cert != nil {
  91. tlscfg.Certificates = []tls.Certificate{*cert}
  92. }
  93. cfg.TLS = tlscfg
  94. return cfg, nil
  95. }