Skip to content

Commit

Permalink
chore: improved diagnostic setings scan performance
Browse files Browse the repository at this point in the history
  • Loading branch information
cmendible committed Jul 31, 2024
1 parent 69673f6 commit aafba14
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 40 deletions.
9 changes: 9 additions & 0 deletions internal/renderers/report_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,15 @@ func (rd *ReportData) ResourceTypesTable() [][]string {
return rows
}

func (rd *ReportData) ResourceIDs() []*string {
ids := []*string{}
for _, r := range rd.Resources {
ids = append(ids, &r.ID)
}

return ids
}

func NewReportData(outputFile string, mask bool) ReportData {
return ReportData{
OutputFileName: outputFile,
Expand Down
13 changes: 8 additions & 5 deletions internal/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (sc Scanner) Scan(params *ScanParams) {
diagnosticsScanner := scanners.DiagnosticSettingsScanner{}
advisorScanner := scanners.AdvisorScanner{}
costScanner := scanners.CostScanner{}
diagResults := map[string]bool{}

// initialize report data
reportData := renderers.NewReportData(outputFile, params.Mask)
Expand All @@ -102,6 +103,9 @@ func (sc Scanner) Scan(params *ScanParams) {
aprlScanner := AprlScanner{}
reportData.Recomendations, reportData.AprlData = aprlScanner.Scan(ctx, cred, params.ServiceScanners, filters, subscriptions)

resourceScanner := scanners.ResourceScanner{}
reportData.Resources = resourceScanner.GetAllResources(ctx, cred, subscriptions)

// For each service scanner, get the recommendations list
if params.UseAzqrRecommendations {
for _, s := range params.ServiceScanners {
Expand All @@ -117,6 +121,10 @@ func (sc Scanner) Scan(params *ScanParams) {
reportData.Recomendations[strings.ToLower(r.ResourceType)][i] = r.ToAzureAprlRecommendation()
}
}

// scan diagnostic settings
diagnosticsScanner.Init(ctx, cred, clientOptions)

Check failure on line 126 in internal/scanner.go

View workflow job for this annotation

GitHub Actions / Build linux_amd64 binaries

Error return value of `diagnosticsScanner.Init` is not checked (errcheck)
diagResults = diagnosticsScanner.Scan(reportData.ResourceIDs())
}

// scan each subscription with AZQR scanners
Expand All @@ -133,9 +141,6 @@ func (sc Scanner) Scan(params *ScanParams) {
// scan private endpoints
peResults := peScanner.Scan(config)

// scan diagnostic settings
diagResults := diagnosticsScanner.Scan(config)

// scan public IPs
pips := pipScanner.Scan(config)

Expand Down Expand Up @@ -200,9 +205,7 @@ func (sc Scanner) Scan(params *ScanParams) {
reportData.CostData.Items = append(reportData.CostData.Items, costs.Items...)
}

resourceScanner := scanners.ResourceScanner{}
reportData.ResourceTypeCount = resourceScanner.GetCountPerResourceType(ctx, cred, subscriptions, reportData.Recomendations)
reportData.Resources = resourceScanner.GetAllResources(ctx, cred, subscriptions)

// render excel report
excel.CreateExcelReport(&reportData)
Expand Down
49 changes: 14 additions & 35 deletions internal/scanners/diagnostics_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"

"github.com/Azure/azqr/internal/azqr"
"github.com/Azure/azqr/internal/graph"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/monitor/armmonitor"
Expand All @@ -20,42 +20,25 @@ import (

// DiagnosticSettingsScanner - scanner for diagnostic settings
type DiagnosticSettingsScanner struct {
config *azqr.ScannerConfig
client *arm.Client
graphQuery *graph.GraphQuery
ctx context.Context
client *arm.Client
}

// Init - Initializes the DiagnosticSettingsScanner
func (d *DiagnosticSettingsScanner) Init(config *azqr.ScannerConfig) error {
d.config = config
client, err := arm.NewClient(moduleName+".DiagnosticSettingsBatch", moduleVersion, d.config.Cred, d.config.ClientOptions)
func (d *DiagnosticSettingsScanner) Init(ctx context.Context, cred azcore.TokenCredential, options *arm.ClientOptions) error {
client, err := arm.NewClient(moduleName+".DiagnosticSettingsBatch", moduleVersion, cred, options)
if err != nil {
return err
}
d.client = client
d.graphQuery = graph.NewGraphQuery(d.config.Cred)
d.ctx = ctx
return nil
}

// ListResourcesWithDiagnosticSettings - Lists all resources with diagnostic settings
func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings() (map[string]bool, error) {
resources := []string{}
func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings(resources []*string) (map[string]bool, error) {
res := map[string]bool{}

azqr.LogSubscriptionScan(d.config.SubscriptionID, "Resource Ids")

result := d.graphQuery.Query(d.config.Ctx, "resources | project id | order by id asc", []*string{&d.config.SubscriptionID})

if result == nil || result.Data == nil {
log.Info().Msg("Preflight: No resources found")
return res, nil
}

for _, row := range result.Data {
m := row.(map[string]interface{})
resources = append(resources, strings.ToLower(m["id"].(string)))
}

batches := int(math.Ceil(float64(len(resources)) / 20))

var wg sync.WaitGroup
Expand All @@ -67,7 +50,7 @@ func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings() (map[s
close(ch)
}()

azqr.LogSubscriptionScan(d.config.SubscriptionID, "Diagnostic Settings")
azqr.LogResourceTypeScan("Diagnostic Settings")

// Split resources into batches of 20 items.
batchSize := 20
Expand All @@ -76,9 +59,9 @@ func (d *DiagnosticSettingsScanner) ListResourcesWithDiagnosticSettings() (map[s
if j > len(resources) {
j = len(resources)
}
go func(r []string) {
go func(r []*string) {
defer wg.Done()
resp, err := d.restCall(d.config.Ctx, r)
resp, err := d.restCall(d.ctx, r)
if err != nil {
log.Fatal().Err(err).Msg("Failed to get diagnostic settings")
}
Expand Down Expand Up @@ -107,7 +90,7 @@ const (
moduleVersion = "v1.1.1"
)

func (d *DiagnosticSettingsScanner) restCall(ctx context.Context, resourceIds []string) (*ArmBatchResponse, error) {
func (d *DiagnosticSettingsScanner) restCall(ctx context.Context, resourceIds []*string) (*ArmBatchResponse, error) {
req, err := runtime.NewRequest(ctx, http.MethodPost, runtime.JoinPaths(d.client.Endpoint(), "batch"))
if err != nil {
return nil, err
Expand All @@ -124,7 +107,7 @@ func (d *DiagnosticSettingsScanner) restCall(ctx context.Context, resourceIds []
for _, resourceId := range resourceIds {
batch.Requests = append(batch.Requests, ArmBatchRequestItem{
HttpMethod: http.MethodGet,
RelativeUrl: resourceId + "/providers/microsoft.insights/diagnosticSettings?api-version=2021-05-01-preview",
RelativeUrl: *resourceId + "/providers/microsoft.insights/diagnosticSettings?api-version=2021-05-01-preview",
})
}

Expand Down Expand Up @@ -175,12 +158,8 @@ type (
}
)

func (d *DiagnosticSettingsScanner) Scan(config *azqr.ScannerConfig) map[string]bool {
err := d.Init(config)
if err != nil {
log.Fatal().Err(err).Msg("Failed to initialize Diagnostic Settings Scanner")
}
diagResults, err := d.ListResourcesWithDiagnosticSettings()
func (d *DiagnosticSettingsScanner) Scan(resources []*string) map[string]bool {
diagResults, err := d.ListResourcesWithDiagnosticSettings(resources)
if err != nil {
if azqr.ShouldSkipError(err) {
diagResults = map[string]bool{}
Expand Down

0 comments on commit aafba14

Please sign in to comment.