config.go 2.8 KB

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