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 439bb1a
Show file tree
Hide file tree
Showing 29 changed files with 441 additions and 163 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
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 439bb1a

Please sign in to comment.