Skip to content

Commit

Permalink
feat: set azure resources parent
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed May 21, 2024
1 parent 4daea93 commit 00fe6ee
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ func upsertAnalysis(ctx api.ScrapeContext, result *v1.ScrapeResult) error {
if err != nil {
return err
}

if ciID == nil {
logger.Warnf("[Source=%s] [%s/%s] unable to find config item for analysis: %+v", analysis.Source, analysis.ConfigType, analysis.ExternalID, analysis)
logger.Warnf("unable to find config item for analysis: (source=%s, configType=%s, externalID=%s, analysis: %+v)", analysis.Source, analysis.ConfigType, analysis.ExternalID, analysis)
return nil
}

logger.Tracef("[%s/%s] ==> %s", analysis.ConfigType, analysis.ExternalID, analysis)
analysis.ConfigID = uuid.MustParse(ciID.ID)
analysis.ID = uuid.MustParse(ulid.MustNew().AsUUID())
analysis.ScraperID = ctx.ScrapeConfig().GetPersistedID()
Expand Down Expand Up @@ -839,7 +839,7 @@ func setConfigPaths(ctx api.ScrapeContext, tree graph.Graph[string, string], roo

func isTreeRoot(configType string) bool {
switch configType {
case "AWS::::Account", "Kubernetes::Cluster":
case "AWS::::Account", "Kubernetes::Cluster", "Azure::Tenant":
return true
}

Expand Down
33 changes: 33 additions & 0 deletions scrapers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
const (
ConfigTypePrefix = "Azure::"
defaultActivityLogMaxage = time.Hour * 24 * 7

ResourceTypeTenant = "Tenant"
ResourceTypeSubscription = "Subscription"
)

// activityLogLastRecordTime keeps track of the time of the last activity log per subscription.
Expand Down Expand Up @@ -134,6 +137,15 @@ func (azure Scraper) Scrape(ctx api.ScrapeContext) v1.ScrapeResults {
azure.config = &config
azure.cred = cred

// Craft a result for the tenant.
results = append(results, v1.ScrapeResult{
ID: config.TenantID,
Type: getARMType(lo.ToPtr(ResourceTypeTenant)),
BaseScraper: config.BaseScraper,
ConfigClass: "Tenant",
Config: map[string]any{"id": config.TenantID},
})

// We fetch resource groups first as they are used to fetch further resources
results = append(results, azure.fetchResourceGroups()...)
results = append(results, azure.fetchVirtualMachines()...)
Expand All @@ -156,6 +168,10 @@ func (azure Scraper) Scrape(ctx api.ScrapeContext) v1.ScrapeResults {

// Post processing of all results
for i := range results {
if results[i].ID == "" {
continue // changes & analysis only
}

// Add subscription id & name tag to all the resources
results[i].Tags = append(results[i].Tags, v1.Tag{
Name: "subscriptionID",
Expand All @@ -166,6 +182,23 @@ func (azure Scraper) Scrape(ctx api.ScrapeContext) v1.ScrapeResults {
Name: "subscriptionName",
Value: azure.subscriptionName,
})

// Set parents where missing
if results[i].ParentExternalID == "" {
switch results[i].Type {
case getARMType(lo.ToPtr(ResourceTypeTenant)):
continue // root

case getARMType(lo.ToPtr(ResourceTypeSubscription)):
results[i].ParentExternalID = config.TenantID
results[i].ParentType = getARMType(lo.ToPtr(ResourceTypeTenant))

default:
subscriptionID := strings.Split(results[i].ID, "/")[2]
results[i].ParentExternalID = getARMID(lo.ToPtr("/subscriptions/" + subscriptionID))
results[i].ParentType = getARMType(lo.ToPtr(ResourceTypeSubscription))
}
}
}
}

Expand Down

0 comments on commit 00fe6ee

Please sign in to comment.