2015-04-07 17:46:47 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-08-31 18:27:49 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/vault"
|
2015-04-07 17:46:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RemountCommand is a Command that remounts a mounted secret backend
|
|
|
|
// to a new endpoint.
|
|
|
|
type RemountCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemountCommand) Run(args []string) int {
|
2015-08-31 18:27:49 +00:00
|
|
|
var defaultLeaseTTL, maxLeaseTTL string
|
2015-04-07 17:46:47 +00:00
|
|
|
flags := c.Meta.FlagSet("remount", FlagSetDefault)
|
2015-09-01 22:29:30 +00:00
|
|
|
flags.StringVar(&defaultLeaseTTL, "default-lease-ttl", "", "")
|
|
|
|
flags.StringVar(&maxLeaseTTL, "max-lease-ttl", "", "")
|
2015-04-07 17:46:47 +00:00
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
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"))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
from := args[0]
|
|
|
|
to := args[1]
|
|
|
|
|
2015-09-01 22:29:30 +00:00
|
|
|
mountConfig := vault.MountConfig{}
|
2015-08-31 18:27:49 +00:00
|
|
|
if defaultLeaseTTL != "" {
|
2015-09-01 22:29:30 +00:00
|
|
|
defTTL, err := time.ParseDuration(defaultLeaseTTL)
|
2015-08-31 18:27:49 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error parsing default lease TTL duration: %s", err))
|
2015-09-01 22:29:30 +00:00
|
|
|
return 2
|
2015-08-31 18:27:49 +00:00
|
|
|
}
|
2015-09-01 22:29:30 +00:00
|
|
|
mountConfig.DefaultLeaseTTL = &defTTL
|
2015-08-31 18:27:49 +00:00
|
|
|
}
|
|
|
|
if maxLeaseTTL != "" {
|
2015-09-01 22:29:30 +00:00
|
|
|
maxTTL, err := time.ParseDuration(maxLeaseTTL)
|
2015-08-31 18:27:49 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error parsing max lease TTL duration: %s", err))
|
2015-09-01 22:29:30 +00:00
|
|
|
return 2
|
2015-08-31 18:27:49 +00:00
|
|
|
}
|
2015-09-01 22:29:30 +00:00
|
|
|
mountConfig.MaxLeaseTTL = &maxTTL
|
2015-08-31 18:27:49 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 17:46:47 +00:00
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client: %s", err))
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:27:49 +00:00
|
|
|
if err := client.Sys().Remount(from, to, mountConfig); err != nil {
|
2015-04-07 17:46:47 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Unmount error: %s", err))
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
"Successfully remounted from '%s' to '%s'!", from, to))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemountCommand) Synopsis() string {
|
|
|
|
return "Remount a secret backend to a new path"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *RemountCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault remount [options] from to
|
|
|
|
|
|
|
|
Remount a mounted secret backend to a new path.
|
|
|
|
|
|
|
|
This command remounts a secret backend that is already mounted to
|
|
|
|
a new path. All the secrets from the old path will be revoked, but
|
|
|
|
the Vault data associated with the backend will be preserved (such
|
|
|
|
as configuration data).
|
|
|
|
|
2015-09-01 22:29:30 +00:00
|
|
|
If the 'from' and 'to' values of the same, performs an in-place
|
|
|
|
remount. This allows you to change mount options.
|
|
|
|
|
2015-04-07 17:46:47 +00:00
|
|
|
Example: vault remount secret/ generic/
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2015-09-01 22:29:30 +00:00
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Mount Options:
|
|
|
|
|
|
|
|
-default-lease-ttl=<duration> Default lease time-to-live for this backend.
|
|
|
|
If not specified, uses the global default, or
|
|
|
|
the previously set value. Set to '0' to
|
|
|
|
explicitly set it to use the global default.
|
|
|
|
|
|
|
|
-max-lease-ttl=<duration> Max lease time-to-live for this backend.
|
|
|
|
If not specified, uses the global default, or
|
|
|
|
the previously set value. Set to '0' to
|
|
|
|
explicitly set it to use the global default.
|
|
|
|
|
|
|
|
`
|
2015-04-07 17:46:47 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|