-
Notifications
You must be signed in to change notification settings - Fork 46
/
client.go
240 lines (217 loc) · 9 KB
/
client.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
// Copyright (c) HashiCorp Inc. All rights reserved.
// Licensed under the MPL-2.0 License. See NOTICE.txt in the project root for license information.
package metadata
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
"runtime"
"time"
)
// NOTE: this Client cannot use the base client since it'd cause a circular reference
type Client struct {
endpoint string
}
func NewClientWithEndpoint(endpoint string) *Client {
return &Client{
endpoint: endpoint,
}
}
// GetMetaData connects to the ARM metadata service at the configured endpoint, to retrieve information about the
// current environment. Sometimes an endpoint will not support the latest schema, in such cases it will not be
// possible to configure all services but a best effort will be made to request and parse an earlier schema version.
// `name` is used when falling back to an earlier schema version where multiple environments are returned and the
// desired one must be matched by name.
func (c *Client) GetMetaData(ctx context.Context, name string) (*MetaData, error) {
metadata, err := c.getMetaDataFrom2022API(ctx, name)
if err != nil {
log.Printf("[DEBUG] Falling back to ARM Metadata version 2019-05-01 for %s", c.endpoint)
metadata, err = c.getMetaDataFrom2019API(ctx, name)
if err != nil {
return nil, fmt.Errorf("retrieving metadata from the 2022-09-01 and 2019-05-01 APIs: %+v", err)
}
}
return &MetaData{
Authentication: Authentication{
Audiences: metadata.Authentication.Audiences,
LoginEndpoint: metadata.Authentication.LoginEndpoint,
IdentityProvider: metadata.Authentication.IdentityProvider,
Tenant: metadata.Authentication.Tenant,
},
DnsSuffixes: DnsSuffixes{
Attestation: metadata.Suffixes.AttestationEndpoint,
FrontDoor: metadata.Suffixes.AzureFrontDoorEndpointSuffix,
KeyVault: metadata.Suffixes.KeyVaultDns,
ManagedHSM: metadata.Suffixes.MhsmDns,
MariaDB: metadata.Suffixes.MariadbServerEndpoint,
MySql: metadata.Suffixes.MysqlServerEndpoint,
Postgresql: metadata.Suffixes.PostgresqlServerEndpoint,
SqlServer: metadata.Suffixes.SqlServerHostname,
Storage: metadata.Suffixes.Storage,
StorageSync: metadata.Suffixes.StorageSyncEndpointSuffix,
Synapse: metadata.Suffixes.SynapseAnalytics,
},
Name: metadata.Name,
ResourceIdentifiers: ResourceIdentifiers{
Attestation: normalizeResourceId(metadata.AttestationResourceId),
Batch: normalizeResourceId(metadata.Batch),
LogAnalytics: normalizeResourceId(metadata.LogAnalyticsResourceId),
Media: normalizeResourceId(metadata.Media),
MicrosoftGraph: normalizeResourceId(metadata.MicrosoftGraphResourceId),
OSSRDBMS: normalizeResourceId(metadata.OssrDbmsResourceId),
Synapse: normalizeResourceId(metadata.SynapseAnalyticsResourceId),
},
ResourceManagerEndpoint: metadata.ResourceManager,
}, nil
}
func (c *Client) getMetaDataFrom2022API(ctx context.Context, name string) (*metaDataResponse, error) {
tlsConfig := tls.Config{
MinVersion: tls.VersionTLS12,
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{Resolver: &net.Resolver{}}
return d.DialContext(ctx, network, addr)
},
TLSClientConfig: &tlsConfig,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
},
}
uri := fmt.Sprintf("%s/metadata/endpoints?api-version=2022-09-01", c.endpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("preparing request: %+v", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("performing request: %+v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("performing request: expected 200 OK but got %d %s", resp.StatusCode, resp.Status)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("parsing response body: %+v", err)
}
resp.Body.Close()
// Trim away a BOM if present
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
var model *metaDataResponse
if err := json.Unmarshal(respBody, &model); err != nil {
log.Printf("[DEBUG] Unrecognised metadata response for %s: %s", uri, respBody)
return nil, fmt.Errorf("unmarshaling response: %+v", err)
}
return model, nil
}
func (c *Client) getMetaDataFrom2019API(ctx context.Context, name string) (*metaDataResponse, error) {
tlsConfig := tls.Config{
MinVersion: tls.VersionTLS12,
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{Resolver: &net.Resolver{}}
return d.DialContext(ctx, network, addr)
},
TLSClientConfig: &tlsConfig,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
ForceAttemptHTTP2: true,
MaxIdleConnsPerHost: runtime.GOMAXPROCS(0) + 1,
},
}
uri := fmt.Sprintf("%s/metadata/endpoints?api-version=2019-05-01", c.endpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("preparing request: %+v", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("performing request: %+v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("performing request: expected 200 OK but got %d %s", resp.StatusCode, resp.Status)
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("parsing response body: %+v", err)
}
resp.Body.Close()
// Trim away a BOM if present
respBody = bytes.TrimPrefix(respBody, []byte("\xef\xbb\xbf"))
var model *[]metaDataResponse
if err := json.Unmarshal(respBody, &model); err != nil {
log.Printf("[DEBUG] Unrecognised metadata response for %s: %s", uri, respBody)
return nil, fmt.Errorf("unmarshaling response: %+v", err)
}
if model == nil {
return nil, fmt.Errorf("unmarshaling response: no environments returned")
}
// This version returns an array of environments, we are only interested in one
var env metaDataResponse
for _, e := range *model {
if name == "" || e.Name == name {
env = e
break
}
}
return &env, nil
}
type metaDataResponse struct {
Portal string `json:"portal"`
Authentication struct {
LoginEndpoint string `json:"loginEndpoint"`
Audiences []string `json:"audiences"`
Tenant string `json:"tenant"`
IdentityProvider string `json:"identityProvider"`
} `json:"authentication"`
Media string `json:"media"`
GraphAudience string `json:"graphAudience"`
Graph string `json:"graph"`
Name string `json:"name"`
Suffixes struct {
AzureDataLakeStoreFileSystem string `json:"azureDataLakeStoreFileSystem"`
AcrLoginServer string `json:"acrLoginServer"`
SqlServerHostname string `json:"sqlServerHostname"`
AzureDataLakeAnalyticsCatalogAndJob string `json:"azureDataLakeAnalyticsCatalogAndJob"`
KeyVaultDns string `json:"keyVaultDns"`
Storage string `json:"storage"`
AzureFrontDoorEndpointSuffix string `json:"azureFrontDoorEndpointSuffix"`
StorageSyncEndpointSuffix string `json:"storageSyncEndpointSuffix"`
MhsmDns string `json:"mhsmDns"`
MysqlServerEndpoint string `json:"mysqlServerEndpoint"`
PostgresqlServerEndpoint string `json:"postgresqlServerEndpoint"`
MariadbServerEndpoint string `json:"mariadbServerEndpoint"`
SynapseAnalytics string `json:"synapseAnalytics"`
AttestationEndpoint string `json:"attestationEndpoint"`
} `json:"suffixes"`
Batch string `json:"batch"`
ResourceManager string `json:"resourceManager"`
VmImageAliasDoc string `json:"vmImageAliasDoc"`
ActiveDirectoryDataLake string `json:"activeDirectoryDataLake"`
SqlManagement string `json:"sqlManagement"`
MicrosoftGraphResourceId string `json:"microsoftGraphResourceId"`
AppInsightsResourceId string `json:"appInsightsResourceId"`
AppInsightsTelemetryChannelResourceId string `json:"appInsightsTelemetryChannelResourceId"`
AttestationResourceId string `json:"attestationResourceId"`
SynapseAnalyticsResourceId string `json:"synapseAnalyticsResourceId"`
LogAnalyticsResourceId string `json:"logAnalyticsResourceId"`
OssrDbmsResourceId string `json:"ossrDbmsResourceId"`
Gallery string `json:"gallery"`
}