2015-03-12 22:21:11 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-03-31 23:44:47 +00:00
|
|
|
"encoding/hex"
|
2015-03-12 22:30:07 +00:00
|
|
|
"fmt"
|
2015-04-04 19:06:41 +00:00
|
|
|
"log"
|
2015-03-13 17:09:38 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2015-05-02 22:57:40 +00:00
|
|
|
"net/url"
|
2015-04-04 19:06:41 +00:00
|
|
|
"os"
|
2016-03-14 18:05:47 +00:00
|
|
|
"os/signal"
|
2015-10-28 17:05:56 +00:00
|
|
|
"runtime"
|
2015-04-04 19:06:41 +00:00
|
|
|
"sort"
|
2015-05-02 22:57:40 +00:00
|
|
|
"strconv"
|
2015-03-12 22:21:11 +00:00
|
|
|
"strings"
|
2016-03-14 18:05:47 +00:00
|
|
|
"syscall"
|
2015-04-15 01:44:09 +00:00
|
|
|
"time"
|
2015-03-12 22:21:11 +00:00
|
|
|
|
2015-04-15 01:44:09 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2016-04-04 14:44:22 +00:00
|
|
|
"github.com/hashicorp/errwrap"
|
2016-03-11 21:46:56 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2015-04-04 19:11:10 +00:00
|
|
|
"github.com/hashicorp/logutils"
|
2015-04-05 01:07:53 +00:00
|
|
|
"github.com/hashicorp/vault/audit"
|
2015-03-12 22:30:07 +00:00
|
|
|
"github.com/hashicorp/vault/command/server"
|
2015-03-12 22:21:11 +00:00
|
|
|
"github.com/hashicorp/vault/helper/flag-slice"
|
2015-04-04 19:06:41 +00:00
|
|
|
"github.com/hashicorp/vault/helper/gated-writer"
|
2015-04-28 22:04:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/mlock"
|
2015-03-13 17:09:38 +00:00
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2015-03-20 18:32:18 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
2016-04-01 17:16:05 +00:00
|
|
|
"github.com/hashicorp/vault/meta"
|
2015-03-13 17:09:38 +00:00
|
|
|
"github.com/hashicorp/vault/physical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
2015-11-09 18:52:55 +00:00
|
|
|
"github.com/hashicorp/vault/version"
|
2015-03-12 22:21:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ServerCommand is a Command that starts the Vault server.
|
|
|
|
type ServerCommand struct {
|
2015-04-05 01:07:53 +00:00
|
|
|
AuditBackends map[string]audit.Factory
|
2015-04-01 22:48:13 +00:00
|
|
|
CredentialBackends map[string]logical.Factory
|
|
|
|
LogicalBackends map[string]logical.Factory
|
2015-03-20 18:32:18 +00:00
|
|
|
|
2016-03-14 18:05:47 +00:00
|
|
|
ShutdownCh chan struct{}
|
|
|
|
SighupCh chan struct{}
|
2016-03-11 21:46:56 +00:00
|
|
|
|
2016-04-01 17:16:05 +00:00
|
|
|
meta.Meta
|
2016-03-11 21:46:56 +00:00
|
|
|
|
2016-03-14 15:18:02 +00:00
|
|
|
ReloadFuncs map[string][]server.ReloadFunc
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerCommand) Run(args []string) int {
|
2015-12-11 20:58:10 +00:00
|
|
|
var dev, verifyOnly bool
|
2015-03-12 22:21:11 +00:00
|
|
|
var configPath []string
|
2016-03-03 15:48:52 +00:00
|
|
|
var logLevel, devRootTokenID, devListenAddress string
|
2016-04-01 17:16:05 +00:00
|
|
|
flags := c.Meta.FlagSet("server", meta.FlagSetDefault)
|
2015-03-31 23:44:47 +00:00
|
|
|
flags.BoolVar(&dev, "dev", false, "")
|
2016-03-03 15:23:43 +00:00
|
|
|
flags.StringVar(&devRootTokenID, "dev-root-token-id", "", "")
|
2016-03-03 15:48:52 +00:00
|
|
|
flags.StringVar(&devListenAddress, "dev-listen-address", "", "")
|
2015-04-04 19:11:10 +00:00
|
|
|
flags.StringVar(&logLevel, "log-level", "info", "")
|
2015-12-11 20:58:10 +00:00
|
|
|
flags.BoolVar(&verifyOnly, "verify-only", false, "")
|
2015-03-12 22:21:11 +00:00
|
|
|
flags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
flags.Var((*sliceflag.StringFlag)(&configPath), "config", "config")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:48:52 +00:00
|
|
|
if os.Getenv("VAULT_DEV_ROOT_TOKEN_ID") != "" {
|
2016-03-03 15:23:43 +00:00
|
|
|
devRootTokenID = os.Getenv("VAULT_DEV_ROOT_TOKEN_ID")
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:48:52 +00:00
|
|
|
if os.Getenv("VAULT_DEV_LISTEN_ADDRESS") != "" {
|
|
|
|
devListenAddress = os.Getenv("VAULT_DEV_LISTEN_ADDRESS")
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
// Validation
|
2016-03-02 16:53:23 +00:00
|
|
|
if !dev {
|
|
|
|
switch {
|
|
|
|
case len(configPath) == 0:
|
|
|
|
c.Ui.Error("At least one config path must be specified with -config")
|
|
|
|
flags.Usage()
|
|
|
|
return 1
|
2016-03-03 15:23:43 +00:00
|
|
|
case devRootTokenID != "":
|
2016-03-02 16:53:23 +00:00
|
|
|
c.Ui.Error("Root token ID can only be specified with -dev")
|
|
|
|
flags.Usage()
|
|
|
|
return 1
|
2016-03-03 15:48:52 +00:00
|
|
|
case devListenAddress != "":
|
|
|
|
c.Ui.Error("Development address can only be specified with -dev")
|
|
|
|
flags.Usage()
|
|
|
|
return 1
|
2016-03-02 16:53:23 +00:00
|
|
|
}
|
2015-03-12 22:21:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load the configuration
|
2015-03-12 22:30:07 +00:00
|
|
|
var config *server.Config
|
2015-03-31 23:44:47 +00:00
|
|
|
if dev {
|
|
|
|
config = server.DevConfig()
|
2016-03-03 15:48:52 +00:00
|
|
|
if devListenAddress != "" {
|
|
|
|
config.Listeners[0].Config["address"] = devListenAddress
|
|
|
|
}
|
2015-03-31 23:44:47 +00:00
|
|
|
}
|
2015-03-12 22:30:07 +00:00
|
|
|
for _, path := range configPath {
|
|
|
|
current, err := server.LoadConfig(path)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error loading configuration from %s: %s", path, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
config = current
|
|
|
|
} else {
|
|
|
|
config = config.Merge(current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-13 00:39:28 +00:00
|
|
|
// Ensure at least one config was found.
|
|
|
|
if config == nil {
|
|
|
|
c.Ui.Error("No configuration files found.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-05-21 00:49:16 +00:00
|
|
|
// Ensure that a backend is provided
|
2015-05-19 03:47:57 +00:00
|
|
|
if config.Backend == nil {
|
2015-05-21 00:49:16 +00:00
|
|
|
c.Ui.Error("A physical backend must be specified")
|
2015-05-19 03:47:57 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-04-05 20:58:26 +00:00
|
|
|
// If mlockall(2) isn't supported, show a warning. We disable this
|
|
|
|
// in dev because it is quite scary to see when first using Vault.
|
|
|
|
if !dev && !mlock.Supported() {
|
2015-04-28 22:04:40 +00:00
|
|
|
c.Ui.Output("==> WARNING: mlock not supported on this system!\n")
|
2016-04-05 19:32:53 +00:00
|
|
|
c.Ui.Output(" An `mlockall(2)`-like syscall to prevent memory from being")
|
|
|
|
c.Ui.Output(" swapped to disk is not supported on this system. Running")
|
|
|
|
c.Ui.Output(" Vault on an mlockall(2) enabled system is much more secure.\n")
|
2015-04-28 22:04:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 19:06:41 +00:00
|
|
|
// Create a logger. We wrap it in a gated writer so that it doesn't
|
|
|
|
// start logging too early.
|
|
|
|
logGate := &gatedwriter.Writer{Writer: os.Stderr}
|
2015-04-04 19:11:10 +00:00
|
|
|
logger := log.New(&logutils.LevelFilter{
|
|
|
|
Levels: []logutils.LogLevel{
|
|
|
|
"TRACE", "DEBUG", "INFO", "WARN", "ERR"},
|
|
|
|
MinLevel: logutils.LogLevel(strings.ToUpper(logLevel)),
|
|
|
|
Writer: logGate,
|
|
|
|
}, "", log.LstdFlags)
|
2015-04-04 19:06:41 +00:00
|
|
|
|
2016-03-18 14:06:49 +00:00
|
|
|
if err := c.setupTelemetry(config); err != nil {
|
2015-12-17 21:38:17 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing telemetry: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:09:38 +00:00
|
|
|
// Initialize the backend
|
|
|
|
backend, err := physical.NewBackend(
|
2016-04-26 03:10:32 +00:00
|
|
|
config.Backend.Type, logger, config.Backend.Config)
|
2015-03-13 17:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing backend of type %s: %s",
|
|
|
|
config.Backend.Type, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:46:33 +00:00
|
|
|
infoKeys := make([]string, 0, 10)
|
|
|
|
info := make(map[string]string)
|
|
|
|
|
|
|
|
var seal vault.Seal = &vault.DefaultSeal{}
|
|
|
|
|
2016-04-28 18:04:31 +00:00
|
|
|
// Ensure that the seal finalizer is called, even if using verify-only
|
|
|
|
defer func() {
|
|
|
|
err = seal.Finalize()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error finalizing seals: %v", err))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-14 22:58:30 +00:00
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
Physical: backend,
|
2015-12-15 02:13:17 +00:00
|
|
|
AdvertiseAddr: config.Backend.AdvertiseAddr,
|
2015-12-14 22:58:30 +00:00
|
|
|
HAPhysical: nil,
|
2016-04-04 17:46:33 +00:00
|
|
|
Seal: seal,
|
2015-12-14 22:58:30 +00:00
|
|
|
AuditBackends: c.AuditBackends,
|
|
|
|
CredentialBackends: c.CredentialBackends,
|
|
|
|
LogicalBackends: c.LogicalBackends,
|
|
|
|
Logger: logger,
|
|
|
|
DisableCache: config.DisableCache,
|
|
|
|
DisableMlock: config.DisableMlock,
|
|
|
|
MaxLeaseTTL: config.MaxLeaseTTL,
|
|
|
|
DefaultLeaseTTL: config.DefaultLeaseTTL,
|
|
|
|
}
|
2015-12-11 20:58:10 +00:00
|
|
|
|
|
|
|
// Initialize the separate HA physical backend, if it exists
|
2016-02-02 20:09:58 +00:00
|
|
|
var ok bool
|
2015-12-11 20:58:10 +00:00
|
|
|
if config.HABackend != nil {
|
2015-12-14 22:58:30 +00:00
|
|
|
habackend, err := physical.NewBackend(
|
2016-04-26 03:10:32 +00:00
|
|
|
config.HABackend.Type, logger, config.HABackend.Config)
|
2015-12-11 20:58:10 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing backend of type %s: %s",
|
|
|
|
config.HABackend.Type, err))
|
|
|
|
return 1
|
|
|
|
}
|
2015-12-14 22:58:30 +00:00
|
|
|
|
|
|
|
if coreConfig.HAPhysical, ok = habackend.(physical.HABackend); !ok {
|
2015-12-11 20:58:10 +00:00
|
|
|
c.Ui.Error("Specified HA backend does not support HA")
|
|
|
|
return 1
|
|
|
|
}
|
2015-12-14 22:58:30 +00:00
|
|
|
coreConfig.AdvertiseAddr = config.HABackend.AdvertiseAddr
|
2016-02-02 20:09:58 +00:00
|
|
|
} else {
|
|
|
|
if coreConfig.HAPhysical, ok = backend.(physical.HABackend); ok {
|
|
|
|
coreConfig.AdvertiseAddr = config.Backend.AdvertiseAddr
|
|
|
|
}
|
2015-12-11 20:58:10 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 02:22:55 +00:00
|
|
|
if envAA := os.Getenv("VAULT_ADVERTISE_ADDR"); envAA != "" {
|
|
|
|
coreConfig.AdvertiseAddr = envAA
|
|
|
|
}
|
|
|
|
|
2016-04-08 18:55:20 +00:00
|
|
|
// Attempt to detect the advertise address, if possible
|
2015-12-15 02:13:17 +00:00
|
|
|
var detect physical.AdvertiseDetect
|
|
|
|
if coreConfig.HAPhysical != nil {
|
|
|
|
detect, ok = coreConfig.HAPhysical.(physical.AdvertiseDetect)
|
|
|
|
} else {
|
|
|
|
detect, ok = coreConfig.Physical.(physical.AdvertiseDetect)
|
|
|
|
}
|
|
|
|
if ok && coreConfig.AdvertiseAddr == "" {
|
2015-05-02 22:57:40 +00:00
|
|
|
advertise, err := c.detectAdvertise(detect, config)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error detecting advertise address: %s", err))
|
|
|
|
} else if advertise == "" {
|
|
|
|
c.Ui.Error("Failed to detect advertise address.")
|
|
|
|
} else {
|
2015-12-14 22:58:30 +00:00
|
|
|
coreConfig.AdvertiseAddr = advertise
|
2015-05-02 22:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-13 17:09:38 +00:00
|
|
|
// Initialize the core
|
2016-04-04 14:44:22 +00:00
|
|
|
core, newCoreError := vault.NewCore(coreConfig)
|
|
|
|
if newCoreError != nil {
|
|
|
|
if !errwrap.ContainsType(newCoreError, new(vault.NonFatalError)) {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing core: %s", newCoreError))
|
|
|
|
return 1
|
|
|
|
}
|
2015-04-28 04:29:40 +00:00
|
|
|
}
|
2015-03-13 17:09:38 +00:00
|
|
|
|
2015-03-31 23:44:47 +00:00
|
|
|
// If we're in dev mode, then initialize the core
|
|
|
|
if dev {
|
2016-03-03 15:23:43 +00:00
|
|
|
init, err := c.enableDev(core, devRootTokenID)
|
2015-03-31 23:44:47 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing dev mode: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-10-22 07:48:46 +00:00
|
|
|
export := "export"
|
|
|
|
quote := "'"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
export = "set"
|
|
|
|
quote = ""
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:44:47 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf(
|
2015-04-28 22:04:40 +00:00
|
|
|
"==> WARNING: Dev mode is enabled!\n\n"+
|
2015-03-31 23:44:47 +00:00
|
|
|
"In this mode, Vault is completely in-memory and unsealed.\n"+
|
|
|
|
"Vault is configured to only have a single unseal key. The root\n"+
|
|
|
|
"token has already been authenticated with the CLI, so you can\n"+
|
|
|
|
"immediately begin using the Vault CLI.\n\n"+
|
2015-04-06 17:28:17 +00:00
|
|
|
"The only step you need to take is to set the following\n"+
|
2015-05-07 22:32:40 +00:00
|
|
|
"environment variables:\n\n"+
|
2016-03-03 23:10:14 +00:00
|
|
|
" "+export+" VAULT_ADDR="+quote+"http://"+config.Listeners[0].Config["address"]+quote+"\n\n"+
|
2015-03-31 23:44:47 +00:00
|
|
|
"The unseal key and root token are reproduced below in case you\n"+
|
|
|
|
"want to seal/unseal the Vault or play with authentication.\n\n"+
|
2015-04-04 19:06:41 +00:00
|
|
|
"Unseal Key: %s\nRoot Token: %s\n",
|
2015-03-31 23:44:47 +00:00
|
|
|
hex.EncodeToString(init.SecretShares[0]),
|
|
|
|
init.RootToken,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2015-04-04 19:06:41 +00:00
|
|
|
// Compile server information for output later
|
|
|
|
info["backend"] = config.Backend.Type
|
2015-04-04 19:11:10 +00:00
|
|
|
info["log level"] = logLevel
|
2015-04-28 22:11:39 +00:00
|
|
|
info["mlock"] = fmt.Sprintf(
|
|
|
|
"supported: %v, enabled: %v",
|
|
|
|
mlock.Supported(), !config.DisableMlock)
|
|
|
|
infoKeys = append(infoKeys, "log level", "mlock", "backend")
|
2015-04-04 19:06:41 +00:00
|
|
|
|
2015-12-11 20:58:10 +00:00
|
|
|
if config.HABackend != nil {
|
|
|
|
info["HA backend"] = config.HABackend.Type
|
2015-12-14 22:58:30 +00:00
|
|
|
info["advertise address"] = coreConfig.AdvertiseAddr
|
2015-12-11 20:58:10 +00:00
|
|
|
infoKeys = append(infoKeys, "HA backend", "advertise address")
|
|
|
|
} else {
|
|
|
|
// If the backend supports HA, then note it
|
2015-12-14 22:58:30 +00:00
|
|
|
if coreConfig.HAPhysical != nil {
|
2015-12-11 20:58:10 +00:00
|
|
|
info["backend"] += " (HA available)"
|
2015-12-14 22:58:30 +00:00
|
|
|
info["advertise address"] = coreConfig.AdvertiseAddr
|
2015-12-11 20:58:10 +00:00
|
|
|
infoKeys = append(infoKeys, "advertise address")
|
|
|
|
}
|
2015-04-17 17:50:37 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 02:55:17 +00:00
|
|
|
// If the backend supports service discovery, run service discovery
|
|
|
|
if coreConfig.HAPhysical != nil {
|
|
|
|
sd, ok := coreConfig.HAPhysical.(physical.ServiceDiscovery)
|
|
|
|
if ok {
|
2016-04-25 20:46:28 +00:00
|
|
|
if err := sd.RunServiceDiscovery(c.ShutdownCh, coreConfig.AdvertiseAddr); err != nil {
|
2016-04-23 02:55:17 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing service discovery: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:30:07 +00:00
|
|
|
// Initialize the listeners
|
2015-03-13 17:09:38 +00:00
|
|
|
lns := make([]net.Listener, 0, len(config.Listeners))
|
2015-04-04 19:06:41 +00:00
|
|
|
for i, lnConfig := range config.Listeners {
|
2016-03-11 22:28:03 +00:00
|
|
|
ln, props, reloadFunc, err := server.NewListener(lnConfig.Type, lnConfig.Config)
|
2015-03-13 17:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf(
|
|
|
|
"Error initializing listener of type %s: %s",
|
|
|
|
lnConfig.Type, err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-04-04 19:06:41 +00:00
|
|
|
// Store the listener props for output later
|
|
|
|
key := fmt.Sprintf("listener %d", i+1)
|
|
|
|
propsList := make([]string, 0, len(props))
|
|
|
|
for k, v := range props {
|
|
|
|
propsList = append(propsList, fmt.Sprintf(
|
|
|
|
"%s: %q", k, v))
|
|
|
|
}
|
|
|
|
sort.Strings(propsList)
|
|
|
|
infoKeys = append(infoKeys, key)
|
|
|
|
info[key] = fmt.Sprintf(
|
|
|
|
"%s (%s)", lnConfig.Type, strings.Join(propsList, ", "))
|
|
|
|
|
2015-03-13 17:09:38 +00:00
|
|
|
lns = append(lns, ln)
|
2016-03-10 02:40:46 +00:00
|
|
|
|
2016-03-11 22:28:03 +00:00
|
|
|
if reloadFunc != nil {
|
2016-03-14 15:18:02 +00:00
|
|
|
relSlice := c.ReloadFuncs["listener|"+lnConfig.Type]
|
|
|
|
relSlice = append(relSlice, reloadFunc)
|
|
|
|
c.ReloadFuncs["listener|"+lnConfig.Type] = relSlice
|
2016-03-11 21:46:56 +00:00
|
|
|
}
|
2015-03-13 17:09:38 +00:00
|
|
|
}
|
|
|
|
|
2015-11-09 18:52:55 +00:00
|
|
|
infoKeys = append(infoKeys, "version")
|
|
|
|
info["version"] = version.GetVersion().String()
|
|
|
|
|
2015-04-04 19:06:41 +00:00
|
|
|
// Server configuration output
|
2016-03-30 16:31:47 +00:00
|
|
|
padding := 24
|
|
|
|
sort.Strings(infoKeys)
|
2015-04-04 19:06:41 +00:00
|
|
|
c.Ui.Output("==> Vault server configuration:\n")
|
|
|
|
for _, k := range infoKeys {
|
|
|
|
c.Ui.Output(fmt.Sprintf(
|
|
|
|
"%s%s: %s",
|
|
|
|
strings.Repeat(" ", padding-len(k)),
|
|
|
|
strings.Title(k),
|
|
|
|
info[k]))
|
|
|
|
}
|
|
|
|
c.Ui.Output("")
|
|
|
|
|
2016-02-02 22:47:02 +00:00
|
|
|
if verifyOnly {
|
|
|
|
for _, listener := range lns {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the HTTP server
|
|
|
|
server := &http.Server{}
|
|
|
|
server.Handler = vaulthttp.Handler(core)
|
|
|
|
for _, ln := range lns {
|
|
|
|
go server.Serve(ln)
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:44:22 +00:00
|
|
|
if newCoreError != nil {
|
|
|
|
c.Ui.Output("==> Warning:\n\nNon-fatal error during initialization; check the logs for more information.")
|
|
|
|
c.Ui.Output("")
|
|
|
|
}
|
|
|
|
|
2015-04-04 19:06:41 +00:00
|
|
|
// Output the header that the server has started
|
|
|
|
c.Ui.Output("==> Vault server started! Log data will stream in below:\n")
|
|
|
|
|
|
|
|
// Release the log gate.
|
|
|
|
logGate.Flush()
|
|
|
|
|
2015-06-18 01:24:56 +00:00
|
|
|
// Wait for shutdown
|
2016-03-11 21:46:56 +00:00
|
|
|
shutdownTriggered := false
|
2016-03-11 22:01:26 +00:00
|
|
|
for !shutdownTriggered {
|
2016-03-11 21:46:56 +00:00
|
|
|
select {
|
|
|
|
case <-c.ShutdownCh:
|
|
|
|
c.Ui.Output("==> Vault shutdown triggered")
|
|
|
|
if err := core.Shutdown(); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error with core shutdown: %s", err))
|
|
|
|
}
|
|
|
|
shutdownTriggered = true
|
|
|
|
case <-c.SighupCh:
|
|
|
|
c.Ui.Output("==> Vault reload triggered")
|
|
|
|
if err := c.Reload(configPath); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error(s) were encountered during reload: %s", err))
|
|
|
|
}
|
|
|
|
}
|
2015-06-18 01:24:56 +00:00
|
|
|
}
|
2016-04-14 01:12:58 +00:00
|
|
|
|
|
|
|
for _, listener := range lns {
|
|
|
|
listener.Close()
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-03-02 16:53:23 +00:00
|
|
|
func (c *ServerCommand) enableDev(core *vault.Core, rootTokenID string) (*vault.InitResult, error) {
|
2015-03-31 23:44:47 +00:00
|
|
|
// Initialize it with a basic single key
|
|
|
|
init, err := core.Initialize(&vault.SealConfig{
|
|
|
|
SecretShares: 1,
|
|
|
|
SecretThreshold: 1,
|
2016-04-04 14:44:22 +00:00
|
|
|
}, nil)
|
2015-03-31 23:44:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the key so that it can be zeroed
|
|
|
|
key := make([]byte, len(init.SecretShares[0]))
|
|
|
|
copy(key, init.SecretShares[0])
|
|
|
|
|
|
|
|
// Unseal the core
|
|
|
|
unsealed, err := core.Unseal(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !unsealed {
|
|
|
|
return nil, fmt.Errorf("failed to unseal Vault for dev mode")
|
|
|
|
}
|
|
|
|
|
2016-03-02 16:53:23 +00:00
|
|
|
if rootTokenID != "" {
|
|
|
|
req := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
ClientToken: init.RootToken,
|
|
|
|
Path: "auth/token/create",
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"id": rootTokenID,
|
|
|
|
"policies": []string{"root"},
|
|
|
|
"no_parent": true,
|
|
|
|
"no_default_policy": true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create root token with ID %s: %s", rootTokenID, err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
return nil, fmt.Errorf("nil response when creating root token with ID %s", rootTokenID)
|
|
|
|
}
|
|
|
|
if resp.Auth == nil {
|
|
|
|
return nil, fmt.Errorf("nil auth when creating root token with ID %s", rootTokenID)
|
|
|
|
}
|
|
|
|
|
|
|
|
init.RootToken = resp.Auth.ClientToken
|
|
|
|
|
|
|
|
req.Path = "auth/token/revoke-self"
|
|
|
|
req.Data = nil
|
|
|
|
resp, err = core.HandleRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to revoke initial root token: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 23:44:47 +00:00
|
|
|
// Set the token
|
2016-04-01 20:02:18 +00:00
|
|
|
tokenHelper, err := c.TokenHelper()
|
2015-03-31 23:44:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := tokenHelper.Store(init.RootToken); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return init, nil
|
|
|
|
}
|
|
|
|
|
2015-05-02 22:57:40 +00:00
|
|
|
// detectAdvertise is used to attempt advertise address detection
|
|
|
|
func (c *ServerCommand) detectAdvertise(detect physical.AdvertiseDetect,
|
|
|
|
config *server.Config) (string, error) {
|
|
|
|
// Get the hostname
|
|
|
|
host, err := detect.DetectHostAddr()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-12-22 20:40:36 +00:00
|
|
|
// set [] for ipv6 addresses
|
|
|
|
if strings.Contains(host, ":") && !strings.Contains(host, "]") {
|
|
|
|
host = "[" + host + "]"
|
|
|
|
}
|
|
|
|
|
2015-05-02 22:57:40 +00:00
|
|
|
// Default the port and scheme
|
|
|
|
scheme := "https"
|
|
|
|
port := 8200
|
|
|
|
|
|
|
|
// Attempt to detect overrides
|
|
|
|
for _, list := range config.Listeners {
|
|
|
|
// Only attempt TCP
|
|
|
|
if list.Type != "tcp" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if TLS is disabled
|
2015-05-15 20:41:30 +00:00
|
|
|
if val, ok := list.Config["tls_disable"]; ok {
|
|
|
|
disable, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("tls_disable: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if disable {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
2015-05-02 22:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for address override
|
|
|
|
addr, ok := list.Config["address"]
|
|
|
|
if !ok {
|
|
|
|
addr = "127.0.0.1:8200"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for localhost
|
|
|
|
hostStr, portStr, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if hostStr == "127.0.0.1" {
|
|
|
|
host = hostStr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for custom port
|
|
|
|
listPort, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
port = listPort
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a URL
|
|
|
|
url := &url.URL{
|
|
|
|
Scheme: scheme,
|
|
|
|
Host: fmt.Sprintf("%s:%d", host, port),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the URL string
|
|
|
|
return url.String(), nil
|
|
|
|
}
|
|
|
|
|
2016-03-18 14:06:49 +00:00
|
|
|
// setupTelemetry is used to setup the telemetry sub-systems
|
|
|
|
func (c *ServerCommand) setupTelemetry(config *server.Config) error {
|
2015-04-15 01:44:09 +00:00
|
|
|
/* Setup telemetry
|
|
|
|
Aggregate on 10 second intervals for 1 minute. Expose the
|
|
|
|
metrics over stderr when there is a SIGUSR1 received.
|
|
|
|
*/
|
|
|
|
inm := metrics.NewInmemSink(10*time.Second, time.Minute)
|
|
|
|
metrics.DefaultInmemSignal(inm)
|
2015-07-14 22:27:18 +00:00
|
|
|
|
|
|
|
var telConfig *server.Telemetry
|
|
|
|
if config.Telemetry == nil {
|
|
|
|
telConfig = &server.Telemetry{}
|
|
|
|
} else {
|
|
|
|
telConfig = config.Telemetry
|
|
|
|
}
|
|
|
|
|
2015-04-15 01:44:09 +00:00
|
|
|
metricsConf := metrics.DefaultConfig("vault")
|
2015-07-14 22:27:18 +00:00
|
|
|
metricsConf.EnableHostname = !telConfig.DisableHostname
|
2015-04-15 01:44:09 +00:00
|
|
|
|
|
|
|
// Configure the statsite sink
|
|
|
|
var fanout metrics.FanoutSink
|
2015-07-14 22:27:18 +00:00
|
|
|
if telConfig.StatsiteAddr != "" {
|
|
|
|
sink, err := metrics.NewStatsiteSink(telConfig.StatsiteAddr)
|
2015-04-15 01:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fanout = append(fanout, sink)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the statsd sink
|
2015-07-14 22:27:18 +00:00
|
|
|
if telConfig.StatsdAddr != "" {
|
|
|
|
sink, err := metrics.NewStatsdSink(telConfig.StatsdAddr)
|
2015-04-15 01:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fanout = append(fanout, sink)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the global sink
|
|
|
|
if len(fanout) > 0 {
|
|
|
|
fanout = append(fanout, inm)
|
|
|
|
metrics.NewGlobal(metricsConf, fanout)
|
|
|
|
} else {
|
|
|
|
metricsConf.EnableHostname = false
|
|
|
|
metrics.NewGlobal(metricsConf, inm)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-11 21:46:56 +00:00
|
|
|
func (c *ServerCommand) Reload(configPath []string) error {
|
|
|
|
// Read the new config
|
|
|
|
var config *server.Config
|
|
|
|
for _, path := range configPath {
|
|
|
|
current, err := server.LoadConfig(path)
|
|
|
|
if err != nil {
|
|
|
|
retErr := fmt.Errorf("Error loading configuration from %s: %s", path, err)
|
|
|
|
c.Ui.Error(retErr.Error())
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
config = current
|
|
|
|
} else {
|
|
|
|
config = config.Merge(current)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure at least one config was found.
|
|
|
|
if config == nil {
|
|
|
|
retErr := fmt.Errorf("No configuration files found")
|
|
|
|
c.Ui.Error(retErr.Error())
|
|
|
|
return retErr
|
|
|
|
}
|
|
|
|
|
|
|
|
var reloadErrors *multierror.Error
|
|
|
|
// Call reload on the listeners. This will call each listener with each
|
2016-03-14 15:18:02 +00:00
|
|
|
// config block, but they verify the address.
|
2016-03-11 21:46:56 +00:00
|
|
|
for _, lnConfig := range config.Listeners {
|
2016-03-14 15:18:02 +00:00
|
|
|
for _, relFunc := range c.ReloadFuncs["listener|"+lnConfig.Type] {
|
2016-03-11 22:28:03 +00:00
|
|
|
if err := relFunc(lnConfig.Config); err != nil {
|
|
|
|
retErr := fmt.Errorf("Error encountered reloading configuration: %s", err)
|
2016-03-11 21:46:56 +00:00
|
|
|
reloadErrors = multierror.Append(retErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return reloadErrors.ErrorOrNil()
|
|
|
|
}
|
|
|
|
|
2015-03-12 22:21:11 +00:00
|
|
|
func (c *ServerCommand) Synopsis() string {
|
|
|
|
return "Start a Vault server"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: vault server [options]
|
|
|
|
|
|
|
|
Start a Vault server.
|
|
|
|
|
|
|
|
This command starts a Vault server that responds to API requests.
|
|
|
|
Vault will start in a "sealed" state. The Vault must be unsealed
|
|
|
|
with "vault unseal" or the API before this server can respond to requests.
|
|
|
|
This must be done for every server.
|
|
|
|
|
|
|
|
If the server is being started against a storage backend that has
|
|
|
|
brand new (no existing Vault data in it), it must be initialized with
|
|
|
|
"vault init" or the API first.
|
|
|
|
|
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2016-03-03 15:48:52 +00:00
|
|
|
-config=<path> Path to the configuration file or directory. This can
|
|
|
|
be specified multiple times. If it is a directory,
|
|
|
|
all files with a ".hcl" or ".json" suffix will be
|
|
|
|
loaded.
|
|
|
|
|
|
|
|
-dev Enables Dev mode. In this mode, Vault is completely
|
|
|
|
in-memory and unsealed. Do not run the Dev server in
|
|
|
|
production!
|
2015-03-12 22:21:11 +00:00
|
|
|
|
2016-03-03 15:48:52 +00:00
|
|
|
-dev-root-token-id="" If set, the root token returned in Dev mode will have
|
|
|
|
the given ID. This *only* has an effect when running
|
|
|
|
in Dev mode. Can also be specified with the
|
|
|
|
VAULT_DEV_ROOT_TOKEN_ID environment variable.
|
2015-05-31 15:52:45 +00:00
|
|
|
|
2016-03-03 15:48:52 +00:00
|
|
|
-dev-listen-address="" If set, this overrides the normal Dev mode listen
|
|
|
|
address of "127.0.0.1:8200". Can also be specified
|
|
|
|
with the VAULT_DEV_LISTEN_ADDRESS environment
|
|
|
|
variable.
|
2015-04-04 19:11:10 +00:00
|
|
|
|
2016-03-03 15:48:52 +00:00
|
|
|
-log-level=info Log verbosity. Defaults to "info", will be output to
|
|
|
|
stderr. Supported values: "trace", "debug", "info",
|
|
|
|
"warn", "err"
|
2015-03-12 22:21:11 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
2016-03-14 18:05:47 +00:00
|
|
|
|
|
|
|
// MakeShutdownCh returns a channel that can be used for shutdown
|
|
|
|
// notifications for commands. This channel will send a message for every
|
2016-04-15 17:03:22 +00:00
|
|
|
// SIGINT or SIGTERM received.
|
2016-03-14 18:05:47 +00:00
|
|
|
func MakeShutdownCh() chan struct{} {
|
|
|
|
resultCh := make(chan struct{})
|
|
|
|
|
2016-04-15 17:03:22 +00:00
|
|
|
shutdownCh := make(chan os.Signal, 4)
|
|
|
|
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)
|
2016-03-14 18:05:47 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2016-04-15 17:03:22 +00:00
|
|
|
<-shutdownCh
|
2016-03-14 18:05:47 +00:00
|
|
|
resultCh <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return resultCh
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeSighupCh returns a channel that can be used for SIGHUP
|
|
|
|
// reloading. This channel will send a message for every
|
|
|
|
// SIGHUP received.
|
|
|
|
func MakeSighupCh() chan struct{} {
|
|
|
|
resultCh := make(chan struct{})
|
|
|
|
|
|
|
|
signalCh := make(chan os.Signal, 4)
|
2016-05-03 18:30:58 +00:00
|
|
|
signal.Notify(signalCh, syscall.SIGHUP)
|
2016-03-14 18:05:47 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-signalCh
|
|
|
|
resultCh <- struct{}{}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return resultCh
|
|
|
|
}
|