Skip to content

Commit

Permalink
Merge pull request #7 from common-fate/sort-SSO-profiles-alphabetical…
Browse files Browse the repository at this point in the history
…ly-fix

sort sso profiles by profile name
  • Loading branch information
shwethaumashanker authored Aug 13, 2024
2 parents 276781c + 66e1a9e commit 1ead8fd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
7 changes: 4 additions & 3 deletions awscfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ func Merge(opts MergeOpts) error {
opts.SectionNameTemplate = "{{ .AccountName }}/{{ .RoleName }}"
}

// Sort profiles by CombinedName (AccountName/RoleName)
sort.SliceStable(opts.Profiles, func(i, j int) bool {
profileNameI := opts.Prefix + opts.Profiles[i].RoleName
profileNameJ := opts.Prefix + opts.Profiles[j].RoleName
return profileNameI < profileNameJ
combinedNameI := opts.Profiles[i].AccountName + "/" + opts.Profiles[i].RoleName
combinedNameJ := opts.Profiles[j].AccountName + "/" + opts.Profiles[j].RoleName
return combinedNameI < combinedNameJ
})

funcMap := sprig.TxtFuncMap()
Expand Down
63 changes: 63 additions & 0 deletions awscfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,69 @@ granted_sso_account_id = 123456789012
granted_sso_role_name = DevRole
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile testing-title-case-with-space/DevRole --url https://commonfate.example.com
`,
},
{
name: "profile names (AccountName/RoleName) sorted alphabetically",
args: MergeOpts{
Config: parseIni(t, ""),
Profiles: []SSOProfile{
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "account1",
RoleName: "DevRoleTwo",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "account1",
RoleName: "DevRoleOne",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
{
SSOStartURL: "https://example.awsapps.com/start",
SSORegion: "ap-southeast-2",
AccountID: "123456789012",
AccountName: "account2",
RoleName: "DevRoleOne",
GeneratedFrom: "aws-sso",
Region: "us-west-2",
},
},
},
want: `
[profile account1/DevRoleOne]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleOne
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile account1/DevRoleOne
region = us-west-2
[profile account1/DevRoleTwo]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleTwo
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile account1/DevRoleTwo
region = us-west-2
[profile account2/DevRoleOne]
granted_sso_start_url = https://example.awsapps.com/start
granted_sso_region = ap-southeast-2
granted_sso_account_id = 123456789012
granted_sso_role_name = DevRoleOne
common_fate_generated_from = aws-sso
credential_process = granted credential-process --profile account2/DevRoleOne
region = us-west-2
`,
},
}
Expand Down

0 comments on commit 1ead8fd

Please sign in to comment.