Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement merge of config and credentials file #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kreuzwerker/awsu
require (
github.com/aws/aws-sdk-go v1.15.32
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mdp/qrterminal v1.0.0
github.com/mitchellh/go-homedir v1.0.0
github.com/pkg/errors v0.8.1
Expand All @@ -11,6 +12,7 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2
github.com/spf13/viper v1.2.0
github.com/stretchr/testify v1.3.0
github.com/yawn/doubledash v0.0.0-20151212175516-fd8a81db93af
github.com/yawn/envmap v0.0.0-20160813152305-a78254303070
github.com/yawn/ykoath v1.0.3
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/aws/aws-sdk-go v1.15.32 h1:tb099RWtGbsXqOWDNKISRyufkdRWOYlXhE4XN0Jm3Bg=
github.com/aws/aws-sdk-go v1.15.32/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d h1:lDrio3iIdNb0Gw9CgH7cQF+iuB5mOOjdJ9ERNJCBgb4=
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand All @@ -12,10 +13,14 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-ini/ini v1.25.4 h1:Mujh4R/dH6YL8bxuISne3xX2+qcQ9p0IxKAP6ExWoUo=
github.com/go-ini/ini v1.25.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8 h1:12VvqtR6Aowv3l/EQUlocDHW2Cp4G9WJVH7uyH8QFJE=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mdp/qrterminal v1.0.0 h1:Lir5hH+jRgOiyqdXIcQ2CHm7g8MQGd/SlpvxXpaoH00=
Expand All @@ -30,11 +35,14 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rsc/qr v0.1.0 h1:WW8q4ZVCqHJvjgK1VrO0Guecromo+4lOKo4cYcGBB2A=
github.com/rsc/qr v0.1.0/go.mod h1:pPTpZ297SDwxHkeVbI8UtK0t44AY2UwaRx/q7CiJXhw=
github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c h1:fyKiXKO1/I/B6Y2U8T7WdQGWzwehOuGIrljPtt7YTTI=
github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg=
Expand All @@ -50,6 +58,7 @@ github.com/spf13/viper v1.2.0/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/yawn/doubledash v0.0.0-20151212175516-fd8a81db93af h1:eV7i4++vdsO+jWDa/0Us8EE4GZW5XFpYMuUAICYNBps=
github.com/yawn/doubledash v0.0.0-20151212175516-fd8a81db93af/go.mod h1:dvhlbINDXiDEf/Cwinko7RWc1cTpvkn5eR9HhaAEyNQ=
Expand All @@ -59,10 +68,14 @@ github.com/yawn/ykoath v1.0.1 h1:buW40ptJqj3ob+hu6EJe4CRM4CG9K6EdBYVgb0mu768=
github.com/yawn/ykoath v1.0.1/go.mod h1:MLAY9NODwv8/Eqr0zdqSrgj5V2UIYBKw28q8Qk8kmR4=
github.com/yawn/ykoath v1.0.3 h1:6ItWA3pH9OmLmWAvu3iTWiWtgJ68mM7m6yIXTSr0/78=
github.com/yawn/ykoath v1.0.3/go.mod h1:dcXMmLrvt6WFkySkG2k8ZEqxiTbu/TWSI4+/Cb54+Lg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.38.2 h1:dGcbywv4RufeGeiMycPT/plKB5FtmLKLnWKwBiLhUA4=
gopkg.in/ini.v1 v1.38.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
Expand Down