2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-03-01 01:09:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/hclutil"
|
2019-01-09 00:48:57 +00:00
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
2018-03-01 01:09:21 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the CLI configuration for Vault that can be specified via
|
|
|
|
// a `$HOME/.vault` file which is HCL-formatted (therefore HCL or JSON).
|
|
|
|
type DefaultConfig struct {
|
|
|
|
// TokenHelper is the executable/command that is executed for storing
|
|
|
|
// and retrieving the authentication token for the Vault CLI. If this
|
|
|
|
// is not specified, then vault's internal token store will be used, which
|
|
|
|
// stores the token on disk unencrypted.
|
|
|
|
TokenHelper string `hcl:"token_helper"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config loads the configuration and returns it. If the configuration
|
|
|
|
// is already loaded, it is returned.
|
|
|
|
func Config() (*DefaultConfig, error) {
|
|
|
|
var err error
|
|
|
|
config, err := LoadConfig("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func LoadConfig(path string) (*DefaultConfig, error) {
|
|
|
|
if path == "" {
|
|
|
|
path = DefaultConfigPath
|
|
|
|
}
|
|
|
|
if v := os.Getenv(ConfigPathEnv); v != "" {
|
|
|
|
path = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: requires HOME env var to be set
|
|
|
|
path, err := homedir.Expand(path)
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error expanding config path %q: %w", path, err)
|
2018-03-01 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-03-19 23:40:51 +00:00
|
|
|
conf, err := ParseConfig(string(contents))
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return nil, fmt.Errorf("error parsing config file at %q: %w; ensure that the file is valid; Ansible Vault is known to conflict with it.", path, err)
|
2018-03-19 23:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
2018-03-01 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the given configuration as a string.
|
|
|
|
func ParseConfig(contents string) (*DefaultConfig, error) {
|
|
|
|
root, err := hcl.Parse(contents)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top-level item should be the object list
|
|
|
|
list, ok := root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
2018-04-05 15:49:21 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse config; does not contain a root object")
|
2018-03-01 01:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valid := []string{
|
|
|
|
"token_helper",
|
|
|
|
}
|
2018-06-12 16:38:08 +00:00
|
|
|
if err := hclutil.CheckHCLKeys(list, valid); err != nil {
|
2018-03-01 01:09:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var c DefaultConfig
|
|
|
|
if err := hcl.DecodeObject(&c, list); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &c, nil
|
|
|
|
}
|