Skip to content

Commit

Permalink
Remove config from Meta; it's only used right now with the token helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai committed Apr 1, 2016
1 parent 48da409 commit 16c8f0b
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 46 deletions.
2 changes: 1 addition & 1 deletion command/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"+
Expand Down
4 changes: 2 additions & 2 deletions command/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 0 additions & 2 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 17 additions & 5 deletions meta/config.go → command/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package meta
package command

import (
"fmt"
Expand All @@ -22,18 +22,30 @@ 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
// 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) (*Config, error) {
func LoadConfig(path string) (*DefaultConfig, error) {
if path == "" {
path = DefaultConfigPath
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions meta/config_test.go → command/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package meta
package command

import (
"path/filepath"
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
File renamed without changes.
9 changes: 3 additions & 6 deletions command/util.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
30 changes: 3 additions & 27 deletions meta/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 16c8f0b

Please sign in to comment.