Skip to content

Commit

Permalink
⭐ Add asset url tree for GCP
Browse files Browse the repository at this point in the history
  • Loading branch information
jaym committed Apr 8, 2024
1 parent 467ed2b commit 5175acb
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 60 deletions.
84 changes: 84 additions & 0 deletions providers/gcp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config

import (
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v10/providers/gcp/connection/gcpinstancesnapshot"
"go.mondoo.com/cnquery/v10/providers/gcp/provider"
Expand Down Expand Up @@ -73,4 +74,87 @@ var Config = plugin.Provider{
},
},
},
AssetUrlTrees: []*inventory.AssetUrlBranch{
{
PathSegments: []string{"technology=gcp"},
Key: "scope",
Title: "Scope",
Values: map[string]*inventory.AssetUrlBranch{
"other": nil,
"resource manager": {
Key: "object",
Title: "Resource Manager Object Type",
Values: map[string]*inventory.AssetUrlBranch{
"organization": nil,
"folder": nil,
"project": nil,
},
},
"project": {
Key: "project",
Title: "Project",
Values: map[string]*inventory.AssetUrlBranch{
"*": {
Key: "service",
Title: "Service",
Values: map[string]*inventory.AssetUrlBranch{
"compute": {
Key: "region",
Title: "Region",
Values: map[string]*inventory.AssetUrlBranch{
"*": {
Key: "object",
Title: "Compute Object",
Values: map[string]*inventory.AssetUrlBranch{
"instance": {
Key: "type",
Values: map[string]*inventory.AssetUrlBranch{
"resource": nil,
// os ... references the os asset tree
},
},
"image": nil,
"network": nil,
"subnetwork": nil,
"other": nil,
},
},
},
},
"storage": {
Key: "region",
Title: "Region",
Values: map[string]*inventory.AssetUrlBranch{
"*": {
Key: "object",
Title: "Storage Object",
Values: map[string]*inventory.AssetUrlBranch{
"bucket": nil,
"other": nil,
},
},
},
},
"gke": {
Key: "region",
Title: "Region",
Values: map[string]*inventory.AssetUrlBranch{
"*": {
Key: "object",
Title: "GKE Object",
Values: map[string]*inventory.AssetUrlBranch{
"cluster": nil,
"other": nil,
},
},
},
},
"other": nil,
},
},
},
},
},
},
},
}
30 changes: 30 additions & 0 deletions providers/gcp/connection/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,33 @@ func getTitleForPlatformName(name string) string {
}
return "Google Cloud Platform"
}

