Forráskód Böngészése

etcdctlv3: support tls

Fixes #4299
Anthony Romano 10 éve
szülő
commit
781bf625af
2 módosított fájl, 25 hozzáadás és 1 törlés
  1. 21 1
      etcdctlv3/command/global.go
  2. 4 0
      etcdctlv3/main.go

+ 21 - 1
etcdctlv3/command/global.go

@@ -17,12 +17,14 @@ package command
 import (
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
 	"github.com/coreos/etcd/clientv3"
+	"github.com/coreos/etcd/pkg/transport"
 )
 
 // GlobalFlags are flags that defined globally
 // and are inherited to all sub-commands.
 type GlobalFlags struct {
 	Endpoints string
+	TLS       transport.TLSInfo
 }
 
 func mustClient(cmd *cobra.Command) *clientv3.Client {
@@ -30,7 +32,25 @@ func mustClient(cmd *cobra.Command) *clientv3.Client {
 	if err != nil {
 		ExitWithError(ExitError, err)
 	}
-	client, err := clientv3.NewFromURL(endpoint)
+
+	// set tls if any one tls option set
+	var cfgtls *transport.TLSInfo
+	tls := transport.TLSInfo{}
+	if tls.CertFile, err = cmd.Flags().GetString("cert"); err == nil {
+		cfgtls = &tls
+	}
+	if tls.KeyFile, err = cmd.Flags().GetString("key"); err == nil {
+		cfgtls = &tls
+	}
+	if tls.CAFile, err = cmd.Flags().GetString("cacert"); err == nil {
+		cfgtls = &tls
+	}
+	cfg := clientv3.Config{
+		Endpoints: []string{endpoint},
+		TLS:       cfgtls,
+	}
+
+	client, err := clientv3.New(cfg)
 	if err != nil {
 		ExitWithError(ExitBadConnection, err)
 	}

+ 4 - 0
etcdctlv3/main.go

@@ -43,6 +43,10 @@ var (
 func init() {
 	rootCmd.PersistentFlags().StringVar(&globalFlags.Endpoints, "endpoint", "127.0.0.1:2378", "gRPC endpoint")
 
+	rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify HTTPS client using this SSL certificate file")
+	rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify HTTPS client using this SSL key file")
+	rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CAFile, "cacert", "", "verify certificates of HTTPS-enabled servers using this CA bundle")
+
 	rootCmd.AddCommand(
 		command.NewRangeCommand(),
 		command.NewPutCommand(),