Skip to content

Commit

Permalink
🧹 align discovery options between k8s + azure + gcp (#1626)
Browse files Browse the repository at this point in the history
After #1625

Taking the k8s pattern. The config should not have to be imported by
other modules (at the time of writing).

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus authored Sep 4, 2023
1 parent 33c80b2 commit 1f66d3f
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 82 deletions.
47 changes: 16 additions & 31 deletions providers/azure/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,9 @@

package config

import "go.mondoo.com/cnquery/providers-sdk/v1/plugin"

// Discovery flags
const (
DiscoveryAuto = "auto"
DiscoveryAll = "all"
DiscoverySubscriptions = "subscriptions"
DiscoveryInstances = "instances"
// TODO: this probably needs some more work on the linking to its OS counterpart side
DiscoveryInstancesApi = "instances-api"
DiscoverySqlServers = "sql-servers"
DiscoveryPostgresServers = "postgres-servers"
DiscoveryMySqlServers = "mysql-servers"
DiscoveryMariaDbServers = "mariadb-servers"
DiscoveryStorageAccounts = "storage-accounts"
DiscoveryStorageContainers = "storage-containers"
DiscoveryKeyVaults = "keyvaults-vaults"
DiscoverySecurityGroups = "security-groups"
import (
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/azure/resources"
)

var Config = plugin.Provider{
Expand All @@ -35,19 +20,19 @@ var Config = plugin.Provider{
MinArgs: 0,
MaxArgs: 8,
Discovery: []string{
DiscoveryAuto,
DiscoveryAll,
DiscoverySubscriptions,
DiscoveryInstances,
DiscoveryInstancesApi,
DiscoverySqlServers,
DiscoveryPostgresServers,
DiscoveryMySqlServers,
DiscoveryMariaDbServers,
DiscoveryStorageAccounts,
DiscoveryStorageContainers,
DiscoveryKeyVaults,
DiscoverySecurityGroups,
resources.DiscoveryAuto,
resources.DiscoveryAll,
resources.DiscoverySubscriptions,
resources.DiscoveryInstances,
resources.DiscoveryInstancesApi,
resources.DiscoverySqlServers,
resources.DiscoveryPostgresServers,
resources.DiscoveryMySqlServers,
resources.DiscoveryMariaDbServers,
resources.DiscoveryStorageAccounts,
resources.DiscoveryStorageContainers,
resources.DiscoveryKeyVaults,
resources.DiscoverySecurityGroups,
},
Flags: []plugin.Flag{
{
Expand Down
3 changes: 1 addition & 2 deletions providers/azure/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers-sdk/v1/upstream"
"go.mondoo.com/cnquery/providers-sdk/v1/vault"
"go.mondoo.com/cnquery/providers/azure/config"
"go.mondoo.com/cnquery/providers/azure/connection"
"go.mondoo.com/cnquery/providers/azure/resources"
)
Expand Down Expand Up @@ -93,7 +92,7 @@ func parseDiscover(flags map[string]*llx.Primitive) *inventory.Discovery {
targets = append(targets, entry)
}
} else {
targets = []string{config.DiscoveryAuto}
targets = []string{resources.DiscoveryAuto}
}
return &inventory.Discovery{Targets: targets}
}
Expand Down
38 changes: 26 additions & 12 deletions providers/azure/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"go.mondoo.com/cnquery/llx"
"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/azure/config"
"go.mondoo.com/cnquery/providers/azure/connection"
"go.mondoo.com/cnquery/utils/stringx"

Expand All @@ -22,6 +21,21 @@ const (
SubscriptionLabel = "azure.mondoo.com/subscription"
RegionLabel = "mondoo.com/region"
InstanceLabel = "mondoo.com/instance"

DiscoveryAuto = "auto"
DiscoveryAll = "all"
DiscoverySubscriptions = "subscriptions"
DiscoveryInstances = "instances"
// TODO: this probably needs some more work on the linking to its OS counterpart side
DiscoveryInstancesApi = "instances-api"
DiscoverySqlServers = "sql-servers"
DiscoveryPostgresServers = "postgres-servers"
DiscoveryMySqlServers = "mysql-servers"
DiscoveryMariaDbServers = "mariadb-servers"
DiscoveryStorageAccounts = "storage-accounts"
DiscoveryStorageContainers = "storage-containers"
DiscoveryKeyVaults = "keyvaults-vaults"
DiscoverySecurityGroups = "security-groups"
)

type azureObject struct {
Expand Down Expand Up @@ -73,76 +87,76 @@ func Discover(runtime *plugin.Runtime, rootConf *inventory.Config) (*inventory.I
subsWithConfigs[i] = subWithConfig{sub: sub, conf: getSubConfig(conn.Conf, sub)}
}

if stringx.ContainsAnyOf(targets, config.DiscoverySubscriptions, config.DiscoveryAll, config.DiscoveryAuto) {
if stringx.ContainsAnyOf(targets, DiscoverySubscriptions, DiscoveryAll, DiscoveryAuto) {
// we've already discovered those, simply add them as assets
for _, s := range subsWithConfigs {
assets = append(assets, subToAsset(s.sub, s.conf))
}
}
if stringx.ContainsAnyOf(targets, config.DiscoveryInstances, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryInstances, DiscoveryAll) {
vms, err := discoverInstances(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, vms...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryInstancesApi, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryInstancesApi, DiscoveryAll) {
vms, err := discoverInstancesApi(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, vms...)
}
if stringx.ContainsAnyOf(targets, config.DiscoverySqlServers, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoverySqlServers, DiscoveryAll) {
sqlServers, err := discoverSqlServers(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, sqlServers...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryMySqlServers, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryMySqlServers, DiscoveryAll) {
mySqlServers, err := discoverMySqlServers(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, mySqlServers...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryPostgresServers, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryPostgresServers, DiscoveryAll) {
postgresServers, err := discoverPostgresqlServers(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, postgresServers...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryMariaDbServers, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryMariaDbServers, DiscoveryAll) {
mariaDbServers, err := discoverMariadbServers(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, mariaDbServers...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryStorageAccounts, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryStorageAccounts, DiscoveryAll) {
accs, err := discoverStorageAccounts(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, accs...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryStorageContainers, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryStorageContainers, DiscoveryAll) {
containers, err := discoverStorageAccountsContainers(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, containers...)
}
if stringx.ContainsAnyOf(targets, config.DiscoverySecurityGroups, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoverySecurityGroups, DiscoveryAll) {
secGrps, err := discoverSecurityGroups(runtime, subsWithConfigs)
if err != nil {
return nil, err
}
assets = append(assets, secGrps...)
}
if stringx.ContainsAnyOf(targets, config.DiscoveryKeyVaults, config.DiscoveryAll) {
if stringx.ContainsAnyOf(targets, DiscoveryKeyVaults, DiscoveryAll) {
kvs, err := discoverVaults(runtime, subsWithConfigs)
if err != nil {
return nil, err
Expand Down
40 changes: 14 additions & 26 deletions providers/gcp/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,9 @@

package config

import "go.mondoo.com/cnquery/providers-sdk/v1/plugin"

const (
// Discovery flags
DiscoveryOrganization = "organization"
DiscoveryFolders = "folders"
DiscoveryInstances = "instances"
DiscoveryProjects = "projects"
DiscoveryComputeImages = "compute-images"
DiscoveryComputeNetworks = "compute-networks"
DiscoveryComputeSubnetworks = "compute-subnetworks"
DiscoveryComputeFirewalls = "compute-firewalls"
DiscoveryGkeClusters = "gke-clusters"
DiscoveryStorageBuckets = "storage-buckets"
DiscoveryBigQueryDatasets = "bigquery-datasets"
import (
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/gcp/resources"
)

var Config = plugin.Provider{
Expand All @@ -30,17 +18,17 @@ var Config = plugin.Provider{
Use: "gcp",
Short: "GCP Cloud",
Discovery: []string{
DiscoveryOrganization,
DiscoveryFolders,
DiscoveryInstances,
DiscoveryProjects,
DiscoveryComputeImages,
DiscoveryComputeNetworks,
DiscoveryComputeSubnetworks,
DiscoveryComputeFirewalls,
DiscoveryGkeClusters,
DiscoveryStorageBuckets,
DiscoveryBigQueryDatasets,
resources.DiscoveryOrganization,
resources.DiscoveryFolders,
resources.DiscoveryInstances,
resources.DiscoveryProjects,
resources.DiscoveryComputeImages,
resources.DiscoveryComputeNetworks,
resources.DiscoveryComputeSubnetworks,
resources.DiscoveryComputeFirewalls,
resources.DiscoveryGkeClusters,
resources.DiscoveryStorageBuckets,
resources.DiscoveryBigQueryDatasets,
},
Flags: []plugin.Flag{
{
Expand Down
36 changes: 25 additions & 11 deletions providers/gcp/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@ package resources
import (
"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/gcp/config"
"go.mondoo.com/cnquery/providers/gcp/connection"
"golang.org/x/exp/slices"
)

const (
// Discovery flags
DiscoveryOrganization = "organization"
DiscoveryFolders = "folders"
DiscoveryInstances = "instances"
DiscoveryProjects = "projects"
DiscoveryComputeImages = "compute-images"
DiscoveryComputeNetworks = "compute-networks"
DiscoveryComputeSubnetworks = "compute-subnetworks"
DiscoveryComputeFirewalls = "compute-firewalls"
DiscoveryGkeClusters = "gke-clusters"
DiscoveryStorageBuckets = "storage-buckets"
DiscoveryBigQueryDatasets = "bigquery-datasets"
)

func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
conn := runtime.Connection.(*connection.GcpConnection)

Expand Down Expand Up @@ -51,7 +65,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
in.Spec.Assets = append(in.Spec.Assets, list...)
}

if slices.Contains(conn.Conf.Discover.Targets, config.DiscoveryProjects) {
if slices.Contains(conn.Conf.Discover.Targets, DiscoveryProjects) {
in.Spec.Assets = append(in.Spec.Assets, &inventory.Asset{
PlatformIds: []string{
connection.NewProjectPlatformID(gcpProject.Id.Data),
Expand All @@ -73,7 +87,7 @@ func Discover(runtime *plugin.Runtime) (*inventory.Inventory, error) {
func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganization, target string) ([]*inventory.Asset, error) {
assetList := []*inventory.Asset{}
switch target {
case config.DiscoveryProjects:
case DiscoveryProjects:
projects := gcpOrg.GetProjects()
if projects.Error != nil {
return nil, projects.Error
Expand Down Expand Up @@ -114,7 +128,7 @@ func discoverOrganization(conn *connection.GcpConnection, gcpOrg *mqlGcpOrganiza
func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject, target string) ([]*inventory.Asset, error) {
assetList := []*inventory.Asset{}
switch target {
case config.DiscoveryInstances:
case DiscoveryInstances:
compute := gcpProject.GetCompute()
if compute.Error != nil {
return nil, compute.Error
Expand Down Expand Up @@ -158,7 +172,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
})
}

case config.DiscoveryComputeImages:
case DiscoveryComputeImages:
compute := gcpProject.GetCompute()
if compute.Error != nil {
return nil, compute.Error
Expand Down Expand Up @@ -187,7 +201,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
Connections: []*inventory.Config{conn.Conf.Clone()}, // pass-in the parent connection config
})
}
case config.DiscoveryComputeNetworks:
case DiscoveryComputeNetworks:
compute := gcpProject.GetCompute()
if compute.Error != nil {
return nil, compute.Error
Expand All @@ -211,7 +225,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
Connections: []*inventory.Config{conn.Conf.Clone()}, // pass-in the parent connection config
})
}
case config.DiscoveryComputeSubnetworks:
case DiscoveryComputeSubnetworks:
compute := gcpProject.GetCompute()
if compute.Error != nil {
return nil, compute.Error
Expand Down Expand Up @@ -239,7 +253,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
Connections: []*inventory.Config{conn.Conf.Clone()}, // pass-in the parent connection config
})
}
case config.DiscoveryComputeFirewalls:
case DiscoveryComputeFirewalls:
compute := gcpProject.GetCompute()
if compute.Error != nil {
return nil, compute.Error
Expand All @@ -263,7 +277,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
Connections: []*inventory.Config{conn.Conf.Clone()}, // pass-in the parent connection config
})
}
case config.DiscoveryGkeClusters:
case DiscoveryGkeClusters:
gke := gcpProject.GetGke()
if gke.Error != nil {
return nil, gke.Error
Expand All @@ -287,7 +301,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
Connections: []*inventory.Config{conn.Conf.Clone()}, // pass-in the parent connection config
})
}
case config.DiscoveryStorageBuckets:
case DiscoveryStorageBuckets:
storage := gcpProject.GetStorage()
if storage.Error != nil {
return nil, storage.Error
Expand All @@ -311,7 +325,7 @@ func discoverProject(conn *connection.GcpConnection, gcpProject *mqlGcpProject,
Connections: []*inventory.Config{conn.Conf.Clone()}, // pass-in the parent connection config
})
}
case config.DiscoveryBigQueryDatasets:
case DiscoveryBigQueryDatasets:
bq := gcpProject.GetBigquery()
if bq.Error != nil {
return nil, bq.Error
Expand Down

0 comments on commit 1f66d3f

Please sign in to comment.