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(iam-role): add properties to policies and instance profile #914

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 12 additions & 5 deletions resources/iam-instance-profile-roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
"fmt"
"time"

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

type IAMInstanceProfileRole struct {
svc *iam.IAM
role string
role *iam.Role
profile *iam.InstanceProfile
}

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

resources = append(resources, &IAMInstanceProfileRole{
svc: svc,
role: *outRole.RoleName,
role: outRole,
profile: profile,
})
}
}

if *resp.IsTruncated == false {
if !*resp.IsTruncated {
break
}

Expand All @@ -63,7 +64,7 @@ func (e *IAMInstanceProfileRole) Remove() error {
_, err := e.svc.RemoveRoleFromInstanceProfile(
&iam.RemoveRoleFromInstanceProfileInput{
InstanceProfileName: e.profile.InstanceProfileName,
RoleName: &e.role,
RoleName: e.role.RoleName,
})
if err != nil {
return err
Expand All @@ -85,7 +86,13 @@ func (e *IAMInstanceProfileRole) Properties() types.Properties {

properties.
Set("InstanceProfile", e.profile.InstanceProfileName).
Set("InstanceRole", e.role)
Set("InstanceRole", e.role.RoleName).
Set("role:Path", e.role.Path).
Set("role:CreateDate", e.role.CreateDate.Format(time.RFC3339)).
Set("role:LastUsedDate", getLastUsedDate(e.role, time.RFC3339))

for _, tagValue := range e.role.Tags {
properties.SetTagWithPrefix("role", tagValue.Key, tagValue.Value)
}
return properties
}
23 changes: 13 additions & 10 deletions resources/iam-role-policy-attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resources
import (
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/iam"
Expand All @@ -14,8 +15,7 @@ type IAMRolePolicyAttachment struct {
svc *iam.IAM
policyArn string
policyName string
roleName string
roleTags []*iam.Tag
role *iam.Role
}

func init() {
Expand Down Expand Up @@ -56,20 +56,19 @@ func ListIAMRolePolicyAttachments(sess *session.Session) ([]Resource, error) {
svc: svc,
policyArn: *pol.PolicyArn,
policyName: *pol.PolicyName,
roleName: *role.RoleName,
roleTags: role.Tags,
role: role,
})
}

if *polResp.IsTruncated == false {
if !*polResp.IsTruncated {
break
}

polParams.Marker = polResp.Marker
}
}

if *roleResp.IsTruncated == false {
if !*roleResp.IsTruncated {
break
}

Expand All @@ -90,7 +89,7 @@ func (e *IAMRolePolicyAttachment) Remove() error {
_, err := e.svc.DetachRolePolicy(
&iam.DetachRolePolicyInput{
PolicyArn: &e.policyArn,
RoleName: &e.roleName,
RoleName: e.role.RoleName,
})
if err != nil {
return err
Expand All @@ -101,15 +100,19 @@ func (e *IAMRolePolicyAttachment) Remove() error {

func (e *IAMRolePolicyAttachment) Properties() types.Properties {
properties := types.NewProperties().
Set("RoleName", e.roleName).
Set("RoleName", e.role.RoleName).
Set("RolePath", e.role.Path).
Set("RoleLastUsed", getLastUsedDate(e.role, time.RFC3339)).
Set("RoleCreateDate", e.role.CreateDate.Format(time.RFC3339)).
Set("PolicyName", e.policyName).
Set("PolicyArn", e.policyArn)
for _, tag := range e.roleTags {

for _, tag := range e.role.Tags {
properties.SetTagWithPrefix("role", tag.Key, tag.Value)
}
return properties
}

func (e *IAMRolePolicyAttachment) String() string {
return fmt.Sprintf("%s -> %s", e.roleName, e.policyName)
return fmt.Sprintf("%s -> %s", *e.role.RoleName, e.policyName)
}
17 changes: 10 additions & 7 deletions resources/iam-role-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resources
import (
"fmt"
"strings"
"time"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -62,15 +63,15 @@ func ListIAMRolePolicies(sess *session.Session) ([]Resource, error) {
})
}

if *policies.IsTruncated == false {
if !*policies.IsTruncated {
break
}

polParams.Marker = policies.Marker
}
}

if *roles.IsTruncated == false {
if !*roles.IsTruncated {
break
}

Expand Down Expand Up @@ -101,11 +102,13 @@ func (e *IAMRolePolicy) Remove() error {
}

func (e *IAMRolePolicy) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("PolicyName", e.policyName)
properties.Set("role:RoleName", e.role.RoleName)
properties.Set("role:RoleID", e.role.RoleId)
properties.Set("role:Path", e.role.Path)
properties := types.NewProperties().
Set("PolicyName", e.policyName).
Set("role:RoleName", e.role.RoleName).
Set("role:RoleID", e.role.RoleId).
Set("role:Path", e.role.Path).
Set("role:LastUsed", getLastUsedDate(&e.role, time.RFC3339)).
Set("role:CreateDate", e.role.CreateDate.Format(time.RFC3339))

for _, tagValue := range e.role.Tags {
properties.SetTagWithPrefix("role", tagValue.Key, tagValue.Value)
Expand Down
30 changes: 19 additions & 11 deletions resources/iam-roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func ListIAMRoles(sess *session.Session) ([]Resource, error) {
})
}

if *resp.IsTruncated == false {
if !*resp.IsTruncated {
break
}

Expand Down Expand Up @@ -88,22 +88,30 @@ func (e *IAMRole) Remove() error {
}

func (role *IAMRole) Properties() types.Properties {
properties := types.NewProperties()
properties := types.NewProperties().
Set("CreateDate", role.role.CreateDate.Format(time.RFC3339)).
Set("LastUsedDate", getLastUsedDate(role.role, time.RFC3339)).
Set("Name", role.name).
Set("Path", role.path)

for _, tagValue := range role.role.Tags {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("CreateDate", role.role.CreateDate.Format(time.RFC3339))
if role.role.RoleLastUsed.LastUsedDate == nil {
properties.Set("LastUsedDate", role.role.CreateDate.Format(time.RFC3339))
} else {
properties.Set("LastUsedDate", role.role.RoleLastUsed.LastUsedDate.Format(time.RFC3339))
}
properties.
Set("Name", role.name).
Set("Path", role.path)

return properties
}

func (e *IAMRole) String() string {
return e.name
}

func getLastUsedDate(role *iam.Role, format string) string {
var lastUsedDate *time.Time
if role.RoleLastUsed.LastUsedDate == nil {
lastUsedDate = role.CreateDate
} else {
lastUsedDate = role.RoleLastUsed.LastUsedDate
}

return lastUsedDate.Format(format)
}