-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathGeoMasterClient.cs
More file actions
292 lines (227 loc) · 12.1 KB
/
GeoMasterClient.cs
File metadata and controls
292 lines (227 loc) · 12.1 KB
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
using Azure.Core;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Vano.Tools.Azure.Model;
using Vano.Tools.Azure.RDFE;
namespace Vano.Tools.Azure
{
public class GeoMasterClient : IAzureClient
{
#region Private members
private const string WebSystemName = "WebSites";
private const long MaxReceivedMessageSize = 10 * 1024 * 1024; // 10 MB
private const int MaxStringContentLength = 1024 * 1024; // 1 MB
private HttpClient _client;
#endregion
#region Constants
/// <summary>
/// Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.
/// </summary>
public static readonly Regex StorageNameValidation = new Regex(@"^[a-z0-9]{3,24}$", RegexOptions.Compiled);
/// <summary>
/// Resource group name can only include alphanumeric characters, periods, underscores, hyphens and parenthesis and cannot end in a period. Length (1,64).
/// </summary>
public static readonly Regex ResourceGroupValidation = new Regex(@"^[-_a-z0-9()\.]{0,63}[-_a-z0-9()]$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
#endregion
#region Constructors
public GeoMasterClient(
string geoMasterEndpoint,
string apiVersion = "2024-11-01")
{
this.GeoMasterEndpoint = geoMasterEndpoint;
this.ApiVersion = apiVersion;
_client = CreateHttpClient();
}
#endregion
#region Public properties
public AzureMetadata Metadata
{
get
{
// not used as we use CSM-Direct but to keep the interface happy.
return new AzureMetadata()
{
PortalEndpoint = "https://portal.azure.com",
Audiences = new [] { this.GeoMasterEndpoint },
GraphEndpoint = "https://graph.windows.net",
LoginEndpoint = "https://login.microsoftonline.com",
};
}
}
public string ResouceManagerEndpoint { get { return this.GeoMasterEndpoint; } }
public string GeoMasterEndpoint { get; private set; }
public string ApiVersion { get; private set; }
public HttpHeadersProcessor HttpHeadersProcessor { get; set; }
#endregion
#region Public Methods - ARM Operations
public async Task Initialize()
{
await GetAuthSecret();
}
public async Task<IEnumerable<Subscription>> GetSubscriptions(CancellationToken cancellationToken = new CancellationToken())
{
List<Subscription> subscriptions = new List<Subscription>();
SubscriptionClient rdfeClient = GetRdfeSubscriptionClient();
var rdfeSubscriptions = rdfeClient.GetSubscriptions(marker: "", recordCount: 100, ownerUserName: "");
foreach(var rdfeSubscription in rdfeSubscriptions)
{
subscriptions.Add(new Subscription()
{
Id = rdfeSubscription.Name,
DisplayName = string.IsNullOrWhiteSpace(rdfeSubscription.Description) ? "[CSM-Direct]" : rdfeSubscription.Description,
TenantId = "CsmDirect"
});
}
// Make async call happy
return await Task.FromResult(subscriptions);
}
public async Task<IEnumerable<Location>> GetLocations(Subscription subscription, CancellationToken cancellationToken = new CancellationToken())
{
List<Location> locations = new List<Location>();
var tenantToken = await GetAuthSecret(subscription.TenantId);
JObject response = await CallAzureResourceManagerAsJObject("GET", string.Format(@"/subscriptions/{0}/providers/Microsoft.Web/geoRegions", subscription.Id), tenantToken, cancellationToken: cancellationToken);
foreach (var region in response["value"])
{
locations.Add(new Location()
{
Id = region["id"]?.ToString(),
Name = region["name"]?.ToString(),
DisplayName = region["properties"]?["displayName"]?.ToString()
});
}
return locations.OrderBy(sub => sub.DisplayName);
}
#endregion
#region Private Methods - Tokens
public async Task<string> GetAuthSecret(string tenantId = null)
{
AccessToken access = await DefaultAzureCredentialHelper.GetUserTokenAsync(UserCredentialExtensions.UserAADAuthParameter.AuthorityHost, UserCredentialExtensions.UserAADAuthParameter.TenantId, UserCredentialExtensions.UserAADAuthParameter.Scope);
return access.Token;
}
#endregion
#region Private Methods - ARM Helper Methods
private HttpClient CreateHttpClient()
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
if (!client.DefaultRequestHeaders.Contains("User-Agent"))
{
client.DefaultRequestHeaders.Add("User-Agent", $"VisualARM/{Assembly.GetExecutingAssembly().GetName().Version}");
}
client.Timeout = TimeSpan.FromSeconds(30);
return client;
}
private Uri CreateAzureResourceManagerUri(string path, Dictionary<string, string> parameters = null, string endpoint = null, string apiVersion = null)
{
string geoEndpoint = string.IsNullOrEmpty(endpoint) ? this.GeoMasterEndpoint : endpoint;
if (path.Contains("api-version="))
{
return new Uri(string.Format("https://{0}{1}{2}",
geoEndpoint,
path,
parameters != null ?
string.Concat("&", string.Join("&", parameters.Select(p => string.Concat(p.Key, "=", p.Value)))) :
string.Empty)
.Replace(" ", "%20"));
}
return new Uri(string.Format("https://{0}{1}?api-version={2}{3}",
geoEndpoint,
path,
apiVersion ?? this.ApiVersion,
parameters != null ?
string.Concat("&", string.Join("&", parameters.Select(p => string.Concat(p.Key, "=", p.Value)))) :
string.Empty)
.Replace(" ", "%20"));
}
private async Task<JObject> CallAzureResourceManagerAsJObject(string method, string path, string token, string body = null, Dictionary<string, string> parameters = null, string armEndpoint = null, string apiVersion = null, bool displaySecrets = false, CancellationToken cancellationToken = new CancellationToken())
{
string response = await CallAzureResourceManager(method, path, token, body, parameters, armEndpoint, apiVersion, displaySecrets, cancellationToken);
if (!string.IsNullOrWhiteSpace(response))
{
return JObject.Parse(response);
}
return new JObject();
}
public async Task<string> CallAzureResourceManager(string method, string path, string token, string body = null, Dictionary<string, string> parameters = null, string armEndpoint = null, string apiVersion = null, bool displaySecrets = false, CancellationToken cancellationToken = new CancellationToken())
{
Uri requestUri = CreateAzureResourceManagerUri(path, parameters, armEndpoint, apiVersion);
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod(method), requestUri);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
// Simulate the CSM headers that are added by ARM.
// http header "x-ms-client-authorization-source" to "legacy" if the caller is admin/co-admin and owner via RBAC.
request.Headers.Add("x-ms-client-authorization-source", "legacy");
if (HttpHeadersProcessor != null)
{
HttpHeadersProcessor.CaptureHttpHeadersFromRequest(requestUri.Host, _client.DefaultRequestHeaders, displaySecrets);
HttpHeadersProcessor.CaptureHttpHeadersFromRequest(requestUri.Host, request.Headers, displaySecrets);
}
if (!string.IsNullOrWhiteSpace(body))
{
request.Content = new StringContent(body, Encoding.UTF8, "application/json");
}
using (HttpResponseMessage response = await _client.SendAsync(request, cancellationToken))
{
if (HttpHeadersProcessor != null)
{
HttpHeadersProcessor.CaptureHttpHeadersFromResponse(response.StatusCode, response.Headers, displaySecrets);
}
string output = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new AzureClientException(response.StatusCode, output);
}
return output;
}
}
#endregion
#region Private Methods - RDFE Helper Methods
private SubscriptionClient GetRdfeSubscriptionClient()
{
ServiceEndpoint endpoint = GetRdfeServiceEndpoint(contractDescription: ContractDescription.GetContract(typeof(Vano.Tools.Azure.RDFE.Contracts.ISubscriptionService)), addressPostfix: UriElements.Subscriptions);
SubscriptionClient client = new SubscriptionClient(endpoint);
return client;
}
private ServiceEndpoint GetRdfeServiceEndpoint(ContractDescription contractDescription, string addressPostfix, bool supportsWebSystem = false)
{
// CSM geomaster.joaquinvmss1.antares-test.windows-int.net:444
// RDFE geomaster.joaquinvmss1.antares-test.windows-int.net:443 GEO
// RDFE geomaster.joaquinvmss1.antares-test.windows-int.net:454 STAMP
Uri csmEndpoint = new Uri("https://" + this.GeoMasterEndpoint);
UriBuilder rdfeEndpoint = new UriBuilder("https", csmEndpoint.Host, portNumber: 443);
string endpointAddress = rdfeEndpoint.ToString();
if (!endpointAddress.EndsWith("/"))
{
endpointAddress += "/";
}
if (addressPostfix.StartsWith("/"))
{
addressPostfix = addressPostfix.Substring(1);
}
WebHttpBinding webHttpBinding = new WebHttpBinding();
webHttpBinding.UseDefaultWebProxy = true;
webHttpBinding.Security.Mode = WebHttpSecurityMode.Transport;
webHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
webHttpBinding.MaxReceivedMessageSize = MaxReceivedMessageSize;
webHttpBinding.ReaderQuotas.MaxStringContentLength = MaxStringContentLength;
if (supportsWebSystem && !String.IsNullOrEmpty(WebSystemName))
{
addressPostfix = String.Format("{0}/{1}/{2}", UriElements.WebSystemsRoot, WebSystemName, addressPostfix);
}
ServiceEndpoint serviceEndpoint = new ServiceEndpoint(contractDescription, webHttpBinding, new EndpointAddress(endpointAddress + addressPostfix));
serviceEndpoint.Behaviors.Add(UserCredentialExtensions.GetWebHttpBehavior());
return serviceEndpoint;
}
#endregion
}
}