2014-09-10 15:49:16 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
2017-02-08 23:25:47 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2014-09-10 15:49:16 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
2017-02-08 23:25:47 +00:00
|
|
|
func testKeyringCommand(t *testing.T) (*cli.MockUi, *KeyringCommand) {
|
2017-05-22 23:26:30 +00:00
|
|
|
ui := cli.NewMockUi()
|
2017-02-08 23:25:47 +00:00
|
|
|
return ui, &KeyringCommand{
|
|
|
|
Command: base.Command{
|
2017-04-21 00:02:42 +00:00
|
|
|
UI: ui,
|
2017-02-08 23:25:47 +00:00
|
|
|
Flags: base.FlagSetClientHTTP,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-13 03:08:54 +00:00
|
|
|
func TestKeyringCommand_implements(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2014-09-13 03:08:54 +00:00
|
|
|
var _ cli.Command = &KeyringCommand{}
|
2014-09-10 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 03:08:54 +00:00
|
|
|
func TestKeyringCommandRun(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2014-09-10 17:11:11 +00:00
|
|
|
key1 := "HS5lJ+XuTlYKWaeGYyG+/A=="
|
|
|
|
key2 := "kZyFABeAmc64UMTrm9XuKA=="
|
2014-09-10 15:49:16 +00:00
|
|
|
|
2014-09-10 17:11:11 +00:00
|
|
|
// Begin with a single key
|
2017-05-22 11:59:36 +00:00
|
|
|
cfg := agent.TestConfig()
|
|
|
|
cfg.EncryptKey = key1
|
|
|
|
a1 := agent.NewTestAgent(t.Name(), cfg)
|
2014-09-10 15:49:16 +00:00
|
|
|
defer a1.Shutdown()
|
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
// The LAN and WAN keyrings were initialized with key1
|
2017-05-22 11:59:36 +00:00
|
|
|
out := listKeys(t, a1.HTTPAddr())
|
2014-09-29 22:49:47 +00:00
|
|
|
if !strings.Contains(out, "dc1 (LAN):\n "+key1) {
|
2014-09-29 19:35:15 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
2014-09-29 22:49:47 +00:00
|
|
|
if !strings.Contains(out, "WAN:\n "+key1) {
|
2014-09-10 17:11:11 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, key2) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install the second key onto the keyring
|
2017-05-22 11:59:36 +00:00
|
|
|
installKey(t, a1.HTTPAddr(), key2)
|
2014-09-10 17:11:11 +00:00
|
|
|
|
|
|
|
// Both keys should be present
|
2017-05-22 11:59:36 +00:00
|
|
|
out = listKeys(t, a1.HTTPAddr())
|
2014-09-10 17:11:11 +00:00
|
|
|
for _, key := range []string{key1, key2} {
|
|
|
|
if !strings.Contains(out, key) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
// Rotate to key2, remove key1
|
2017-05-22 11:59:36 +00:00
|
|
|
useKey(t, a1.HTTPAddr(), key2)
|
|
|
|
removeKey(t, a1.HTTPAddr(), key1)
|
2014-09-10 17:11:11 +00:00
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
// Only key2 is present now
|
2017-05-22 11:59:36 +00:00
|
|
|
out = listKeys(t, a1.HTTPAddr())
|
2014-09-29 22:49:47 +00:00
|
|
|
if !strings.Contains(out, "dc1 (LAN):\n "+key2) {
|
2014-09-10 17:11:11 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
2014-09-29 22:49:47 +00:00
|
|
|
if !strings.Contains(out, "WAN:\n "+key2) {
|
2014-09-21 18:52:28 +00:00
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
if strings.Contains(out, key1) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
2014-09-10 17:11:11 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 03:08:54 +00:00
|
|
|
func TestKeyringCommandRun_help(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
2014-09-10 17:11:11 +00:00
|
|
|
code := c.Run(nil)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2014-09-10 17:20:40 +00:00
|
|
|
|
|
|
|
// Test that we didn't actually try to dial the RPC server.
|
2014-09-10 17:11:11 +00:00
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "Usage:") {
|
|
|
|
t.Fatalf("bad: %#v", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-13 03:08:54 +00:00
|
|
|
func TestKeyringCommandRun_failedConnection(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
|
|
|
args := []string{"-list", "-http-addr=127.0.0.1:0"}
|
2014-09-10 17:20:40 +00:00
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d, %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
if !strings.Contains(ui.ErrorWriter.String(), "dial") {
|
|
|
|
t.Fatalf("bad: %#v", ui.OutputWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:42:41 +00:00
|
|
|
func TestKeyringCommandRun_invalidRelayFactor(t *testing.T) {
|
2017-05-22 18:18:53 +00:00
|
|
|
t.Parallel()
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
2017-02-02 02:42:41 +00:00
|
|
|
|
|
|
|
args := []string{"-list", "-relay-factor=6"}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
func listKeys(t *testing.T, addr string) string {
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
2014-09-10 17:11:11 +00:00
|
|
|
|
2017-02-08 23:25:47 +00:00
|
|
|
args := []string{"-list", "-http-addr=" + addr}
|
2014-09-10 17:11:11 +00:00
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return ui.OutputWriter.String()
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
func installKey(t *testing.T, addr string, key string) {
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
2014-09-10 17:11:11 +00:00
|
|
|
|
2017-02-08 23:25:47 +00:00
|
|
|
args := []string{"-install=" + key, "-http-addr=" + addr}
|
2014-09-10 17:11:11 +00:00
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
func useKey(t *testing.T, addr string, key string) {
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
2014-09-10 17:11:11 +00:00
|
|
|
|
2017-02-08 23:25:47 +00:00
|
|
|
args := []string{"-use=" + key, "-http-addr=" + addr}
|
2014-09-10 15:49:16 +00:00
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
2014-09-10 17:11:11 +00:00
|
|
|
}
|
2014-09-10 15:49:16 +00:00
|
|
|
|
2014-09-29 19:35:15 +00:00
|
|
|
func removeKey(t *testing.T, addr string, key string) {
|
2017-02-08 23:25:47 +00:00
|
|
|
ui, c := testKeyringCommand(t)
|
2014-09-10 17:11:11 +00:00
|
|
|
|
2017-02-08 23:25:47 +00:00
|
|
|
args := []string{"-remove=" + key, "-http-addr=" + addr}
|
2014-09-10 17:11:11 +00:00
|
|
|
code := c.Run(args)
|
|
|
|
if code != 0 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
2014-09-10 15:49:16 +00:00
|
|
|
}
|
|
|
|
}
|