-
Notifications
You must be signed in to change notification settings - Fork 24
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
🐛 fix asset name for local, ssh conn #1740
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package clouddetect | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/rs/zerolog/log" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/inventory" | ||
"go.mondoo.com/cnquery/providers/os/connection/shared" | ||
"go.mondoo.com/cnquery/providers/os/id/aws" | ||
"go.mondoo.com/cnquery/providers/os/id/azure" | ||
"go.mondoo.com/cnquery/providers/os/id/gcp" | ||
) | ||
|
||
type ( | ||
RelatedPlatformID = string | ||
PlatformName = string | ||
PlatformID = string | ||
) | ||
|
||
type detectorFunc func(conn shared.Connection, p *inventory.Platform) (PlatformID, PlatformName, []RelatedPlatformID) | ||
|
||
var detectors = []detectorFunc{ | ||
aws.Detect, | ||
azure.Detect, | ||
gcp.Detect, | ||
} | ||
|
||
type detectResult struct { | ||
platformId string | ||
platformName string | ||
relatedPlatformIds []string | ||
} | ||
|
||
func Detect(conn shared.Connection, p *inventory.Platform) (PlatformID, PlatformName, []RelatedPlatformID) { | ||
wg := sync.WaitGroup{} | ||
wg.Add(len(detectors)) | ||
|
||
valChan := make(chan detectResult, len(detectors)) | ||
for i := range detectors { | ||
go func(f detectorFunc) { | ||
defer wg.Done() | ||
|
||
v, name, related := f(conn, p) | ||
if v != "" { | ||
valChan <- detectResult{ | ||
platformName: name, | ||
platformId: v, | ||
relatedPlatformIds: related, | ||
} | ||
} | ||
}(detectors[i]) | ||
} | ||
|
||
wg.Wait() | ||
close(valChan) | ||
|
||
platformIds := []string{} | ||
relatedPlatformIds := []string{} | ||
var name string | ||
for v := range valChan { | ||
platformIds = append(platformIds, v.platformId) | ||
name = v.platformName | ||
relatedPlatformIds = append(relatedPlatformIds, v.relatedPlatformIds...) | ||
} | ||
|
||
if len(platformIds) == 0 { | ||
return "", "", nil | ||
} else if len(platformIds) > 1 { | ||
log.Error().Strs("detected", platformIds).Msg("multiple cloud platform ids detected") | ||
return "", "", nil | ||
} | ||
|
||
return platformIds[0], name, relatedPlatformIds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package ids | ||
|
||
const ( | ||
IdDetector_Hostname = "hostname" | ||
IdDetector_MachineID = "machine-id" | ||
IdDetector_CloudDetect = "cloud-detect" | ||
IdDetector_SshHostkey = "ssh-host-key" | ||
IdDetector_AwsEcs = "aws-ecs" | ||
|
||
// FIXME: DEPRECATED, remove in v9.0 vv | ||
// this is now cloud-detect | ||
IdDetector_AwsEc2 = "aws-ec2" | ||
// ^^ | ||
|
||
// IdDetector_PlatformID = "transport-platform-id" // TODO: how does this work? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package provider | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/rs/zerolog/log" | ||
"go.mondoo.com/cnquery/providers-sdk/v1/inventory" | ||
"go.mondoo.com/cnquery/providers/os/connection/shared" | ||
"go.mondoo.com/cnquery/providers/os/detector" | ||
"go.mondoo.com/cnquery/providers/os/id/awsec2" | ||
"go.mondoo.com/cnquery/providers/os/id/awsecs" | ||
"go.mondoo.com/cnquery/providers/os/id/clouddetect" | ||
"go.mondoo.com/cnquery/providers/os/id/hostname" | ||
"go.mondoo.com/cnquery/providers/os/id/ids" | ||
"go.mondoo.com/cnquery/providers/os/id/machineid" | ||
"go.mondoo.com/cnquery/providers/os/id/sshhostkey" | ||
) | ||
|
||
type PlatformFingerprint struct { | ||
PlatformIDs []string | ||
Name string | ||
Runtime string | ||
Kind string | ||
RelatedAssets []PlatformFingerprint | ||
} | ||
|
||
type PlatformInfo struct { | ||
IDs []string | ||
Name string | ||
RelatedPlatformIDs []string | ||
} | ||
|
||
func IdentifyPlatform(conn shared.Connection, p *inventory.Platform, idDetectors []string) (*PlatformFingerprint, error) { | ||
var ok bool | ||
if p == nil { | ||
p, ok = detector.DetectOS(conn) | ||
if !ok { | ||
return nil, errors.New("cannot detect os") | ||
} | ||
} | ||
|
||
var fingerprint PlatformFingerprint | ||
var ids []string | ||
var relatedIds []string | ||
|
||
for i := range idDetectors { | ||
idDetector := idDetectors[i] | ||
platformInfo, err := GatherPlatformInfo(conn, p, idDetector) | ||
if err != nil { | ||
// we only err if we found zero platform ids, if we try multiple, a fail of an individual one is okay | ||
log.Debug().Err(err).Str("detector", string(idDetector)).Msg("could not determine platform info") | ||
continue | ||
} | ||
if len(platformInfo.IDs) > 0 { | ||
ids = append(ids, platformInfo.IDs...) | ||
} | ||
if len(platformInfo.RelatedPlatformIDs) > 0 { | ||
relatedIds = append(relatedIds, platformInfo.RelatedPlatformIDs...) | ||
} | ||
|
||
if len(platformInfo.Name) > 0 { | ||
fingerprint.Name = platformInfo.Name | ||
} else { | ||
// check if we get a name for the asset, eg. aws instance id | ||
for _, id := range platformInfo.IDs { | ||
name := GatherNameForPlatformId(id) | ||
if name != "" { | ||
fingerprint.Name = name | ||
} | ||
} | ||
} | ||
// check whether we can extract runtime and kind information | ||
for _, id := range platformInfo.IDs { | ||
runtime, kind := ExtractPlatformAndKindFromPlatformId(id) | ||
if runtime != "" { | ||
fingerprint.Runtime = runtime | ||
fingerprint.Kind = kind | ||
} | ||
} | ||
} | ||
|
||
// if we found zero platform ids something went wrong | ||
if len(ids) == 0 { | ||
return nil, errors.New("could not determine a platform identifier") | ||
} | ||
|
||
fingerprint.PlatformIDs = ids | ||
for _, v := range relatedIds { | ||
fingerprint.RelatedAssets = append(fingerprint.RelatedAssets, PlatformFingerprint{ | ||
PlatformIDs: []string{v}, | ||
Name: GatherNameForPlatformId(v), | ||
}) | ||
} | ||
|
||
log.Debug().Interface("id-detector", idDetectors).Strs("platform-ids", ids).Msg("detected platform ids") | ||
return &fingerprint, nil | ||
} | ||
|
||
func GatherNameForPlatformId(id string) string { | ||
if awsec2.IsValidMondooInstanceId(id) { | ||
structId, _ := awsec2.ParseMondooInstanceID(id) | ||
return structId.Id | ||
} else if accountID, err := awsec2.ParseMondooAccountID(id); err == nil { | ||
return fmt.Sprintf("AWS Account %s", accountID) | ||
} | ||
return "" | ||
} | ||
|
||
func ExtractPlatformAndKindFromPlatformId(id string) (string, string) { | ||
if awsec2.ParseEc2PlatformID(id) != nil { | ||
return "aws-ec2", "virtual-machine" | ||
} else if awsec2.IsValidMondooAccountId(id) { | ||
return "aws", "api" | ||
} else if awsecs.IsValidMondooECSContainerId(id) { | ||
return "aws-ecs", "container" | ||
} | ||
return "", "" | ||
} | ||
|
||
func GatherPlatformInfo(conn shared.Connection, pf *inventory.Platform, idDetector string) (*PlatformInfo, error) { | ||
var identifier string | ||
switch { | ||
case idDetector == ids.IdDetector_Hostname: | ||
// NOTE: we need to be careful with hostname's since they are not required to be unique | ||
hostname, ok := hostname.Hostname(conn, pf) | ||
if ok && len(hostname) > 0 { | ||
identifier = "//platformid.api.mondoo.app/hostname/" + hostname | ||
return &PlatformInfo{ | ||
IDs: []string{identifier}, | ||
Name: hostname, | ||
RelatedPlatformIDs: []string{}, | ||
}, nil | ||
} | ||
return &PlatformInfo{}, nil | ||
case idDetector == ids.IdDetector_MachineID: | ||
guid, hostErr := machineid.MachineId(conn, pf) | ||
if hostErr == nil && len(guid) > 0 { | ||
identifier = "//platformid.api.mondoo.app/machineid/" + guid | ||
return &PlatformInfo{ | ||
IDs: []string{identifier}, | ||
Name: "", | ||
RelatedPlatformIDs: []string{}, | ||
}, hostErr | ||
} | ||
return &PlatformInfo{}, nil | ||
case idDetector == ids.IdDetector_AwsEc2: | ||
metadata, err := awsec2.Resolve(conn, pf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ident, err := metadata.Identify() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if ident.InstanceID != "" { | ||
return &PlatformInfo{ | ||
IDs: []string{ident.InstanceID}, | ||
Name: ident.InstanceName, | ||
RelatedPlatformIDs: []string{ident.AccountID}, | ||
}, nil | ||
} | ||
return &PlatformInfo{}, nil | ||
case idDetector == ids.IdDetector_AwsEcs: | ||
metadata, err := awsecs.Resolve(conn, pf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ident, err := metadata.Identify() | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(ident.PlatformIds) != 0 { | ||
return &PlatformInfo{ | ||
IDs: ident.PlatformIds, | ||
Name: ident.Name, | ||
RelatedPlatformIDs: []string{ident.AccountPlatformID}, | ||
}, nil | ||
} | ||
return &PlatformInfo{}, nil | ||
case idDetector == ids.IdDetector_CloudDetect: | ||
identifier, name, relatedIdentifiers := clouddetect.Detect(conn, pf) | ||
if identifier != "" { | ||
return &PlatformInfo{ | ||
IDs: []string{identifier}, | ||
Name: name, | ||
RelatedPlatformIDs: relatedIdentifiers, | ||
}, nil | ||
} | ||
return &PlatformInfo{}, nil | ||
case idDetector == ids.IdDetector_SshHostkey: | ||
identifier, err := sshhostkey.Detect(conn, pf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &PlatformInfo{ | ||
IDs: identifier, | ||
Name: "", | ||
RelatedPlatformIDs: []string{}, | ||
}, nil | ||
default: | ||
return nil, errors.New(fmt.Sprintf("the provided id-detector is not supported: %s", idDetector)) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this PR is for v9 and detection, perhaps we can directly remove it here.