Skip to content

Commit

Permalink
Merge pull request #54 from envato/allow-user-tags
Browse files Browse the repository at this point in the history
Allow user tags
  • Loading branch information
mtibben authored Jan 10, 2019
2 parents 74e1e4b + bba88f8 commit 9978624
Show file tree
Hide file tree
Showing 30 changed files with 11,615 additions and 7,093 deletions.
14 changes: 10 additions & 4 deletions iamy/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,13 @@ func (a *AwsFetcher) populateIamData(resp *iam.GetAccountAuthorizationDetailsOut
continue
}

user := User{iamService: iamService{
Name: *userResp.UserName,
Path: *userResp.Path,
}}
user := User{
iamService: iamService{
Name: *userResp.UserName,
Path: *userResp.Path,
},
Tags: make(map[string]string),
}

for _, g := range userResp.GroupList {
user.Groups = append(user.Groups, *g)
Expand All @@ -232,6 +235,9 @@ func (a *AwsFetcher) populateIamData(resp *iam.GetAccountAuthorizationDetailsOut
if err := a.populateInlinePolicies(userResp.UserPolicyList, &user.InlinePolicies); err != nil {
return err
}
for _, t := range userResp.Tags {
user.Tags[*t.Key] = *t.Value
}

a.data.Users = append(a.data.Users, &user)
}
Expand Down
20 changes: 19 additions & 1 deletion iamy/awsdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func path(v string) string {
return v
}

func mapTagsToString(tags map[string]string) string {
var result []string
for k, v := range tags {
result = append(result, "Key="+k+",Value="+v)
}
return strings.Join(result, ",")
}

type awsSyncCmdGenerator struct {
from, to *AccountData
cmds CmdList
Expand Down Expand Up @@ -397,9 +405,19 @@ func (a *awsSyncCmdGenerator) updateUsers() {
a.cmds.Add("aws", "iam", "attach-user-policy", "--user-name", toUser.Name, "--policy-arn", a.to.Account.policyArnFromString(p))
}

// remove old tags
for tagKey, _ := range mapStringSetDifference(fromUser.Tags, toUser.Tags) {
a.cmds.Add("aws", "iam", "untag-user", "--user-name", toUser.Name, "--tag-keys", tagKey)
}

// attach new tags
for tagKey, tagValue := range mapStringSetDifference(toUser.Tags, fromUser.Tags) {
a.cmds.Add("aws", "iam", "tag-user", "--user-name", toUser.Name, "--tags", "Key="+tagKey+",Value="+tagValue)
}

} else {
// Create user
a.cmds.Add("aws", "iam", "create-user", "--user-name", toUser.Name, "--path", path(toUser.Path))
a.cmds.Add("aws", "iam", "create-user", "--user-name", toUser.Name, "--path", path(toUser.Path), "--tags", mapTagsToString(toUser.Tags))

// add new groups
for _, g := range toUser.Groups {
Expand Down
11 changes: 11 additions & 0 deletions iamy/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package iamy

func mapStringSetDifference(aa, bb map[string]string) map[string]string {
rr := make(map[string]string)
for k, v := range aa {
if bb[k] != v {
rr[k] = v
}
}
return rr
}
7 changes: 4 additions & 3 deletions iamy/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ func (s iamService) ResourcePath() string {

type User struct {
iamService `json:"-"`
Groups []string `json:"Groups,omitempty"`
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
Policies []string `json:"Policies,omitempty"`
Groups []string `json:"Groups,omitempty"`
InlinePolicies []InlinePolicy `json:"InlinePolicies,omitempty"`
Policies []string `json:"Policies,omitempty"`
Tags map[string]string `json:"Tags,omitempty"`
}

func (u User) ResourceType() string {
Expand Down
5 changes: 4 additions & 1 deletion iamy/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ func (a *YamlLoadDumper) Load() ([]AccountData, error) {

switch entity {
case "iam/user":
u := User{iamService: nameAndPath}
u := User{
iamService: nameAndPath,
Tags: make(map[string]string),
}
err = a.unmarshalYamlFile(fp, &u)
accounts[accountid].addUser(&u)
case "iam/group":
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 76 additions & 10 deletions vendor/github.com/aws/aws-sdk-go/aws/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions vendor/github.com/aws/aws-sdk-go/aws/context_1_6.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/aws/convert_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions vendor/github.com/aws/aws-sdk-go/aws/errors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/aws/aws-sdk-go/aws/logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9978624

Please sign in to comment.