Update remount command
This commit is contained in:
parent
a72ab1ecf5
commit
8df3be5656
|
@ -4,49 +4,91 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/meta"
|
||||
"github.com/hashicorp/vault-enterprise/meta"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
|
||||
// Ensure we are implementing the right interfaces.
|
||||
var _ cli.Command = (*RemountCommand)(nil)
|
||||
var _ cli.CommandAutocomplete = (*RemountCommand)(nil)
|
||||
|
||||
// RemountCommand is a Command that remounts a mounted secret backend
|
||||
// to a new endpoint.
|
||||
type RemountCommand struct {
|
||||
meta.Meta
|
||||
*BaseCommand
|
||||
}
|
||||
|
||||
func (c *RemountCommand) Synopsis() string {
|
||||
return "Remounts a secret backend to a new path"
|
||||
}
|
||||
|
||||
func (c *RemountCommand) Help() string {
|
||||
helpText := `
|
||||
Usage: vault remount [options] SOURCE DESTINATION
|
||||
|
||||
Remounts an existing secret backend to a new path. Any leases from the old
|
||||
backend are revoked, but the data associated with the backend (such as
|
||||
configuration), is preserved.
|
||||
|
||||
Move the existing mount at secret/ to generic/:
|
||||
|
||||
$ vault remount secret/ generic/
|
||||
|
||||
For a full list of examples, please see the documentation.
|
||||
|
||||
` + c.Flags().Help()
|
||||
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (c *RemountCommand) Flags() *FlagSets {
|
||||
return c.flagSet(FlagSetHTTP)
|
||||
}
|
||||
|
||||
func (c *RemountCommand) AutocompleteArgs() complete.Predictor {
|
||||
return c.PredictVaultMounts()
|
||||
}
|
||||
|
||||
func (c *RemountCommand) AutocompleteFlags() complete.Flags {
|
||||
return c.Flags().Completions()
|
||||
}
|
||||
|
||||
func (c *RemountCommand) Run(args []string) int {
|
||||
flags := c.Meta.FlagSet("remount", meta.FlagSetDefault)
|
||||
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
||||
if err := flags.Parse(args); err != nil {
|
||||
f := c.Flags()
|
||||
|
||||
if err := f.Parse(args); err != nil {
|
||||
c.UI.Error(err.Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
args = flags.Args()
|
||||
if len(args) != 2 {
|
||||
flags.Usage()
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"\nremount expects two arguments: the from and to path"))
|
||||
args = f.Args()
|
||||
switch len(args) {
|
||||
case 0, 1:
|
||||
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 2, got %d)", len(args)))
|
||||
return 1
|
||||
case 2:
|
||||
default:
|
||||
c.UI.Error(fmt.Sprintf("Too many arguments (expected 2, got %d)", len(args)))
|
||||
return 1
|
||||
}
|
||||
|
||||
from := args[0]
|
||||
to := args[1]
|
||||
// Grab the source and destination
|
||||
source := ensureTrailingSlash(args[0])
|
||||
destination := ensureTrailingSlash(args[1])
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Error initializing client: %s", err))
|
||||
c.UI.Error(err.Error())
|
||||
return 2
|
||||
}
|
||||
|
||||
if err := client.Sys().Remount(from, to); err != nil {
|
||||
c.Ui.Error(fmt.Sprintf(
|
||||
"Unmount error: %s", err))
|
||||
if err := client.Sys().Remount(source, destination); err != nil {
|
||||
c.UI.Error(fmt.Sprintf("Error remounting %s to %s: %s", source, destination, err))
|
||||
return 2
|
||||
}
|
||||
|
||||
c.Ui.Output(fmt.Sprintf(
|
||||
"Successfully remounted from '%s' to '%s'!", from, to))
|
||||
|
||||
c.UI.Output(fmt.Sprintf("Success! Remounted %s to: %s", source, destination))
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
@ -1,52 +1,135 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/vault/http"
|
||||
"github.com/hashicorp/vault/meta"
|
||||
"github.com/hashicorp/vault/vault"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
func TestRemount(t *testing.T) {
|
||||
core, _, token := vault.TestCoreUnsealed(t)
|
||||
ln, addr := http.TestServer(t, core)
|
||||
defer ln.Close()
|
||||
func testRemountCommand(tb testing.TB) (*cli.MockUi, *RemountCommand) {
|
||||
tb.Helper()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &RemountCommand{
|
||||
Meta: meta.Meta{
|
||||
ClientToken: token,
|
||||
Ui: ui,
|
||||
ui := cli.NewMockUi()
|
||||
return ui, &RemountCommand{
|
||||
BaseCommand: &BaseCommand{
|
||||
UI: ui,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemountCommand_Run(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
args []string
|
||||
out string
|
||||
code int
|
||||
}{
|
||||
{
|
||||
"not_enough_args",
|
||||
nil,
|
||||
"Not enough arguments",
|
||||
1,
|
||||
},
|
||||
{
|
||||
"too_many_args",
|
||||
[]string{"foo", "bar", "baz"},
|
||||
"Too many arguments",
|
||||
1,
|
||||
},
|
||||
{
|
||||
"non_existent",
|
||||
[]string{"not_real", "over_here"},
|
||||
"Error remounting not_real/ to over_here/",
|
||||
2,
|
||||
},
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-address", addr,
|
||||
"secret/", "kv",
|
||||
}
|
||||
if code := c.Run(args); code != 0 {
|
||||
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
|
||||
}
|
||||
t.Run("validations", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client, err := c.Client()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
|
||||
mounts, err := client.Sys().ListMounts()
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, ok := mounts["secret/"]
|
||||
if ok {
|
||||
t.Fatal("should not have mount")
|
||||
}
|
||||
ui, cmd := testRemountCommand(t)
|
||||
|
||||
_, ok = mounts["kv/"]
|
||||
if !ok {
|
||||
t.Fatal("should have kv")
|
||||
}
|
||||
code := cmd.Run(tc.args)
|
||||
if code != tc.code {
|
||||
t.Errorf("expected %d to be %d", code, tc.code)
|
||||
}
|
||||
|
||||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
||||
if !strings.Contains(combined, tc.out) {
|
||||
t.Errorf("expected %q to contain %q", combined, tc.out)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("integration", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client, closer := testVaultServer(t)
|
||||
defer closer()
|
||||
|
||||
ui, cmd := testRemountCommand(t)
|
||||
cmd.client = client
|
||||
|
||||
code := cmd.Run([]string{
|
||||
"secret/", "generic/",
|
||||
})
|
||||
if exp := 0; code != exp {
|
||||
t.Errorf("expected %d to be %d", code, exp)
|
||||
}
|
||||
|
||||
expected := "Success! Remounted secret/ to: generic/"
|
||||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
||||
if !strings.Contains(combined, expected) {
|
||||
t.Errorf("expected %q to contain %q", combined, expected)
|
||||
}
|
||||
|
||||
mounts, err := client.Sys().ListMounts()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, ok := mounts["generic/"]; !ok {
|
||||
t.Errorf("expected mount at generic/: %#v", mounts)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("communication_failure", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client, closer := testVaultServerBad(t)
|
||||
defer closer()
|
||||
|
||||
ui, cmd := testRemountCommand(t)
|
||||
cmd.client = client
|
||||
|
||||
code := cmd.Run([]string{
|
||||
"secret/", "generic/",
|
||||
})
|
||||
if exp := 2; code != exp {
|
||||
t.Errorf("expected %d to be %d", code, exp)
|
||||
}
|
||||
|
||||
expected := "Error remounting secret/ to generic/: "
|
||||
combined := ui.OutputWriter.String() + ui.ErrorWriter.String()
|
||||
if !strings.Contains(combined, expected) {
|
||||
t.Errorf("expected %q to contain %q", combined, expected)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("no_tabs", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, cmd := testRemountCommand(t)
|
||||
assertNoTabs(t, cmd)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue