client.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "log"
  21. "net"
  22. "net/url"
  23. "strings"
  24. "time"
  25. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  26. "golang.org/x/net/context"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/credentials"
  29. "google.golang.org/grpc/metadata"
  30. )
  31. var (
  32. ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
  33. )
  34. // Client provides and manages an etcd v3 client session.
  35. type Client struct {
  36. Cluster
  37. KV
  38. Lease
  39. Watcher
  40. Auth
  41. Maintenance
  42. conn *grpc.ClientConn
  43. cfg Config
  44. creds *credentials.TransportAuthenticator
  45. ctx context.Context
  46. cancel context.CancelFunc
  47. // Username is a username for authentication
  48. Username string
  49. // Password is a password for authentication
  50. Password string
  51. }
  52. // New creates a new etcdv3 client from a given configuration.
  53. func New(cfg Config) (*Client, error) {
  54. if len(cfg.Endpoints) == 0 {
  55. return nil, ErrNoAvailableEndpoints
  56. }
  57. return newClient(&cfg)
  58. }
  59. // NewFromURL creates a new etcdv3 client from a URL.
  60. func NewFromURL(url string) (*Client, error) {
  61. return New(Config{Endpoints: []string{url}})
  62. }
  63. // NewFromConfigFile creates a new etcdv3 client from a configuration file.
  64. func NewFromConfigFile(path string) (*Client, error) {
  65. cfg, err := configFromFile(path)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return New(*cfg)
  70. }
  71. // Close shuts down the client's etcd connections.
  72. func (c *Client) Close() error {
  73. c.cancel()
  74. return toErr(c.ctx, c.conn.Close())
  75. }
  76. // Ctx is a context for "out of band" messages (e.g., for sending
  77. // "clean up" message when another context is canceled). It is
  78. // canceled on client Close().
  79. func (c *Client) Ctx() context.Context { return c.ctx }
  80. // Endpoints lists the registered endpoints for the client.
  81. func (c *Client) Endpoints() []string { return c.cfg.Endpoints }
  82. type authTokenCredential struct {
  83. token string
  84. }
  85. func (cred authTokenCredential) RequireTransportSecurity() bool {
  86. return false
  87. }
  88. func (cred authTokenCredential) GetRequestMetadata(ctx context.Context, s ...string) (map[string]string, error) {
  89. return map[string]string{
  90. "token": cred.token,
  91. }, nil
  92. }
  93. // Dial establishes a connection for a given endpoint using the client's config
  94. func (c *Client) Dial(endpoint string) (*grpc.ClientConn, error) {
  95. opts := []grpc.DialOption{
  96. grpc.WithBlock(),
  97. grpc.WithTimeout(c.cfg.DialTimeout),
  98. }
  99. proto := "tcp"
  100. creds := c.creds
  101. if url, uerr := url.Parse(endpoint); uerr == nil && strings.Contains(endpoint, "://") {
  102. switch url.Scheme {
  103. case "unix":
  104. proto = "unix"
  105. case "http":
  106. creds = nil
  107. case "https":
  108. if creds == nil {
  109. tlsconfig := &tls.Config{InsecureSkipVerify: true}
  110. emptyCreds := credentials.NewTLS(tlsconfig)
  111. creds = &emptyCreds
  112. }
  113. default:
  114. return nil, fmt.Errorf("unknown scheme %q for %q", url.Scheme, endpoint)
  115. }
  116. // strip scheme:// prefix since grpc dials by host
  117. endpoint = url.Host
  118. }
  119. f := func(a string, t time.Duration) (net.Conn, error) {
  120. select {
  121. case <-c.ctx.Done():
  122. return nil, c.ctx.Err()
  123. default:
  124. }
  125. return net.DialTimeout(proto, a, t)
  126. }
  127. opts = append(opts, grpc.WithDialer(f))
  128. if creds != nil {
  129. opts = append(opts, grpc.WithTransportCredentials(*creds))
  130. } else {
  131. opts = append(opts, grpc.WithInsecure())
  132. }
  133. if c.Username != "" && c.Password != "" {
  134. auth, err := newAuthenticator(endpoint, opts)
  135. if err != nil {
  136. return nil, err
  137. }
  138. defer auth.close()
  139. resp, err := auth.authenticate(c.ctx, c.Username, c.Password)
  140. if err != nil {
  141. return nil, err
  142. }
  143. opts = append(opts, grpc.WithPerRPCCredentials(authTokenCredential{token: resp.Token}))
  144. }
  145. conn, err := grpc.Dial(endpoint, opts...)
  146. if err != nil {
  147. return nil, err
  148. }
  149. return conn, nil
  150. }
  151. // WithRequireLeader requires client requests to only succeed
  152. // when the cluster has a leader.
  153. func WithRequireLeader(ctx context.Context) context.Context {
  154. md := metadata.Pairs(rpctypes.MetadataRequireLeaderKey, rpctypes.MetadataHasLeader)
  155. return metadata.NewContext(ctx, md)
  156. }
  157. func newClient(cfg *Config) (*Client, error) {
  158. if cfg == nil {
  159. cfg = &Config{}
  160. }
  161. var creds *credentials.TransportAuthenticator
  162. if cfg.TLS != nil {
  163. c := credentials.NewTLS(cfg.TLS)
  164. creds = &c
  165. }
  166. // use a temporary skeleton client to bootstrap first connection
  167. ctx, cancel := context.WithCancel(context.TODO())
  168. client := &Client{
  169. conn: nil,
  170. cfg: *cfg,
  171. creds: creds,
  172. ctx: ctx,
  173. cancel: cancel,
  174. }
  175. if cfg.Username != "" && cfg.Password != "" {
  176. client.Username = cfg.Username
  177. client.Password = cfg.Password
  178. }
  179. // TODO: use grpc balancer
  180. conn, err := client.Dial(cfg.Endpoints[0])
  181. if err != nil {
  182. return nil, err
  183. }
  184. client.conn = conn
  185. client.Cluster = NewCluster(client)
  186. client.KV = NewKV(client)
  187. client.Lease = NewLease(client)
  188. client.Watcher = NewWatcher(client)
  189. client.Auth = NewAuth(client)
  190. client.Maintenance = NewMaintenance(client)
  191. if cfg.Logger != nil {
  192. logger.Set(cfg.Logger)
  193. } else {
  194. // disable client side grpc by default
  195. logger.Set(log.New(ioutil.Discard, "", 0))
  196. }
  197. return client, nil
  198. }
  199. // ActiveConnection returns the current in-use connection
  200. func (c *Client) ActiveConnection() *grpc.ClientConn { return c.conn }
  201. // isHaltErr returns true if the given error and context indicate no forward
  202. // progress can be made, even after reconnecting.
  203. func isHaltErr(ctx context.Context, err error) bool {
  204. if ctx != nil && ctx.Err() != nil {
  205. return true
  206. }
  207. if err == nil {
  208. return false
  209. }
  210. return strings.HasPrefix(grpc.ErrorDesc(err), "etcdserver: ") ||
  211. strings.Contains(err.Error(), grpc.ErrClientConnClosing.Error())
  212. }
  213. func toErr(ctx context.Context, err error) error {
  214. if err == nil {
  215. return nil
  216. }
  217. err = rpctypes.Error(err)
  218. if ctx.Err() != nil && strings.Contains(err.Error(), "context") {
  219. err = ctx.Err()
  220. } else if strings.Contains(err.Error(), grpc.ErrClientConnClosing.Error()) {
  221. err = grpc.ErrClientConnClosing
  222. }
  223. return err
  224. }