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

🐛 Assign SSM connection if available to ec2 instances. #4849

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
29 changes: 15 additions & 14 deletions providers/aws/resources/discovery_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
MondooContainerNameLabelKey = "mondoo.com/container-name"
MondooClusterNameLabelKey = "mondoo.com/cluster-name"
MondooTaskArnLabelKey = "mondoo.com/task-arn"
MondooSsmConnection = "mondoo.com/ssm-connection"
)

type mqlObject struct {
Expand Down Expand Up @@ -305,14 +306,13 @@ func addConnectionInfoToEc2Asset(instance *mqlAwsEc2Instance, accountId string,
info.image = &instance.GetImage().Data.Id.Data
}
addMondooLabels(info, asset)

imageName := ""
if instance.GetImage().Data != nil {
imageName = instance.GetImage().Data.Name.Data
}
probableUsername := getProbableUsernameFromImageName(imageName)
// if there is a public ip & it is running, we assume ssh is an option
if instance.PublicIp.Data != "" && instance.State.Data == string(types.InstanceStateNameRunning) {
imageName := ""
if instance.GetImage().Data != nil {
imageName = instance.GetImage().Data.Name.Data
}
probableUsername := getProbableUsernameFromImageName(imageName)
asset.Connections = []*inventory.Config{{
Type: "ssh",
Host: instance.PublicIp.Data,
Expand All @@ -330,14 +330,15 @@ func addConnectionInfoToEc2Asset(instance *mqlAwsEc2Instance, accountId string,
"instance": instance.InstanceId.Data,
},
}}
if instance.GetSsm() != nil && instance.GetSsm().Data != nil && len(instance.GetSsm().Data.(map[string]interface{})["InstanceInformationList"].([]interface{})) > 0 {
if instance.GetSsm().Data.(map[string]interface{})["InstanceInformationList"].([]interface{})[0].(map[string]interface{})["PingStatus"] == "Online" {
asset.Connections[0].Credentials = append(asset.Connections[0].Credentials, &vault.Credential{
User: probableUsername,
Type: vault.CredentialType_aws_ec2_ssm_session,
})
asset.Labels["mondoo.com/ssm-connection"] = "Online"
}
}
// if the ssm agent indicates it is online, we assume ssm is an option
if instance.GetSsm() != nil && instance.GetSsm().Data != nil && len(instance.GetSsm().Data.(map[string]interface{})["InstanceInformationList"].([]interface{})) > 0 {
if instance.GetSsm().Data.(map[string]interface{})["InstanceInformationList"].([]interface{})[0].(map[string]interface{})["PingStatus"] == "Online" {
asset.Connections[0].Credentials = append(asset.Connections[0].Credentials, &vault.Credential{
User: probableUsername,
Type: vault.CredentialType_aws_ec2_ssm_session,
})
asset.Labels[MondooSsmConnection] = "Online"
}
}
return asset
Expand Down
Loading