forked from jvano/VisualARM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoMasterClient.cs
More file actions
346 lines (283 loc) · 12.9 KB
/
GeoMasterClient.cs
File metadata and controls
346 lines (283 loc) · 12.9 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
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
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
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 SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
private string _certThumbprint;
private X509Certificate2 _cert;
#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 certThumbprint,
string apiVersion = "2015-01-01")
{
_certThumbprint = certThumbprint;
_cert = GetCertificate(certThumbprint);
this.GeoMasterEndpoint = geoMasterEndpoint;
this.ApiVersion = apiVersion;
}
#endregion
#region Public properties
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<IEnumerable<Subscription>> GetSubscriptions()
{
List<Subscription> subscriptions = new List<Subscription>();
try
{
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 = _certThumbprint
});
}
}
catch
{
subscriptions.AddRange(new Subscription[]
{
new Subscription()
{
Id = "00000000-0000-0000-0000-000000000000",
DisplayName = "[CSM-Direct]",
TenantId = _certThumbprint
}
});
}
// Make async call happy
await Task.Delay(0);
return subscriptions.OrderBy(sub => sub.DisplayName);
}
public async Task<IEnumerable<Location>> GetLocations(Subscription subscription)
{
List<Location> locations = new List<Location>();
try
{
SubscriptionClient rdfeClient = GetRdfeSubscriptionClient();
var webspaces = rdfeClient.GetWebSpaces(subscription.Id);
foreach (var webspace in webspaces)
{
locations.Add(new Location()
{
Id = webspace.Name,
Name = webspace.GeoRegion,
DisplayName = webspace.GeoRegion
});
}
}
catch
{
// Return a fixed list
locations.AddRange(new Location[]
{
new Location()
{
Id = "USAAnywhere",
Name = "USA Anywhere",
DisplayName = "USA Anywhere"
}
});
}
// Make async call happy
await Task.Delay(0);
return locations.OrderBy(sub => sub.DisplayName);
}
#endregion
#region Private Methods - Tokens
public async Task<string> GetAuthSecret(string tenantId = null)
{
return await Task.FromResult<string>(_certThumbprint);
}
#endregion
#region ARM Helper Methods
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 = null, string body = null, Dictionary<string, string> parameters = null, string armEndpoint = null, string apiVersion = null)
{
string response = await CallAzureResourceManager(method, path, token, body, parameters, armEndpoint, apiVersion);
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)
{
Uri requestUri = CreateAzureResourceManagerUri(path, parameters, armEndpoint, apiVersion);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
request.Method = method;
request.ContentType = "application/json";
request.ClientCertificates.Add(_cert);
if (HttpHeadersProcessor != null)
{
HttpHeadersProcessor.CaptureRequest(request.Host, request.Headers);
}
if (string.IsNullOrWhiteSpace(body))
{
request.ContentLength = 0;
}
else
{
using (Stream requestStream = await request.GetRequestStreamAsync())
{
byte[] content = Encoding.UTF8.GetBytes(body);
requestStream.Write(content, 0, content.Length);
}
}
try
{
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
if (HttpHeadersProcessor != null)
{
HttpHeadersProcessor.CaptureResponse(response.StatusCode, response.Headers);
}
using (Stream receiveStream = response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
return await readStream.ReadToEndAsync();
}
}
}
catch (WebException e)
{
if (e.Response != null)
{
using (Stream receiveStream = e.Response.GetResponseStream())
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
throw new Exception(e.Message + Environment.NewLine + readStream.ReadToEnd());
}
}
}
throw;
}
}
#endregion
#region Private Static Methods - Certs
private static X509Certificate2 GetCertificate(string thumbprint)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
try
{
var results = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
if (results.Count == 0)
{
throw new Exception(string.Format("Failed to load certificate with thumbprint {0}.", thumbprint));
}
return results[0];
}
finally
{
store.Close();
}
}
#endregion
private SubscriptionClient GetRdfeSubscriptionClient()
{
ServiceEndpoint endpoint = GetRdfeServiceEndpoint(contractDescription: ContractDescription.GetContract(typeof(Vano.Tools.Azure.RDFE.Contracts.ISubscriptionService)), addressPostfix: UriElements.Subscriptions);
SubscriptionClient client = new SubscriptionClient(endpoint);
ApplyRdfeClientSettings(client);
return client;
}
private ServiceEndpoint GetRdfeServiceEndpoint(ContractDescription contractDescription, string addressPostfix, bool supportsWebSystem = false)
{
// CSM joaquinvvmssgeo.cloudapp.net:444
// RDFE joaquinvvmssgeo.cloudapp.net:443 GEO
// RDFE joaquinvvmssgeo.cloudapp.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.Certificate;
webHttpBinding.MaxReceivedMessageSize = MaxReceivedMessageSize;
webHttpBinding.ReaderQuotas.MaxStringContentLength = MaxStringContentLength;
// Do not check a validity of the certificate
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
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(new WebHttpBehavior());
return serviceEndpoint;
}
private void ApplyRdfeClientSettings<T>(AdministrationClientBase<T> client)
where T : class
{
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser, StoreName.My, X509FindType.FindByThumbprint, _certThumbprint);
}
}
}