commands: move keyring command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-11 15:42:45 +02:00 committed by Frank Schröder
parent 9e19207918
commit 631502009d
3 changed files with 57 additions and 54 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/hashicorp/consul/command/info" "github.com/hashicorp/consul/command/info"
"github.com/hashicorp/consul/command/join" "github.com/hashicorp/consul/command/join"
"github.com/hashicorp/consul/command/keygen" "github.com/hashicorp/consul/command/keygen"
"github.com/hashicorp/consul/command/keyring"
"github.com/hashicorp/consul/command/kv" "github.com/hashicorp/consul/command/kv"
"github.com/hashicorp/consul/command/kvdel" "github.com/hashicorp/consul/command/kvdel"
"github.com/hashicorp/consul/command/kvexp" "github.com/hashicorp/consul/command/kvexp"
@ -111,12 +112,7 @@ func init() {
}, },
"keyring": func() (cli.Command, error) { "keyring": func() (cli.Command, error) {
return &KeyringCommand{ return keyring.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetClientHTTP,
UI: ui,
},
}, nil
}, },
"kv": func() (cli.Command, error) { "kv": func() (cli.Command, error) {

View File

@ -1,17 +1,25 @@
package command package keyring
import ( import (
"flag"
"fmt" "fmt"
"github.com/hashicorp/consul/agent" "github.com/hashicorp/consul/agent"
consulapi "github.com/hashicorp/consul/api" consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
// KeyringCommand is a Command implementation that handles querying, installing, func New(ui cli.Ui) *cmd {
// and removing gossip encryption keys from a keyring. c := &cmd{UI: ui}
type KeyringCommand struct { c.initFlags()
BaseCommand return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
// flags // flags
installKey string installKey string
@ -21,29 +29,32 @@ type KeyringCommand struct {
relay int relay int
} }
func (c *KeyringCommand) initFlags() { func (c *cmd) initFlags() {
c.InitFlagSet() c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.FlagSet.StringVar(&c.installKey, "install", "", c.flags.StringVar(&c.installKey, "install", "",
"Install a new encryption key. This will broadcast the new key to "+ "Install a new encryption key. This will broadcast the new key to "+
"all members in the cluster.") "all members in the cluster.")
c.FlagSet.StringVar(&c.useKey, "use", "", c.flags.StringVar(&c.useKey, "use", "",
"Change the primary encryption key, which is used to encrypt "+ "Change the primary encryption key, which is used to encrypt "+
"messages. The key must already be installed before this operation "+ "messages. The key must already be installed before this operation "+
"can succeed.") "can succeed.")
c.FlagSet.StringVar(&c.removeKey, "remove", "", c.flags.StringVar(&c.removeKey, "remove", "",
"Remove the given key from the cluster. This operation may only be "+ "Remove the given key from the cluster. This operation may only be "+
"performed on keys which are not currently the primary key.") "performed on keys which are not currently the primary key.")
c.FlagSet.BoolVar(&c.listKeys, "list", false, c.flags.BoolVar(&c.listKeys, "list", false,
"List all keys currently in use within the cluster.") "List all keys currently in use within the cluster.")
c.FlagSet.IntVar(&c.relay, "relay-factor", 0, c.flags.IntVar(&c.relay, "relay-factor", 0,
"Setting this to a non-zero value will cause nodes to relay their response "+ "Setting this to a non-zero value will cause nodes to relay their response "+
"to the operation through this many randomly-chosen other nodes in the "+ "to the operation through this many randomly-chosen other nodes in the "+
"cluster. The maximum allowed value is 5.") "cluster. The maximum allowed value is 5.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
} }
func (c *KeyringCommand) Run(args []string) int { func (c *cmd) Run(args []string) int {
c.initFlags() if err := c.flags.Parse(args); err != nil {
if err := c.FlagSet.Parse(args); err != nil {
return 1 return 1
} }
@ -78,7 +89,7 @@ func (c *KeyringCommand) Run(args []string) int {
} }
// All other operations will require a client connection // All other operations will require a client connection
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1 return 1
@ -130,7 +141,7 @@ func (c *KeyringCommand) Run(args []string) int {
return 0 return 0
} }
func (c *KeyringCommand) handleList(responses []*consulapi.KeyringResponse) { func (c *cmd) handleList(responses []*consulapi.KeyringResponse) {
for _, response := range responses { for _, response := range responses {
pool := response.Datacenter + " (LAN)" pool := response.Datacenter + " (LAN)"
if response.Segment != "" { if response.Segment != "" {
@ -148,10 +159,12 @@ func (c *KeyringCommand) handleList(responses []*consulapi.KeyringResponse) {
} }
} }
func (c *KeyringCommand) Help() string { func (c *cmd) Synopsis() string {
c.initFlags() return "Manages gossip layer encryption keys"
return c.HelpCommand(` }
Usage: consul keyring [options]
func (c *cmd) Help() string {
s := `Usage: consul keyring [options]
Manages encryption keys used for gossip messages. Gossip encryption is Manages encryption keys used for gossip messages. Gossip encryption is
optional. When enabled, this command may be used to examine active encryption optional. When enabled, this command may be used to examine active encryption
@ -164,11 +177,6 @@ Usage: consul keyring [options]
All variations of the keyring command return 0 if all nodes reply and there All variations of the keyring command return 0 if all nodes reply and there
are no errors. If any node fails to reply or reports failure, the exit code are no errors. If any node fails to reply or reports failure, the exit code
will be 1. will be 1.`
return flags.Usage(s, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
`)
}
func (c *KeyringCommand) Synopsis() string {
return "Manages gossip layer encryption keys"
} }

View File

@ -1,4 +1,4 @@
package command package keyring
import ( import (
"strings" "strings"
@ -8,19 +8,11 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func testKeyringCommand(t *testing.T) (*cli.MockUi, *KeyringCommand) { func TestKeyringCommand_noTabs(t *testing.T) {
ui := cli.NewMockUi()
return ui, &KeyringCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetClientHTTP,
},
}
}
func TestKeyringCommand_implements(t *testing.T) {
t.Parallel() t.Parallel()
var _ cli.Command = &KeyringCommand{} if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestKeyringCommandRun(t *testing.T) { func TestKeyringCommandRun(t *testing.T) {
@ -76,7 +68,8 @@ func TestKeyringCommandRun(t *testing.T) {
func TestKeyringCommandRun_help(t *testing.T) { func TestKeyringCommandRun_help(t *testing.T) {
t.Parallel() t.Parallel()
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
code := c.Run(nil) code := c.Run(nil)
if code != 1 { if code != 1 {
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
@ -90,7 +83,8 @@ func TestKeyringCommandRun_help(t *testing.T) {
func TestKeyringCommandRun_failedConnection(t *testing.T) { func TestKeyringCommandRun_failedConnection(t *testing.T) {
t.Parallel() t.Parallel()
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{"-list", "-http-addr=127.0.0.1:0"} args := []string{"-list", "-http-addr=127.0.0.1:0"}
code := c.Run(args) code := c.Run(args)
if code != 1 { if code != 1 {
@ -103,7 +97,8 @@ func TestKeyringCommandRun_failedConnection(t *testing.T) {
func TestKeyringCommandRun_invalidRelayFactor(t *testing.T) { func TestKeyringCommandRun_invalidRelayFactor(t *testing.T) {
t.Parallel() t.Parallel()
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{"-list", "-relay-factor=6"} args := []string{"-list", "-relay-factor=6"}
code := c.Run(args) code := c.Run(args)
@ -113,7 +108,8 @@ func TestKeyringCommandRun_invalidRelayFactor(t *testing.T) {
} }
func listKeys(t *testing.T, addr string) string { func listKeys(t *testing.T, addr string) string {
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{"-list", "-http-addr=" + addr} args := []string{"-list", "-http-addr=" + addr}
code := c.Run(args) code := c.Run(args)
@ -125,7 +121,8 @@ func listKeys(t *testing.T, addr string) string {
} }
func installKey(t *testing.T, addr string, key string) { func installKey(t *testing.T, addr string, key string) {
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{"-install=" + key, "-http-addr=" + addr} args := []string{"-install=" + key, "-http-addr=" + addr}
code := c.Run(args) code := c.Run(args)
@ -135,7 +132,8 @@ func installKey(t *testing.T, addr string, key string) {
} }
func useKey(t *testing.T, addr string, key string) { func useKey(t *testing.T, addr string, key string) {
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{"-use=" + key, "-http-addr=" + addr} args := []string{"-use=" + key, "-http-addr=" + addr}
code := c.Run(args) code := c.Run(args)
@ -145,7 +143,8 @@ func useKey(t *testing.T, addr string, key string) {
} }
func removeKey(t *testing.T, addr string, key string) { func removeKey(t *testing.T, addr string, key string) {
ui, c := testKeyringCommand(t) ui := cli.NewMockUi()
c := New(ui)
args := []string{"-remove=" + key, "-http-addr=" + addr} args := []string{"-remove=" + key, "-http-addr=" + addr}
code := c.Run(args) code := c.Run(args)