|
1 | 1 | package auditor
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "context"
|
6 |
| - "encoding/json" |
7 |
| - "fmt" |
8 |
| - "io" |
9 |
| - "net/http" |
10 | 5 | "time"
|
11 | 6 |
|
12 |
| - "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" |
13 | 7 | armoperationalinsights "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/operationalinsights/armoperationalinsights/v2"
|
14 | 8 | log "github.com/sirupsen/logrus"
|
15 | 9 | azureCommon "github.com/webdevops/go-common/azuresdk/armclient"
|
16 |
| - "github.com/webdevops/go-common/azuresdk/cloudconfig" |
| 10 | + "github.com/webdevops/go-common/azuresdk/loganalytics" |
17 | 11 | prometheusCommon "github.com/webdevops/go-common/prometheus"
|
18 | 12 | "github.com/webdevops/go-common/utils/to"
|
19 | 13 |
|
@@ -60,18 +54,6 @@ func (auditor *AzureAuditor) auditLogAnalytics(ctx context.Context, logger *log.
|
60 | 54 | }
|
61 | 55 |
|
62 | 56 | func (auditor *AzureAuditor) queryLogAnalytics(ctx context.Context, logger *log.Entry, config *validator.AuditConfigValidation) (list []*validator.AzureObject) {
|
63 |
| - var ( |
64 |
| - baseUrl string |
65 |
| - ) |
66 |
| - |
67 |
| - switch auditor.azure.client.GetCloudName() { |
68 |
| - case cloudconfig.AzurePublicCloud: |
69 |
| - baseUrl = "https://api.loganalytics.io" |
70 |
| - case cloudconfig.AzureChinaCloud: |
71 |
| - baseUrl = "https://api.loganalytics.azure.cn" |
72 |
| - case cloudconfig.AzureGovernmentCloud: |
73 |
| - baseUrl = "https://api.loganalytics.us" |
74 |
| - } |
75 | 57 |
|
76 | 58 | subscriptionList := auditor.getSubscriptionList(ctx)
|
77 | 59 |
|
@@ -111,58 +93,18 @@ func (auditor *AzureAuditor) queryLogAnalytics(ctx context.Context, logger *log.
|
111 | 93 | }
|
112 | 94 | }
|
113 | 95 |
|
114 |
| - scopeUrl := fmt.Sprintf("%s/.default", baseUrl) |
115 |
| - queryUrl := fmt.Sprintf("%s/v1/workspaces/%s/query", baseUrl, to.String(mainWorkspaceId)) |
116 |
| - |
117 |
| - credToken, err := auditor.azure.client.GetCred().GetToken(ctx, policy.TokenRequestOptions{ |
118 |
| - Scopes: []string{scopeUrl}, |
119 |
| - }) |
120 |
| - if err != nil { |
121 |
| - workspaceLogger.Error(err) |
122 |
| - return |
123 |
| - } |
124 |
| - |
125 |
| - // execute query |
126 | 96 | workspaceLogger.WithField("workspaces", workspaces).Debug("sending query")
|
127 | 97 | startTime := time.Now()
|
128 |
| - requestBody := struct { |
129 |
| - Query *string `json:"query"` |
130 |
| - Workspaces *[]string `json:"workspaces"` |
131 |
| - Timespan *string `json:"timespan"` |
132 |
| - }{ |
133 |
| - Query: config.Query, |
134 |
| - Workspaces: &workspaces, |
135 |
| - Timespan: config.Timespan, |
136 |
| - } |
137 |
| - |
138 |
| - requestBodyBytes, err := json.Marshal(requestBody) |
139 |
| - if err != nil { |
140 |
| - log.Fatal(err) |
141 |
| - } |
142 |
| - bytes.NewBuffer(requestBodyBytes) |
143 |
| - |
144 |
| - req, err := http.NewRequest(http.MethodPost, queryUrl, bytes.NewBuffer(requestBodyBytes)) |
| 98 | + |
| 99 | + queryResults, err := loganalytics.ExecuteQuery( |
| 100 | + ctx, |
| 101 | + auditor.azure.client, |
| 102 | + *mainWorkspaceId, |
| 103 | + *config.Query, |
| 104 | + config.Timespan, |
| 105 | + &workspaces, |
| 106 | + ) |
145 | 107 | if err != nil {
|
146 |
| - log.Fatal(err) |
147 |
| - } |
148 |
| - req.Method = http.MethodPost |
149 |
| - req.Header.Add("Content-Type", "application/json") |
150 |
| - req.Header.Add("authorization", fmt.Sprintf("Bearer %s", credToken.Token)) |
151 |
| - response, err := http.DefaultClient.Do(req) |
152 |
| - if err != nil { |
153 |
| - workspaceLogger.Error(err) |
154 |
| - return |
155 |
| - } |
156 |
| - defer response.Body.Close() |
157 |
| - |
158 |
| - responseBody, err := io.ReadAll(response.Body) |
159 |
| - if err != nil { |
160 |
| - workspaceLogger.Error(err) |
161 |
| - return |
162 |
| - } |
163 |
| - |
164 |
| - var queryResults LogAnaltyicsQueryResult |
165 |
| - if err := json.Unmarshal(responseBody, &queryResults); err != nil { |
166 | 108 | workspaceLogger.Error(err)
|
167 | 109 | return
|
168 | 110 | }
|
|
0 commit comments