global.go 6.5 KB

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