Skip to content

Commit

Permalink
Implement merge of config and credentials file
Browse files Browse the repository at this point in the history
Loading the profile data from the config and credentials files
will new merge the data of the two files. The data in the second
file is preferred. This means in the current load order that the
credentials file will overwrite configuration in the config
file.

Test data and test was added for this case.
  • Loading branch information
suxor42 committed Mar 3, 2020
1 parent 5e08595 commit 515c850
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
38 changes: 32 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
assertmod "github.com/stretchr/testify/assert"
requiremod "github.com/stretchr/testify/require"
)

func TestConfigInitLoad(t *testing.T) {

var (
assert = assert.New(t)
require = require.New(t)
assert = assertmod.New(t)
require = requiremod.New(t)
)

split := &Config{
Expand Down Expand Up @@ -55,14 +55,13 @@ func TestConfigInitLoad(t *testing.T) {
assert.Equal("arn:aws:iam::123456789012:role/marketingadmin", profile.RoleARN)
assert.Equal(profile.AccessKeyID, profile.Value().AccessKeyID)
assert.Equal(profile.SecretAccessKey, profile.Value().SecretAccessKey)

}

}

func TestConfigInitValidate(t *testing.T) {

var assert = assert.New(t)
var assert = assertmod.New(t)

valid := &Config{
Duration: 5 * time.Minute,
Expand All @@ -79,3 +78,30 @@ func TestConfigInitValidate(t *testing.T) {
assert.Errorf(invalid.Init(), "invalid grace (f) for duration (d)")

}

func TestMergeConfigAndCredentials(t *testing.T) {

var (
assert = assertmod.New(t)
require = requiremod.New(t)
)

config := &Config{
ConfigFile: "testdata/config",
Duration: 5 * time.Minute,
Grace: 1 * time.Minute,
SharedCredentialsFile: "testdata/credentials",
}

err := config.Init()
require.NoError(err)

mfaProfile, ok := config.Profiles["test-mfa"]
require.True(ok)

assert.Equal("test-mfa", mfaProfile.Name)

assert.Equal("arn:aws:iam::123456789012:mfa/jondoe", mfaProfile.MFASerial)
assert.Equal("AKIAIOSFODNN7EXAMPLE", mfaProfile.Value().AccessKeyID)
assert.Equal("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", mfaProfile.Value().SecretAccessKey)
}
13 changes: 13 additions & 0 deletions config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"github.com/aws/aws-sdk-go/aws/credentials"
"reflect"
)

// Profile is a long- or short-time credential profile managed in a shared config
Expand Down Expand Up @@ -30,3 +31,15 @@ func (p *Profile) Value() credentials.Value {
}

}

func (p *Profile) Merge(profileToMerge *Profile) {
val1 := reflect.ValueOf(p).Elem()
val2 := reflect.ValueOf(profileToMerge).Elem()

for i := 0; i < val1.NumField(); i++ {
newFieldValue := val2.Field(i)
if !newFieldValue.IsZero() {
val1.Field(i).Set(newFieldValue)
}
}
}
10 changes: 6 additions & 4 deletions config/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"io/ioutil"
"strings"

"github.com/pkg/errors"
ini "gopkg.in/ini.v1"
Expand Down Expand Up @@ -50,6 +51,7 @@ func Load(files ...string) (Profiles, error) {
for _, section := range f.Sections() {

name := section.Name()
name = strings.TrimPrefix(name, "profile ")

if name == "preview" {
continue
Expand All @@ -64,12 +66,12 @@ func Load(files ...string) (Profiles, error) {
goto init
}

if err := section.MapTo(profile); err != nil {
sectionProfile := new(Profile)
if err := section.MapTo(sectionProfile); err != nil {
return nil, err
}

profile.Name = name

sectionProfile.Name = name
profile.Merge(sectionProfile)
}

}
Expand Down
5 changes: 5 additions & 0 deletions config/testdata/config
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[default]
region=us-west-2
output=json

[profile test-mfa]
region=us-west-2
output=json
mfa_serial=arn:aws:iam::123456789012:mfa/jondoe
4 changes: 4 additions & 0 deletions config/testdata/credentials
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ role_arn=arn:aws:iam::123456789012:role/marketingadmin
source_profile=default
external_id=123456
mfa_serial=arn:aws:iam::123456789012:mfa/jonsmith

[test-mfa]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

0 comments on commit 515c850

Please sign in to comment.