Skip to content

Commit

Permalink
🧹 support sysinfo without providers (#2969)
Browse files Browse the repository at this point in the history
* 🧹 support sysinfo without providers

Normally we require to download and install the OS provider when we want to gather the OS info. This is now no longer necessary. Instead we grab a few very isolated libraries from the OS provider itself and embed them into the runtime. This yielded no significant increase in the runtime (500kb). While we want to further decrease this size over time, it is important to support scanning other provider targets without requiring the OS provider to be pulled.

Signed-off-by: Dominik Richter <[email protected]>

* 🧹 decouple os connection type constant
* 🧹 extract os filesystem connection into submodule

Signed-off-by: Dominik Richter <[email protected]>

---------

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Jan 7, 2024
1 parent 607021d commit 1b1aecd
Show file tree
Hide file tree
Showing 32 changed files with 177 additions and 234 deletions.
4 changes: 2 additions & 2 deletions apps/cnquery/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"go.mondoo.com/cnquery/v9"
"go.mondoo.com/cnquery/v9/cli/config"
cli_errors "go.mondoo.com/cnquery/v9/cli/errors"
"go.mondoo.com/cnquery/v9/cli/sysinfo"
cnquery_providers "go.mondoo.com/cnquery/v9/providers"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/sysinfo"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream"
"go.mondoo.com/ranger-rpc"
"go.mondoo.com/ranger-rpc/plugins/authentication/statictoken"
Expand Down Expand Up @@ -61,7 +61,7 @@ func register(token string, annotations map[string]string) error {
var credential *upstream.ServiceAccountCredentials

// determine information about the client
sysInfo, err := sysinfo.GatherSystemInfo()
sysInfo, err := sysinfo.Get()
if err != nil {
return cli_errors.NewCommandError(errors.Wrap(err, "could not gather client information"), 1)
}
Expand Down
4 changes: 2 additions & 2 deletions apps/cnquery/cmd/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/spf13/viper"
"go.mondoo.com/cnquery/v9/cli/config"
cli_errors "go.mondoo.com/cnquery/v9/cli/errors"
"go.mondoo.com/cnquery/v9/cli/sysinfo"
cnquery_providers "go.mondoo.com/cnquery/v9/providers"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/sysinfo"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -49,7 +49,7 @@ ensure the credentials cannot be used in the future.
config.DisplayUsedConfig()

// determine information about the client
sysInfo, err := sysinfo.GatherSystemInfo()
sysInfo, err := sysinfo.Get()
if err != nil {
return errors.Wrap(err, "could not gather client information")
}
Expand Down
2 changes: 1 addition & 1 deletion apps/cnquery/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"go.mondoo.com/cnquery/v9/cli/config"
cli_errors "go.mondoo.com/cnquery/v9/cli/errors"
"go.mondoo.com/cnquery/v9/cli/providers"
"go.mondoo.com/cnquery/v9/cli/sysinfo"
"go.mondoo.com/cnquery/v9/cli/theme"
"go.mondoo.com/cnquery/v9/logger"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/sysinfo"
"go.mondoo.com/ranger-rpc"
"go.mondoo.com/ranger-rpc/plugins/scope"
)
Expand Down
4 changes: 2 additions & 2 deletions apps/cnquery/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"go.mondoo.com/cnquery/v9"
"go.mondoo.com/cnquery/v9/cli/config"
cli_errors "go.mondoo.com/cnquery/v9/cli/errors"
"go.mondoo.com/cnquery/v9/cli/sysinfo"
"go.mondoo.com/cnquery/v9/cli/theme"
"go.mondoo.com/cnquery/v9/providers"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/sysinfo"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream/health"
"go.mondoo.com/ranger-rpc"
Expand Down Expand Up @@ -64,7 +64,7 @@ Status sends a ping to Mondoo Platform to verify the credentials.
return cli_errors.NewCommandError(errors.Wrap(err, "failed to set up Mondoo API client"), 1)
}

