2016-04-01 20:02:18 +00:00
|
|
|
package command
|
2015-03-30 16:21:59 +00:00
|
|
|
|
2015-03-30 17:25:24 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2016-03-10 18:36:17 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2018-03-01 01:09:21 +00:00
|
|
|
"github.com/hashicorp/vault/command/config"
|
2015-03-30 17:25:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultConfigPath is the default path to the configuration file
|
|
|
|
DefaultConfigPath = "~/.vault"
|
|
|
|
|
|
|
|
// ConfigPathEnv is the environment variable that can be used to
|
|
|
|
// override where the Vault configuration is.
|
|
|
|
ConfigPathEnv = "VAULT_CONFIG_PATH"
|
|
|
|
)
|
|
|
|
|
2015-03-30 16:21:59 +00:00
|
|
|
// Config is the CLI configuration for Vault that can be specified via
|
|
|
|
// a `$HOME/.vault` file which is HCL-formatted (therefore HCL or JSON).
|
2016-04-01 20:02:18 +00:00
|
|
|
type DefaultConfig struct {
|
2015-03-30 16:21:59 +00:00
|
|
|
// TokenHelper is the executable/command that is executed for storing
|
|
|
|
// and retrieving the authentication token for the Vault CLI. If this
|
2015-12-14 21:23:04 +00:00
|
|
|
// is not specified, then vault's internal token store will be used, which
|
|
|
|
// stores the token on disk unencrypted.
|
2015-03-30 16:21:59 +00:00
|
|
|
TokenHelper string `hcl:"token_helper"`
|
|
|
|
}
|
2015-03-30 17:25:24 +00:00
|
|
|
|
2016-04-01 20:02:18 +00:00
|
|
|
// Config loads the configuration and returns it. If the configuration
|
|
|
|
// is already loaded, it is returned.
|
2018-03-01 01:09:21 +00:00
|
|
|
//
|
|
|
|
// Config just calls into config.Config for backwards compatibility purposes.
|
|
|
|
// Use config.Config instead.
|
2016-04-01 20:02:18 +00:00
|
|
|
func Config() (*DefaultConfig, error) {
|
2018-03-01 01:09:21 +00:00
|
|
|
conf, err := config.Config()
|
|
|
|
return (*DefaultConfig)(conf), err
|
2016-04-01 20:02:18 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 17:25:24 +00:00
|
|
|
// LoadConfig reads the configuration from the given path. If path is
|
|
|
|
// empty, then the default path will be used, or the environment variable
|
|
|
|
// if set.
|
2018-03-01 01:09:21 +00:00
|
|
|
//
|
|
|
|
// LoadConfig just calls into config.LoadConfig for backwards compatibility
|
|
|
|
// purposes. Use config.LoadConfig instead.
|
2016-04-01 20:02:18 +00:00
|
|
|
func LoadConfig(path string) (*DefaultConfig, error) {
|
2018-03-01 01:09:21 +00:00
|
|
|
conf, err := config.LoadConfig(path)
|
|
|
|
return (*DefaultConfig)(conf), err
|
2016-03-10 18:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the given configuration as a string.
|
2018-03-01 01:09:21 +00:00
|
|
|
//
|
|
|
|
// ParseConfig just calls into config.ParseConfig for backwards compatibility
|
|
|
|
// purposes. Use config.ParseConfig instead.
|
2016-04-01 20:02:18 +00:00
|
|
|
func ParseConfig(contents string) (*DefaultConfig, error) {
|
2018-03-01 01:09:21 +00:00
|
|
|
conf, err := config.ParseConfig(contents)
|
|
|
|
return (*DefaultConfig)(conf), err
|
2016-03-10 18:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkHCLKeys(node ast.Node, valid []string) error {
|
|
|
|
var list *ast.ObjectList
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.ObjectList:
|
|
|
|
list = n
|
|
|
|
case *ast.ObjectType:
|
|
|
|
list = n.List
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("cannot check HCL keys of type %T", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
validMap := make(map[string]struct{}, len(valid))
|
|
|
|
for _, v := range valid {
|
|
|
|
validMap[v] = struct{}{}
|
|
|
|
}
|
2015-03-30 17:25:24 +00:00
|
|
|
|
2016-03-10 18:36:17 +00:00
|
|
|
var result error
|
|
|
|
for _, item := range list.Items {
|
|
|
|
key := item.Keys[0].Token.Value().(string)
|
|
|
|
if _, ok := validMap[key]; !ok {
|
2018-04-05 15:49:21 +00:00
|
|
|
result = multierror.Append(result, fmt.Errorf("invalid key %q on line %d", key, item.Assign.Line))
|
2015-03-30 17:55:41 +00:00
|
|
|
}
|
2015-03-30 17:25:24 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 18:36:17 +00:00
|
|
|
return result
|
2015-03-30 17:25:24 +00:00
|
|
|
}
|