global.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2015 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 command
  15. import (
  16. "crypto/tls"
  17. "errors"
  18. "io"
  19. "io/ioutil"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/coreos/etcd/pkg/flags"
  23. "github.com/coreos/etcd/pkg/transport"
  24. "github.com/spf13/cobra"
  25. )
  26. // GlobalFlags are flags that defined globally
  27. // and are inherited to all sub-commands.
  28. type GlobalFlags struct {
  29. Insecure bool
  30. InsecureSkipVerify bool
  31. Endpoints []string
  32. DialTimeout time.Duration
  33. CommandTimeOut time.Duration
  34. TLS transport.TLSInfo
  35. OutputFormat string
  36. IsHex bool
  37. }
  38. type secureCfg struct {
  39. cert string
  40. key string
  41. cacert string
  42. insecureTransport bool
  43. insecureSkipVerify bool
  44. }
  45. var display printer = &simplePrinter{}
  46. func mustClientFromCmd(cmd *cobra.Command) *clientv3.Client {
  47. flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags())
  48. endpoints, err := cmd.Flags().GetStringSlice("endpoints")
  49. if err != nil {
  50. ExitWithError(ExitError, err)
  51. }
  52. dialTimeout := dialTimeoutFromCmd(cmd)
  53. sec := secureCfgFromCmd(cmd)
  54. isHex, err := cmd.Flags().GetBool("hex")
  55. if err != nil {
  56. ExitWithError(ExitError, err)
  57. }
  58. outputType, err := cmd.Flags().GetString("write-out")
  59. if err != nil {
  60. ExitWithError(ExitError, err)
  61. }
  62. if display = NewPrinter(outputType, isHex); display == nil {
  63. ExitWithError(ExitBadFeature, errors.New("unsupported output format"))
  64. }
  65. return mustClient(endpoints, dialTimeout, sec)
  66. }
  67. func mustClient(endpoints []string, dialTimeout time.Duration, scfg *secureCfg) *clientv3.Client {
  68. cfg, err := newClientCfg(endpoints, dialTimeout, scfg)
  69. if err != nil {
  70. ExitWithError(ExitBadArgs, err)
  71. }
  72. client, err := clientv3.New(*cfg)
  73. if err != nil {
  74. ExitWithError(ExitBadConnection, err)
  75. }
  76. return client
  77. }
  78. func newClientCfg(endpoints []string, dialTimeout time.Duration, scfg *secureCfg) (*clientv3.Config, error) {
  79. // set tls if any one tls option set
  80. var cfgtls *transport.TLSInfo
  81. tlsinfo := transport.TLSInfo{}
  82. if scfg.cert != "" {
  83. tlsinfo.CertFile = scfg.cert
  84. cfgtls = &tlsinfo
  85. }
  86. if scfg.key != "" {
  87. tlsinfo.KeyFile = scfg.key
  88. cfgtls = &tlsinfo
  89. }
  90. if scfg.cacert != "" {
  91. tlsinfo.CAFile = scfg.cacert
  92. cfgtls = &tlsinfo
  93. }
  94. cfg := &clientv3.Config{
  95. Endpoints: endpoints,
  96. DialTimeout: dialTimeout,
  97. }
  98. if cfgtls != nil {
  99. clientTLS, err := cfgtls.ClientConfig()
  100. if err != nil {
  101. return nil, err
  102. }
  103. cfg.TLS = clientTLS
  104. }
  105. // if key/cert is not given but user wants secure connection, we
  106. // should still setup an empty tls configuration for gRPC to setup
  107. // secure connection.
  108. if cfg.TLS == nil && !scfg.insecureTransport {
  109. cfg.TLS = &tls.Config{}
  110. }
  111. // If the user wants to skip TLS verification then we should set
  112. // the InsecureSkipVerify flag in tls configuration.
  113. if scfg.insecureSkipVerify && cfg.TLS != nil {
  114. cfg.TLS.InsecureSkipVerify = true
  115. }
  116. return cfg, nil
  117. }
  118. func argOrStdin(args []string, stdin io.Reader, i int) (string, error) {
  119. if i < len(args) {
  120. return args[i], nil
  121. }
  122. bytes, err := ioutil.ReadAll(stdin)
  123. if string(bytes) == "" || err != nil {
  124. return "", errors.New("no available argument and stdin")
  125. }
  126. return string(bytes), nil
  127. }
  128. func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
  129. dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
  130. if err != nil {
  131. ExitWithError(ExitError, err)
  132. }
  133. return dialTimeout
  134. }
  135. func secureCfgFromCmd(cmd *cobra.Command) *secureCfg {
  136. cert, key, cacert := keyAndCertFromCmd(cmd)
  137. insecureTr := insecureTransportFromCmd(cmd)
  138. skipVerify := insecureSkipVerifyFromCmd(cmd)
  139. return &secureCfg{
  140. cert: cert,
  141. key: key,
  142. cacert: cacert,
  143. insecureTransport: insecureTr,
  144. insecureSkipVerify: skipVerify,
  145. }
  146. }
  147. func insecureTransportFromCmd(cmd *cobra.Command) bool {
  148. insecureTr, err := cmd.Flags().GetBool("insecure-transport")
  149. if err != nil {
  150. ExitWithError(ExitError, err)
  151. }
  152. return insecureTr
  153. }
  154. func insecureSkipVerifyFromCmd(cmd *cobra.Command) bool {
  155. skipVerify, err := cmd.Flags().GetBool("insecure-skip-tls-verify")
  156. if err != nil {
  157. ExitWithError(ExitError, err)
  158. }
  159. return skipVerify
  160. }
  161. func keyAndCertFromCmd(cmd *cobra.Command) (cert, key, cacert string) {
  162. var err error
  163. if cert, err = cmd.Flags().GetString("cert"); err != nil {
  164. ExitWithError(ExitBadArgs, err)
  165. } else if cert == "" && cmd.Flags().Changed("cert") {
  166. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cert option"))
  167. }
  168. if key, err = cmd.Flags().GetString("key"); err != nil {
  169. ExitWithError(ExitBadArgs, err)
  170. } else if key == "" && cmd.Flags().Changed("key") {
  171. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --key option"))
  172. }
  173. if cacert, err = cmd.Flags().GetString("cacert"); err != nil {
  174. ExitWithError(ExitBadArgs, err)
  175. } else if cacert == "" && cmd.Flags().Changed("cacert") {
  176. ExitWithError(ExitBadArgs, errors.New("empty string is passed to --cacert option"))
  177. }
  178. return cert, key, cacert
  179. }