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

feat: add support to alternative destination of profiles #27

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ dist

coverage.txt
node_modules

# ignore the executable
aws-mfa-login
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ clusters:
accountId: "1234"
role: DeveloperAccessRole
region: eu-central-1
destination: superProfile #overrides default destination
- name: eks-prod
alias: suite-academic
accountId: "4321"
Expand Down
23 changes: 15 additions & 8 deletions action/awsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package action

import (
"fmt"
"log"
"os"

"github.com/ghodss/yaml"
"github.com/go-ini/ini"
"github.com/spf13/viper"
"log"
"os"
)

type State int
Expand All @@ -24,11 +25,12 @@ type Clusters struct {
}

type ClusterConfig struct {
Name string `yaml:"name"`
Alias string `yaml:"alias"`
AccountID string `yaml:"accountId"`
Role string `yaml:"role"`
Region string `yaml:"region"`
Name string `yaml:"name"`
Alias string `yaml:"alias"`
AccountID string `yaml:"accountId"`
Role string `yaml:"role"`
Region string `yaml:"region"`
Destination string `yaml:"destination"`
karlderkaefer marked this conversation as resolved.
Show resolved Hide resolved
}

func (clusters *Clusters) InitConfig() {
Expand Down Expand Up @@ -87,7 +89,12 @@ func (c *ClusterConfig) Write(filePath string) (State, error) {
if err != nil {
return Error, err
}
_, err = section.NewKey("source_profile", viper.GetString("destination"))

destinationProfile := viper.GetString("destination")
if c.Destination != "" {
destinationProfile = c.Destination
}
_, err = section.NewKey("source_profile", destinationProfile)
if err != nil {
return Error, err
}
Expand Down
36 changes: 28 additions & 8 deletions action/awsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package action

import (
"fmt"
"github.com/go-ini/ini"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"os"
"testing"

"github.com/go-ini/ini"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestWrite(t *testing.T) {
Expand All @@ -25,6 +26,13 @@ func TestWrite(t *testing.T) {
AccountID: "123456",
Role: "admin",
},
{
Name: "testname3",
Alias: "testalias3",
AccountID: "123456",
Role: "admin",
Destination: "altProfile",
},
}
viper.Set("clusters", conf)
viper.Set("destination", "test-mfa")
Expand Down Expand Up @@ -74,12 +82,24 @@ func TestWrite(t *testing.T) {
}

// one section DEFAULT will be added by default
assert.ElementsMatch(t, result.SectionStrings(), []string{"DEFAULT", conf[0].Alias, conf[1].Alias})
assert.ElementsMatch(t, result.SectionStrings(), []string{"DEFAULT", conf[0].Alias, conf[1].Alias, conf[2].Alias})
assert.Equal(t, foundRole.Value(), getArn(conf[0].AccountID, conf[0].Role))
assert.Equal(t, foundProfile.Value(), viper.GetString("destination"))
assert.Equal(t, clusters.states[Created], 2)
assert.Equal(t, clusters.states[Updated], 0)
assert.Equal(t, clusters.states[Deleted], 0)
assert.Equal(t, 3, clusters.states[Created], "Expected 3 cluster to be created")
assert.Equal(t, 0, clusters.states[Updated], "Expected no cluster to be updated")
assert.Equal(t, 0, clusters.states[Deleted], "Expected no cluster to be deleted")

foundSectionDestinationModified, err := result.GetSection(conf[2].Alias)
if err != nil {
log.Fatal(err)
}
foundDestination, err := foundSectionDestinationModified.GetKey("source_profile")
if err != nil {
log.Fatal(err)
}

assert.Equal(t, conf[2].Destination, foundDestination.Value(), "expect that destination has been overwritten")
assert.NotEqual(t, conf[2].Destination, viper.GetString("destination"), "expect that the profile does no return the default")

modified := &ClusterConfig{
Name: "changed",
Expand Down Expand Up @@ -112,7 +132,7 @@ func TestWrite(t *testing.T) {
log.Fatal(err)
}

assert.Len(t, result.SectionStrings(), 3)
assert.Len(t, result.SectionStrings(), 4)
assert.Equal(t, foundRole.Value(), getArn(modified.AccountID, modified.Role))
assert.Equal(t, foundProfile.Value(), viper.GetString("destination"))
assert.Equal(t, state, Updated)
Expand Down