Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Add asset urls for azure #3920

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions providers/azure/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/v11/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers/azure/connection/azureinstancesnapshot"
"go.mondoo.com/cnquery/v11/providers/azure/provider"
Expand Down Expand Up @@ -121,4 +122,46 @@ var Config = plugin.Provider{
},
},
},
AssetUrlTrees: []*inventory.AssetUrlBranch{
{
PathSegments: []string{"technology=azure"},
Key: "tenant",
Title: "Tenant",
Values: map[string]*inventory.AssetUrlBranch{
"*": {
Key: "subscription",
Title: "Subscription",
Values: map[string]*inventory.AssetUrlBranch{
"*": {
Key: "service",
Values: map[string]*inventory.AssetUrlBranch{
"account": nil,
"compute": nil,
"mysql": nil,
"postgres": nil,
"mariadb": nil,
"sql": nil,
"storage": {
Key: "object",
Values: map[string]*inventory.AssetUrlBranch{
"account": nil,
"container": nil,
"other": nil,
},
},
"network": {
Key: "object",
Values: map[string]*inventory.AssetUrlBranch{
"security-group": nil,
"other": nil,
},
},
"keyvault": nil,
},
},
},
},
},
},
},
}
55 changes: 36 additions & 19 deletions providers/azure/resources/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func discoverInstancesApi(runtime *plugin.Runtime, subsWithConfigs []subWithConf
service: "compute",
objectType: "vm-api",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
labels, err := getInstancesLabels(vm)
if err != nil {
return nil, err
Expand Down Expand Up @@ -258,7 +258,7 @@ func discoverInstances(runtime *plugin.Runtime, subsWithConfigs []subWithConfig)
service: "compute",
objectType: "vm",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
for _, ip := range ipAddresses.Data {
ipAddress := ip.(*mqlAzureSubscriptionNetworkServiceIpAddress)
// TODO: we need to make this work via another provider maybe?
Expand Down Expand Up @@ -309,7 +309,7 @@ func discoverSqlServers(runtime *plugin.Runtime, subsWithConfigs []subWithConfig
service: "sql",
objectType: "server",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func discoverMySqlServers(runtime *plugin.Runtime, subsWithConfigs []subWithConf
service: "mysql",
objectType: "server",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -375,7 +375,7 @@ func discoverPostgresqlServers(runtime *plugin.Runtime, subsWithConfigs []subWit
service: "postgresql",
objectType: "server",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -408,7 +408,7 @@ func discoverMariadbServers(runtime *plugin.Runtime, subsWithConfigs []subWithCo
service: "mariadb",
objectType: "server",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -441,7 +441,7 @@ func discoverStorageAccounts(runtime *plugin.Runtime, subsWithConfig []subWithCo
service: "storage",
objectType: "account",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, true)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -480,7 +480,7 @@ func discoverStorageAccountsContainers(runtime *plugin.Runtime, subsWithConfig [
service: "storage",
objectType: "container",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, true)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -514,7 +514,7 @@ func discoverSecurityGroups(runtime *plugin.Runtime, subsWithConfigs []subWithCo
service: "network",
objectType: "security-group",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, true)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -547,7 +547,7 @@ func discoverVaults(runtime *plugin.Runtime, subsWithConfigs []subWithConfig) ([
service: "keyvault",
objectType: "vault",
},
}, subWithConfig.conf.Clone())
}, subWithConfig.conf.Clone(), subWithConfig, false)
assets = append(assets, asset)
}
}
Expand Down Expand Up @@ -628,13 +628,18 @@ func discoverSubscriptions(conn *connection.AzureConnection, filter connection.S

func subToAsset(sub subscriptions.Subscription, conf *inventory.Config) *inventory.Asset {
platformId := "//platformid.api.mondoo.app/runtime/azure/subscriptions/" + *sub.SubscriptionID
tenantId := "unknown"
if sub.TenantID != nil {
tenantId = *sub.TenantID
}
return &inventory.Asset{
Id: platformId,
Platform: &inventory.Platform{
Title: "Azure Subscription",
Name: "azure",
Runtime: "azure",
Kind: "api",
Title: "Azure Subscription",
Name: "azure",
Runtime: "azure",
Kind: "api",
TechnologyUrlSegments: []string{"azure", tenantId, *sub.SubscriptionID, "account"},
},
Name: fmt.Sprintf("Azure subscription %s", *sub.DisplayName),
Connections: []*inventory.Config{conf},
Expand Down Expand Up @@ -701,7 +706,7 @@ func getTitleFamily(azureObject azureObject) (azureObjectPlatformInfo, error) {
return azureObjectPlatformInfo{}, fmt.Errorf("missing runtime info for azure object service %s type %s", azureObject.service, azureObject.objectType)
}

func mqlObjectToAsset(mqlObject mqlObject, conf *inventory.Config) *inventory.Asset {
func mqlObjectToAsset(mqlObject mqlObject, conf *inventory.Config, subWithConf subWithConfig, includeObjectTypeInUrl bool) *inventory.Asset {
if mqlObject.name == "" {
mqlObject.name = mqlObject.azureObject.id
}
Expand All @@ -712,14 +717,26 @@ func mqlObjectToAsset(mqlObject mqlObject, conf *inventory.Config) *inventory.As
platformid := AzureObjectPlatformId(mqlObject.azureObject.id)
cfg := conf.Clone()
cfg.PlatformId = platformid

tenantId := "unknown"
if subWithConf.sub.TenantID != nil {
tenantId = *subWithConf.sub.TenantID
}

assetUrl := []string{"azure", tenantId, mqlObject.azureObject.subscription,
mqlObject.azureObject.service}
if includeObjectTypeInUrl {
assetUrl = append(assetUrl, mqlObject.azureObject.objectType)
}
return &inventory.Asset{
PlatformIds: []string{platformid, mqlObject.azureObject.id},
Name: mqlObject.name,
Platform: &inventory.Platform{
Name: info.platform,
Title: info.title,
Kind: "azure-object",
Runtime: "azure",
Name: info.platform,
Title: info.title,
Kind: "azure-object",
Runtime: "azure",
TechnologyUrlSegments: assetUrl,
},
State: inventory.State_STATE_ONLINE,
Labels: addInformationalLabels(mqlObject.labels, mqlObject),
Expand Down
Loading