Skip to content

Commit

Permalink
✨ gcp snapshot connection for v9
Browse files Browse the repository at this point in the history
Fixes #1613
Fixes #1627

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Sep 4, 2023
1 parent ab89a0f commit 59a9fd4
Show file tree
Hide file tree
Showing 30 changed files with 470 additions and 175 deletions.
21 changes: 0 additions & 21 deletions _motor/providers/os/snapshot/localcmd.go

This file was deleted.

23 changes: 15 additions & 8 deletions apps/cnquery/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,23 @@ func (c *cnqueryPlugin) RunQuery(conf *run.RunQueryConfig, runtime *providers.Ru
return nil
}

err := runtime.Connect(&pp.ConnectReq{
Features: config.Features,
Asset: conf.Inventory.Spec.Assets[0],
Upstream: nil,
})
if err != nil {
return err
assetList := []*inventory.Asset{}
assetList = append(assetList, conf.Inventory.Spec.Assets...)

if conf.Inventory.Spec.Assets[0].Connections[0].Discover != nil {
err := runtime.Connect(&pp.ConnectReq{
Features: config.Features,
Asset: conf.Inventory.Spec.Assets[0],
Upstream: nil,
})
if err != nil {
return err
}
if runtime.Provider.Connection.Inventory != nil {
assetList = append(assetList, runtime.Provider.Connection.Inventory.Spec.Assets...)
}
}

assetList := runtime.Provider.Connection.Inventory.Spec.Assets
log.Debug().Msgf("resolved %d assets", len(assetList))

filteredAssets := []*inventory.Asset{}
Expand Down
41 changes: 29 additions & 12 deletions providers/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ import (
"go.mondoo.com/cnquery/providers-sdk/v1/resources"
coreconf "go.mondoo.com/cnquery/providers/core/config"
core "go.mondoo.com/cnquery/providers/core/provider"
gcpconf "go.mondoo.com/cnquery/providers/gcp/config"
gcp "go.mondoo.com/cnquery/providers/gcp/provider"
osconf "go.mondoo.com/cnquery/providers/os/config"
os "go.mondoo.com/cnquery/providers/os/provider"
)

var BuiltinCoreID = coreconf.Config.ID

//go:embed core/resources/core.resources.json
var coreInfo []byte

// //go:embed os/resources/os.resources.json
// var osInfo []byte
//go:embed gcp.resources.json
var gcpInfo []byte

//go:embed os.resources.json
var osInfo []byte

// //go:embed network/resources/network.resources.json
// var networkInfo []byte
Expand All @@ -47,16 +54,26 @@ var builtinProviders = map[string]*builtinProvider{
},
Config: &coreconf.Config,
},
// osconf.Config.ID: {
// Runtime: &RunningProvider{
// Name: osconf.Config.Name,
// ID: osconf.Config.ID,
// Plugin: os.Init(),
// Schema: MustLoadSchema("os", osInfo),
// isClosed: false,
// },
// Config: &osconf.Config,
// },
osconf.Config.ID: {
Runtime: &RunningProvider{
Name: osconf.Config.Name,
ID: osconf.Config.ID,
Plugin: os.Init(),
Schema: MustLoadSchema("os", osInfo),
isClosed: false,
},
Config: &osconf.Config,
},
gcpconf.Config.ID: {
Runtime: &RunningProvider{
Name: gcpconf.Config.Name,
ID: gcpconf.Config.ID,
Plugin: gcp.Init(),
Schema: MustLoadSchema("gcp", gcpInfo),
isClosed: false,
},
Config: &gcpconf.Config,
},
// networkconf.Config.ID: {
// Runtime: &RunningProvider{
// Name: networkconf.Config.Name,
Expand Down
1 change: 1 addition & 0 deletions providers/gcp.resources.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions providers/gcp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ var Config = plugin.Provider{
Default: "",
Desc: "The path to the service account credentials to access the APIs with",
},
{
Long: "project-id",
Type: plugin.FlagType_String,
Default: "",
Desc: "specify the GCP project ID where the target instance is located (only used for snapshots)",
},
{
Long: "zone",
Type: plugin.FlagType_String,
Default: "",
Desc: "specify the GCP zone where the target instance is located (only used for snapshots)",
},
},
},
},
Expand Down
34 changes: 20 additions & 14 deletions providers/gcp/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ package connection

import (
"errors"

"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers-sdk/v1/vault"
"go.mondoo.com/cnquery/providers/gcp/connection/shared"
)

const (
Gcp shared.ConnectionType = "gcp"
)

type ResourceType int
Expand All @@ -18,6 +24,7 @@ const (
Project
Organization
Folder
Snapshot
)

type GcpConnection struct {
Expand Down Expand Up @@ -47,10 +54,7 @@ func NewGcpConnection(id uint32, asset *inventory.Asset, conf *inventory.Config)
cred = conf.Credentials[0]
}
if conf.Type == "gcp" {
// FIXME: DEPRECATED, update in v8.0 vv
// The options "project" and "organization" have been deprecated in favor of project-id and organization-id
if conf.Options == nil || (conf.Options["project-id"] == "" && conf.Options["project"] == "" && conf.Options["organization-id"] == "" && conf.Options["organization"] == "" && conf.Options["folder-id"] == "") {
// ^^
if conf.Options == nil || (conf.Options["project-id"] == "" && conf.Options["organization-id"] == "" && conf.Options["folder-id"] == "") {
return nil, errors.New("google provider requires a gcp organization id, gcp project id or google workspace customer id. please set option `project-id` or `organization-id` or `customer-id` or `folder-id`")
}
} else {
Expand All @@ -59,24 +63,18 @@ func NewGcpConnection(id uint32, asset *inventory.Asset, conf *inventory.Config)

var resourceType ResourceType
var resourceID string
if conf.Options["project-id"] != "" {
if conf.Options["project-id"] != "" && conf.Options["snapshot-name"] == "" {
resourceType = Project
resourceID = conf.Options["project-id"]

// FIXME: DEPRECATED, remove in v8.0 vv
// The options "project" and "organization" have been deprecated in favor of project-id and organization-id
} else if conf.Options["project"] != "" {
resourceType = Project
resourceID = conf.Options["project"]
// ^^

} else if conf.Options["organization-id"] != "" {
resourceType = Organization
resourceID = conf.Options["organization-id"]

} else if conf.Options["folder-id"] != "" {
resourceType = Folder
resourceID = conf.Options["folder-id"]
} else if conf.Options["snapshot-name"] != "" {
resourceType = Snapshot
resourceID = conf.Options["snapshot-name"]
}

var override string
Expand Down Expand Up @@ -119,3 +117,11 @@ func (c *GcpConnection) ID() uint32 {
func (c *GcpConnection) Asset() *inventory.Asset {
return c.asset
}

func (c *GcpConnection) Type() shared.ConnectionType {
return Gcp
}

func (c *GcpConnection) Config() *inventory.Config {
return c.Conf
}
Loading

0 comments on commit 59a9fd4

Please sign in to comment.