open-vault/command/rekey.go

421 lines
13 KiB
Go
Raw Normal View History

2015-05-28 22:08:09 +00:00
package command
import (
"fmt"
"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"
"github.com/hashicorp/vault/helper/password"
"github.com/hashicorp/vault/helper/pgpkeys"
2016-04-01 17:16:05 +00:00
"github.com/hashicorp/vault/meta"
2015-05-28 22:08:09 +00:00
)
// RekeyCommand is a Command that rekeys the vault.
type RekeyCommand struct {
2016-04-01 17:16:05 +00:00
meta.Meta
2015-05-28 22:08:09 +00:00
// Key can be used to pre-seed the key. If it is set, it will not
// be asked with the `password` helper.
Key string
2015-12-16 21:56:15 +00:00
// The nonce for the rekey request to send along
Nonce string
2016-04-04 14:44:22 +00:00
// Whether to use the recovery key instead of barrier key, if available
RecoveryKey bool
2015-05-28 22:08:09 +00:00
}
func (c *RekeyCommand) Run(args []string) int {
2016-04-04 14:44:22 +00:00
var init, cancel, status, delete, retrieve, backup, recoveryKey bool
2015-05-28 22:08:09 +00:00
var shares, threshold int
2015-12-16 21:56:15 +00:00
var nonce string
var pgpKeys pgpkeys.PubKeyFilesFlag
2016-04-01 17:16:05 +00:00
flags := c.Meta.FlagSet("rekey", meta.FlagSetDefault)
2015-05-28 22:08:09 +00:00
flags.BoolVar(&init, "init", false, "")
flags.BoolVar(&cancel, "cancel", false, "")
flags.BoolVar(&status, "status", false, "")
2015-12-16 21:56:15 +00:00
flags.BoolVar(&delete, "delete", false, "")
flags.BoolVar(&retrieve, "retrieve", false, "")
flags.BoolVar(&backup, "backup", false, "")
2016-04-04 14:44:22 +00:00
flags.BoolVar(&recoveryKey, "recovery-key", c.RecoveryKey, "")
2015-08-25 22:33:58 +00:00
flags.IntVar(&shares, "key-shares", 5, "")
2015-05-28 22:08:09 +00:00
flags.IntVar(&threshold, "key-threshold", 3, "")
2015-12-16 21:56:15 +00:00
flags.StringVar(&nonce, "nonce", "", "")
flags.Var(&pgpKeys, "pgp-keys", "")
2015-05-28 22:08:09 +00:00
flags.Usage = func() { c.Ui.Error(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
2015-12-16 21:56:15 +00:00
if nonce != "" {
c.Nonce = nonce
}
2015-05-28 22:08:09 +00:00
client, err := c.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error initializing client: %s", err))
return 2
}
// Check if we are running doing any restricted variants
2015-12-16 21:56:15 +00:00
switch {
case init:
2016-04-04 14:44:22 +00:00
return c.initRekey(client, shares, threshold, pgpKeys, backup, recoveryKey)
2015-12-16 21:56:15 +00:00
case cancel:
2016-04-04 14:44:22 +00:00
return c.cancelRekey(client, recoveryKey)
2015-12-16 21:56:15 +00:00
case status:
2016-04-04 14:44:22 +00:00
return c.rekeyStatus(client, recoveryKey)
2015-12-16 21:56:15 +00:00
case retrieve:
2016-04-04 14:44:22 +00:00
return c.rekeyRetrieveStored(client, recoveryKey)
2015-12-16 21:56:15 +00:00
case delete:
2016-04-04 14:44:22 +00:00
return c.rekeyDeleteStored(client, recoveryKey)
2015-05-28 22:08:09 +00:00
}
// Check if the rekey is started
2016-04-04 14:44:22 +00:00
var rekeyStatus *api.RekeyStatusResponse
if recoveryKey {
rekeyStatus, err = client.Sys().RekeyRecoveryKeyStatus()
} else {
rekeyStatus, err = client.Sys().RekeyStatus()
}
2015-05-28 22:08:09 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error reading rekey status: %s", err))
return 1
}
// Start the rekey process if not started
if !rekeyStatus.Started {
2016-04-04 14:44:22 +00:00
if recoveryKey {
rekeyStatus, err = client.Sys().RekeyRecoveryKeyInit(&api.RekeyInitRequest{
SecretShares: shares,
SecretThreshold: threshold,
PGPKeys: pgpKeys,
Backup: backup,
2016-04-04 14:44:22 +00:00
})
} else {
rekeyStatus, err = client.Sys().RekeyInit(&api.RekeyInitRequest{
SecretShares: shares,
SecretThreshold: threshold,
PGPKeys: pgpKeys,
Backup: backup,
2016-04-04 14:44:22 +00:00
})
}
2015-05-28 22:08:09 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing rekey: %s", err))
return 1
}
2015-12-16 21:56:15 +00:00
c.Nonce = rekeyStatus.Nonce
2015-05-28 22:08:09 +00:00
}
2015-12-16 21:56:15 +00:00
shares = rekeyStatus.N
threshold = rekeyStatus.T
serverNonce := rekeyStatus.Nonce
2015-05-28 22:08:09 +00:00
// Get the unseal key
args = flags.Args()
2015-12-16 21:56:15 +00:00
key := c.Key
2015-05-28 22:08:09 +00:00
if len(args) > 0 {
2015-12-16 21:56:15 +00:00
key = args[0]
2015-05-28 22:08:09 +00:00
}
2015-12-16 21:56:15 +00:00
if key == "" {
c.Nonce = serverNonce
fmt.Printf("Rekey operation nonce: %s\n", serverNonce)
2015-05-28 22:08:09 +00:00
fmt.Printf("Key (will be hidden): ")
2015-12-16 21:56:15 +00:00
key, err = password.Read(os.Stdin)
2015-05-28 22:08:09 +00:00
fmt.Printf("\n")
if err != nil {
c.Ui.Error(fmt.Sprintf(
"Error attempting to ask for password. The raw error message\n"+
"is shown below, but the most common reason for this error is\n"+
"that you attempted to pipe a value into unseal or you're\n"+
"executing `vault rekey` from outside of a terminal.\n\n"+
"You should use `vault rekey` from a terminal for maximum\n"+
"security. If this isn't an option, the unseal key can be passed\n"+
"in using the first parameter.\n\n"+
"Raw error: %s", err))
return 1
}
}
// Provide the key, this may potentially complete the update
2016-04-04 14:44:22 +00:00
var result *api.RekeyUpdateResponse
if recoveryKey {
result, err = client.Sys().RekeyRecoveryKeyUpdate(strings.TrimSpace(key), c.Nonce)
} else {
result, err = client.Sys().RekeyUpdate(strings.TrimSpace(key), c.Nonce)
}
2015-05-28 22:08:09 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error attempting rekey update: %s", err))
return 1
}
// If we are not complete, then dump the status
if !result.Complete {
2016-04-04 14:44:22 +00:00
return c.rekeyStatus(client, recoveryKey)
2015-05-28 22:08:09 +00:00
}
// Space between the key prompt, if any, and the output
c.Ui.Output("\n")
2015-05-28 22:08:09 +00:00
// Provide the keys
var haveB64 bool
if result.KeysB64 != nil && len(result.KeysB64) == len(result.Keys) {
haveB64 = true
}
2015-05-28 22:08:09 +00:00
for i, key := range result.Keys {
2015-12-16 21:56:15 +00:00
if len(result.PGPFingerprints) > 0 {
if haveB64 {
c.Ui.Output(fmt.Sprintf("Key %d fingerprint: %s; value: %s", i+1, result.PGPFingerprints[i], result.KeysB64[i]))
} else {
c.Ui.Output(fmt.Sprintf("Key %d fingerprint: %s; value: %s", i+1, result.PGPFingerprints[i], key))
}
2015-12-16 21:56:15 +00:00
} else {
if haveB64 {
c.Ui.Output(fmt.Sprintf("Key %d: %s", i+1, result.KeysB64[i]))
} else {
c.Ui.Output(fmt.Sprintf("Key %d: %s", i+1, key))
}
2015-12-16 21:56:15 +00:00
}
}
c.Ui.Output(fmt.Sprintf("\nOperation nonce: %s", result.Nonce))
if len(result.PGPFingerprints) > 0 && result.Backup {
c.Ui.Output(fmt.Sprintf(
"\n" +
2016-01-08 19:09:40 +00:00
"The encrypted unseal keys have been backed up to \"core/unseal-keys-backup\"\n" +
"in your physical backend. It is your responsibility to remove these if and\n" +
"when desired.",
2015-12-16 21:56:15 +00:00
))
2015-05-28 22:08:09 +00:00
}
c.Ui.Output(fmt.Sprintf(
"\n"+
"Vault rekeyed with %d keys and a key threshold of %d. Please\n"+
"securely distribute the above keys. When the Vault is re-sealed,\n"+
"restarted, or stopped, you must provide at least %d of these keys\n"+
"to unseal it again.\n\n"+
"Vault does not store the master key. Without at least %d keys,\n"+
"your Vault will remain permanently sealed.",
2015-05-28 22:08:09 +00:00
shares,
threshold,
threshold,
threshold,
2015-05-28 22:08:09 +00:00
))
2015-05-28 22:08:09 +00:00
return 0
}
// initRekey is used to start the rekey process
2015-12-16 21:56:15 +00:00
func (c *RekeyCommand) initRekey(client *api.Client,
shares, threshold int,
pgpKeys pgpkeys.PubKeyFilesFlag,
2016-04-04 14:44:22 +00:00
backup, recoveryKey bool) int {
2015-05-28 22:08:09 +00:00
// Start the rekey
2016-04-04 14:44:22 +00:00
request := &api.RekeyInitRequest{
2015-05-28 22:08:09 +00:00
SecretShares: shares,
SecretThreshold: threshold,
2015-08-25 22:33:58 +00:00
PGPKeys: pgpKeys,
2015-12-16 21:56:15 +00:00
Backup: backup,
2016-04-04 14:44:22 +00:00
}
var status *api.RekeyStatusResponse
var err error
if recoveryKey {
status, err = client.Sys().RekeyRecoveryKeyInit(request)
} else {
status, err = client.Sys().RekeyInit(request)
}
2015-05-28 22:08:09 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing rekey: %s", err))
return 1
}
if pgpKeys == nil || len(pgpKeys) == 0 {
c.Ui.Output(`
WARNING: If you lose the keys after they are returned to you, there is no
recovery. Consider using the '-pgp-keys' option to protect the returned unseal
keys along with '-backup=true' to allow recovery of the encrypted keys in case
of emergency. They can easily be deleted at a later time with
'vault rekey -delete'.
`)
}
if pgpKeys != nil && len(pgpKeys) > 0 && !backup {
c.Ui.Output(`
WARNING: You are using PGP keys for encryption, but have not set the option to
back up the new unseal keys to physical storage. If you lose the keys after
they are returned to you, there is no recovery. Consider setting '-backup=true'
to allow recovery of the encrypted keys in case of emergency. They can easily
be deleted at a later time with 'vault rekey -delete'.
`)
}
2015-05-28 22:08:09 +00:00
// Provide the current status
return c.dumpRekeyStatus(status)
2015-05-28 22:08:09 +00:00
}
// cancelRekey is used to abort the rekey process
2016-04-04 14:44:22 +00:00
func (c *RekeyCommand) cancelRekey(client *api.Client, recovery bool) int {
var err error
if recovery {
err = client.Sys().RekeyRecoveryKeyCancel()
} else {
err = client.Sys().RekeyCancel()
}
2015-05-28 22:08:09 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to cancel rekey: %s", err))
return 1
}
c.Ui.Output("Rekey canceled.")
return 0
}
// rekeyStatus is used just to fetch and dump the status
2016-04-04 14:44:22 +00:00
func (c *RekeyCommand) rekeyStatus(client *api.Client, recovery bool) int {
2015-05-28 22:08:09 +00:00
// Check the status
2016-04-04 14:44:22 +00:00
var status *api.RekeyStatusResponse
var err error
if recovery {
status, err = client.Sys().RekeyRecoveryKeyStatus()
} else {
status, err = client.Sys().RekeyStatus()
}
2015-05-28 22:08:09 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error reading rekey status: %s", err))
return 1
}
return c.dumpRekeyStatus(status)
}
func (c *RekeyCommand) dumpRekeyStatus(status *api.RekeyStatusResponse) int {
2015-05-28 22:08:09 +00:00
// Dump the status
2015-12-16 21:56:15 +00:00
statString := fmt.Sprintf(
"Nonce: %s\n"+
2016-04-04 14:44:22 +00:00
"Started: %t\n"+
2015-05-28 22:08:09 +00:00
"Key Shares: %d\n"+
"Key Threshold: %d\n"+
"Rekey Progress: %d\n"+
"Required Keys: %d",
2015-12-16 21:56:15 +00:00
status.Nonce,
2015-05-28 22:08:09 +00:00
status.Started,
status.N,
status.T,
status.Progress,
status.Required,
2015-12-16 21:56:15 +00:00
)
if len(status.PGPFingerprints) != 0 {
statString = fmt.Sprintf("%s\nPGP Key Fingerprints: %s", statString, status.PGPFingerprints)
statString = fmt.Sprintf("%s\nBackup Storage: %t", statString, status.Backup)
2015-12-16 21:56:15 +00:00
}
c.Ui.Output(statString)
return 0
}
2016-04-04 14:44:22 +00:00
func (c *RekeyCommand) rekeyRetrieveStored(client *api.Client, recovery bool) int {
var storedKeys *api.RekeyRetrieveResponse
var err error
if recovery {
storedKeys, err = client.Sys().RekeyRetrieveRecoveryBackup()
} else {
storedKeys, err = client.Sys().RekeyRetrieveBackup()
}
2015-12-16 21:56:15 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving stored keys: %s", err))
return 1
}
secret := &api.Secret{
Data: structs.New(storedKeys).Map(),
}
return OutputSecret(c.Ui, "table", secret)
}
2016-04-04 14:44:22 +00:00
func (c *RekeyCommand) rekeyDeleteStored(client *api.Client, recovery bool) int {
var err error
if recovery {
err = client.Sys().RekeyDeleteRecoveryBackup()
} else {
err = client.Sys().RekeyDeleteBackup()
}
2015-12-16 21:56:15 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to delete stored keys: %s", err))
return 1
}
c.Ui.Output("Stored keys deleted.")
2015-05-28 22:08:09 +00:00
return 0
}
func (c *RekeyCommand) Synopsis() string {
return "Rekeys Vault to generate new unseal keys"
}
func (c *RekeyCommand) Help() string {
helpText := `
Usage: vault rekey [options] [key]
2015-05-28 22:22:42 +00:00
Rekey is used to change the unseal keys. This can be done to generate
a new set of unseal keys or to change the number of shares and the
required threshold.
2015-05-28 22:08:09 +00:00
2015-05-28 22:22:42 +00:00
Rekey can only be done when the Vault is already unsealed. The operation
is done online, but requires that a threshold of the current unseal
keys be provided.
2015-05-28 22:08:09 +00:00
General Options:
` + meta.GeneralOptionsUsage() + `
2015-12-16 21:56:15 +00:00
Rekey Options:
2015-05-28 22:08:09 +00:00
2015-05-28 22:22:42 +00:00
-init Initialize the rekey operation by setting the desired
number of shares and the key threshold. This can only be
done if no rekey is already initiated.
-cancel Reset the rekey process by throwing away
2015-05-28 22:22:42 +00:00
prior keys and the rekey configuration.
-status Prints the status of the current rekey operation.
This can be used to see the status without attempting
to provide an unseal key.
2015-12-16 21:56:15 +00:00
-retrieve Retrieve backed-up keys. Only available if the PGP keys
were provided and the backup has not been deleted.
-delete Delete any backed-up keys.
2015-05-28 22:22:42 +00:00
-key-shares=5 The number of key shares to split the master key
into.
2015-05-28 22:08:09 +00:00
2015-05-28 22:22:42 +00:00
-key-threshold=3 The number of key shares required to reconstruct
the master key.
2015-12-16 21:56:15 +00:00
-nonce=abcd The nonce provided at rekey initialization time. This
same nonce value must be provided with each unseal
key. If the unseal key is not being passed in via the
the command line the nonce parameter is not required,
and will instead be displayed with the key prompt.
-pgp-keys If provided, must be a comma-separated list of
files on disk containing binary- or base64-format
public PGP keys, or Keybase usernames specified as
"keybase:<username>". The number of given entries
must match 'key-shares'. The output unseal keys will
be encrypted and base64-encoded, in order, with the
2016-02-26 13:43:56 +00:00
given public keys. If you want to use them with the
'vault unseal' command, you will need to base64-decode
2016-02-26 13:43:56 +00:00
and decrypt; this will be the plaintext unseal key.
2015-12-16 21:56:15 +00:00
-backup=false If true, and if the key shares are PGP-encrypted, a
plaintext backup of the PGP-encrypted keys will be
stored at "core/unseal-keys-backup" in your physical
storage. You can retrieve or delete them via the
'sys/rekey/backup' endpoint.
2016-04-04 14:44:22 +00:00
-recovery-key=false Whether to rekey the recovery key instead of the
barrier key. Only used with Vault HSM.
2015-05-28 22:08:09 +00:00
`
return strings.TrimSpace(helpText)
}