Skip to content

Commit

Permalink
Remove profile warning for now
Browse files Browse the repository at this point in the history
  • Loading branch information
lox committed Oct 11, 2017
1 parent e4d1705 commit db73b84
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 14 deletions.
24 changes: 17 additions & 7 deletions cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"log"
"os"

"github.com/99designs/aws-vault/prompt"
Expand All @@ -12,9 +13,10 @@ import (
)

type AddCommandInput struct {
Profile string
Keyring keyring.Keyring
FromEnv bool
Profile string
Keyring keyring.Keyring
FromEnv bool
AddConfig bool
}

func ConfigureAddCommand(app *kingpin.Application) {
Expand All @@ -28,6 +30,10 @@ func ConfigureAddCommand(app *kingpin.Application) {
cmd.Flag("env", "Read the credentials from the environment").
BoolVar(&input.FromEnv)

cmd.Flag("add-config", "Whether to add an entry to aws config if one doesn't exist").
Default("true").
BoolVar(&input.AddConfig)

cmd.Action(func(c *kingpin.ParseContext) error {
input.Keyring = keyringImpl
AddCommand(app, input)
Expand All @@ -38,10 +44,7 @@ func ConfigureAddCommand(app *kingpin.Application) {
func AddCommand(app *kingpin.Application, input AddCommandInput) {
var accessKeyId, secretKey string

if _, hasProfile := awsConfig.Profile(input.Profile); !hasProfile {
fmt.Printf("Profile %q doesn't exist in your config\n", input.Profile)

} else if source, ok := awsConfig.SourceProfile(input.Profile); ok {
if source, ok := awsConfig.SourceProfile(input.Profile); ok {
app.Fatalf("Your profile has a source_profile of %s, adding credentials to %s won't have any effect",
source.Name, input.Profile)
return
Expand Down Expand Up @@ -87,4 +90,11 @@ func AddCommand(app *kingpin.Application, input AddCommandInput) {
if n, _ := sessions.Delete(input.Profile); n > 0 {
fmt.Printf("Deleted %d existing sessions.\n", n)
}

if _, hasProfile := awsConfig.Profile(input.Profile); !hasProfile {
if input.AddConfig {
log.Printf("Adding profile %s to config at %s", input.Profile, awsConfig.Path)
awsConfig.Add(input.Profile, vault.Profile{})
}
}
}
3 changes: 2 additions & 1 deletion cli/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"log"
"testing"

"github.com/99designs/aws-vault/prompt"
"github.com/99designs/aws-vault/vault"
Expand Down Expand Up @@ -43,7 +44,7 @@ func ConfigureGlobals(app *kingpin.Application) {
EnumVar(&GlobalFlags.PromptDriver, promptsAvailable...)

app.PreAction(func(c *kingpin.ParseContext) (err error) {
if !GlobalFlags.Debug {
if !GlobalFlags.Debug && !testing.Verbose() {
log.SetOutput(ioutil.Discard)
}
if keyringImpl == nil {
Expand Down
40 changes: 34 additions & 6 deletions vault/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vault
import (
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -54,7 +55,8 @@ func LoadConfigFromEnv() (*Config, error) {
return nil, err
}

return &Config{Path: file}, nil
log.Printf("Loading config file %s", file)
return LoadConfig(file)
}

func (c *Config) parseFile() error {
Expand Down Expand Up @@ -85,19 +87,45 @@ func (p Profile) Hash() ([]byte, error) {
return b, nil
}

func readProfileFromIni(f *ini.File, sectionName string, profile *Profile) error {
if f == nil {
return errors.New("No ini file available")
}
section, err := f.GetSection(sectionName)
if err != nil {
return err
}
if err = section.MapTo(&profile); err != nil {
return err
}
return nil
}

// Default returns the default profile settings
func (c *Config) Default() (Profile, bool) {
profile := Profile{
Name: "default",
}

if err := readProfileFromIni(c.iniFile, "default", &profile); err != nil {
return profile, false
}

return profile, true

}

// Profile returns the profile with the matching name. If there isn't any,
// an empty profile with the provided name is returned, along with false.
func (c *Config) Profile(name string) (Profile, bool) {
profile := Profile{
Name: name,
}
section, err := c.iniFile.GetSection("profile " + name)
if err != nil {

if err := readProfileFromIni(c.iniFile, "profile "+name, &profile); err != nil {
return profile, false
}
if err = section.MapTo(&profile); err != nil {
panic(err)
}

return profile, true
}

Expand Down
42 changes: 42 additions & 0 deletions vault/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ func TestConfigParsingProfiles(t *testing.T) {
}
}

func TestConfigParsingDefault(t *testing.T) {
f := newConfigFile(t)
defer os.Remove(f)

cfg, err := vault.LoadConfig(f)
if err != nil {
t.Fatal(err)
}

def, ok := cfg.Default()
if !ok {
t.Fatalf("Expected to find default profile")
}

expected := vault.Profile{
Name: "default",
Region: "us-west-2",
}

if !reflect.DeepEqual(def, expected) {
t.Fatalf("Expected %+v, got %+v", expected, def)
}
}

func TestSourceProfileFromConfig(t *testing.T) {
f := newConfigFile(t)
defer os.Remove(f)
Expand All @@ -98,3 +122,21 @@ func TestSourceProfileFromConfig(t *testing.T) {
t.Fatalf("Expected source name %q, got %q", "user2", source.Name)
}
}

func TestAddingProfileToConfig(t *testing.T) {
f := newConfigFile(t)
defer os.Remove(f)

cfg, err := vault.LoadConfig(f)
if err != nil {
t.Fatal(err)
}

err = cfg.Add(vault.Profile{
Name: "llamas",
Region: "test",
})
if err != nil {
t.Fatal(err)
}
}

0 comments on commit db73b84

Please sign in to comment.