Skip to content

Commit

Permalink
🐛 Fix vagrant asset information (#1807)
Browse files Browse the repository at this point in the history
Fixes #1761

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Sep 21, 2023
1 parent 55a1586 commit 6125c6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
10 changes: 10 additions & 0 deletions providers-sdk/v1/util/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"errors"
"fmt"
"go/format"
"os"
Expand Down Expand Up @@ -170,6 +171,15 @@ func rewireDependencies(providers []string) {
deps := ""
replace := ""
for _, provider := range providers {
_, err := os.Stat("providers/" + provider + "/go.mod")
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Info().Str("provider", provider).Msg("skipping provider without go.mod")
continue
} else {
log.Fatal().Err(err).Str("provider", provider).Msg("failed to stat provider go.mod")
}
}
// we don't care about the specific version for dev
deps += "\n\tgo.mondoo.com/cnquery/providers/" + provider + " v0.0.0"
replace += "\nreplace go.mondoo.com/cnquery/providers/" + provider + " => ./providers/" + provider
Expand Down
26 changes: 11 additions & 15 deletions providers/os/connection/vagrant.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.mondoo.com/cnquery/providers-sdk/v1/vault"
"go.mondoo.com/cnquery/providers/os/connection/shared"
"go.mondoo.com/cnquery/providers/os/connection/vagrant"
"go.mondoo.com/cnquery/providers/os/id/ids"
)

const (
Expand Down Expand Up @@ -86,22 +87,23 @@ func resolveVagrantSshConf(id uint32, conf *inventory.Config, root *inventory.As
return nil, err
}

a, err := newVagrantAsset(id, vmSshConfig[k], conf)
err = migrateVagrantAssetToSsh(id, vmSshConfig[k], conf, root)
if err != nil {
return nil, err
}
return NewSshConnection(id, a.Connections[0], a)
return NewSshConnection(id, root.Connections[0], root)
}

func newVagrantAsset(id uint32, sshConfig *vagrant.VagrantVmSSHConfig, rootTransportConfig *inventory.Config) (*inventory.Asset, error) {
func migrateVagrantAssetToSsh(id uint32, sshConfig *vagrant.VagrantVmSSHConfig, rootTransportConfig *inventory.Config, asset *inventory.Asset) error {
if sshConfig == nil {
return nil, errors.New("missing vagrant ssh config")
return errors.New("missing vagrant ssh config")
}

cc := &inventory.Config{
// TODO: do we need to support winrm?
Backend: "ssh",
Type: "ssh",
Runtime: "vagrant",
Host: sshConfig.HostName,
Insecure: strings.ToLower(sshConfig.StrictHostKeyChecking) == "no",

Expand All @@ -112,19 +114,13 @@ func newVagrantAsset(id uint32, sshConfig *vagrant.VagrantVmSSHConfig, rootTrans
// load secret
credential, err := vault.NewPrivateKeyCredentialFromPath(sshConfig.User, sshConfig.IdentityFile, "")
if err != nil {
return nil, err
return err
}
cc.Credentials = append(cc.Credentials, credential)

assetObj := &inventory.Asset{
Name: sshConfig.Host,
PlatformIds: []string{},
Connections: []*inventory.Config{cc},
Platform: &inventory.Platform{
// FIXME: use const like before?
Kind: "vm",
},
}
asset.Name = sshConfig.Host
asset.Connections = []*inventory.Config{cc}
asset.IdDetector = []string{ids.IdDetector_Hostname, ids.IdDetector_SshHostkey, ids.IdDetector_MachineID}

return assetObj, nil
return nil
}
4 changes: 4 additions & 0 deletions providers/os/provider/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (s *Service) detect(asset *inventory.Asset, conn shared.Connection) error {
if !ok {
return errors.New("failed to detect OS")
}
if asset.Connections[0].Runtime == "vagrant" {
// detect overrides this
asset.Platform.Kind = "virtualmachine"
}

var detectors map[string]struct{}
if asset.Platform.Kind != "container-image" {
Expand Down
4 changes: 3 additions & 1 deletion providers/os/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
conn, err = connection.NewSshConnection(s.lastConnectionID, conf, asset)
fingerprint, err := IdentifyPlatform(conn, asset.Platform, []string{ids.IdDetector_Hostname, ids.IdDetector_CloudDetect, ids.IdDetector_SshHostkey})
if err == nil {
asset.Name = fingerprint.Name
if conn.Asset().Connections[0].Runtime != "vagrant" {
asset.Name = fingerprint.Name
}
asset.PlatformIds = fingerprint.PlatformIDs
}

Expand Down

0 comments on commit 6125c6c

Please sign in to comment.