sysInfo, err := sysinfo.GatherSystemInfo()
sysInfo, err := sysinfo.Get()
if err == nil {
s.Client.Platform = sysInfo.Platform
s.Client.Hostname = sysInfo.Hostname
Expand Down
132 changes: 0 additions & 132 deletions cli/sysinfo/sysinfo.go

This file was deleted.

78 changes: 78 additions & 0 deletions providers-sdk/v1/sysinfo/sysinfo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package sysinfo

import (
"errors"

"github.com/rs/zerolog/log"

"go.mondoo.com/cnquery/v9"
"go.mondoo.com/cnquery/v9/cli/execruntime"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/providers/os/connection/local"
"go.mondoo.com/cnquery/v9/providers/os/detector"
"go.mondoo.com/cnquery/v9/providers/os/id"
"go.mondoo.com/cnquery/v9/providers/os/id/hostname"
"go.mondoo.com/cnquery/v9/providers/os/resources/networkinterface"
)

type SystemInfo struct {
Version string `json:"version,omitempty"`
Build string `json:"build,omitempty"`
Platform *inventory.Platform `json:"platform,omitempty"`
IP string `json:"ip,omitempty"`
Hostname string `json:"platform_hostname,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
PlatformId string `json:"platform_id,omitempty"`
}

func Get() (*SystemInfo, error) {
log.Debug().Msg("Gathering system information")

sysInfo := &SystemInfo{
Version: cnquery.GetVersion(),
Build: cnquery.GetBuild(),
}

asset := inventory.Asset{
Connections: []*inventory.Config{{
Type: "local",
Discover: &inventory.Discovery{Targets: []string{"none"}},
}},
}

conn := local.NewConnection(0, &inventory.Config{
Type: "local",
}, &asset)

fingerprint, err := id.IdentifyPlatform(conn, asset.Platform, asset.IdDetector)
if err == nil {
if len(fingerprint.PlatformIDs) > 0 {
sysInfo.PlatformId = fingerprint.PlatformIDs[0]
}
}

var ok bool
sysInfo.Platform, ok = detector.DetectOS(conn)
if !ok {
return nil, errors.New("failed to detect the OS")
}

sysInfo.Hostname, _ = hostname.Hostname(conn, sysInfo.Platform)

// determine ip address
ipAddr, err := networkinterface.GetOutboundIP()
if err == nil {
sysInfo.IP = ipAddr.String()
}

// detect the execution runtime
execEnv := execruntime.Detect()
sysInfo.Labels = map[string]string{
"environment": execEnv.Id,
}

return sysInfo, nil
}
6 changes: 3 additions & 3 deletions providers/aws/connection/awsec2ebsconn/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"go.mondoo.com/cnquery/v9/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v9/providers-sdk/v1/util/convert"
awsec2ebstypes "go.mondoo.com/cnquery/v9/providers/aws/connection/awsec2ebsconn/types"
"go.mondoo.com/cnquery/v9/providers/os/connection"
"go.mondoo.com/cnquery/v9/providers/os/connection/fs"
"go.mondoo.com/cnquery/v9/providers/os/connection/shared"
"go.mondoo.com/cnquery/v9/providers/os/connection/snapshot"
"go.mondoo.com/cnquery/v9/providers/os/detector"
Expand All @@ -34,7 +34,7 @@ const (
type AwsEbsConnection struct {
id uint32
asset *inventory.Asset
FsProvider *connection.FileSystemConnection
FsProvider *fs.FileSystemConnection
scannerRegionEc2svc *ec2.Client
targetRegionEc2svc *ec2.Client
config aws.Config
Expand Down Expand Up @@ -182,7 +182,7 @@ func NewAwsEbsConnection(id uint32, conf *inventory.Config, asset *inventory.Ass
log.Debug().Interface("info", c.target).Str("type", c.targetType).Msg("target")
// Create and initialize fs provider
conf.Options["path"] = volumeMounter.ScanDir
fsConn, err := connection.NewFileSystemConnection(id, &inventory.Config{
fsConn, err := fs.NewConnection(id, &inventory.Config{
Type: "filesystem",
Path: volumeMounter.ScanDir,
PlatformId: conf.PlatformId,
Expand Down
4 changes: 2 additions & 2 deletions providers/azure/connection/azureinstancesnapshot/lun.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v9/providers/os/connection"
"go.mondoo.com/cnquery/v9/providers/os/connection/local"
)

type deviceInfo struct {
Expand Down Expand Up @@ -52,7 +52,7 @@ func (a *azureScannerInstance) getAvailableLun(mountedDevices []deviceInfo) (int

// https://learn.microsoft.com/en-us/azure/virtual-machines/linux/azure-to-guest-disk-mapping
// for more information. we want to find the LUNs of the data disks and their mount location
func getMountedDevices(localConn *connection.LocalConnection) ([]deviceInfo, error) {
func getMountedDevices(localConn *local.LocalConnection) ([]deviceInfo, error) {
cmd, err := localConn.RunCommand("lsscsi --brief")
if err != nil {
return nil, err
Expand Down
11 changes: 6 additions & 5 deletions providers/azure/connection/azureinstancesnapshot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"go.mondoo.com/cnquery/v9/providers-sdk/v1/vault"
"go.mondoo.com/cnquery/v9/providers/azure/connection/auth"
"go.mondoo.com/cnquery/v9/providers/azure/connection/shared"
"go.mondoo.com/cnquery/v9/providers/os/connection"
"go.mondoo.com/cnquery/v9/providers/os/connection/fs"
"go.mondoo.com/cnquery/v9/providers/os/connection/local"
"go.mondoo.com/cnquery/v9/providers/os/connection/snapshot"
"go.mondoo.com/cnquery/v9/providers/os/detector"
"go.mondoo.com/cnquery/v9/providers/os/id/azcompute"
Expand Down Expand Up @@ -44,7 +45,7 @@ type mountInfo struct {
diskName string
}

func determineScannerInstanceInfo(localConn *connection.LocalConnection, token azcore.TokenCredential) (*azureScannerInstance, error) {
func determineScannerInstanceInfo(localConn *local.LocalConnection, token azcore.TokenCredential) (*azureScannerInstance, error) {
pf, detected := detector.DetectOS(localConn)
if !detected {
return nil, errors.New("could not detect platform")
Expand Down Expand Up @@ -117,7 +118,7 @@ func NewAzureSnapshotConnection(id uint32, conf *inventory.Config, asset *invent
if err != nil {
return nil, err
}
localConn := connection.NewLocalConnection(id, conf, asset)
localConn := local.NewConnection(id, conf, asset)

// check if we run on an azure instance
scanner, err := determineScannerInstanceInfo(localConn, token)
Expand Down Expand Up @@ -230,7 +231,7 @@ func NewAzureSnapshotConnection(id uint32, conf *inventory.Config, asset *invent

conf.Options["path"] = volumeMounter.ScanDir
// create and initialize fs provider
fsConn, err := connection.NewFileSystemConnection(id, &inventory.Config{
fsConn, err := fs.NewConnection(id, &inventory.Config{
Path: volumeMounter.ScanDir,
PlatformId: conf.PlatformId,
Options: conf.Options,
Expand Down Expand Up @@ -260,7 +261,7 @@ func NewAzureSnapshotConnection(id uint32, conf *inventory.Config, asset *invent
}

type AzureSnapshotConnection struct {
*connection.FileSystemConnection
*fs.FileSystemConnection
opts map[string]string
volumeMounter *snapshot.VolumeMounter
snapshotCreator *SnapshotCreator
Expand Down
1 change: 0 additions & 1 deletion providers/builtin_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,4 @@ func init() {
// },
// Config: &osconf.Config,
// }

}
Loading

0 comments on commit 1b1aecd

Please sign in to comment.