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 11, 2023
1 parent f7bc837 commit d2a5a1d
Show file tree
Hide file tree
Showing 26 changed files with 440 additions and 164 deletions.
11 changes: 9 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
"type": "go",
"request": "attach",
"mode": "remote",
"debugAdapter": "dlv-dap",
"remotePath": "${workspaceFolder}",
"port": 2345,
"host": "127.0.0.1"
"port": 12345,
"host": "35.241.232.183",
"substitutePath":[
{
"from": "${workspaceFolder}",
"to": "/home/christian/workspace/cnquery"
}
]
},
{
"name": "Launch Package",
Expand Down
21 changes: 0 additions & 21 deletions _motor/providers/os/snapshot/localcmd.go

This file was deleted.

8 changes: 8 additions & 0 deletions apps/cnquery/cmd/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func (c *cnqueryPlugin) RunQuery(conf *run.RunQueryConfig, runtime *providers.Ru
}
}

// FIXME: workaround for gcp-snapshot
// For a gcp-snapshot asset, we start with a GCP connection.
// This get's overriden by a filesystem connection. The fileswystem connection is what we need for the scan
// But later, we need the GCP runtime to cleanup the snapshot disk
if runtime.Provider.Instance.Name == "gcp" && runtime.Provider.Connection.Name == "filesystem" {
defer runtime.Close()
}

