Skip to content

Commit 0ecd005

Browse files
authored
Merge pull request #202 from synfinatic/via-tag
Via role option is now a tag
2 parents 138c655 + 1873776 commit 0ecd005

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

CHANGELOG.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## [Unreleased]
44

5+
### New Features
6+
* The `Via` role option is now a searchable tag #199
7+
* The `tags` command now returns the keys in sorted order
8+
9+
### Bug Fixes
10+
* Consistently pad AccountID with zeros whenever necessary
11+
512
## [1.6.0] - 2021-12-24
613

714
### Breaking Changes
@@ -13,7 +20,7 @@
1320
* Add support for role chaining using `Via` tag #38
1421
* Cache file is now versioned for better compatibility across versions of `aws-sso` #195
1522

16-
### Bugs Fixes
23+
### Bug Fixes
1724
* Incorrect `--level` value now correctly tells user the correct name of the flag
1825
* `exec` command now uses `cmd.exe` when no command is specified
1926

cmd/exec_cmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"os"
2525
"os/exec"
2626
"runtime"
27-
"strconv"
2827
"strings"
2928
"text/template"
3029

@@ -124,7 +123,8 @@ func firstItem(items []string) string {
124123
}
125124

126125
func accountIdToStr(id int64) string {
127-
return strconv.FormatInt(id, 10)
126+
i, _ := utils.AccountIdToString(id)
127+
return i
128128
}
129129

130130
// Executes Cmd+Args in the context of the AWS Role creds

cmd/tags_cmd.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package main
2020

2121
import (
2222
"fmt"
23+
"sort"
2324

2425
log "github.com/sirupsen/logrus"
2526
"github.com/synfinatic/aws-sso-cli/sso"
@@ -57,6 +58,10 @@ func (cc *TagsCmd) Run(ctx *RunContext) error {
5758

5859
if err := set.Cache.Expired(s); err != nil {
5960
log.Warn(err.Error())
61+
c := &CacheCmd{}
62+
if err = c.Run(ctx); err != nil {
63+
return err
64+
}
6065
}
6166
}
6267
roles := []*sso.AWSRoleFlat{}
@@ -84,8 +89,13 @@ func (cc *TagsCmd) Run(ctx *RunContext) error {
8489

8590
for _, fRole := range roles {
8691
fmt.Printf("%s\n", fRole.Arn)
87-
for k, v := range fRole.Tags {
88-
fmt.Printf(" %s: %s\n", k, v)
92+
keys := make([]string, 0, len(fRole.Tags))
93+
for k := range fRole.Tags {
94+
keys = append(keys, k)
95+
}
96+
sort.Strings(keys)
97+
for _, k := range keys {
98+
fmt.Printf(" %s: %s\n", k, fRole.Tags[k])
8999
}
90100
fmt.Printf("\n")
91101
}

sso/cache.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ type AWSRoleFlat struct {
279279
SSORegion string `json:"SSORegion" header:"SSORegion"`
280280
StartUrl string `json:"StartUrl" header:"StartUrl"`
281281
Tags map[string]string `json:"Tags"` // not supported by GenerateTable
282-
Via string `json:"Via" header:"Via"`
282+
Via string `json:"Via,omitempty" header:"Via"`
283283
// SelectTags map[string]string // tags without spaces
284284
}
285285

@@ -305,7 +305,7 @@ func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig) (*Roles, error) {
305305
for _, aInfo := range accounts {
306306
accountId := aInfo.GetAccountId64()
307307
r.Accounts[accountId] = &AWSAccount{
308-
Alias: aInfo.AccountName,
308+
Alias: aInfo.AccountName, // AWS SSO calls it `AccountName`
309309
EmailAddress: aInfo.EmailAddress,
310310
Tags: map[string]string{},
311311
Roles: map[string]*AWSRole{},
@@ -320,7 +320,7 @@ func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig) (*Roles, error) {
320320
Arn: utils.MakeRoleARN(accountId, role.RoleName),
321321
Tags: map[string]string{
322322
"AccountID": aInfo.AccountId,
323-
"AccountAlias": aInfo.AccountName,
323+
"AccountAlias": aInfo.AccountName, // AWS SSO calls it `AccountName`
324324
"Email": aInfo.EmailAddress,
325325
"Role": role.RoleName,
326326
},
@@ -358,7 +358,7 @@ func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig) (*Roles, error) {
358358

359359
// set the AWS SSO tags for all the SSO roles
360360
for roleName := range r.Accounts[accountId].Roles {
361-
aId := strconv.FormatInt(accountId, 10)
361+
aId, _ := utils.AccountIdToString(accountId)
362362
r.Accounts[accountId].Roles[roleName].Tags["AccountID"] = aId
363363
r.Accounts[accountId].Roles[roleName].Tags["AccountName"] = r.Accounts[accountId].Name
364364
r.Accounts[accountId].Roles[roleName].Tags["AccountAlias"] = r.Accounts[accountId].Alias
@@ -378,8 +378,8 @@ func (c *Cache) NewRoles(as *AWSSSO, config *SSOConfig) (*Roles, error) {
378378
}
379379
r.Accounts[accountId].Roles[roleName].Arn = utils.MakeRoleARN(accountId, roleName)
380380
r.Accounts[accountId].Roles[roleName].Profile = role.Profile
381-
r.Accounts[accountId].Roles[roleName].Via = role.Via
382381
r.Accounts[accountId].Roles[roleName].DefaultRegion = r.Accounts[accountId].DefaultRegion
382+
r.Accounts[accountId].Roles[roleName].Via = role.Via
383383
if role.DefaultRegion != "" {
384384
r.Accounts[accountId].Roles[roleName].DefaultRegion = role.DefaultRegion
385385
}
@@ -528,11 +528,14 @@ func (r *Roles) GetRole(accountId int64, roleName string) (*AWSRoleFlat, error)
528528
flat.DefaultRegion = role.DefaultRegion
529529
}
530530
// Automatic tags
531-
flat.Tags["AccountID"] = strconv.FormatInt(accountId, 10)
531+
flat.Tags["AccountID"], _ = utils.AccountIdToString(accountId)
532532
flat.Tags["Email"] = account.EmailAddress
533533
if role.Profile != "" {
534534
flat.Tags["Profile"] = role.Profile
535535
}
536+
if role.Via != "" {
537+
flat.Tags["Via"] = role.Via
538+
}
536539

537540
// Account name is by default the alias, but can be manually overridden
538541
flat.Tags["AccountName"] = flat.AccountAlias

sso/settings.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"io/ioutil"
2525
"os"
2626
"path/filepath"
27-
"strconv"
2827
"strings"
2928

3029
// "github.com/davecgh/go-spew/spew"
@@ -403,7 +402,7 @@ func (a *SSOAccount) GetAllTags(id int64) map[string]string {
403402
"AccountName": accountName,
404403
}
405404
if id > 0 {
406-
accountId := strconv.FormatInt(id, 10)
405+
accountId, _ := utils.AccountIdToString(id)
407406
tags["AccountId"] = accountId
408407
}
409408
if a.DefaultRegion != "" {

0 commit comments

Comments
 (0)