2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2017-10-11 13:42:45 +00:00
|
|
|
package keyring
|
2014-09-10 15:49:16 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
2020-09-24 18:04:20 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-11-15 15:51:14 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2014-09-10 15:49:16 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 13:42:45 +00:00
|
|
|
func TestKeyringCommand_noTabs(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-11 13:42:45 +00:00
|
|
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
2017-10-17 13:44:20 +00:00
|
|
|
t.Fatal("help has tabs")
|
2017-10-11 13:42:45 +00:00
|
|
|
}
|
2014-09-10 15:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestKeyringCommand(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:48 +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
|
2020-03-31 19:59:56 +00:00
|
|
|
a1 := agent.NewTestAgent(t, `
|
2017-09-25 18:40:42 +00:00
|
|
|
encrypt = "`+key1+`"
|
|
|
|
`)
|
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)
|
2020-09-24 18:04:20 +00:00
|
|
|
|
|
|
|
// New key should be present
|
|
|
|
out = listPrimaryKeys(t, a1.HTTPAddr())
|
|
|
|
if strings.Contains(out, key1) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
if !strings.Contains(out, key2) {
|
|
|
|
t.Fatalf("bad: %#v", out)
|
|
|
|
}
|
|
|
|
|
2017-05-22 11:59:36 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestKeyringCommand_help(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestKeyringCommand_failedConnection(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
2017-02-08 23:25:47 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:11:11 +00:00
|
|
|
func TestKeyringCommand_invalidLocalOnly(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{"-install=blah", "-local-only=true"}
|
|
|
|
code := c.Run(args)
|
|
|
|
if code != 1 {
|
|
|
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 12:04:51 +00:00
|
|
|
func TestKeyringCommand_invalidRelayFactor(t *testing.T) {
|
2017-10-17 14:07:48 +00:00
|
|
|
t.Parallel()
|
2017-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
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-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
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()
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:04:20 +00:00
|
|
|
func listPrimaryKeys(t *testing.T, addr string) string {
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
|
|
|
|
|
|
|
args := []string{"-list-primary", "-http-addr=" + addr}
|
|
|
|
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-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
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-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
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-10-11 13:42:45 +00:00
|
|
|
ui := cli.NewMockUi()
|
|
|
|
c := New(ui)
|
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
|
|
|
}
|
|
|
|
}
|
2020-09-24 18:04:20 +00:00
|
|
|
|
|
|
|
func TestKeyringCommand_poolName(t *testing.T) {
|
2021-11-15 15:51:14 +00:00
|
|
|
require.Equal(t, "dc1 (LAN):\n", poolName("dc1", false, "", ""))
|
|
|
|
require.Equal(t, "WAN:\n", poolName("dc1", true, "", ""))
|
2020-09-24 18:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyringCommand_formatKeys(t *testing.T) {
|
|
|
|
require.Equal(t, "", formatKeys(map[string]int{}, 0))
|
|
|
|
keys := formatKeys(map[string]int{"key1": 1, "key2": 2}, 2)
|
|
|
|
require.Contains(t, keys, " key1 [1/2]\n")
|
|
|
|
require.Contains(t, keys, " key2 [2/2]\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyringCommand_formatMessages(t *testing.T) {
|
|
|
|
require.Equal(t, "", formatMessages(map[string]string{}))
|
|
|
|
messages := formatMessages(map[string]string{"n1": "hello", "n2": "world"})
|
|
|
|
require.Contains(t, messages, " ===> n1: hello\n")
|
|
|
|
require.Contains(t, messages, " ===> n2: world\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyringCommand_formatResponse(t *testing.T) {
|
|
|
|
response := &consulapi.KeyringResponse{Datacenter: "dc1", NumNodes: 1}
|
|
|
|
keys := map[string]int{"key1": 1}
|
|
|
|
require.Equal(t, "\ndc1 (LAN):\n key1 [1/1]", formatResponse(response, keys))
|
|
|
|
|
|
|
|
response = &consulapi.KeyringResponse{WAN: true, Datacenter: "dc1", NumNodes: 1}
|
|
|
|
keys = map[string]int{"key1": 1}
|
|
|
|
require.Equal(t, "\nWAN:\n key1 [1/1]", formatResponse(response, keys))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyringCommand_numActions(t *testing.T) {
|
|
|
|
require.Equal(t, 0, numberActions(false, false, "", "", ""))
|
|
|
|
require.Equal(t, 1, numberActions(true, false, "", "", ""))
|
|
|
|
require.Equal(t, 1, numberActions(false, true, "", "", ""))
|
|
|
|
require.Equal(t, 1, numberActions(false, false, "1", "", ""))
|
|
|
|
require.Equal(t, 2, numberActions(true, false, "1", "", ""))
|
|
|
|
require.Equal(t, 2, numberActions(false, false, "1", "1", ""))
|
|
|
|
}
|