Skip to content

Commit

Permalink
✨ add state transition time to aws ec2 instance (#1822)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey authored Oct 1, 2023
1 parent 280e4b3 commit 6f7c407
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions providers/aws/resources/aws.lr
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,8 @@ private aws.ec2.instance @defaults("arn state") {
privateDnsName string
// Keypair associated with the instance
keypair() aws.ec2.keypair
// Time when the last state transition occurred
stateTransitionTime time
}

// Amazon EC2 Key Pair
Expand Down
12 changes: 12 additions & 0 deletions providers/aws/resources/aws.lr.go

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

2 changes: 2 additions & 0 deletions providers/aws/resources/aws.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,8 @@ resources:
state: {}
stateReason: {}
stateTransitionReason: {}
stateTransitionTime:
min_mondoo_version: 9.0.0
tags: {}
vpc: {}
is_private: true
Expand Down
14 changes: 13 additions & 1 deletion providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package resources
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand Down Expand Up @@ -694,7 +696,16 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
if err != nil {
return nil, err
}

var stateTransitionTime time.Time
reg := regexp.MustCompile(`.*\((\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}) GMT\)`)
timeString := reg.FindStringSubmatch(convert.ToString(instance.StateTransitionReason))
if len(timeString) == 2 {
stateTransitionTime, err = time.Parse(time.DateTime, timeString[1])
if err != nil {
log.Error().Err(err).Msg("cannot parse state transition time for ec2 instance")
stateTransitionTime = llx.NeverPastTime
}
}
args := map[string]*llx.RawData{
"platformDetails": llx.StringData(convert.ToString(instance.PlatformDetails)),
"arn": llx.StringData(fmt.Sprintf(ec2InstanceArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.InstanceId))),
Expand All @@ -715,6 +726,7 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
"launchTime": llx.TimeData(toTime(instance.LaunchTime)),
"privateIp": llx.StringData(convert.ToString(instance.PrivateIpAddress)),
"privateDnsName": llx.StringData(convert.ToString(instance.PrivateDnsName)),
"stateTransitionTime": llx.TimeData(stateTransitionTime),
}

if instance.ImageId != nil {
Expand Down

0 comments on commit 6f7c407

Please sign in to comment.