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

⭐ Create AWS resource aws.ec2.instances {iamInstanceProfile} #4767

Merged
merged 20 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions providers/aws/resources/aws.lr
Original file line number Diff line number Diff line change
Expand Up @@ -3014,6 +3014,8 @@ private aws.ec2.instance @defaults("instanceId region state instanceType archite
instanceType string
// Tags on the instance
tags map[string]string
// Instance profile of the instance.
mm-weber marked this conversation as resolved.
Show resolved Hide resolved
iamInstanceProfile() dict
mm-weber marked this conversation as resolved.
Show resolved Hide resolved
// Image that was used for the instance
image() aws.ec2.image
// Launch time of the instance
Expand Down
14 changes: 14 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.

1 change: 1 addition & 0 deletions providers/aws/resources/aws.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ resources:
httpTokens: {}
hypervisor:
min_mondoo_version: 9.0.0
iamInstanceProfile: {}
image: {}
instanceId: {}
instanceLifecycle:
Expand Down
36 changes: 36 additions & 0 deletions providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,12 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, regionV
if err != nil {
return nil, err
}

iamInstanceProfile, err := convert.JsonToDict(instance.IamInstanceProfile)
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))
Expand Down Expand Up @@ -899,6 +905,7 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, regionV
"rootDeviceType": llx.StringData(string(instance.RootDeviceType)),
"state": llx.StringData(string(instance.State.Name)),
"stateReason": llx.MapData(stateReason, types.Any),
"iamInstanceProfile": llx.MapData(iamInstanceProfile, types.Any),
"stateTransitionReason": llx.StringDataPtr(instance.StateTransitionReason),
"stateTransitionTime": llx.TimeData(stateTransitionTime),
"tags": llx.MapData(Ec2TagsToMap(instance.Tags), types.String),
Expand Down Expand Up @@ -1310,6 +1317,35 @@ func (a *mqlAwsEc2Instance) instanceStatus() (interface{}, error) {
return res, nil
}

func (a *mqlAwsEc2Instance) iamInstanceProfile() (interface{}, error) {
var res interface{}
instanceId := a.InstanceId.Data
region := a.Region.Data
conn := a.MqlRuntime.Connection.(*connection.AwsConnection)

svc := conn.Ec2(region)
ctx := context.Background()

instanceStatus, err := svc.DescribeInstanceStatus(ctx, &ec2.DescribeInstanceStatusInput{
InstanceIds: []string{instanceId},
IncludeAllInstances: aws.Bool(true),
})
if err != nil {
return nil, err
}

if len(instanceStatus.InstanceStatuses) > 0 {
if instanceId == convert.ToString(instanceStatus.InstanceStatuses[0].InstanceId) {
res, err = convert.JsonToDict(instanceStatus.InstanceStatuses[0])
if err != nil {
return nil, err
}
}
}

return res, nil
}

func (a *mqlAwsEc2) volumes() ([]interface{}, error) {
conn := a.MqlRuntime.Connection.(*connection.AwsConnection)
res := []interface{}{}
Expand Down
Loading