2015-05-28 22:08:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2017-09-05 04:03:44 +00:00
|
|
|
"bytes"
|
2015-05-28 22:08:09 +00:00
|
|
|
"fmt"
|
2017-09-05 04:03:44 +00:00
|
|
|
"io"
|
2015-05-28 22:08:09 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2015-12-16 21:56:15 +00:00
|
|
|
"github.com/fatih/structs"
|
2015-05-28 22:08:09 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2015-08-25 21:24:19 +00:00
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
2019-04-13 07:44:06 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/password"
|
2017-09-05 04:03:44 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-08-24 22:23:40 +00:00
|
|
|
"github.com/posener/complete"
|
2015-05-28 22:08:09 +00:00
|
|
|
)
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
var _ cli.Command = (*OperatorRekeyCommand)(nil)
|
|
|
|
var _ cli.CommandAutocomplete = (*OperatorRekeyCommand)(nil)
|
2017-09-05 04:03:44 +00:00
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
type OperatorRekeyCommand struct {
|
2017-09-05 04:03:44 +00:00
|
|
|
*BaseCommand
|
2015-05-28 22:08:09 +00:00
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
flagCancel bool
|
|
|
|
flagInit bool
|
|
|
|
flagKeyShares int
|
|
|
|
flagKeyThreshold int
|
|
|
|
flagNonce string
|
|
|
|
flagPGPKeys []string
|
|
|
|
flagStatus bool
|
|
|
|
flagTarget string
|
2018-05-28 04:39:53 +00:00
|
|
|
flagVerify bool
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
// Backup options
|
|
|
|
flagBackup bool
|
|
|
|
flagBackupDelete bool
|
|
|
|
flagBackupRetrieve bool
|
|
|
|
|
|
|
|
testStdin io.Reader // for tests
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) Synopsis() string {
|
2017-09-05 04:03:44 +00:00
|
|
|
return "Generates new unseal keys"
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) Help() string {
|
2017-09-05 04:03:44 +00:00
|
|
|
helpText := `
|
2018-06-07 04:11:21 +00:00
|
|
|
Usage: vault operator rekey [options] [KEY]
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
Generates a new set of unseal keys. This can optionally change the total
|
|
|
|
number of key shares or the required threshold of those key shares to
|
|
|
|
reconstruct the master key. This operation is zero downtime, but it requires
|
|
|
|
the Vault is unsealed and a quorum of existing unseal keys are provided.
|
|
|
|
|
|
|
|
An unseal key may be provided directly on the command line as an argument to
|
|
|
|
the command. If key is specified as "-", the command will read from stdin. If
|
|
|
|
a TTY is available, the command will prompt for text.
|
|
|
|
|
|
|
|
Initialize a rekey:
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
$ vault operator rekey \
|
2017-09-05 04:03:44 +00:00
|
|
|
-init \
|
|
|
|
-key-shares=15 \
|
|
|
|
-key-threshold=9
|
|
|
|
|
|
|
|
Rekey and encrypt the resulting unseal keys with PGP:
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
$ vault operator rekey \
|
2017-09-05 04:03:44 +00:00
|
|
|
-init \
|
|
|
|
-key-shares=3 \
|
|
|
|
-key-threshold=2 \
|
|
|
|
-pgp-keys="keybase:hashicorp,keybase:jefferai,keybase:sethvargo"
|
|
|
|
|
|
|
|
Store encrypted PGP keys in Vault's core:
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
$ vault operator rekey \
|
2017-09-05 04:03:44 +00:00
|
|
|
-init \
|
|
|
|
-pgp-keys="..." \
|
|
|
|
-backup
|
|
|
|
|
|
|
|
Retrieve backed-up unseal keys:
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
$ vault operator rekey -backup-retrieve
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
Delete backed-up unseal keys:
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
$ vault operator rekey -backup-delete
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
` + c.Flags().Help()
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) Flags() *FlagSets {
|
2018-02-12 23:12:16 +00:00
|
|
|
set := c.flagSet(FlagSetHTTP | FlagSetOutputFormat)
|
2017-09-05 04:03:44 +00:00
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
f := set.NewFlagSet("Common Options")
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "init",
|
|
|
|
Target: &c.flagInit,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Initialize the rekeying operation. This can only be done if no " +
|
|
|
|
"rekeying operation is in progress. Customize the new number of key " +
|
|
|
|
"shares and key threshold using the -key-shares and -key-threshold " +
|
|
|
|
"flags.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "cancel",
|
|
|
|
Target: &c.flagCancel,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Reset the rekeying progress. This will discard any submitted " +
|
|
|
|
"unseal keys or configuration.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "status",
|
|
|
|
Target: &c.flagStatus,
|
|
|
|
Default: false,
|
2017-09-08 02:03:12 +00:00
|
|
|
Usage: "Print the status of the current attempt without providing an " +
|
2017-09-05 04:03:44 +00:00
|
|
|
"unseal key.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.IntVar(&IntVar{
|
|
|
|
Name: "key-shares",
|
|
|
|
Aliases: []string{"n"},
|
|
|
|
Target: &c.flagKeyShares,
|
|
|
|
Default: 5,
|
|
|
|
Completion: complete.PredictAnything,
|
|
|
|
Usage: "Number of key shares to split the generated master key into. " +
|
|
|
|
"This is the number of \"unseal keys\" to generate.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.IntVar(&IntVar{
|
|
|
|
Name: "key-threshold",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Target: &c.flagKeyThreshold,
|
|
|
|
Default: 3,
|
|
|
|
Completion: complete.PredictAnything,
|
|
|
|
Usage: "Number of key shares required to reconstruct the master key. " +
|
|
|
|
"This must be less than or equal to -key-shares.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: "nonce",
|
|
|
|
Target: &c.flagNonce,
|
|
|
|
Default: "",
|
|
|
|
EnvVar: "",
|
|
|
|
Completion: complete.PredictAnything,
|
|
|
|
Usage: "Nonce value provided at initialization. The same nonce value " +
|
|
|
|
"must be provided with each unseal key.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.StringVar(&StringVar{
|
|
|
|
Name: "target",
|
|
|
|
Target: &c.flagTarget,
|
|
|
|
Default: "barrier",
|
|
|
|
EnvVar: "",
|
|
|
|
Completion: complete.PredictSet("barrier", "recovery"),
|
|
|
|
Usage: "Target for rekeying. \"recovery\" only applies when HSM support " +
|
|
|
|
"is enabled.",
|
|
|
|
})
|
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "verify",
|
|
|
|
Target: &c.flagVerify,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Indicates that the action (-status, -cancel, or providing a key " +
|
|
|
|
"share) will be affecting verification for the current rekey " +
|
|
|
|
"attempt.",
|
|
|
|
})
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
f.VarFlag(&VarFlag{
|
|
|
|
Name: "pgp-keys",
|
|
|
|
Value: (*pgpkeys.PubKeyFilesFlag)(&c.flagPGPKeys),
|
|
|
|
Completion: complete.PredictAnything,
|
|
|
|
Usage: "Comma-separated list of paths to files on disk containing " +
|
|
|
|
"public GPG keys OR a comma-separated list of Keybase usernames using " +
|
|
|
|
"the format \"keybase:<username>\". When supplied, the generated " +
|
|
|
|
"unseal keys will be encrypted and base64-encoded in the order " +
|
2017-09-08 02:03:12 +00:00
|
|
|
"specified in this list.",
|
2017-09-05 04:03:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
f = set.NewFlagSet("Backup Options")
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "backup",
|
|
|
|
Target: &c.flagBackup,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Store a backup of the current PGP encrypted unseal keys in " +
|
|
|
|
"Vault's core. The encrypted values can be recovered in the event of " +
|
|
|
|
"failure or discarded after success. See the -backup-delete and " +
|
|
|
|
"-backup-retrieve options for more information. This option only " +
|
|
|
|
"applies when the existing unseal keys were PGP encrypted.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "backup-delete",
|
|
|
|
Target: &c.flagBackupDelete,
|
|
|
|
Default: false,
|
|
|
|
Usage: "Delete any stored backup unseal keys.",
|
|
|
|
})
|
|
|
|
|
|
|
|
f.BoolVar(&BoolVar{
|
|
|
|
Name: "backup-retrieve",
|
|
|
|
Target: &c.flagBackupRetrieve,
|
|
|
|
Default: false,
|
2017-09-08 02:03:12 +00:00
|
|
|
Usage: "Retrieve the backed-up unseal keys. This option is only available " +
|
2017-09-05 04:03:44 +00:00
|
|
|
"if the PGP keys were provided and the backup has not been deleted.",
|
|
|
|
})
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) AutocompleteArgs() complete.Predictor {
|
2017-09-05 04:03:44 +00:00
|
|
|
return complete.PredictAnything
|
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) AutocompleteFlags() complete.Flags {
|
2017-09-05 04:03:44 +00:00
|
|
|
return c.Flags().Completions()
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) Run(args []string) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
f := c.Flags()
|
|
|
|
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
|
|
c.UI.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
args = f.Args()
|
|
|
|
if len(args) > 1 {
|
|
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 0-1, got %d)", len(args)))
|
2015-05-28 22:08:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Create the client
|
2015-05-28 22:08:09 +00:00
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
2017-09-05 04:03:44 +00:00
|
|
|
c.UI.Error(err.Error())
|
2015-05-28 22:08:09 +00:00
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2015-12-16 21:56:15 +00:00
|
|
|
switch {
|
2017-09-05 04:03:44 +00:00
|
|
|
case c.flagBackupDelete:
|
|
|
|
return c.backupDelete(client)
|
|
|
|
case c.flagBackupRetrieve:
|
|
|
|
return c.backupRetrieve(client)
|
|
|
|
case c.flagCancel:
|
|
|
|
return c.cancel(client)
|
|
|
|
case c.flagInit:
|
|
|
|
return c.init(client)
|
|
|
|
case c.flagStatus:
|
|
|
|
return c.status(client)
|
|
|
|
default:
|
|
|
|
// If there are no other flags, prompt for an unseal key.
|
|
|
|
key := ""
|
|
|
|
if len(args) > 0 {
|
|
|
|
key = strings.TrimSpace(args[0])
|
|
|
|
}
|
|
|
|
return c.provide(client, key)
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// init starts the rekey process.
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) init(client *api.Client) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
// Handle the different API requests
|
|
|
|
var fn func(*api.RekeyInitRequest) (*api.RekeyStatusResponse, error)
|
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
|
|
|
fn = client.Sys().RekeyInit
|
|
|
|
case "recovery", "hsm":
|
|
|
|
fn = client.Sys().RekeyRecoveryKeyInit
|
|
|
|
default:
|
|
|
|
c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
|
2015-05-28 22:08:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Make the request
|
|
|
|
status, err := fn(&api.RekeyInitRequest{
|
2018-05-28 04:39:53 +00:00
|
|
|
SecretShares: c.flagKeyShares,
|
|
|
|
SecretThreshold: c.flagKeyThreshold,
|
|
|
|
PGPKeys: c.flagPGPKeys,
|
|
|
|
Backup: c.flagBackup,
|
|
|
|
RequireVerification: c.flagVerify,
|
2017-09-05 04:03:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error initializing rekey: %s", err))
|
|
|
|
return 2
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Print warnings about recovery, etc.
|
|
|
|
if len(c.flagPGPKeys) == 0 {
|
2018-05-23 21:13:39 +00:00
|
|
|
if Format(c.UI) == "table" {
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"WARNING! If you lose the keys after they are returned, there is no " +
|
|
|
|
"recovery. Consider canceling this operation and re-initializing " +
|
|
|
|
"with the -pgp-keys flag to protect the returned unseal keys along " +
|
|
|
|
"with -backup to allow recovery of the encrypted keys in case of " +
|
|
|
|
"emergency. You can delete the stored keys later using the -delete " +
|
|
|
|
"flag."))
|
|
|
|
c.UI.Output("")
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
}
|
|
|
|
if len(c.flagPGPKeys) > 0 && !c.flagBackup {
|
2018-05-23 21:13:39 +00:00
|
|
|
if Format(c.UI) == "table" {
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"WARNING! You are using PGP keys for encrypted the resulting unseal " +
|
|
|
|
"keys, but you did not enable the option to backup the keys to " +
|
|
|
|
"Vault's core. If you lose the encrypted keys after they are " +
|
|
|
|
"returned, you will not be able to recover them. Consider canceling " +
|
|
|
|
"this operation and re-running with -backup to allow recovery of the " +
|
|
|
|
"encrypted unseal keys in case of emergency. You can delete the " +
|
|
|
|
"stored keys later using the -delete flag."))
|
|
|
|
c.UI.Output("")
|
|
|
|
}
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
// Provide the current status
|
|
|
|
return c.printStatus(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancel is used to abort the rekey process.
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) cancel(client *api.Client) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
// Handle the different API requests
|
|
|
|
var fn func() error
|
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
|
|
|
fn = client.Sys().RekeyCancel
|
2018-05-28 04:39:53 +00:00
|
|
|
if c.flagVerify {
|
|
|
|
fn = client.Sys().RekeyVerificationCancel
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
case "recovery", "hsm":
|
|
|
|
fn = client.Sys().RekeyRecoveryKeyCancel
|
2018-05-28 04:39:53 +00:00
|
|
|
if c.flagVerify {
|
|
|
|
fn = client.Sys().RekeyRecoveryKeyVerificationCancel
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
default:
|
|
|
|
c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
|
|
|
|
return 1
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Make the request
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error canceling rekey: %s", err))
|
|
|
|
return 2
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
c.UI.Output("Success! Canceled rekeying (if it was started)")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// provide prompts the user for the seal key and posts it to the update root
|
|
|
|
// endpoint. If this is the last unseal, this function outputs it.
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) provide(client *api.Client, key string) int {
|
2018-05-28 04:39:53 +00:00
|
|
|
var statusFn func() (interface{}, error)
|
|
|
|
var updateFn func(string, string) (interface{}, error)
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
2018-05-28 04:39:53 +00:00
|
|
|
statusFn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyStatus()
|
|
|
|
}
|
|
|
|
updateFn = func(s1 string, s2 string) (interface{}, error) {
|
|
|
|
return client.Sys().RekeyUpdate(s1, s2)
|
|
|
|
}
|
|
|
|
if c.flagVerify {
|
|
|
|
statusFn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyVerificationStatus()
|
|
|
|
}
|
|
|
|
updateFn = func(s1 string, s2 string) (interface{}, error) {
|
|
|
|
return client.Sys().RekeyVerificationUpdate(s1, s2)
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
case "recovery", "hsm":
|
2018-05-28 04:39:53 +00:00
|
|
|
statusFn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyRecoveryKeyStatus()
|
|
|
|
}
|
|
|
|
updateFn = func(s1 string, s2 string) (interface{}, error) {
|
|
|
|
return client.Sys().RekeyRecoveryKeyUpdate(s1, s2)
|
|
|
|
}
|
|
|
|
if c.flagVerify {
|
|
|
|
statusFn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyRecoveryKeyVerificationStatus()
|
|
|
|
}
|
|
|
|
updateFn = func(s1 string, s2 string) (interface{}, error) {
|
|
|
|
return client.Sys().RekeyRecoveryKeyVerificationUpdate(s1, s2)
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
default:
|
|
|
|
c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
|
2015-05-28 22:08:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
status, err := statusFn()
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error getting rekey status: %s", err))
|
|
|
|
return 2
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
var started bool
|
|
|
|
var nonce string
|
|
|
|
|
|
|
|
switch status.(type) {
|
|
|
|
case *api.RekeyStatusResponse:
|
|
|
|
stat := status.(*api.RekeyStatusResponse)
|
|
|
|
started = stat.Started
|
|
|
|
nonce = stat.Nonce
|
|
|
|
case *api.RekeyVerificationStatusResponse:
|
|
|
|
stat := status.(*api.RekeyVerificationStatusResponse)
|
|
|
|
started = stat.Started
|
|
|
|
nonce = stat.Nonce
|
|
|
|
default:
|
|
|
|
c.UI.Error("Unknown status type")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Verify a root token generation is in progress. If there is not one in
|
|
|
|
// progress, return an error instructing the user to start one.
|
2018-05-28 04:39:53 +00:00
|
|
|
if !started {
|
2017-09-05 04:03:44 +00:00
|
|
|
c.UI.Error(wrapAtLength(
|
|
|
|
"No rekey is in progress. Start a rekey process by running " +
|
2018-06-07 04:11:21 +00:00
|
|
|
"\"vault operator rekey -init\"."))
|
2017-09-05 04:03:44 +00:00
|
|
|
return 1
|
2016-08-15 20:01:15 +00:00
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "-": // Read from stdin
|
|
|
|
nonce = c.flagNonce
|
|
|
|
|
|
|
|
// Pull our fake stdin if needed
|
|
|
|
stdin := (io.Reader)(os.Stdin)
|
|
|
|
if c.testStdin != nil {
|
|
|
|
stdin = c.testStdin
|
2015-12-16 21:56:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if _, err := io.Copy(&buf, stdin); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Failed to read from stdin: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2015-12-16 21:56:15 +00:00
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
key = buf.String()
|
|
|
|
case "": // Prompt using the tty
|
|
|
|
// Nonce value is not required if we are prompting via the terminal
|
|
|
|
w := getWriterFromUI(c.UI)
|
|
|
|
fmt.Fprintf(w, "Rekey operation nonce: %s\n", nonce)
|
|
|
|
fmt.Fprintf(w, "Unseal Key (will be hidden): ")
|
|
|
|
key, err = password.Read(os.Stdin)
|
|
|
|
fmt.Fprintf(w, "\n")
|
|
|
|
if err != nil {
|
|
|
|
if err == password.ErrInterrupted {
|
|
|
|
c.UI.Error("user canceled")
|
|
|
|
return 1
|
|
|
|
}
|
2015-06-01 11:08:20 +00:00
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
c.UI.Error(wrapAtLength(fmt.Sprintf("An error occurred attempting to "+
|
|
|
|
"ask for the unseal key. The raw error message is shown below, but "+
|
|
|
|
"usually this is because you attempted to pipe a value into the "+
|
|
|
|
"command or you are executing outside of a terminal (tty). If you "+
|
|
|
|
"want to pipe the value, pass \"-\" as the argument to read from "+
|
|
|
|
"stdin. The raw error was: %s", err)))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
default: // Supplied directly as an arg
|
|
|
|
nonce = c.flagNonce
|
2017-10-23 21:39:21 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Trim any whitespace from they key, especially since we might have
|
|
|
|
// prompted the user for it.
|
|
|
|
key = strings.TrimSpace(key)
|
2015-05-28 22:08:09 +00:00
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Verify we have a nonce value
|
|
|
|
if nonce == "" {
|
|
|
|
c.UI.Error("Missing nonce value: specify it via the -nonce flag")
|
2015-05-28 22:08:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Provide the key, this may potentially complete the update
|
|
|
|
resp, err := updateFn(key, nonce)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error posting unseal key: %s", err))
|
|
|
|
return 2
|
2016-01-08 22:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
var complete bool
|
|
|
|
var mightContainUnsealKeys bool
|
|
|
|
|
|
|
|
switch resp.(type) {
|
|
|
|
case *api.RekeyUpdateResponse:
|
|
|
|
complete = resp.(*api.RekeyUpdateResponse).Complete
|
|
|
|
mightContainUnsealKeys = true
|
|
|
|
case *api.RekeyVerificationUpdateResponse:
|
|
|
|
complete = resp.(*api.RekeyVerificationUpdateResponse).Complete
|
|
|
|
default:
|
|
|
|
c.UI.Error("Unknown update response type")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if !complete {
|
2017-09-05 04:03:44 +00:00
|
|
|
return c.status(client)
|
2016-01-08 22:35:07 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
if mightContainUnsealKeys {
|
|
|
|
return c.printUnsealKeys(client, status.(*api.RekeyStatusResponse),
|
|
|
|
resp.(*api.RekeyUpdateResponse))
|
|
|
|
}
|
|
|
|
|
2018-05-29 18:59:19 +00:00
|
|
|
c.UI.Output(wrapAtLength("Rekey verification successful. The rekey operation is complete and the new keys are now active."))
|
2018-05-28 04:39:53 +00:00
|
|
|
return 0
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// status is used just to fetch and dump the status.
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) status(client *api.Client) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
// Handle the different API requests
|
2018-05-28 04:39:53 +00:00
|
|
|
var fn func() (interface{}, error)
|
2017-09-05 04:03:44 +00:00
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
2018-05-28 04:39:53 +00:00
|
|
|
fn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyStatus()
|
|
|
|
}
|
|
|
|
if c.flagVerify {
|
|
|
|
fn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyVerificationStatus()
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
case "recovery", "hsm":
|
2018-05-28 04:39:53 +00:00
|
|
|
fn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyRecoveryKeyStatus()
|
|
|
|
}
|
|
|
|
if c.flagVerify {
|
|
|
|
fn = func() (interface{}, error) {
|
|
|
|
return client.Sys().RekeyRecoveryKeyVerificationStatus()
|
|
|
|
}
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
default:
|
|
|
|
c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
|
2015-05-28 22:08:09 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Make the request
|
|
|
|
status, err := fn()
|
2015-05-28 22:08:09 +00:00
|
|
|
if err != nil {
|
2017-09-05 04:03:44 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error reading rekey status: %s", err))
|
|
|
|
return 2
|
2015-05-28 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
return c.printStatus(status)
|
2016-02-12 19:24:36 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// backupRetrieve retrieves the stored backup keys.
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) backupRetrieve(client *api.Client) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
// Handle the different API requests
|
|
|
|
var fn func() (*api.RekeyRetrieveResponse, error)
|
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
|
|
|
fn = client.Sys().RekeyRetrieveBackup
|
|
|
|
case "recovery", "hsm":
|
|
|
|
fn = client.Sys().RekeyRetrieveRecoveryBackup
|
|
|
|
default:
|
|
|
|
c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
|
|
|
|
return 1
|
2016-04-04 14:44:22 +00:00
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
// Make the request
|
|
|
|
storedKeys, err := fn()
|
2015-12-16 21:56:15 +00:00
|
|
|
if err != nil {
|
2017-09-05 04:03:44 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error retrieving rekey stored keys: %s", err))
|
|
|
|
return 2
|
2015-12-16 21:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
secret := &api.Secret{
|
|
|
|
Data: structs.New(storedKeys).Map(),
|
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
return OutputSecret(c.UI, secret)
|
2015-12-16 21:56:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// backupDelete deletes the stored backup keys.
|
2017-09-08 02:03:12 +00:00
|
|
|
func (c *OperatorRekeyCommand) backupDelete(client *api.Client) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
// Handle the different API requests
|
|
|
|
var fn func() error
|
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
|
|
|
fn = client.Sys().RekeyDeleteBackup
|
|
|
|
case "recovery", "hsm":
|
|
|
|
fn = client.Sys().RekeyDeleteRecoveryBackup
|
|
|
|
default:
|
|
|
|
c.UI.Error(fmt.Sprintf("Unknown target: %s", c.flagTarget))
|
2015-12-16 21:56:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
// Make the request
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error deleting rekey stored keys: %s", err))
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Output("Success! Delete stored keys (if they existed)")
|
2015-05-28 22:08:09 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// printStatus dumps the status to output
|
2018-05-28 04:39:53 +00:00
|
|
|
func (c *OperatorRekeyCommand) printStatus(in interface{}) int {
|
2017-09-05 04:03:44 +00:00
|
|
|
out := []string{}
|
2017-09-21 20:56:11 +00:00
|
|
|
out = append(out, "Key | Value")
|
2015-05-28 22:08:09 +00:00
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
switch in.(type) {
|
|
|
|
case *api.RekeyStatusResponse:
|
|
|
|
status := in.(*api.RekeyStatusResponse)
|
|
|
|
out = append(out, fmt.Sprintf("Nonce | %s", status.Nonce))
|
|
|
|
out = append(out, fmt.Sprintf("Started | %t", status.Started))
|
|
|
|
if status.Started {
|
2018-05-29 17:13:47 +00:00
|
|
|
if status.Progress == status.Required {
|
|
|
|
out = append(out, fmt.Sprintf("Rekey Progress | %d/%d (verification in progress)", status.Progress, status.Required))
|
|
|
|
} else {
|
|
|
|
out = append(out, fmt.Sprintf("Rekey Progress | %d/%d", status.Progress, status.Required))
|
|
|
|
}
|
2018-05-28 04:39:53 +00:00
|
|
|
out = append(out, fmt.Sprintf("New Shares | %d", status.N))
|
|
|
|
out = append(out, fmt.Sprintf("New Threshold | %d", status.T))
|
|
|
|
out = append(out, fmt.Sprintf("Verification Required | %t", status.VerificationRequired))
|
2018-05-29 17:18:52 +00:00
|
|
|
if status.VerificationNonce != "" {
|
|
|
|
out = append(out, fmt.Sprintf("Verification Nonce | %s", status.VerificationNonce))
|
|
|
|
}
|
2018-05-28 04:39:53 +00:00
|
|
|
}
|
|
|
|
if len(status.PGPFingerprints) > 0 {
|
|
|
|
out = append(out, fmt.Sprintf("PGP Fingerprints | %s", status.PGPFingerprints))
|
|
|
|
out = append(out, fmt.Sprintf("Backup | %t", status.Backup))
|
|
|
|
}
|
|
|
|
case *api.RekeyVerificationStatusResponse:
|
|
|
|
status := in.(*api.RekeyVerificationStatusResponse)
|
|
|
|
out = append(out, fmt.Sprintf("Started | %t", status.Started))
|
2017-09-05 04:03:44 +00:00
|
|
|
out = append(out, fmt.Sprintf("New Shares | %d", status.N))
|
|
|
|
out = append(out, fmt.Sprintf("New Threshold | %d", status.T))
|
2018-05-29 17:18:52 +00:00
|
|
|
out = append(out, fmt.Sprintf("Verification Nonce | %s", status.Nonce))
|
2018-05-28 04:39:53 +00:00
|
|
|
out = append(out, fmt.Sprintf("Verification Progress | %d/%d", status.Progress, status.T))
|
|
|
|
default:
|
|
|
|
c.UI.Error("Unknown status type")
|
|
|
|
return 1
|
2017-09-05 04:03:44 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 23:12:16 +00:00
|
|
|
switch Format(c.UI) {
|
|
|
|
case "table":
|
|
|
|
c.UI.Output(tableOutput(out, nil))
|
|
|
|
return 0
|
|
|
|
default:
|
2018-05-28 04:39:53 +00:00
|
|
|
return OutputData(c.UI, in)
|
2018-02-12 23:12:16 +00:00
|
|
|
}
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
func (c *OperatorRekeyCommand) printUnsealKeys(client *api.Client, status *api.RekeyStatusResponse, resp *api.RekeyUpdateResponse) int {
|
2018-02-12 23:12:16 +00:00
|
|
|
switch Format(c.UI) {
|
|
|
|
case "table":
|
|
|
|
default:
|
|
|
|
return OutputData(c.UI, resp)
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:03:44 +00:00
|
|
|
// Space between the key prompt, if any, and the output
|
|
|
|
c.UI.Output("")
|
|
|
|
|
|
|
|
// Provide the keys
|
|
|
|
var haveB64 bool
|
|
|
|
if resp.KeysB64 != nil && len(resp.KeysB64) == len(resp.Keys) {
|
|
|
|
haveB64 = true
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
for i, key := range resp.Keys {
|
|
|
|
if len(resp.PGPFingerprints) > 0 {
|
|
|
|
if haveB64 {
|
|
|
|
c.UI.Output(fmt.Sprintf("Key %d fingerprint: %s; value: %s", i+1, resp.PGPFingerprints[i], resp.KeysB64[i]))
|
|
|
|
} else {
|
|
|
|
c.UI.Output(fmt.Sprintf("Key %d fingerprint: %s; value: %s", i+1, resp.PGPFingerprints[i], key))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if haveB64 {
|
|
|
|
c.UI.Output(fmt.Sprintf("Key %d: %s", i+1, resp.KeysB64[i]))
|
|
|
|
} else {
|
|
|
|
c.UI.Output(fmt.Sprintf("Key %d: %s", i+1, key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.UI.Output("")
|
|
|
|
c.UI.Output(fmt.Sprintf("Operation nonce: %s", resp.Nonce))
|
|
|
|
|
|
|
|
if len(resp.PGPFingerprints) > 0 && resp.Backup {
|
|
|
|
c.UI.Output("")
|
2019-06-11 08:05:44 +00:00
|
|
|
switch strings.ToLower(strings.TrimSpace(c.flagTarget)) {
|
|
|
|
case "barrier":
|
|
|
|
c.UI.Output(wrapAtLength(fmt.Sprintf(
|
|
|
|
"The encrypted unseal keys are backed up to \"core/unseal-keys-backup\" " +
|
|
|
|
"in the storage backend. Remove these keys at any time using " +
|
|
|
|
"\"vault operator rekey -backup-delete\". Vault does not automatically " +
|
|
|
|
"remove these keys.",
|
|
|
|
)))
|
|
|
|
case "recovery", "hsm":
|
|
|
|
c.UI.Output(wrapAtLength(fmt.Sprintf(
|
|
|
|
"The encrypted unseal keys are backed up to \"core/recovery-keys-backup\" " +
|
|
|
|
"in the storage backend. Remove these keys at any time using " +
|
|
|
|
"\"vault operator rekey -backup-delete -target=recovery\". Vault does not automatically " +
|
|
|
|
"remove these keys.",
|
|
|
|
)))
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 04:39:53 +00:00
|
|
|
switch status.VerificationRequired {
|
|
|
|
case false:
|
|
|
|
c.UI.Output("")
|
|
|
|
c.UI.Output(wrapAtLength(fmt.Sprintf(
|
|
|
|
"Vault rekeyed with %d key shares and a key threshold of %d. Please "+
|
2018-11-01 14:13:20 +00:00
|
|
|
"securely distribute the key shares printed above. When Vault is "+
|
2018-05-28 04:39:53 +00:00
|
|
|
"re-sealed, restarted, or stopped, you must supply at least %d of "+
|
|
|
|
"these keys to unseal it before it can start servicing requests.",
|
|
|
|
status.N,
|
|
|
|
status.T,
|
|
|
|
status.T)))
|
|
|
|
default:
|
|
|
|
c.UI.Output("")
|
|
|
|
c.UI.Output(wrapAtLength(fmt.Sprintf(
|
|
|
|
"Vault has created a new key, split into %d key shares and a key threshold "+
|
|
|
|
"of %d. These will not be active until after verification is complete. "+
|
2018-11-01 14:13:20 +00:00
|
|
|
"Please securely distribute the key shares printed above. When Vault "+
|
2018-05-28 04:39:53 +00:00
|
|
|
"is re-sealed, restarted, or stopped, you must supply at least %d of "+
|
|
|
|
"these keys to unseal it before it can start servicing requests.",
|
|
|
|
status.N,
|
|
|
|
status.T,
|
|
|
|
status.T)))
|
|
|
|
c.UI.Output("")
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"Again, these key shares are _not_ valid until verification is performed. " +
|
|
|
|
"Do not lose or discard your current key shares until after verification " +
|
2018-05-29 16:42:33 +00:00
|
|
|
"is complete or you will be unable to unseal Vault. If you cancel the " +
|
|
|
|
"rekey process or seal Vault before verification is complete the new " +
|
|
|
|
"shares will be discarded and the current shares will remain valid.",
|
|
|
|
))
|
|
|
|
c.UI.Output("")
|
|
|
|
c.UI.Warn(wrapAtLength(
|
|
|
|
"The current verification status, including initial nonce, is shown below.",
|
2018-05-28 04:39:53 +00:00
|
|
|
))
|
|
|
|
c.UI.Output("")
|
|
|
|
|
|
|
|
c.flagVerify = true
|
|
|
|
return c.status(client)
|
|
|
|
}
|
2017-09-05 04:03:44 +00:00
|
|
|
|
|
|
|
return 0
|
2017-08-24 22:23:40 +00:00
|
|
|
}
|