From b0888e8af1b51e3eafc146f3689e2c1267626671 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Fri, 1 Apr 2016 16:02:18 -0400 Subject: [PATCH] Remove config from Meta; it's only used right now with the token helper. --- command/auth.go | 2 +- command/auth_test.go | 4 +-- command/command_test.go | 2 -- {meta => command}/config.go | 22 ++++++++++++---- {meta => command}/config_test.go | 4 +-- command/server.go | 2 +- {meta => command}/test-fixtures/config.hcl | 0 command/util.go | 9 +++---- meta/meta.go | 30 +++------------------- 9 files changed, 29 insertions(+), 46 deletions(-) rename {meta => command}/config.go (85%) rename {meta => command}/config_test.go (95%) rename {meta => command}/test-fixtures/config.hcl (100%) diff --git a/command/auth.go b/command/auth.go index 1f451cc6f..4bc3061e3 100644 --- a/command/auth.go +++ b/command/auth.go @@ -52,7 +52,7 @@ func (c *AuthCommand) Run(args []string) int { args = flags.Args() - tokenHelper, err := c.TokenHelper(&c.Meta) + tokenHelper, err := c.TokenHelper() if err != nil { c.Ui.Error(fmt.Sprintf( "Error initializing token helper: %s\n\n"+ diff --git a/command/auth_test.go b/command/auth_test.go index fddafbfa0..9ffd0ac1d 100644 --- a/command/auth_test.go +++ b/command/auth_test.go @@ -69,7 +69,7 @@ func TestAuth_token(t *testing.T) { t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) } - helper, err := c.TokenHelper(&c.Meta) + helper, err := c.TokenHelper() if err != nil { 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()) } - helper, err := c.TokenHelper(&c.Meta) + helper, err := c.TokenHelper() if err != nil { t.Fatalf("err: %s", err) } diff --git a/command/command_test.go b/command/command_test.go index 4aa33ad58..763587a05 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -6,8 +6,6 @@ import ( "github.com/hashicorp/vault/api" ) -const FixturePath = "./test-fixtures" - func testClient(t *testing.T, addr string, token string) *api.Client { config := api.DefaultConfig() config.Address = addr diff --git a/meta/config.go b/command/config.go similarity index 85% rename from meta/config.go rename to command/config.go index aaf581b0f..ffe7903b9 100644 --- a/meta/config.go +++ b/command/config.go @@ -1,4 +1,4 @@ -package meta +package command import ( "fmt" @@ -22,7 +22,7 @@ const ( // 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 Config struct { +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 @@ -30,10 +30,22 @@ type Config struct { 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) (*Config, error) { +func LoadConfig(path string) (*DefaultConfig, error) { if path == "" { path = DefaultConfigPath } @@ -55,7 +67,7 @@ func LoadConfig(path string) (*Config, error) { } // 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) if err != nil { return nil, err @@ -74,7 +86,7 @@ func ParseConfig(contents string) (*Config, error) { return nil, err } - var c Config + var c DefaultConfig if err := hcl.DecodeObject(&c, list); err != nil { return nil, err } diff --git a/meta/config_test.go b/command/config_test.go similarity index 95% rename from meta/config_test.go rename to command/config_test.go index 115d89334..edbf5579c 100644 --- a/meta/config_test.go +++ b/command/config_test.go @@ -1,4 +1,4 @@ -package meta +package command import ( "path/filepath" @@ -15,7 +15,7 @@ func TestLoadConfig(t *testing.T) { t.Fatalf("err: %s", err) } - expected := &Config{ + expected := &DefaultConfig{ TokenHelper: "foo", } if !reflect.DeepEqual(expected, config) { diff --git a/command/server.go b/command/server.go index ee67f8f01..acca899ca 100644 --- a/command/server.go +++ b/command/server.go @@ -424,7 +424,7 @@ func (c *ServerCommand) enableDev(core *vault.Core, rootTokenID string) (*vault. } // Set the token - tokenHelper, err := c.TokenHelper(&c.Meta) + tokenHelper, err := c.TokenHelper() if err != nil { return nil, err } diff --git a/meta/test-fixtures/config.hcl b/command/test-fixtures/config.hcl similarity index 100% rename from meta/test-fixtures/config.hcl rename to command/test-fixtures/config.hcl diff --git a/command/util.go b/command/util.go index 0717f1f1d..1a409ce1a 100644 --- a/command/util.go +++ b/command/util.go @@ -1,13 +1,10 @@ package command -import ( - "github.com/hashicorp/vault/command/token" - "github.com/hashicorp/vault/meta" -) +import "github.com/hashicorp/vault/command/token" // DefaultTokenHelper returns the token helper that is configured for Vault. -func DefaultTokenHelper(m *meta.Meta) (token.TokenHelper, error) { - config, err := m.Config() +func DefaultTokenHelper() (token.TokenHelper, error) { + config, err := LoadConfig("") if err != nil { return nil, err } diff --git a/meta/meta.go b/meta/meta.go index 0af5789d0..cd07d0f57 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -24,7 +24,7 @@ import ( // default FlagSet returned by Meta.FlagSet. type FlagSetFlags uint -type TokenHelperFunc func(*Meta) (token.TokenHelper, error) +type TokenHelperFunc func() (token.TokenHelper, error) const ( FlagSetNone FlagSetFlags = 0 @@ -39,8 +39,7 @@ type Meta struct { Ui cli.Ui // The things below can be set, but aren't common - ForceAddress string // Address to force for API clients - ForceConfig *Config // Force a config, don't load from disk + ForceAddress string // Address to force for API clients // These are set by the command line flags. flagAddress string @@ -50,10 +49,6 @@ type Meta struct { flagClientKey string 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 TokenHelper TokenHelperFunc } @@ -127,7 +122,7 @@ func (m *Meta) Client() (*api.Client, error) { if token == "" { if m.TokenHelper != nil { // If we have a token, then set that - tokenHelper, err := m.TokenHelper(m) + tokenHelper, err := m.TokenHelper() if err != nil { return nil, err } @@ -146,25 +141,6 @@ func (m *Meta) Client() (*api.Client, error) { 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 // command implements. The exact behavior of FlagSet can be configured // using the flags as the second parameter, for example to disable