Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit a6f57ac

Browse files
authored
feat(iam-role): add properties to policies and instance profile) (#914)
1 parent ba40969 commit a6f57ac

4 files changed

+54
-33
lines changed

resources/iam-instance-profile-roles.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resources
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/aws/aws-sdk-go/aws/session"
78
"github.com/aws/aws-sdk-go/service/iam"
@@ -11,7 +12,7 @@ import (
1112

1213
type IAMInstanceProfileRole struct {
1314
svc *iam.IAM
14-
role string
15+
role *iam.Role
1516
profile *iam.InstanceProfile
1617
}
1718

@@ -43,13 +44,13 @@ func ListIAMInstanceProfileRoles(sess *session.Session) ([]Resource, error) {
4344

4445
resources = append(resources, &IAMInstanceProfileRole{
4546
svc: svc,
46-
role: *outRole.RoleName,
47+
role: outRole,
4748
profile: profile,
4849
})
4950
}
5051
}
5152

52-
if *resp.IsTruncated == false {
53+
if !*resp.IsTruncated {
5354
break
5455
}
5556

@@ -63,7 +64,7 @@ func (e *IAMInstanceProfileRole) Remove() error {
6364
_, err := e.svc.RemoveRoleFromInstanceProfile(
6465
&iam.RemoveRoleFromInstanceProfileInput{
6566
InstanceProfileName: e.profile.InstanceProfileName,
66-
RoleName: &e.role,
67+
RoleName: e.role.RoleName,
6768
})
6869
if err != nil {
6970
return err
@@ -85,7 +86,13 @@ func (e *IAMInstanceProfileRole) Properties() types.Properties {
8586

8687
properties.
8788
Set("InstanceProfile", e.profile.InstanceProfileName).
88-
Set("InstanceRole", e.role)
89+
Set("InstanceRole", e.role.RoleName).
90+
Set("role:Path", e.role.Path).
91+
Set("role:CreateDate", e.role.CreateDate.Format(time.RFC3339)).
92+
Set("role:LastUsedDate", getLastUsedDate(e.role, time.RFC3339))
8993

94+
for _, tagValue := range e.role.Tags {
95+
properties.SetTagWithPrefix("role", tagValue.Key, tagValue.Value)
96+
}
9097
return properties
9198
}

resources/iam-role-policy-attachments.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"github.com/aws/aws-sdk-go/aws/session"
89
"github.com/aws/aws-sdk-go/service/iam"
@@ -14,8 +15,7 @@ type IAMRolePolicyAttachment struct {
1415
svc *iam.IAM
1516
policyArn string
1617
policyName string
17-
roleName string
18-
roleTags []*iam.Tag
18+
role *iam.Role
1919
}
2020

2121
func init() {
@@ -56,20 +56,19 @@ func ListIAMRolePolicyAttachments(sess *session.Session) ([]Resource, error) {
5656
svc: svc,
5757
policyArn: *pol.PolicyArn,
5858
policyName: *pol.PolicyName,
59-
roleName: *role.RoleName,
60-
roleTags: role.Tags,
59+
role: role,
6160
})
6261
}
6362

64-
if *polResp.IsTruncated == false {
63+
if !*polResp.IsTruncated {
6564
break
6665
}
6766

6867
polParams.Marker = polResp.Marker
6968
}
7069
}
7170

72-
if *roleResp.IsTruncated == false {
71+
if !*roleResp.IsTruncated {
7372
break
7473
}
7574

@@ -90,7 +89,7 @@ func (e *IAMRolePolicyAttachment) Remove() error {
9089
_, err := e.svc.DetachRolePolicy(
9190
&iam.DetachRolePolicyInput{
9291
PolicyArn: &e.policyArn,
93-
RoleName: &e.roleName,
92+
RoleName: e.role.RoleName,
9493
})
9594
if err != nil {
9695
return err
@@ -101,15 +100,19 @@ func (e *IAMRolePolicyAttachment) Remove() error {
101100

102101
func (e *IAMRolePolicyAttachment) Properties() types.Properties {
103102
properties := types.NewProperties().
104-
Set("RoleName", e.roleName).
103+
Set("RoleName", e.role.RoleName).
104+
Set("RolePath", e.role.Path).
105+
Set("RoleLastUsed", getLastUsedDate(e.role, time.RFC3339)).
106+
Set("RoleCreateDate", e.role.CreateDate.Format(time.RFC3339)).
105107
Set("PolicyName", e.policyName).
106108
Set("PolicyArn", e.policyArn)
107-
for _, tag := range e.roleTags {
109+
110+
for _, tag := range e.role.Tags {
108111
properties.SetTagWithPrefix("role", tag.Key, tag.Value)
109112
}
110113
return properties
111114
}
112115

113116
func (e *IAMRolePolicyAttachment) String() string {
114-
return fmt.Sprintf("%s -> %s", e.roleName, e.policyName)
117+
return fmt.Sprintf("%s -> %s", *e.role.RoleName, e.policyName)
115118
}

resources/iam-role-policy.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"fmt"
55
"strings"
6+
"time"
67

78
"github.com/sirupsen/logrus"
89

@@ -62,15 +63,15 @@ func ListIAMRolePolicies(sess *session.Session) ([]Resource, error) {
6263
})
6364
}
6465

65-
if *policies.IsTruncated == false {
66+
if !*policies.IsTruncated {
6667
break
6768
}
6869

6970
polParams.Marker = policies.Marker
7071
}
7172
}
7273

73-
if *roles.IsTruncated == false {
74+
if !*roles.IsTruncated {
7475
break
7576
}
7677

@@ -101,11 +102,13 @@ func (e *IAMRolePolicy) Remove() error {
101102
}
102103

103104
func (e *IAMRolePolicy) Properties() types.Properties {
104-
properties := types.NewProperties()
105-
properties.Set("PolicyName", e.policyName)
106-
properties.Set("role:RoleName", e.role.RoleName)
107-
properties.Set("role:RoleID", e.role.RoleId)
108-
properties.Set("role:Path", e.role.Path)
105+
properties := types.NewProperties().
106+
Set("PolicyName", e.policyName).
107+
Set("role:RoleName", e.role.RoleName).
108+
Set("role:RoleID", e.role.RoleId).
109+
Set("role:Path", e.role.Path).
110+
Set("role:LastUsed", getLastUsedDate(&e.role, time.RFC3339)).
111+
Set("role:CreateDate", e.role.CreateDate.Format(time.RFC3339))
109112

110113
for _, tagValue := range e.role.Tags {
111114
properties.SetTagWithPrefix("role", tagValue.Key, tagValue.Value)

resources/iam-roles.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func ListIAMRoles(sess *session.Session) ([]Resource, error) {
5959
})
6060
}
6161

62-
if *resp.IsTruncated == false {
62+
if !*resp.IsTruncated {
6363
break
6464
}
6565

@@ -88,22 +88,30 @@ func (e *IAMRole) Remove() error {
8888
}
8989

9090
func (role *IAMRole) Properties() types.Properties {
91-
properties := types.NewProperties()
91+
properties := types.NewProperties().
92+
Set("CreateDate", role.role.CreateDate.Format(time.RFC3339)).
93+
Set("LastUsedDate", getLastUsedDate(role.role, time.RFC3339)).
94+
Set("Name", role.name).
95+
Set("Path", role.path)
96+
9297
for _, tagValue := range role.role.Tags {
9398
properties.SetTag(tagValue.Key, tagValue.Value)
9499
}
95-
properties.Set("CreateDate", role.role.CreateDate.Format(time.RFC3339))
96-
if role.role.RoleLastUsed.LastUsedDate == nil {
97-
properties.Set("LastUsedDate", role.role.CreateDate.Format(time.RFC3339))
98-
} else {
99-
properties.Set("LastUsedDate", role.role.RoleLastUsed.LastUsedDate.Format(time.RFC3339))
100-
}
101-
properties.
102-
Set("Name", role.name).
103-
Set("Path", role.path)
100+
104101
return properties
105102
}
106103

107104
func (e *IAMRole) String() string {
108105
return e.name
109106
}
107+
108+
func getLastUsedDate(role *iam.Role, format string) string {
109+
var lastUsedDate *time.Time
110+
if role.RoleLastUsed.LastUsedDate == nil {
111+
lastUsedDate = role.CreateDate
112+
} else {
113+
lastUsedDate = role.RoleLastUsed.LastUsedDate
114+
}
115+
116+
return lastUsedDate.Format(format)
117+
}

0 commit comments

Comments
 (0)