Update remount command

This commit is contained in:
Seth Vargo 2017-09-05 00:03:50 -04:00
parent a72ab1ecf5
commit 8df3be5656
No known key found for this signature in database
GPG Key ID: C921994F9C27E0FF
2 changed files with 180 additions and 55 deletions

View File

@ -4,49 +4,91 @@ import (
"fmt" "fmt"
"strings" "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 // RemountCommand is a Command that remounts a mounted secret backend
// to a new endpoint. // to a new endpoint.
type RemountCommand struct { 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 { func (c *RemountCommand) Run(args []string) int {
flags := c.Meta.FlagSet("remount", meta.FlagSetDefault) f := c.Flags()
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil { if err := f.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1 return 1
} }
args = flags.Args() args = f.Args()
if len(args) != 2 { switch len(args) {
flags.Usage() case 0, 1:
c.Ui.Error(fmt.Sprintf( c.UI.Error(fmt.Sprintf("Not enough arguments (expected 2, got %d)", len(args)))
"\nremount expects two arguments: the from and to path")) return 1
case 2:
default:
c.UI.Error(fmt.Sprintf("Too many arguments (expected 2, got %d)", len(args)))
return 1 return 1
} }
from := args[0] // Grab the source and destination
to := args[1] source := ensureTrailingSlash(args[0])
destination := ensureTrailingSlash(args[1])
client, err := c.Client() client, err := c.Client()
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf( c.UI.Error(err.Error())
"Error initializing client: %s", err))
return 2 return 2
} }
if err := client.Sys().Remount(from, to); err != nil { if err := client.Sys().Remount(source, destination); err != nil {
c.Ui.Error(fmt.Sprintf( c.UI.Error(fmt.Sprintf("Error remounting %s to %s: %s", source, destination, err))
"Unmount error: %s", err))
return 2 return 2
} }
c.Ui.Output(fmt.Sprintf( c.UI.Output(fmt.Sprintf("Success! Remounted %s to: %s", source, destination))
"Successfully remounted from '%s' to '%s'!", from, to))
return 0 return 0
} }

View File

@ -1,52 +1,135 @@
package command package command
import ( import (
"strings"
"testing" "testing"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/meta"
"github.com/hashicorp/vault/vault"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestRemount(t *testing.T) { func testRemountCommand(tb testing.TB) (*cli.MockUi, *RemountCommand) {
core, _, token := vault.TestCoreUnsealed(t) tb.Helper()
ln, addr := http.TestServer(t, core)
defer ln.Close()
ui := new(cli.MockUi) ui := cli.NewMockUi()
c := &RemountCommand{ return ui, &RemountCommand{
Meta: meta.Meta{ BaseCommand: &BaseCommand{
ClientToken: token, UI: ui,
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{ t.Run("validations", func(t *testing.T) {
"-address", addr, t.Parallel()
"secret/", "kv",
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
client, err := c.Client() for _, tc := range cases {
if err != nil { tc := tc
t.Fatalf("err: %s", err)
}
mounts, err := client.Sys().ListMounts() t.Run(tc.name, func(t *testing.T) {
if err != nil { t.Parallel()
t.Fatalf("err: %s", err)
}
_, ok := mounts["secret/"] ui, cmd := testRemountCommand(t)
if ok {
t.Fatal("should not have mount")
}
_, ok = mounts["kv/"] code := cmd.Run(tc.args)
if !ok { if code != tc.code {
t.Fatal("should have kv") 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)
})
} }