command/rotate: Adding new rotate command

This commit is contained in:
Armon Dadgar 2015-05-28 10:16:33 -07:00
parent 388022bac1
commit 42b91fe411
3 changed files with 115 additions and 0 deletions

View File

@ -208,6 +208,12 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
}, nil
},
"rotate": func() (cli.Command, error) {
return &command.RotateCommand{
Meta: meta,
}, nil
},
"unmount": func() (cli.Command, error) {
return &command.UnmountCommand{
Meta: meta,

79
command/rotate.go Normal file
View File

@ -0,0 +1,79 @@
package command
import (
"fmt"
"strings"
)
// RotateCommand is a Command that rotates the encryption key being used
type RotateCommand struct {
Meta
}
func (c *RotateCommand) Run(args []string) int {
flags := c.Meta.FlagSet("rotate", FlagSetDefault)
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
// Rotate the key
err = client.Sys().Rotate()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error with key rotation: %s", err))
return 2
}
// Print the key status
status, err := client.Sys().KeyStatus()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error reading audits: %s", err))
return 2
}
c.Ui.Output(fmt.Sprintf("Key Term: %d", status.Term))
c.Ui.Output(fmt.Sprintf("Installation Time: %v", status.InstallTime))
return 0
}
func (c *RotateCommand) Synopsis() string {
return "Rotates the backend encryption key used to persist data"
}
func (c *RotateCommand) Help() string {
helpText := `
Usage: vault rotate [options]
Rotates the backend encryption key which is used to secure data
written to the storage backend. This is done by installing a new key
which encrypts new data, while old keys are still used to decrypt
secrets written previously. This is an online operation and is not
disruptive.
General Options:
-address=addr The address of the Vault server.
-ca-cert=path Path to a PEM encoded CA cert file to use to
verify the Vault server SSL certificate.
-ca-path=path Path to a directory of PEM encoded CA cert files
to verify the Vault server SSL certificate. If both
-ca-cert and -ca-path are specified, -ca-path is used.
-tls-skip-verify Do not verify TLS certificate. This is highly
not recommended. This is especially not recommended
for unsealing a vault.
`
return strings.TrimSpace(helpText)
}

30
command/rotate_test.go Normal file
View File

@ -0,0 +1,30 @@
package command
import (
"testing"
"github.com/hashicorp/vault/http"
"github.com/hashicorp/vault/vault"
"github.com/mitchellh/cli"
)
func TestRotate(t *testing.T) {
core, _, token := vault.TestCoreUnsealed(t)
ln, addr := http.TestServer(t, core)
defer ln.Close()
ui := new(cli.MockUi)
c := &RotateCommand{
Meta: Meta{
ClientToken: token,
Ui: ui,
},
}
args := []string{
"-address", addr,
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
}
}