Skip to content

Commit

Permalink
update(plugins/cloudtrail): Remove a dependency
Browse files Browse the repository at this point in the history
Remove our dependency on str2duration. Fix our interval regex.

Signed-off-by: Gerald Combs <[email protected]>
  • Loading branch information
geraldcombs committed May 10, 2023
1 parent e60fa02 commit c42c0b4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 0 additions & 1 deletion plugins/cloudtrail/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ require (
github.com/aws/smithy-go v1.13.3
github.com/falcosecurity/plugin-sdk-go v0.6.2
github.com/valyala/fastjson v1.6.3
github.com/xhit/go-str2duration/v2 v2.1.0
)
2 changes: 0 additions & 2 deletions plugins/cloudtrail/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
29 changes: 21 additions & 8 deletions plugins/cloudtrail/pkg/cloudtrail/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ package cloudtrail

import (
"regexp"
"strconv"
"time"

"github.com/xhit/go-str2duration/v2"
)

var RFC3339Simple = "2006-01-02T03:04:05Z"
Expand All @@ -30,10 +29,24 @@ func parseEndpoint(endpoint string) (time.Time, error) {
var endpointTime time.Time
var err error

epRE := regexp.MustCompile(`^\d+[wdhms]$`)
if (epRE.MatchString(endpoint)) {
duration, err := str2duration.ParseDuration(endpoint)
if (err == nil) {
durationRE := regexp.MustCompile(`^(\d+)([wdhms])$`)
matches := durationRE.FindStringSubmatch(endpoint)
if matches != nil {
durI, err := strconv.Atoi(matches[1])
if err == nil {
duration := time.Duration(durI)
switch matches[2] {
case "w":
duration *= time.Hour * 24 * 7
case "d":
duration *= time.Hour * 24
case "h":
duration *= time.Hour
case "m":
duration *= time.Minute
case "s":
duration *= time.Second
}
endpointTime = utc.Add(- duration)
}
} else {
Expand All @@ -49,12 +62,12 @@ func ParseInterval(interval string) (time.Time, time.Time, error) {
var err error

// First, see if we have an interval.
intervalRE := regexp.MustCompile(`(.*)(\s*-\s*)(\d+[wdhms]$|\d{4}-\d{2}\d{2}[^Z]*Z)`)
intervalRE := regexp.MustCompile(`(.*)\s*-\s*(\d+[wdhms]|\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)$`)
matches := intervalRE.FindStringSubmatch(interval)
if matches != nil {
startTime, err = parseEndpoint(matches[1])
if err == nil {
endTime, err = parseEndpoint(matches[3])
endTime, err = parseEndpoint(matches[2])
}
} else {
startTime, err = parseEndpoint(interval)
Expand Down

0 comments on commit c42c0b4

Please sign in to comment.