Skip to content

Commit

Permalink
Support for filtering with ResourceName and ResourceType
Browse files Browse the repository at this point in the history
  • Loading branch information
guessi committed Jan 2, 2025
1 parent f31d594 commit 6e9fb21
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ var Flags = []cli.Flag{
Usage: "Filter events with user name",
Required: false,
},
&cli.StringFlag{
Name: "resource-name",
Usage: "Filter events with resource name",
Required: false,
},
&cli.StringFlag{
Name: "resource-type",
Usage: "Filter events with resource type",
Required: false,
},
&cli.StringFlag{
Name: "event-source",
Usage: "Filter events with event source",
Expand Down
2 changes: 2 additions & 0 deletions cmd/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func Wrapper(c *cli.Context) {
c.String("event-id"),
c.String("event-name"),
c.String("user-name"),
c.String("resource-name"),
c.String("resource-type"),
c.String("event-source"),
c.String("access-key-id"),
c.Bool("read-only"),
Expand Down
4 changes: 3 additions & 1 deletion pkg/utils/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/jedib0t/go-pretty/v6/text"
)

func EventsHandler(profile, region string, startTime, endTime *time.Time, eventId, eventName, userName, eventSource, accessKeyId string, readOnly, noReadOnly bool, maxResults int, errorOnly, truncateUserName, truncateUserAgent bool) {
func EventsHandler(profile, region string, startTime, endTime *time.Time, eventId, eventName, userName, resourceName, resourceType, eventSource, accessKeyId string, readOnly, noReadOnly bool, maxResults int, errorOnly, truncateUserName, truncateUserAgent bool) {
// do nothing if maxResults is invalid input
if maxResults <= 0 {
log.Fatalf("Can not pass --max-results with a value lower or equal to 0.\n")
Expand Down Expand Up @@ -45,6 +45,8 @@ func EventsHandler(profile, region string, startTime, endTime *time.Time, eventI
readOnly,
noReadOnly,
userName,
resourceName,
resourceType,
eventSource,
accessKeyId,
),
Expand Down
20 changes: 19 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func LookupEvents(ctx context.Context, svc *cloudtrail.Client, input *cloudtrail
return events[:returnSize], nil
}

func composeLookupAttributesInput(eventId, eventName string, readOnly, noReadOnly bool, userName, eventSource, accessKeyId string) []ctypes.LookupAttribute {
func composeLookupAttributesInput(eventId, eventName string, readOnly, noReadOnly bool, userName, resourceName, resourceType, eventSource, accessKeyId string) []ctypes.LookupAttribute {
lookupAttributesInput := []ctypes.LookupAttribute{}

// LookupAttributeKeyEventId
Expand Down Expand Up @@ -129,6 +129,24 @@ func composeLookupAttributesInput(eventId, eventName string, readOnly, noReadOnl
lookupAttributesInput = append(lookupAttributesInput, attrUserName)
}

// LookupAttributeKeyResourceName
if len(resourceName) > 0 {
attrResourceName := ctypes.LookupAttribute{
AttributeKey: ctypes.LookupAttributeKeyResourceName,
AttributeValue: aws.String(resourceName),
}
lookupAttributesInput = append(lookupAttributesInput, attrResourceName)
}

// LookupAttributeKeyResourceType
if len(resourceType) > 0 {
attrResourceType := ctypes.LookupAttribute{
AttributeKey: ctypes.LookupAttributeKeyResourceType,
AttributeValue: aws.String(resourceType),
}
lookupAttributesInput = append(lookupAttributesInput, attrResourceType)
}

// LookupAttributeKeyEventSource
const EVENT_SOURCE_SUFFIX = ".amazonaws.com"
if len(eventSource) > len(EVENT_SOURCE_SUFFIX) && strings.HasSuffix(eventSource, EVENT_SOURCE_SUFFIX) {
Expand Down

0 comments on commit 6e9fb21

Please sign in to comment.