forked from Azure/azure-kusto-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudinfo.go
115 lines (96 loc) · 3.37 KB
/
cloudinfo.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
package kusto
import (
"encoding/json"
"fmt"
kustoErrors "github.com/andyliuliming/azure-kusto-go/kusto/data/errors"
"github.com/andyliuliming/azure-kusto-go/kusto/utils"
"io"
"net/http"
"net/url"
"os"
"sync"
)
// abstraction to query metadata and use this information for providing all
// information needed for connection string builder to provide all the requisite information
const (
metadataPath = "v1/rest/auth/metadata"
defaultAuthEnvVarName = "AadAuthorityUri"
defaultKustoClientAppId = "db662dc1-0cfe-4e1c-a843-19a68e65be58"
defaultPublicLoginUrl = "https://login.microsoftonline.com"
defaultRedirectUri = "https://microsoft/kustoclient"
defaultKustoServiceResourceId = "https://kusto.kusto.windows.net"
defaultFirstPartyAuthorityUrl = "https://login.microsoftonline.com/f8cdef31-a31e-4b4a-93e4-5f571e91255a"
)
// retrieved metadata
type metaResp struct {
AzureAD CloudInfo
}
type CloudInfo struct {
LoginEndpoint string `json:"LoginEndpoint"`
LoginMfaRequired bool `json:"LoginMfaRequired"`
KustoClientAppID string `json:"KustoClientAppId"`
KustoClientRedirectURI string `json:"KustoClientRedirectUri"`
KustoServiceResourceID string `json:"KustoServiceResourceId"`
FirstPartyAuthorityURL string `json:"FirstPartyAuthorityUrl"`
}
var defaultCloudInfo = CloudInfo{
LoginEndpoint: getEnvOrDefault(defaultAuthEnvVarName, defaultPublicLoginUrl),
LoginMfaRequired: false,
KustoClientAppID: defaultKustoClientAppId,
KustoClientRedirectURI: defaultRedirectUri,
KustoServiceResourceID: defaultKustoServiceResourceId,
FirstPartyAuthorityURL: defaultFirstPartyAuthorityUrl,
}
// cache to query it once per instance
var cloudInfoCache sync.Map
func GetMetadata(kustoUri string, httpClient *http.Client) (CloudInfo, error) {
// retrieve &return if exists
once, ok := cloudInfoCache.Load(kustoUri)
if !ok {
once = utils.NewOnce[CloudInfo]()
cloudInfoCache.Store(kustoUri, once)
}
return once.(utils.Once[CloudInfo]).Do(func() (CloudInfo, error) {
u, err := url.Parse(kustoUri)
if err != nil {
return CloudInfo{}, err
}
u.Path = metadataPath
// TODO should we make this timeout configurable.
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return CloudInfo{}, kustoErrors.E(kustoErrors.OpCloudInfo, kustoErrors.KHTTPError, err)
}
resp, err := httpClient.Do(req)
if err != nil {
return CloudInfo{}, err
}
// Handle internal server error as a special case and return as an error (to be consistent with other SDK's)
if resp.StatusCode >= http.StatusInternalServerError {
return CloudInfo{}, kustoErrors.E(kustoErrors.OpCloudInfo, kustoErrors.KHTTPError, fmt.Errorf("error %s when querying endpoint %s",
resp.Status, u.String()),
)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return CloudInfo{}, kustoErrors.E(kustoErrors.OpCloudInfo, kustoErrors.KHTTPError, err)
}
// Covers scenarios of 200/OK with no body or a 404 where there is no body
if len(b) == 0 {
return defaultCloudInfo, nil
}
md := metaResp{}
if err := json.Unmarshal(b, &md); err != nil {
return CloudInfo{}, err
}
// this should be set in the map by now
return md.AzureAD, nil
})
}
func getEnvOrDefault(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}