Remove config from Meta; it's only used right now with the token helper.

This commit is contained in:
Jeff Mitchell 2016-04-01 16:02:18 -04:00
parent 48da40964c
commit 16c8f0b5ad
9 changed files with 29 additions and 46 deletions

View File

@ -52,7 +52,7 @@ func (c *AuthCommand) Run(args []string) int {
args = flags.Args() args = flags.Args()
tokenHelper, err := c.TokenHelper(&c.Meta) tokenHelper, err := c.TokenHelper()
if err != nil { if err != nil {
c.Ui.Error(fmt.Sprintf( c.Ui.Error(fmt.Sprintf(
"Error initializing token helper: %s\n\n"+ "Error initializing token helper: %s\n\n"+

View File

@ -69,7 +69,7 @@ func TestAuth_token(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
helper, err := c.TokenHelper(&c.Meta) helper, err := c.TokenHelper()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
@ -166,7 +166,7 @@ func TestAuth_method(t *testing.T) {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
} }
helper, err := c.TokenHelper(&c.Meta) helper, err := c.TokenHelper()
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -6,8 +6,6 @@ import (
"github.com/hashicorp/vault/api" "github.com/hashicorp/vault/api"
) )
const FixturePath = "./test-fixtures"
func testClient(t *testing.T, addr string, token string) *api.Client { func testClient(t *testing.T, addr string, token string) *api.Client {
config := api.DefaultConfig() config := api.DefaultConfig()
config.Address = addr config.Address = addr

View File

@ -1,4 +1,4 @@
package meta package command
import ( import (
"fmt" "fmt"
@ -22,7 +22,7 @@ const (
// Config is the CLI configuration for Vault that can be specified via // Config is the CLI configuration for Vault that can be specified via
// a `$HOME/.vault` file which is HCL-formatted (therefore HCL or JSON). // a `$HOME/.vault` file which is HCL-formatted (therefore HCL or JSON).
type Config struct { type DefaultConfig struct {
// TokenHelper is the executable/command that is executed for storing // TokenHelper is the executable/command that is executed for storing
// and retrieving the authentication token for the Vault CLI. If this // and retrieving the authentication token for the Vault CLI. If this
// is not specified, then vault's internal token store will be used, which // is not specified, then vault's internal token store will be used, which
@ -30,10 +30,22 @@ type Config struct {
TokenHelper string `hcl:"token_helper"` 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 // LoadConfig reads the configuration from the given path. If path is
// empty, then the default path will be used, or the environment variable // empty, then the default path will be used, or the environment variable
// if set. // if set.
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*DefaultConfig, error) {
if path == "" { if path == "" {
path = DefaultConfigPath path = DefaultConfigPath
} }
@ -55,7 +67,7 @@ func LoadConfig(path string) (*Config, error) {
} }
// ParseConfig parses the given configuration as a string. // ParseConfig parses the given configuration as a string.
func ParseConfig(contents string) (*Config, error) { func ParseConfig(contents string) (*DefaultConfig, error) {
root, err := hcl.Parse(contents) root, err := hcl.Parse(contents)
if err != nil { if err != nil {
return nil, err return nil, err
@ -74,7 +86,7 @@ func ParseConfig(contents string) (*Config, error) {
return nil, err return nil, err
} }
var c Config var c DefaultConfig
if err := hcl.DecodeObject(&c, list); err != nil { if err := hcl.DecodeObject(&c, list); err != nil {
return nil, err return nil, err
} }

View File

@ -1,4 +1,4 @@
package meta package command
import ( import (
"path/filepath" "path/filepath"
@ -15,7 +15,7 @@ func TestLoadConfig(t *testing.T) {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
expected := &Config{ expected := &DefaultConfig{
TokenHelper: "foo", TokenHelper: "foo",
} }
if !reflect.DeepEqual(expected, config) { if !reflect.DeepEqual(expected, config) {

View File

@ -424,7 +424,7 @@ func (c *ServerCommand) enableDev(core *vault.Core, rootTokenID string) (*vault.
} }
// Set the token // Set the token
tokenHelper, err := c.TokenHelper(&c.Meta) tokenHelper, err := c.TokenHelper()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,13 +1,10 @@
package command package command
import ( import "github.com/hashicorp/vault/command/token"
"github.com/hashicorp/vault/command/token"
"github.com/hashicorp/vault/meta"
)
// DefaultTokenHelper returns the token helper that is configured for Vault. // DefaultTokenHelper returns the token helper that is configured for Vault.
func DefaultTokenHelper(m *meta.Meta) (token.TokenHelper, error) { func DefaultTokenHelper() (token.TokenHelper, error) {
config, err := m.Config() config, err := LoadConfig("")
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -24,7 +24,7 @@ import (
// default FlagSet returned by Meta.FlagSet. // default FlagSet returned by Meta.FlagSet.
type FlagSetFlags uint type FlagSetFlags uint
type TokenHelperFunc func(*Meta) (token.TokenHelper, error) type TokenHelperFunc func() (token.TokenHelper, error)
const ( const (
FlagSetNone FlagSetFlags = 0 FlagSetNone FlagSetFlags = 0
@ -39,8 +39,7 @@ type Meta struct {
Ui cli.Ui Ui cli.Ui
// The things below can be set, but aren't common // The things below can be set, but aren't common
ForceAddress string // Address to force for API clients ForceAddress string // Address to force for API clients
ForceConfig *Config // Force a config, don't load from disk
// These are set by the command line flags. // These are set by the command line flags.
flagAddress string flagAddress string
@ -50,10 +49,6 @@ type Meta struct {
flagClientKey string flagClientKey string
flagInsecure bool flagInsecure bool
// These are internal and shouldn't be modified or access by anyone
// except Meta.
config *Config
// Queried if no token can be found // Queried if no token can be found
TokenHelper TokenHelperFunc TokenHelper TokenHelperFunc
} }
@ -127,7 +122,7 @@ func (m *Meta) Client() (*api.Client, error) {
if token == "" { if token == "" {
if m.TokenHelper != nil { if m.TokenHelper != nil {
// If we have a token, then set that // If we have a token, then set that
tokenHelper, err := m.TokenHelper(m) tokenHelper, err := m.TokenHelper()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -146,25 +141,6 @@ func (m *Meta) Client() (*api.Client, error) {
return client, nil return client, nil
} }
// Config loads the configuration and returns it. If the configuration
// is already loaded, it is returned.
func (m *Meta) Config() (*Config, error) {
if m.config != nil {
return m.config, nil
}
if m.ForceConfig != nil {
return m.ForceConfig, nil
}
var err error
m.config, err = LoadConfig("")
if err != nil {
return nil, err
}
return m.config, nil
}
// FlagSet returns a FlagSet with the common flags that every // FlagSet returns a FlagSet with the common flags that every
// command implements. The exact behavior of FlagSet can be configured // command implements. The exact behavior of FlagSet can be configured
// using the flags as the second parameter, for example to disable // using the flags as the second parameter, for example to disable