2015-03-13 18:33:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-04-20 20:37:32 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/api"
|
2016-04-01 17:16:05 +00:00
|
|
|
"github.com/hashicorp/vault/meta"
|
2015-03-13 18:33:17 +00:00
|
|
|
)
|
|
|
|
|
2015-04-20 19:11:21 +00:00
|
|
|
// StatusCommand is a Command that outputs the status of whether
|
|
|
|
// Vault is sealed or not as well as HA information.
|
|
|
|
type StatusCommand struct {
|
2016-04-01 17:16:05 +00:00
|
|
|
meta.Meta
|
2015-03-13 18:33:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:11:21 +00:00
|
|
|
func (c *StatusCommand) Run(args []string) int {
|
2016-04-01 17:16:05 +00:00
|
|
|
flags := c.Meta.FlagSet("status", meta.FlagSetDefault)
|
2015-03-13 18:33:17 +00:00
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
if err := flags.Parse(args); err != nil {
|
2016-01-29 15:55:31 +00:00
|
|
|
return 1
|
2015-03-13 18:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := c.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing client: %s", err))
|
2016-01-29 15:55:31 +00:00
|
|
|
return 1
|
2015-03-13 18:33:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:08:54 +00:00
|
|
|
sealStatus, err := client.Sys().SealStatus()
|
2015-03-13 18:33:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error checking seal status: %s", err))
|
2016-01-29 15:55:31 +00:00
|
|
|
return 1
|
2015-03-13 18:33:17 +00:00
|
|
|
}
|
2016-07-29 18:13:53 +00:00
|
|
|
|
|
|
|
outStr := fmt.Sprintf(
|
2015-04-20 20:37:32 +00:00
|
|
|
"Sealed: %v\n"+
|
2015-04-21 18:11:15 +00:00
|
|
|
"Key Shares: %d\n"+
|
|
|
|
"Key Threshold: %d\n"+
|
2016-07-28 21:37:26 +00:00
|
|
|
"Unseal Progress: %d\n"+
|
|
|
|
"Version: %s",
|
2015-04-20 20:37:32 +00:00
|
|
|
sealStatus.Sealed,
|
|
|
|
sealStatus.N,
|
|
|
|
sealStatus.T,
|
2016-07-28 21:37:26 +00:00
|
|
|
sealStatus.Progress,
|
2016-07-29 18:13:53 +00:00
|
|
|
sealStatus.Version)
|
|
|
|
|
|
|
|
if sealStatus.ClusterName != "" && sealStatus.ClusterID != "" {
|
|
|
|
outStr = fmt.Sprintf("%s\nCluster Name: %s\nCluster ID: %s", outStr, sealStatus.ClusterName, sealStatus.ClusterID)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(outStr)
|
2015-03-13 18:33:17 +00:00
|
|
|
|
2015-04-20 20:37:32 +00:00
|
|
|
// Mask the 'Vault is sealed' error, since this means HA is enabled,
|
|
|
|
// but that we cannot query for the leader since we are sealed.
|
2015-04-20 19:08:54 +00:00
|
|
|
leaderStatus, err := client.Sys().Leader()
|
2015-04-20 20:37:32 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "Vault is sealed") {
|
|
|
|
leaderStatus = &api.LeaderResponse{HAEnabled: true}
|
|
|
|
err = nil
|
|
|
|
}
|
2015-04-20 19:08:54 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error checking leader status: %s", err))
|
2016-01-29 15:55:31 +00:00
|
|
|
return 1
|
2015-04-20 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 20:37:32 +00:00
|
|
|
// Output if HA is enabled
|
2015-04-21 18:11:15 +00:00
|
|
|
c.Ui.Output("")
|
2015-04-20 20:37:32 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("High-Availability Enabled: %v", leaderStatus.HAEnabled))
|
|
|
|
if leaderStatus.HAEnabled {
|
|
|
|
if sealStatus.Sealed {
|
|
|
|
c.Ui.Output("\tMode: sealed")
|
|
|
|
} else {
|
|
|
|
mode := "standby"
|
|
|
|
if leaderStatus.IsSelf {
|
|
|
|
mode = "active"
|
|
|
|
}
|
|
|
|
c.Ui.Output(fmt.Sprintf("\tMode: %s", mode))
|
2015-04-20 19:19:25 +00:00
|
|
|
|
2015-04-20 20:37:32 +00:00
|
|
|
if leaderStatus.LeaderAddress == "" {
|
|
|
|
leaderStatus.LeaderAddress = "<none>"
|
|
|
|
}
|
|
|
|
c.Ui.Output(fmt.Sprintf("\tLeader: %s", leaderStatus.LeaderAddress))
|
|
|
|
}
|
|
|
|
}
|
2015-03-13 18:33:17 +00:00
|
|
|
|
2015-04-20 19:08:54 +00:00
|
|
|
if sealStatus.Sealed {
|
2016-01-29 15:55:31 +00:00
|
|
|
return 2
|
2015-03-13 18:33:17 +00:00
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 19:11:21 +00:00
|
|
|
func (c *StatusCommand) Synopsis() string {
|
|
|
|
return "Outputs status of whether Vault is sealed and if HA mode is enabled"
|
2015-03-13 18:33:17 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 19:11:21 +00:00
|
|
|
func (c *StatusCommand) Help() string {
|
2015-03-13 18:33:17 +00:00
|
|
|
helpText := `
|
2015-04-20 19:11:21 +00:00
|
|
|
Usage: vault status [options]
|
2015-03-13 18:33:17 +00:00
|
|
|
|
2015-04-20 19:11:21 +00:00
|
|
|
Outputs the state of the Vault, sealed or unsealed and if HA is enabled.
|
2015-03-13 18:33:17 +00:00
|
|
|
|
|
|
|
This command outputs whether or not the Vault is sealed. The exit
|
2016-02-08 16:36:08 +00:00
|
|
|
code also reflects the seal status (0 unsealed, 2 sealed, 1 error).
|
2015-03-13 18:33:17 +00:00
|
|
|
|
|
|
|
General Options:
|
2016-04-01 20:50:12 +00:00
|
|
|
` + meta.GeneralOptionsUsage()
|
2015-03-13 18:33:17 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|