grpc_proxy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 etcdmain
  15. import (
  16. "fmt"
  17. "net"
  18. "os"
  19. "time"
  20. "github.com/coreos/etcd/clientv3"
  21. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/pkg/transport"
  23. "github.com/coreos/etcd/proxy/grpcproxy"
  24. "github.com/spf13/cobra"
  25. "google.golang.org/grpc"
  26. )
  27. var (
  28. grpcProxyListenAddr string
  29. grpcProxyEndpoints []string
  30. grpcProxyCert string
  31. grpcProxyKey string
  32. grpcProxyCA string
  33. )
  34. func init() {
  35. rootCmd.AddCommand(newGRPCProxyCommand())
  36. }
  37. // newGRPCProxyCommand returns the cobra command for "grpc-proxy".
  38. func newGRPCProxyCommand() *cobra.Command {
  39. lpc := &cobra.Command{
  40. Use: "grpc-proxy <subcommand>",
  41. Short: "grpc-proxy related command",
  42. }
  43. lpc.AddCommand(newGRPCProxyStartCommand())
  44. return lpc
  45. }
  46. func newGRPCProxyStartCommand() *cobra.Command {
  47. cmd := cobra.Command{
  48. Use: "start",
  49. Short: "start the grpc proxy",
  50. Run: startGRPCProxy,
  51. }
  52. cmd.Flags().StringVar(&grpcProxyListenAddr, "listen-addr", "127.0.0.1:23790", "listen address")
  53. cmd.Flags().StringSliceVar(&grpcProxyEndpoints, "endpoints", []string{"127.0.0.1:2379"}, "comma separated etcd cluster endpoints")
  54. cmd.Flags().StringVar(&grpcProxyCert, "cert", "", "identify secure connections with etcd servers using this TLS certificate file")
  55. cmd.Flags().StringVar(&grpcProxyKey, "key", "", "identify secure connections with etcd servers using this TLS key file")
  56. cmd.Flags().StringVar(&grpcProxyCA, "cacert", "", "verify certificates of TLS-enabled secure etcd servers using this CA bundle")
  57. return &cmd
  58. }
  59. func startGRPCProxy(cmd *cobra.Command, args []string) {
  60. l, err := net.Listen("tcp", grpcProxyListenAddr)
  61. if err != nil {
  62. fmt.Fprintln(os.Stderr, err)
  63. os.Exit(1)
  64. }
  65. cfg, err := newClientCfg()
  66. if err != nil {
  67. fmt.Fprintln(os.Stderr, err)
  68. os.Exit(1)
  69. }
  70. client, err := clientv3.New(*cfg)
  71. if err != nil {
  72. fmt.Fprintln(os.Stderr, err)
  73. os.Exit(1)
  74. }
  75. kvp := grpcproxy.NewKvProxy(client)
  76. watchp := grpcproxy.NewWatchProxy(client)
  77. clusterp := grpcproxy.NewClusterProxy(client)
  78. server := grpc.NewServer()
  79. pb.RegisterKVServer(server, kvp)
  80. pb.RegisterWatchServer(server, watchp)
  81. pb.RegisterClusterServer(server, clusterp)
  82. server.Serve(l)
  83. }
  84. func newClientCfg() (*clientv3.Config, error) {
  85. // set tls if any one tls option set
  86. var cfgtls *transport.TLSInfo
  87. tlsinfo := transport.TLSInfo{}
  88. if grpcProxyCert != "" {
  89. tlsinfo.CertFile = grpcProxyCert
  90. cfgtls = &tlsinfo
  91. }
  92. if grpcProxyKey != "" {
  93. tlsinfo.KeyFile = grpcProxyKey
  94. cfgtls = &tlsinfo
  95. }
  96. if grpcProxyCA != "" {
  97. tlsinfo.CAFile = grpcProxyCA
  98. cfgtls = &tlsinfo
  99. }
  100. cfg := clientv3.Config{
  101. Endpoints: grpcProxyEndpoints,
  102. DialTimeout: 5 * time.Second,
  103. }
  104. if cfgtls != nil {
  105. clientTLS, err := cfgtls.ClientConfig()
  106. if err != nil {
  107. return nil, err
  108. }
  109. cfg.TLS = clientTLS
  110. }
  111. // TODO: support insecure tls
  112. return &cfg, nil
  113. }