-
Notifications
You must be signed in to change notification settings - Fork 351
/
metadata.go
70 lines (63 loc) · 1.93 KB
/
metadata.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
package azure
import (
"crypto/md5" //nolint:gosec
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/treeverse/lakefs/pkg/cloud"
"github.com/treeverse/lakefs/pkg/logging"
)
const azureMetadataIP = "169.254.169.254"
const metadataRequestTimeout = 5 * time.Second
type MetadataProvider struct {
logger logging.Logger
}
func NewMetadataProvider(logger logging.Logger) *MetadataProvider {
return &MetadataProvider{logger: logger}
}
type instanceMetadataResponse struct {
Compute struct {
SubscriptionID string `json:"subscriptionId"`
} `json:"compute"`
}
func (m *MetadataProvider) GetMetadata() map[string]string {
client := http.Client{Timeout: metadataRequestTimeout}
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%s/metadata/instance", azureMetadataIP), nil)
if err != nil {
m.logger.WithError(err).Warn("Failed to create request for Azure instance metadata")
return nil
}
req.Header.Add("Metadata", "True")
q := url.Values{"format": {"json"}, "api-version": {"2019-03-11"}}
req.URL.RawQuery = q.Encode()
resp, err := client.Do(req)
if err != nil {
m.logger.WithError(err).Warn("Failed to get Azure subscription ID from instance metadata", err)
return nil
}
defer func() {
_ = resp.Body.Close()
}()
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
m.logger.WithError(err).Warn("Failed to get Azure subscription ID from instance metadata", err)
return nil
}
responseObj := &instanceMetadataResponse{}
err = json.Unmarshal(responseBody, responseObj)
if err != nil {
m.logger.WithError(err).Warn("Failed to get Azure subscription ID from instance metadata", err)
return nil
}
if responseObj.Compute.SubscriptionID == "" {
m.logger.Info("Got empty subscription id from azure")
return nil
}
return map[string]string{
cloud.IDKey: fmt.Sprintf("%x", md5.Sum([]byte(responseObj.Compute.SubscriptionID))), //nolint:gosec
cloud.IDTypeKey: "azure_subscription_id",
}
}