Skip to content

Commit

Permalink
✨ gcp instance sub-command for v9 (#1681)
Browse files Browse the repository at this point in the history
This allows to scan a GCP VM via a snapshot/clone.

Fixes #1675

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker authored Sep 12, 2023
1 parent 34db175 commit 25f623f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
10 changes: 8 additions & 2 deletions providers/gcp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@ var Config = plugin.Provider{
Long: "project-id",
Type: plugin.FlagType_String,
Default: "",
Desc: "specify the GCP project ID where the target instance is located (only used for snapshots)",
Desc: "specify the GCP project ID where the target instance is located",
},
{
Long: "zone",
Type: plugin.FlagType_String,
Default: "",
Desc: "specify the GCP zone where the target instance is located (only used for snapshots)",
Desc: "specify the GCP zone where the target instance is located",
},
{
Long: "create-snapshot",
Type: plugin.FlagType_Bool,
Default: "false",
Desc: "create a new snapshot instead of using the latest available snapshot (only used for instance)",
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions providers/gcp/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
Folder
Gcr
Snapshot
Instance
)

type GcpConnection struct {
Expand Down Expand Up @@ -79,6 +80,9 @@ func NewGcpConnection(id uint32, asset *inventory.Asset, conf *inventory.Config)
} else if conf.Options["snapshot-name"] != "" {
resourceType = Snapshot
resourceID = conf.Options["snapshot-name"]
} else if conf.Options["instance-name"] != "" {
resourceType = Instance
resourceID = conf.Options["instance-name"]
}

var override string
Expand Down
8 changes: 5 additions & 3 deletions providers/gcp/connection/gcpinstancesnapshot/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func NewGcpSnapshotConnection(id uint32, conf *inventory.Config, asset *inventor
mi.diskUrl = diskUrl

}
asset.Name = instanceInfo.InstanceName
asset.PlatformIds = []string{instanceInfo.PlatformMrn}
case "snapshot":
snapshotInfo, err := sc.SnapshotInfo(target.ProjectID, target.SnapshotName)
if err != nil {
Expand All @@ -176,6 +178,8 @@ func NewGcpSnapshotConnection(id uint32, conf *inventory.Config, asset *inventor
}
log.Debug().Str("disk", diskUrl).Msg("created disk from snapshot")
mi.diskUrl = diskUrl
asset.Name = conf.Options["snapshot-name"]
asset.PlatformIds = []string{snapshotInfo.PlatformMrn}
default:
return nil, errors.New("invalid target type")
}
Expand Down Expand Up @@ -217,6 +221,7 @@ func NewGcpSnapshotConnection(id uint32, conf *inventory.Config, asset *inventor
PlatformId: conf.PlatformId,
Options: conf.Options,
Type: conf.Type,
Record: conf.Record,
}, asset)
if err != nil {
errorHandler()
Expand All @@ -241,11 +246,8 @@ func NewGcpSnapshotConnection(id uint32, conf *inventory.Config, asset *inventor
return nil, errors.New("failed to detect OS")
}
asset.Id = conf.Type
asset.Name = conf.Options["snapshot-name"]
asset.Platform.Kind = c.Kind()
asset.Platform.Runtime = c.Runtime()
platformId := fmt.Sprintf("//platformid.api.mondoo.app/runtime/gcp/compute/v1/projects/%s/snapshots/%s", conf.Options["project-id"], conf.Options["snapshot-name"])
asset.PlatformIds = []string{platformId}

return c, nil
}
Expand Down
4 changes: 2 additions & 2 deletions providers/gcp/connection/gcpinstancesnapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ func (sc *SnapshotCreator) InstanceInfo(projectID, zone, instanceName string) (i
}

ii.ProjectID = projectID
ii.Zone = instance.Zone
ii.Zone = zone
ii.InstanceName = instance.Name
ii.PlatformMrn = gce.MondooGcpInstancePlatformMrn(projectID, instance.Zone, instance.Name)
ii.PlatformMrn = gce.MondooGcpInstancePlatformMrn(projectID, zone, instance.Name)

// search for boot disk
var bootDisk *compute.AttachedDisk
Expand Down
21 changes: 18 additions & 3 deletions providers/gcp/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
}

if len(req.Args) != 2 {
return nil, errors.New("missing argument, use `gcp project id`, `gcp organization id`, `gcp folder id` or `gcp snapshot name`")
return nil, errors.New("missing argument, use `gcp project id`, `gcp organization id`, `gcp folder id`, `gcp instance name`, or `gcp snapshot name`")
}

conf := &inventory.Config{
Expand All @@ -92,7 +92,7 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
credentialsPath = string(x.Value)
}

// these flags are currently only used for the snapshot sub-command
// used for snapshot and instance sub-commands
var projectId string
if x, ok := flags["project-id"]; ok && len(x.Value) != 0 {
projectId = string(x.Value)
Expand All @@ -102,7 +102,14 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
if x, ok := flags["zone"]; ok && len(x.Value) != 0 {
zone = string(x.Value)
}
// ^^ snapshot flags
// ^^ snapshot and instance flags

// these flags are currently only used for the instnace sub-command
var createSnapshot string
if x, ok := flags["create-snapshot"]; ok && len(x.Value) != 0 {
createSnapshot = string(x.Value)
}
// ^^ instance flags

envVars := []string{
"GOOGLE_APPLICATION_CREDENTIALS",
Expand Down Expand Up @@ -149,6 +156,14 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
conf.Options["type"] = "snapshot"
conf.Type = string(gcpinstancesnapshot.SnapshotConnectionType)
conf.Discover = nil
case "instance":
conf.Options["instance-name"] = req.Args[1]
conf.Options["type"] = "instance"
conf.Options["project-id"] = projectId
conf.Options["zone"] = zone
conf.Options["create-snapshot"] = createSnapshot
conf.Type = string(gcpinstancesnapshot.SnapshotConnectionType)
conf.Discover = nil
}

asset := inventory.Asset{
Expand Down

0 comments on commit 25f623f

Please sign in to comment.