-
Notifications
You must be signed in to change notification settings - Fork 70
/
azure.go
657 lines (576 loc) · 20 KB
/
azure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"time"
"github.com/RobustPerception/azure_metrics_exporter/config"
)
var (
apiVersionDate = regexp.MustCompile("^\\d{4}-\\d{2}-\\d{2}")
)
// AzureMetricDefinitionResponse represents metric definition response for a given resource from Azure.
type AzureMetricDefinitionResponse struct {
MetricDefinitionResponses []metricDefinitionResponse `json:"value"`
}
type metricDefinitionResponse struct {
Dimensions []struct {
LocalizedValue string `json:"localizedValue"`
Value string `json:"value"`
} `json:"dimensions"`
ID string `json:"id"`
IsDimensionRequired bool `json:"isDimensionRequired"`
MetricAvailabilities []struct {
Retention string `json:"retention"`
TimeGrain string `json:"timeGrain"`
} `json:"metricAvailabilities"`
Name struct {
LocalizedValue string `json:"localizedValue"`
Value string `json:"value"`
} `json:"name"`
PrimaryAggregationType string `json:"primaryAggregationType"`
ResourceID string `json:"resourceId"`
Unit string `json:"unit"`
}
// MetricNamespaceCollectionResponse represents metric namespace response for a given resource from Azure.
type MetricNamespaceCollectionResponse struct {
MetricNamespaceCollection []metricNamespaceResponse `json:"value"`
}
type metricNamespaceResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Classification string `json:"classification"`
Properties struct {
MetricNamespaceName string `json:"metricNamespaceName"`
} `json:"properties"`
}
// AzureMetricValueResponse represents a metric value response for a given metric definition.
type AzureMetricValueResponse struct {
Value []struct {
Timeseries []struct {
Data []struct {
TimeStamp string `json:"timeStamp"`
Total float64 `json:"total"`
Average float64 `json:"average"`
Minimum float64 `json:"minimum"`
Maximum float64 `json:"maximum"`
} `json:"data"`
} `json:"timeseries"`
ID string `json:"id"`
Name struct {
LocalizedValue string `json:"localizedValue"`
Value string `json:"value"`
} `json:"name"`
Type string `json:"type"`
Unit string `json:"unit"`
} `json:"value"`
APIError struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
type AzureBatchMetricResponse struct {
Responses []struct {
HttpStatusCode int `json:"httpStatusCode"`
Content AzureMetricValueResponse `json:"content"`
} `json:"responses"`
}
type AzureBatchLookupResponse struct {
Responses []struct {
HttpStatusCode int `json:"httpStatusCode"`
Content AzureResource `json:"content"`
} `json:"responses"`
}
type AzureResourceListResponse struct {
Value []AzureResource `json:"value"`
}
type AzureResource struct {
ID string `json:"id" pretty:"id"`
Name string `json:"name" pretty:"resource_name"`
Location string `json:"location" pretty:"azure_location"`
Type string `json:"type" pretty:"resource_type"`
Tags map[string]string `json:"tags" pretty:"tags"`
ManagedBy string `json:"managedBy" pretty:"managed_by"`
Subscription string `pretty:"azure_subscription"`
}
type APIVersionResponse struct {
Value []struct {
ID string `json:"id"`
Namespace string `json:"namespace"`
ResourceTypes []struct {
ResourceType string `json:"resourceType"`
Locations []string `json:"locations"`
APIVersions []string `json:"apiVersions"`
} `json:"resourceTypes"`
RegistrationState string `json:"registrationState"`
} `json:"value"`
}
type APIVersionData struct {
Endpoint string
Date time.Time
}
type APIVersionMap map[string]string
func latestVersionFrom(apiList []string) string {
var latest = &APIVersionData{}
format := "2006-01-02"
for _, api := range apiList {
dateStr := apiVersionDate.FindString(api)
date, err := time.Parse(format, dateStr)
if err != nil {
log.Println(err)
continue
}
if latest == nil || latest.Date.Before(date) {
latest = &APIVersionData{Endpoint: api, Date: date}
}
}
return latest.Endpoint
}
func (r *APIVersionResponse) extractAPIVersions() APIVersionMap {
var apiVersions = APIVersionMap{}
for _, val := range r.Value {
for _, t := range val.ResourceTypes {
if len(t.APIVersions) == 0 {
continue
}
resourceType := strings.Join([]string{val.Namespace, t.ResourceType}, "/")
apiVersions[resourceType] = latestVersionFrom(t.APIVersions)
}
}
return apiVersions
}
func (m *APIVersionMap) findBy(resourceType string) string {
var apiVersion string
for mType, mVersion := range *m {
if mType == resourceType {
apiVersion = mVersion
break
}
}
return apiVersion
}
// AzureClient represents our client to talk to the Azure api
type AzureClient struct {
client *http.Client
accessToken string
accessTokenExpiresOn time.Time
APIVersions APIVersionMap
}
// NewAzureClient returns an Azure client to talk the Azure API
func NewAzureClient() *AzureClient {
return &AzureClient{
client: &http.Client{},
accessToken: "",
accessTokenExpiresOn: time.Time{},
}
}
func (ac *AzureClient) getAccessToken() error {
var resp *http.Response
var err error
if len(sc.C.Credentials.ClientID) == 0 {
log.Printf("Using managed identity")
target := fmt.Sprintf("http://169.254.169.254/metadata/identity/oauth2/token?resource=%s&api-version=2018-02-01", sc.C.ResourceManagerURL)
req, err := http.NewRequest("GET", target, nil)
if err != nil {
return fmt.Errorf("Error getting token against Azure MSI endpoint: %v", err)
}
req.Header.Add("Metadata", "true")
resp, err = ac.client.Do(req)
} else {
target := fmt.Sprintf("%s/%s/oauth2/token", sc.C.ActiveDirectoryAuthorityURL, sc.C.Credentials.TenantID)
form := url.Values{
"grant_type": {"client_credentials"},
"resource": {sc.C.ResourceManagerURL},
"client_id": {sc.C.Credentials.ClientID},
"client_secret": {sc.C.Credentials.ClientSecret},
}
resp, err = ac.client.PostForm(target, form)
}
if err != nil {
return fmt.Errorf("Error authenticating against Azure API: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
respBytest, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("Did not get status code 200, got: %d with body: %s", resp.StatusCode, string(respBytest))
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Error reading body of response: %v", err)
}
var data map[string]interface{}
err = json.Unmarshal(body, &data)
if err != nil {
return fmt.Errorf("Error unmarshalling response body: %v", err)
}
ac.accessToken = data["access_token"].(string)
expiresOn, err := strconv.ParseInt(data["expires_on"].(string), 10, 64)
if err != nil {
return fmt.Errorf("Error ParseInt of expires_on failed: %v", err)
}
ac.accessTokenExpiresOn = time.Unix(expiresOn, 0).UTC()
return nil
}
// Returns metric definitions for all configured target and resource groups
func (ac *AzureClient) getMetricDefinitions() (map[string]AzureMetricDefinitionResponse, error) {
definitions := make(map[string]AzureMetricDefinitionResponse)
for _, target := range sc.C.Targets {
def, err := ac.getAzureMetricDefinitionResponse(target.Resource, target.MetricNamespace)
if err != nil {
return nil, err
}
defKey := target.Resource
if len(target.MetricNamespace) > 0 {
defKey = fmt.Sprintf("%s (Metric namespace: %s)", defKey, target.MetricNamespace)
}
definitions[defKey] = *def
}
for _, resourceGroup := range sc.C.ResourceGroups {
resources, err := ac.filteredListFromResourceGroup(resourceGroup)
if err != nil {
return nil, fmt.Errorf("Failed to get resources for resource group %s and resource types %s: %v",
resourceGroup.ResourceGroup, resourceGroup.ResourceTypes, err)
}
for _, resource := range resources {
def, err := ac.getAzureMetricDefinitionResponse(resource.ID, resourceGroup.MetricNamespace)
if err != nil {
return nil, err
}
defKey := resource.ID
if len(resourceGroup.MetricNamespace) > 0 {
defKey = fmt.Sprintf("%s (Metric namespace: %s)", defKey, resourceGroup.MetricNamespace)
}
definitions[defKey] = *def
}
}
return definitions, nil
}
// Returns metric namespaces for all configured target and resource groups.
func (ac *AzureClient) getMetricNamespaces() (map[string]MetricNamespaceCollectionResponse, error) {
namespaces := make(map[string]MetricNamespaceCollectionResponse)
for _, target := range sc.C.Targets {
namespaceCollection, err := ac.getMetricNamespaceCollectionResponse(target.Resource)
if err != nil {
return nil, err
}
namespaces[target.Resource] = *namespaceCollection
}
for _, resourceGroup := range sc.C.ResourceGroups {
resources, err := ac.filteredListFromResourceGroup(resourceGroup)
if err != nil {
return nil, fmt.Errorf("Failed to get resources for resource group %s and resource types %s: %v",
resourceGroup.ResourceGroup, resourceGroup.ResourceTypes, err)
}
for _, resource := range resources {
namespaceCollection, err := ac.getMetricNamespaceCollectionResponse(resource.ID)
if err != nil {
return nil, err
}
namespaces[resource.ID] = *namespaceCollection
}
}
return namespaces, nil
}
// Returns AzureMetricDefinitionResponse for a given resource
func (ac *AzureClient) getAzureMetricDefinitionResponse(resource string, metricNamespace string) (*AzureMetricDefinitionResponse, error) {
apiVersion := "2018-01-01"
metricsResource := fmt.Sprintf("subscriptions/%s%s", sc.C.Credentials.SubscriptionID, resource)
metricsTarget := fmt.Sprintf("%s/%s/providers/microsoft.insights/metricDefinitions?api-version=%s", sc.C.ResourceManagerURL, metricsResource, apiVersion)
if metricNamespace != "" {
metricsTarget = fmt.Sprintf("%s&metricnamespace=%s", metricsTarget, url.QueryEscape(metricNamespace))
}
req, err := http.NewRequest("GET", metricsTarget, nil)
if err != nil {
return nil, fmt.Errorf("Error creating HTTP request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+ac.accessToken)
resp, err := ac.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error reading body of response: %v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error: %v", string(body))
}
def := &AzureMetricDefinitionResponse{}
err = json.Unmarshal(body, def)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling response body: %v", err)
}
return def, nil
}
// Returns MetricNamespaceCollectionResponse for a given resource
func (ac *AzureClient) getMetricNamespaceCollectionResponse(resource string) (*MetricNamespaceCollectionResponse, error) {
apiVersion := "2017-12-01-preview"
nsResource := fmt.Sprintf("subscriptions/%s%s", sc.C.Credentials.SubscriptionID, resource)
nsTarget := fmt.Sprintf("%s/%s/providers/microsoft.insights/metricNamespaces?api-version=%s", sc.C.ResourceManagerURL, nsResource, apiVersion)
req, err := http.NewRequest("GET", nsTarget, nil)
if err != nil {
return nil, fmt.Errorf("Error creating HTTP request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+ac.accessToken)
resp, err := ac.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error reading body of response: %v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error: %v", string(body))
}
namespaceCollection := &MetricNamespaceCollectionResponse{}
err = json.Unmarshal(body, namespaceCollection)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling response body: %v", err)
}
return namespaceCollection, nil
}
// Returns resource list resolved and filtered from resource_groups configuration
func (ac *AzureClient) filteredListFromResourceGroup(resourceGroup config.ResourceGroup) ([]AzureResource, error) {
resources, err := ac.listFromResourceGroup(resourceGroup.ResourceGroup, resourceGroup.ResourceTypes)
if err != nil {
return nil, err
}
filteredResources := ac.filterResources(resources, resourceGroup)
return filteredResources, nil
}
// Returns resource list filtered by tag name and tag value
func (ac *AzureClient) filteredListByTag(resourceTag config.ResourceTag, resourcesMap map[string][]byte) ([]AzureResource, error) {
resources, err := ac.listByTag(resourceTag.ResourceTagName, resourceTag.ResourceTagValue, resourceTag.ResourceTypes, resourcesMap)
if err != nil {
return nil, err
}
return resources, nil
}
// Returns all resources for given resource group and types
func (ac *AzureClient) listFromResourceGroup(resourceGroup string, resourceTypes []string) ([]AzureResource, error) {
apiVersion := "2018-02-01"
var filterTypesElements []string
for _, filterType := range resourceTypes {
filterTypesElements = append(filterTypesElements, fmt.Sprintf("resourcetype eq '%s'", filterType))
}
filterTypes := url.QueryEscape(strings.Join(filterTypesElements, " or "))
subscription := fmt.Sprintf("subscriptions/%s", sc.C.Credentials.SubscriptionID)
resourcesEndpoint := fmt.Sprintf("%s/%s/resourceGroups/%s/resources?api-version=%s&$filter=%s", sc.C.ResourceManagerURL, subscription, resourceGroup, apiVersion, filterTypes)
body, err := getAzureMonitorResponse(resourcesEndpoint)
if err != nil {
return nil, err
}
var data AzureResourceListResponse
err = json.Unmarshal(body, &data)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling response body: %v", err)
}
return data.extendResources(), nil
}
// Returns all resource with the given couple tagname, tagvalue
func (ac *AzureClient) listByTag(tagName string, tagValue string, types []string, resourcesMap map[string][]byte) ([]AzureResource, error) {
apiVersion := "2018-05-01"
securedTagName := secureString(tagName)
securedTagValue := secureString(tagValue)
filterTypes := url.QueryEscape(fmt.Sprintf("tagName eq '%s' and tagValue eq '%s'", securedTagName, securedTagValue))
subscription := fmt.Sprintf("subscriptions/%s", sc.C.Credentials.SubscriptionID)
resourcesEndpoint := fmt.Sprintf("%s/%s/resources?api-version=%s&$filter=%s", sc.C.ResourceManagerURL, subscription, apiVersion, filterTypes)
body, ok := resourcesMap[resourcesEndpoint]
if !ok {
var err error
body, err = getAzureMonitorResponse(resourcesEndpoint)
if err != nil {
return nil, err
}
resourcesMap[resourcesEndpoint] = body
}
var data AzureResourceListResponse
err := json.Unmarshal(body, &data)
if err != nil {
return nil, fmt.Errorf("Error unmarshalling response body: %v", err)
}
if len(types) > 0 {
data.Value = data.filterTypesInResourceList(types)
}
return data.extendResources(), nil
}
func (ac *AzureClient) listAPIVersions() error {
apiVersion := "2021-04-01"
var versionResponse APIVersionResponse
subscription := fmt.Sprintf("subscriptions/%s", sc.C.Credentials.SubscriptionID)
resourcesEndpoint := fmt.Sprintf("%s/%s/providers?api-version=%s", sc.C.ResourceManagerURL, subscription, apiVersion)
body, err := getAzureMonitorResponse(resourcesEndpoint)
if err != nil {
return err
}
err = json.Unmarshal(body, &versionResponse)
if err != nil {
return fmt.Errorf("Error unmarshalling response body: %v", err)
}
ac.APIVersions = versionResponse.extractAPIVersions()
return nil
}
func (response *AzureResourceListResponse) filterTypesInResourceList(types []string) []AzureResource {
typesMap := make(map[string]struct{})
for _, resourceType := range types {
typesMap[resourceType] = struct{}{}
}
var filteredResources []AzureResource
for _, resource := range response.Value {
if _, typeExist := typesMap[resource.Type]; typeExist {
filteredResources = append(filteredResources, resource)
}
}
return filteredResources
}
func secureString(value string) string {
securedValue := strings.Replace(value, "'", "\\'", -1)
return securedValue
}
func getAzureMonitorResponse(azureManagementEndpoint string) ([]byte, error) {
req, err := http.NewRequest("GET", azureManagementEndpoint, nil)
if err != nil {
return nil, fmt.Errorf("Error creating HTTP request: %v", err)
}
req.Header.Set("Authorization", "Bearer "+ac.accessToken)
resp, err := ac.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("Unable to query API with status code: %d and with body: %s", resp.StatusCode, body)
}
if err != nil {
return nil, fmt.Errorf("Error reading body of response: %v", err)
}
return body, err
}
func (ar *AzureResourceListResponse) extendResources() []AzureResource {
subscription := fmt.Sprintf("subscriptions/%s", sc.C.Credentials.SubscriptionID)
var subscriptionPrefixLen = len(subscription) + 1
for i, val := range ar.Value {
ar.Value[i].ID = val.ID[subscriptionPrefixLen:]
ar.Value[i].Subscription = sc.C.Credentials.SubscriptionID
}
return ar.Value
}
// Returns a filtered resource list based on a given resource list and regular expressions from the configuration
func (ac *AzureClient) filterResources(resources []AzureResource, resourceGroup config.ResourceGroup) []AzureResource {
filteredResources := []AzureResource{}
for _, resource := range resources {
if len(resourceGroup.ResourceNameIncludeRe) != 0 {
include := false
for _, rx := range resourceGroup.ResourceNameIncludeRe {
if rx.MatchString(resource.Name) {
include = true
break
}
}
if !include {
continue
}
}
exclude := false
for _, rx := range resourceGroup.ResourceNameExcludeRe {
if rx.MatchString(resource.Name) {
exclude = true
break
}
}
if exclude {
continue
}
filteredResources = append(filteredResources, resource)
}
return filteredResources
}
func (ac *AzureClient) refreshAccessToken() error {
now := time.Now().UTC()
refreshAt := ac.accessTokenExpiresOn.Add(-10 * time.Minute)
if now.After(refreshAt) {
err := ac.getAccessToken()
if err != nil {
return fmt.Errorf("Error refreshing access token: %v", err)
}
}
return nil
}
type batchBody struct {
Requests []batchRequest `json:"requests"`
}
type batchRequest struct {
RelativeURL string `json:"relativeUrl"`
Method string `json:"httpMethod"`
}
func resourceURLFrom(resource string, metricNamespace string, metricNames string, aggregations []string) string {
apiVersion := "2018-01-01"
path := fmt.Sprintf(
"/subscriptions/%s%s/providers/microsoft.insights/metrics",
sc.C.Credentials.SubscriptionID,
resource,
)
endTime, startTime := GetTimes()
values := url.Values{}
if metricNames != "" {
values.Add("metricnames", metricNames)
}
if metricNamespace != "" {
values.Add("metricnamespace", metricNamespace)
}
filtered := filterAggregations(aggregations)
values.Add("aggregation", strings.Join(filtered, ","))
values.Add("timespan", fmt.Sprintf("%s/%s", startTime, endTime))
values.Add("api-version", apiVersion)
url := url.URL{
Path: path,
RawQuery: values.Encode(),
}
return url.String()
}
func (ac *AzureClient) getBatchResponseBody(urls []string) ([]byte, error) {
rmBaseURL := sc.C.ResourceManagerURL
if !strings.HasSuffix(sc.C.ResourceManagerURL, "/") {
rmBaseURL += "/"
}
apiURL := fmt.Sprintf("%sbatch?api-version=2017-03-01", rmBaseURL)
batch := batchBody{}
for _, u := range urls {
batch.Requests = append(batch.Requests, batchRequest{
RelativeURL: u,
Method: "GET",
})
}
batchJSON, err := json.Marshal(batch)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(batchJSON))
if err != nil {
return nil, fmt.Errorf("Error creating HTTP request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+ac.accessToken)
resp, err := ac.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}