func ResourceTechnologyUrl(service, project, region, objectType, name string) []string {
switch service {
case "compute":
switch objectType {
case "instance":
return []string{"gcp", "project", project, "compute", region, "instance", "resource"}
case "image", "network", "subnetwork":
return []string{"gcp", "project", project, "compute", region, objectType}
default:
return []string{"gcp", "project", project, "compute", region, "other"}
}
case "storage":
switch objectType {
case "bucket":
return []string{"gcp", "project", project, "storage", region, objectType}
default:
return []string{"gcp", "project", project, "storage", region, "other"}
}
case "gke":
switch objectType {
case "cluster":
return []string{"gcp", "project", project, "gke", region, objectType}
default:
return []string{"gcp", "project", project, "gke", region, "other"}
}
default:
return []string{"gcp", "project", project, "other"}
}
}
132 changes: 72 additions & 60 deletions providers/gcp/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
},
Name: "GCP Folder " + gcpFolder.Id.Data,
Platform: &inventory.Platform{
Name: "gcp-folder",
Title: "GCP Folder",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-folder",
Title: "GCP Folder",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: []string{"gcp", "resource manager", "folder"},
},
Labels: map[string]string{},
// NOTE: we explicitly do not exclude discovery here, as we want to discover the projects for the folder
Expand Down Expand Up @@ -102,11 +103,12 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
},
Name: "GCP Project " + gcpProject.Id.Data,
Platform: &inventory.Platform{
Name: "gcp-project",
Title: "GCP Project",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-project",
Title: "GCP Project",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: []string{"gcp", "resource manager", "project"},
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))},
Expand Down Expand Up @@ -170,11 +172,12 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
},
Name: project.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-project",
Title: "GCP Project " + project.Name.Data,
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-project",
Title: "GCP Project " + project.Name.Data,
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: []string{"gcp", "resource manager", "project"},
},
Labels: map[string]string{},
Connections: []*inventory.Config{projectConf}, // pass-in the parent connection config
Expand Down Expand Up @@ -207,11 +210,12 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
},
Name: "GCP Folder " + folder.Id.Data,
Platform: &inventory.Platform{
Name: "gcp-folder",
Title: "GCP Folder",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-folder",
Title: "GCP Folder",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: []string{"gcp", "resource manager", "folder"},
},
Labels: map[string]string{},
// NOTE: we explicitly do not exclude discovery here, as we want to discover the projects for the folder
Expand Down Expand Up @@ -303,11 +307,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: instance.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-compute-instance",
Title: "GCP Compute Instance",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-compute-instance",
Title: "GCP Compute Instance",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("compute", gcpProject.Id.Data, zone.Data.Name.Data, "instance", instance.Name.Data),
},
Labels: labels,
// TODO: the current connection handling does not work well for instances
Expand Down Expand Up @@ -337,11 +342,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: image.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-compute-image",
Title: "GCP Compute Image",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-compute-image",
Title: "GCP Compute Image",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("compute", gcpProject.Id.Data, "global", "image", image.Name.Data),
},
Labels: labels,
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand All @@ -365,11 +371,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: network.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-compute-network",
Title: "GCP Compute Network",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-compute-network",
Title: "GCP Compute Network",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("compute", gcpProject.Id.Data, "global", "network", network.Name.Data),
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand Down Expand Up @@ -397,11 +404,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: network.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-compute-subnetwork",
Title: "GCP Compute Subnetwork",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-compute-subnetwork",
Title: "GCP Compute Subnetwork",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("compute", gcpProject.Id.Data, RegionNameFromRegionUrl(region.Data), "subnetwork", network.Name.Data),
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand All @@ -425,11 +433,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: firewall.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-compute-firewall",
Title: "GCP Compute Firewall",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-compute-firewall",
Title: "GCP Compute Firewall",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("compute", gcpProject.Id.Data, "global", "firewall", firewall.Name.Data),
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand All @@ -453,11 +462,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: cluster.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-gke-cluster",
Title: "GCP GKE Cluster",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-gke-cluster",
Title: "GCP GKE Cluster",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("gke", gcpProject.Id.Data, cluster.GetLocation().Data, "cluster", cluster.Name.Data),
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand All @@ -481,11 +491,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: bucket.Name.Data,
Platform: &inventory.Platform{
Name: "gcp-storage-bucket",
Title: "GCP Storage Bucket",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-storage-bucket",
Title: "GCP Storage Bucket",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("storage", gcpProject.Id.Data, bucket.GetLocation().Data, "bucket", bucket.Name.Data),
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand All @@ -509,11 +520,12 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject)
},
Name: dataset.Id.Data,
Platform: &inventory.Platform{
Name: "gcp-bigquery-dataset",
Title: "GCP BigQuery Dataset",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
Name: "gcp-bigquery-dataset",
Title: "GCP BigQuery Dataset",
Runtime: "gcp",
Kind: "gcp-object",
Family: []string{"google"},
TechnologyUrlSegments: connection.ResourceTechnologyUrl("bigquery", gcpProject.Id.Data, dataset.GetLocation().Data, "dataset", dataset.Id.Data),
},
Labels: map[string]string{},
Connections: []*inventory.Config{conn.Conf.Clone(inventory.WithoutDiscovery(), inventory.WithParentConnectionId(conn.Conf.Id))}, // pass-in the parent connection config
Expand Down

0 comments on commit 5175acb

Please sign in to comment.