assets, err := providers.ProcessAssetCandidates(runtime, runtime.Provider.Connection, upstreamConfig, conf.PlatformId)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion providers/core/resources/core.resources.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"resources":{"asset":{"id":"asset","name":"asset","fields":{"arch":{"name":"arch","type":"\u0007","is_mandatory":true,"title":"Architecture this OS is running on","provider":"go.mondoo.com/cnquery/providers/core"},"build":{"name":"build","type":"\u0007","is_mandatory":true,"title":"Build version of the platform (optional)","provider":"go.mondoo.com/cnquery/providers/core"},"family":{"name":"family","type":"\u0019\u0007","is_mandatory":true,"title":"List of platform families that this platform belongs to","provider":"go.mondoo.com/cnquery/providers/core"},"fqdn":{"name":"fqdn","type":"\u0007","is_mandatory":true,"title":"Fully qualified domain name (optional)","provider":"go.mondoo.com/cnquery/providers/core"},"ids":{"name":"ids","type":"\u0019\u0007","is_mandatory":true,"title":"All identifiers for this asset","provider":"go.mondoo.com/cnquery/providers/core"},"kind":{"name":"kind","type":"\u0007","is_mandatory":true,"title":"Kind of platform, for example:","desc":"api, baremetal, vm, vm-image, container, container-image, network, ...","provider":"go.mondoo.com/cnquery/providers/core"},"labels":{"name":"labels","type":"\u001a\u0007\u0007","is_mandatory":true,"title":"Optional platform information","provider":"go.mondoo.com/cnquery/providers/core"},"name":{"name":"name","type":"\u0007","is_mandatory":true,"title":"Human readable name of the asset","provider":"go.mondoo.com/cnquery/providers/core"},"platform":{"name":"platform","type":"\u0007","is_mandatory":true,"title":"Platform for this asset (redhat, windows, k8s-pod)","provider":"go.mondoo.com/cnquery/providers/core"},"runtime":{"name":"runtime","type":"\u0007","is_mandatory":true,"title":"Runtime is the specific kind of the platform. Examples include:","desc":"docker-container, podman-container, aws-ec2-instance, ...","provider":"go.mondoo.com/cnquery/providers/core"},"title":{"name":"title","type":"\u0007","is_mandatory":true,"title":"Human-readable title of the platform (e.g. \"Red Hat 8, Container\")","provider":"go.mondoo.com/cnquery/providers/core"},"version":{"name":"version","type":"\u0007","is_mandatory":true,"title":"Version of the platform","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"General asset information","min_mondoo_version":"6.13.0","defaults":"name platform version","provider":"go.mondoo.com/cnquery/providers/core"},"mondoo":{"id":"mondoo","name":"mondoo","fields":{"arch":{"name":"arch","type":"\u0007","title":"The architecture of this client (e.g. linux-amd64)","provider":"go.mondoo.com/cnquery/providers/core"},"build":{"name":"build","type":"\u0007","title":"The build of the client (e.g. production, development)","provider":"go.mondoo.com/cnquery/providers/core"},"capabilities":{"name":"capabilities","type":"\u0019\u0007","title":"Connection capabilities","provider":"go.mondoo.com/cnquery/providers/core"},"jobEnvironment":{"name":"jobEnvironment","type":"\n","title":"Returns the agent execution environment","provider":"go.mondoo.com/cnquery/providers/core"},"version":{"name":"version","type":"\u0007","title":"Version of the client running on the asset","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"Provide contextual information about MQL runtime and environment","min_mondoo_version":"5.15.0","defaults":"version","provider":"go.mondoo.com/cnquery/providers/core"},"parse":{"id":"parse","name":"parse","title":"Parse provides common parsers (json, ini, certs, etc)","min_mondoo_version":"5.15.0","provider":"go.mondoo.com/cnquery/providers/core"},"regex":{"id":"regex","name":"regex","fields":{"creditCard":{"name":"creditCard","type":"\u0008","title":"Matches credit card numbers","provider":"go.mondoo.com/cnquery/providers/core"},"email":{"name":"email","type":"\u0008","title":"Matches email addresses","provider":"go.mondoo.com/cnquery/providers/core"},"emoji":{"name":"emoji","type":"\u0008","title":"Matches emojis","provider":"go.mondoo.com/cnquery/providers/core"},"ipv4":{"name":"ipv4","type":"\u0008","title":"Matches IPv4 addresses","provider":"go.mondoo.com/cnquery/providers/core"},"ipv6":{"name":"ipv6","type":"\u0008","title":"Matches IPv6 addresses","provider":"go.mondoo.com/cnquery/providers/core"},"mac":{"name":"mac","type":"\u0008","title":"Matches MAC addresses","provider":"go.mondoo.com/cnquery/providers/core"},"semver":{"name":"semver","type":"\u0008","title":"Matches semantic version numbers","provider":"go.mondoo.com/cnquery/providers/core"},"url":{"name":"url","type":"\u0008","title":"Matches URL addresses (HTTP/HTTPS)","provider":"go.mondoo.com/cnquery/providers/core"},"uuid":{"name":"uuid","type":"\u0008","title":"Matches hyphen-deliminated UUIDs","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"Builtin regular expression functions","min_mondoo_version":"5.15.0","provider":"go.mondoo.com/cnquery/providers/core"},"time":{"id":"time","name":"time","fields":{"day":{"name":"day","type":"\t","title":"One day, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"hour":{"name":"hour","type":"\t","title":"One hour, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"minute":{"name":"minute","type":"\t","title":"One minute, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"now":{"name":"now","type":"\t","title":"The current time on the local system","provider":"go.mondoo.com/cnquery/providers/core"},"second":{"name":"second","type":"\t","title":"One second, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"today":{"name":"today","type":"\t","title":"The current day starting at midnight","provider":"go.mondoo.com/cnquery/providers/core"},"tomorrow":{"name":"tomorrow","type":"\t","title":"The next day starting at midnight","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"Date and time functions","min_mondoo_version":"5.15.0","provider":"go.mondoo.com/cnquery/providers/core"},"uuid":{"id":"uuid","name":"uuid","fields":{"urn":{"name":"urn","type":"\u0007","title":"URN returns the RFC 2141 URN form of uuid","provider":"go.mondoo.com/cnquery/providers/core"},"value":{"name":"value","type":"\u0007","is_mandatory":true,"title":"Canonical string representation xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","provider":"go.mondoo.com/cnquery/providers/core"},"variant":{"name":"variant","type":"\u0007","title":"Variant encoded in uuid","provider":"go.mondoo.com/cnquery/providers/core"},"version":{"name":"version","type":"\u0005","title":"Version of uuid","provider":"go.mondoo.com/cnquery/providers/core"}},"init":{"args":[{"name":"value","type":"\u0007"}]},"title":"UUIDs based on RFC 4122 and DCE 1.1","min_mondoo_version":"5.15.0","defaults":"value","provider":"go.mondoo.com/cnquery/providers/core"}}}
{"resources":{"asset":{"id":"asset","name":"asset","fields":{"arch":{"name":"arch","type":"\u0007","is_mandatory":true,"title":"Architecture this OS is running on","provider":"go.mondoo.com/cnquery/providers/core"},"build":{"name":"build","type":"\u0007","is_mandatory":true,"title":"Build version of the platform (optional)","provider":"go.mondoo.com/cnquery/providers/core"},"family":{"name":"family","type":"\u0019\u0007","is_mandatory":true,"title":"List of platform families that this platform belongs to","provider":"go.mondoo.com/cnquery/providers/core"},"fqdn":{"name":"fqdn","type":"\u0007","is_mandatory":true,"title":"Fully qualified domain name (optional)","provider":"go.mondoo.com/cnquery/providers/core"},"ids":{"name":"ids","type":"\u0019\u0007","is_mandatory":true,"title":"All identifiers for this asset","provider":"go.mondoo.com/cnquery/providers/core"},"kind":{"name":"kind","type":"\u0007","is_mandatory":true,"title":"Kind of platform, for example:","desc":"api, baremetal, vm, vm-image, container, container-image, network, ...","provider":"go.mondoo.com/cnquery/providers/core"},"labels":{"name":"labels","type":"\u001a\u0007\u0007","is_mandatory":true,"title":"Optional platform information","provider":"go.mondoo.com/cnquery/providers/core"},"name":{"name":"name","type":"\u0007","is_mandatory":true,"title":"Human readable name of the asset","provider":"go.mondoo.com/cnquery/providers/core"},"platform":{"name":"platform","type":"\u0007","is_mandatory":true,"title":"Platform for this asset (redhat, windows, k8s-pod)","provider":"go.mondoo.com/cnquery/providers/core"},"runtime":{"name":"runtime","type":"\u0007","is_mandatory":true,"title":"Runtime is the specific kind of the platform. Examples include:","desc":"docker-container, podman-container, aws-ec2-instance, ...","provider":"go.mondoo.com/cnquery/providers/core"},"title":{"name":"title","type":"\u0007","is_mandatory":true,"title":"Human-readable title of the platform (e.g. \"Red Hat 8, Container\")","provider":"go.mondoo.com/cnquery/providers/core"},"version":{"name":"version","type":"\u0007","is_mandatory":true,"title":"Version of the platform","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"General asset information","min_mondoo_version":"6.13.0","defaults":"name platform version","provider":"go.mondoo.com/cnquery/providers/core"},"mondoo":{"id":"mondoo","name":"mondoo","fields":{"arch":{"name":"arch","type":"\u0007","title":"The architecture of this client (e.g. linux-amd64)","min_mondoo_version":"latest","provider":"go.mondoo.com/cnquery/providers/core"},"build":{"name":"build","type":"\u0007","title":"The build of the client (e.g. production, development)","provider":"go.mondoo.com/cnquery/providers/core"},"capabilities":{"name":"capabilities","type":"\u0019\u0007","title":"Connection capabilities","provider":"go.mondoo.com/cnquery/providers/core"},"jobEnvironment":{"name":"jobEnvironment","type":"\n","title":"Returns the agent execution environment","provider":"go.mondoo.com/cnquery/providers/core"},"version":{"name":"version","type":"\u0007","title":"Version of the client running on the asset","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"Provide contextual information about MQL runtime and environment","min_mondoo_version":"5.15.0","defaults":"version","provider":"go.mondoo.com/cnquery/providers/core"},"parse":{"id":"parse","name":"parse","title":"Parse provides common parsers (json, ini, certs, etc)","min_mondoo_version":"5.15.0","provider":"go.mondoo.com/cnquery/providers/core"},"regex":{"id":"regex","name":"regex","fields":{"creditCard":{"name":"creditCard","type":"\u0008","title":"Matches credit card numbers","provider":"go.mondoo.com/cnquery/providers/core"},"email":{"name":"email","type":"\u0008","title":"Matches email addresses","provider":"go.mondoo.com/cnquery/providers/core"},"emoji":{"name":"emoji","type":"\u0008","title":"Matches emojis","provider":"go.mondoo.com/cnquery/providers/core"},"ipv4":{"name":"ipv4","type":"\u0008","title":"Matches IPv4 addresses","provider":"go.mondoo.com/cnquery/providers/core"},"ipv6":{"name":"ipv6","type":"\u0008","title":"Matches IPv6 addresses","provider":"go.mondoo.com/cnquery/providers/core"},"mac":{"name":"mac","type":"\u0008","title":"Matches MAC addresses","provider":"go.mondoo.com/cnquery/providers/core"},"semver":{"name":"semver","type":"\u0008","title":"Matches semantic version numbers","provider":"go.mondoo.com/cnquery/providers/core"},"url":{"name":"url","type":"\u0008","title":"Matches URL addresses (HTTP/HTTPS)","provider":"go.mondoo.com/cnquery/providers/core"},"uuid":{"name":"uuid","type":"\u0008","title":"Matches hyphen-deliminated UUIDs","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"Builtin regular expression functions","min_mondoo_version":"5.15.0","provider":"go.mondoo.com/cnquery/providers/core"},"time":{"id":"time","name":"time","fields":{"day":{"name":"day","type":"\t","title":"One day, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"hour":{"name":"hour","type":"\t","title":"One hour, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"minute":{"name":"minute","type":"\t","title":"One minute, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"now":{"name":"now","type":"\t","title":"The current time on the local system","provider":"go.mondoo.com/cnquery/providers/core"},"second":{"name":"second","type":"\t","title":"One second, used for durations","provider":"go.mondoo.com/cnquery/providers/core"},"today":{"name":"today","type":"\t","title":"The current day starting at midnight","provider":"go.mondoo.com/cnquery/providers/core"},"tomorrow":{"name":"tomorrow","type":"\t","title":"The next day starting at midnight","provider":"go.mondoo.com/cnquery/providers/core"}},"title":"Date and time functions","min_mondoo_version":"5.15.0","provider":"go.mondoo.com/cnquery/providers/core"},"uuid":{"id":"uuid","name":"uuid","fields":{"urn":{"name":"urn","type":"\u0007","title":"URN returns the RFC 2141 URN form of uuid","provider":"go.mondoo.com/cnquery/providers/core"},"value":{"name":"value","type":"\u0007","is_mandatory":true,"title":"Canonical string representation xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx","provider":"go.mondoo.com/cnquery/providers/core"},"variant":{"name":"variant","type":"\u0007","title":"Variant encoded in uuid","provider":"go.mondoo.com/cnquery/providers/core"},"version":{"name":"version","type":"\u0005","title":"Version of uuid","provider":"go.mondoo.com/cnquery/providers/core"}},"init":{"args":[{"name":"value","type":"\u0007"}]},"title":"UUIDs based on RFC 4122 and DCE 1.1","min_mondoo_version":"5.15.0","defaults":"value","provider":"go.mondoo.com/cnquery/providers/core"}}}
24 changes: 20 additions & 4 deletions providers/gcp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ package config

import (
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/gcp/connection/gcpinstancesnapshot"
"go.mondoo.com/cnquery/providers/gcp/provider"
"go.mondoo.com/cnquery/providers/gcp/resources"
)

var Config = plugin.Provider{
Name: "gcp",
ID: "go.mondoo.com/cnquery/providers/gcp",
Version: "9.0.0",
ConnectionTypes: []string{provider.ConnectionType},
Name: "gcp",
ID: "go.mondoo.com/cnquery/providers/gcp",
Version: "9.0.0",
ConnectionTypes: []string{
provider.ConnectionType,
string(gcpinstancesnapshot.SnapshotConnectionType),
},
Connectors: []plugin.Connector{
{
Name: "gcp",
Expand Down Expand Up @@ -47,6 +51,18 @@ var Config = plugin.Provider{
Default: "",
Desc: "specify the GCR repository to scan (only used for gcr sub command)",
},
{
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
31 changes: 18 additions & 13 deletions providers/gcp/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
"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 @@ -20,6 +25,7 @@ const (
Organization
Folder
Gcr
Snapshot
)

type GcpConnection struct {
Expand Down Expand Up @@ -49,10 +55,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 @@ -64,24 +67,18 @@ func NewGcpConnection(id uint32, asset *inventory.Asset, conf *inventory.Config)
if _, ok := conf.Options["repository"]; ok {
resourceType = Gcr
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["project-id"] != "" {
resourceType = Project
resourceID = conf.Options["project-id"]
} else if conf.Options["snapshot-name"] != "" {
resourceType = Snapshot
resourceID = conf.Options["snapshot-name"]
}

var override string
Expand Down Expand Up @@ -124,3 +121,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 d2a5a1d

Please sign in to comment.