Skip to content

Commit

Permalink
bug: improved ARG results parsing #248
Browse files Browse the repository at this point in the history
  • Loading branch information
cmendible committed Aug 20, 2024
1 parent 0872a3a commit 243f42a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
40 changes: 31 additions & 9 deletions internal/aprl_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package internal
import (
"context"
"embed"
"encoding/json"
"fmt"
"io/fs"
"math"
"strings"
Expand Down Expand Up @@ -189,39 +191,42 @@ func (sc AprlScanner) graphScan(ctx context.Context, graphClient *graph.GraphQue
m := row.(map[string]interface{})

tags := ""
// if m["tags"] != nil {
// tags = m["tags"].(string)
// }
if m["tags"] != nil {
tags = convertInterfaceToString(m["tags"])
}

param1 := ""
if m["param1"] != nil {
param1 = m["param1"].(string)
param1 = convertInterfaceToString(m["param1"])
}

param2 := ""
if m["param2"] != nil {
param2 = m["param2"].(string)
param2 = convertInterfaceToString(m["param2"])
}

param3 := ""
if m["param3"] != nil {
param3 = m["param3"].(string)
param3 = convertInterfaceToString(m["param3"])
}

param4 := ""
if m["param4"] != nil {
param4 = m["param4"].(string)
param4 = convertInterfaceToString(m["param4"])
}

param5 := ""
if m["param5"] != nil {
param5 = m["param5"].(string)
param5 = convertInterfaceToString(m["param5"])
}

log.Debug().Msg(rule.GraphQuery)

subscription := azqr.GetSubsctiptionFromResourceID(m["id"].(string))
subscriptionName := subscriptions[subscription]
subscriptionName, ok := subscriptions[subscription]
if !ok {
subscriptionName = ""
}

results = append(results, azqr.AprlResult{
RecommendationID: rule.RecommendationID,
Expand Down Expand Up @@ -270,3 +275,20 @@ func (sc AprlScanner) getGraphRules(service string, filters *azqr.Filters, aprl
}
return r
}

func convertInterfaceToString(i interface{}) string {
switch v := i.(type) {
case string:
return v
case int:
return fmt.Sprintf("%d", v)
case bool:
return fmt.Sprintf("%t", v)
default:
jsonStr, err := json.Marshal(i)
if err != nil {
log.Fatal().Err(err).Msg("unssupported type in ARG query result")
}
return string(jsonStr)
}
}
3 changes: 3 additions & 0 deletions internal/azqr/azqr.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ func ListResourceGroup(ctx context.Context, cred azcore.TokenCredential, subscri
// GetSubsctiptionFromResourceID - Get Subscription ID from Resource ID
func GetSubsctiptionFromResourceID(resourceID string) string {
parts := strings.Split(resourceID, "/")
if len(parts) < 3 {
return ""
}
return parts[2]
}

Expand Down

0 comments on commit 243f42a

Please sign in to comment.