2015-03-13 17:32:39 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2015-08-25 21:24:19 +00:00
|
|
|
"github.com/hashicorp/vault/helper/pgpkeys"
|
2015-03-13 17:32:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitCommand is a Command that initializes a new Vault server.
|
|
|
|
type InitCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Run(args []string) int {
|
2015-08-25 16:32:45 +00:00
|
|
|
var threshold, shares int
|
2015-08-25 21:24:19 +00:00
|
|
|
var pgpKeys pgpkeys.PubKeyFilesFlag
|
2015-03-13 17:32:39 +00:00
|
|
|
flags := c.Meta.FlagSet("init", FlagSetDefault)
|
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
2015-08-25 22:33:58 +00:00
|
|
|
flags.IntVar(&shares, "key-shares", 5, "")
|
2015-03-13 17:32:39 +00:00
|
|
|
flags.IntVar(&threshold, "key-threshold", 3, "")
|
2015-08-25 16:32:45 +00:00
|
|
|
flags.Var(&pgpKeys, "pgp-keys", "")
|
2015-03-13 17:32:39 +00:00
|
|
|
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 1
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Sys().Init(&api.InitRequest{
|
|
|
|
SecretShares: shares,
|
|
|
|
SecretThreshold: threshold,
|
2015-08-25 22:33:58 +00:00
|
|
|
PGPKeys: pgpKeys,
|
2015-03-13 17:32:39 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing Vault: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:38:19 +00:00
|
|
|
for i, key := range resp.Keys {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Key %d: %s", i+1, key))
|
2015-03-13 17:32:39 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 23:25:53 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Initial Root Token: %s", resp.RootToken))
|
|
|
|
|
2015-03-13 17:38:19 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
"\n"+
|
2015-05-29 00:02:09 +00:00
|
|
|
"Vault initialized with %d keys and a key threshold of %d. Please\n"+
|
2015-05-29 00:07:38 +00:00
|
|
|
"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-03-13 17:38:19 +00:00
|
|
|
shares,
|
|
|
|
threshold,
|
|
|
|
threshold,
|
2015-05-29 00:02:09 +00:00
|
|
|
threshold,
|
2015-03-13 17:38:19 +00:00
|
|
|
))
|
|
|
|
|
2015-03-13 17:32:39 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Synopsis() string {
|
|
|
|
return "Initialize a new Vault server"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *InitCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault init [options]
|
|
|
|
|
|
|
|
Initialize a new Vault server.
|
|
|
|
|
|
|
|
This command connects to a Vault server and initializes it for the
|
|
|
|
first time. This sets up the initial set of master keys and sets up the
|
|
|
|
backend data store structure.
|
|
|
|
|
|
|
|
This command can't be called on an already-initialized Vault.
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2015-06-30 19:01:23 +00:00
|
|
|
` + generalOptionsUsage() + `
|
2015-03-13 17:32:39 +00:00
|
|
|
|
|
|
|
Init Options:
|
|
|
|
|
|
|
|
-key-shares=5 The number of key shares to split the master key
|
|
|
|
into.
|
|
|
|
|
|
|
|
-key-threshold=3 The number of key shares required to reconstruct
|
|
|
|
the master key.
|
|
|
|
|
2015-08-25 16:32:45 +00:00
|
|
|
-pgp-keys If provided, must be a comma-separated list of
|
2015-10-08 15:09:30 +00:00
|
|
|
files on disk containing binary- or base64-format
|
|
|
|
public PGP keys. The number of files must match
|
|
|
|
'key-shares'. The output unseal keys will encrypted
|
|
|
|
and hex-encoded, in order, with the given public keys.
|
2015-08-25 16:32:45 +00:00
|
|
|
If you want to use them with the 'vault unseal'
|
2015-08-26 14:59:50 +00:00
|
|
|
command, you will need to hex decode and decrypt;
|
|
|
|
this will be the plaintext unseal key.
|
2015-03-13 17:32:39 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|