| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for license information. |
| | 3 | |
|
| | 4 | | namespace IntegrationTestCommon |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.IO; |
| | 9 | | using System.IO.Compression; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Net; |
| | 12 | | using System.Net.Security; |
| | 13 | | using System.Security.Cryptography.X509Certificates; |
| | 14 | | using System.Threading.Tasks; |
| | 15 | | using Microsoft.Azure.Batch.IntegrationTestCommon.Tests.Helpers; |
| | 16 | | using Microsoft.Azure.Management.Batch; |
| | 17 | | using Microsoft.Azure.Management.Batch.Models; |
| | 18 | | using Microsoft.IdentityModel.Clients.ActiveDirectory; |
| | 19 | | using System.Text; |
| | 20 | | using Microsoft.WindowsAzure.Storage.Blob; |
| | 21 | | using BatchTestCommon; |
| | 22 | | using Microsoft.Rest; |
| | 23 | |
|
| | 24 | | public class IntegrationTestCommon |
| | 25 | | { |
| | 26 | | public static BatchManagementClient OpenBatchManagementClient() |
| | 27 | | { |
| | 28 | | ServiceClientCredentials credentials; |
| | 29 | |
|
| 0 | 30 | | if (IsManagementUrlAValidProductionURL()) |
| | 31 | | { |
| 0 | 32 | | string accessToken = GetAuthenticationTokenAsync("https://management.core.windows.net/").Result; |
| 0 | 33 | | credentials = new TokenCredentials(accessToken); |
| | 34 | | } |
| | 35 | | else |
| | 36 | | { |
| 0 | 37 | | credentials = GetBatchTestTenantCloudCredentials(); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | string managementUrl = TestCommon.Configuration.BatchManagementUrl; |
| | 41 | |
|
| 0 | 42 | | var managementClient = new BatchManagementClient(credentials) |
| 0 | 43 | | { |
| 0 | 44 | | BaseUri = new Uri(managementUrl), |
| 0 | 45 | | SubscriptionId = TestCommon.Configuration.BatchSubscription |
| 0 | 46 | | }; |
| | 47 | |
|
| | 48 | | //Add the extra headers as specified in the configuration |
| 0 | 49 | | foreach (KeyValuePair<string, string> extraHeader in TestCommon.Configuration.BatchTRPExtraHeaders) |
| | 50 | | { |
| 0 | 51 | | managementClient.HttpClient.DefaultRequestHeaders.Add(extraHeader.Key, extraHeader.Value); |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | return managementClient; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | private static bool IsManagementUrlAValidProductionURL() |
| | 58 | | { |
| 0 | 59 | | return new[] { "https://management.azure.com" }.Any(x => TestCommon.Configuration.BatchManagementUrl.StartsW |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public static async Task<string> GetAuthenticationTokenAsync(string resource) |
| | 63 | | { |
| 0 | 64 | | var authContext = new AuthenticationContext(TestCommon.Configuration.BatchAuthorityUrl); |
| | 65 | |
|
| 0 | 66 | | var authResult = await authContext.AcquireTokenAsync(resource, new ClientCredential(TestCommon.Configuration |
| | 67 | |
|
| 0 | 68 | | return authResult.AccessToken; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private static ServiceClientCredentials GetBatchTestTenantCloudCredentials() |
| | 72 | | { |
| 0 | 73 | | X509Certificate2 certificate2 = null; |
| 0 | 74 | | X509Store store = null; |
| | 75 | |
|
| | 76 | | try |
| | 77 | | { |
| 0 | 78 | | store = new X509Store(StoreLocation.CurrentUser); |
| | 79 | |
|
| 0 | 80 | | store.Open(OpenFlags.ReadOnly); |
| | 81 | |
|
| 0 | 82 | | X509Certificate2Collection certificates = store.Certificates; |
| | 83 | |
|
| 0 | 84 | | certificate2 = certificates.Cast<X509Certificate2>().FirstOrDefault( |
| 0 | 85 | | c => String.Equals( |
| 0 | 86 | | c.Thumbprint, |
| 0 | 87 | | TestCommon.Configuration.BatchTRPCertificateThumbprint(), |
| 0 | 88 | | StringComparison.OrdinalIgnoreCase)); |
| | 89 | |
|
| 0 | 90 | | if (certificate2 == null) |
| | 91 | | { |
| 0 | 92 | | throw new InvalidOperationException("The certificate with thumbprint '" + TestCommon.Configuration.B |
| 0 | 93 | | "' is not installed on your machine. Refer to the howto.txt for information about setting up you |
| | 94 | | } |
| 0 | 95 | | } |
| | 96 | | finally |
| | 97 | | { |
| 0 | 98 | | if (store != null) |
| | 99 | | { |
| 0 | 100 | | store.Close(); |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | | #if FullNetFx |
| | 104 | | return new CertificateCredentials(certificate2); |
| | 105 | | # else |
| 0 | 106 | | return new CertificateCredentialsNetCore(certificate2); |
| | 107 | | #endif |
| | 108 | | } |
| | 109 | |
|
| | 110 | | public static async Task EnableAutoStorageAsync() |
| | 111 | | { |
| 0 | 112 | | using (BatchManagementClient managementClient = OpenBatchManagementClient()) |
| | 113 | | { |
| | 114 | | //TODO: Why do we need this...? |
| 0 | 115 | | ServicePointManager.ServerCertificateValidationCallback = |
| 0 | 116 | | delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErro |
| 0 | 117 | | { |
| 0 | 118 | | HttpWebRequest request = sender as HttpWebRequest; |
| 0 | 119 | | if (request != null) |
| 0 | 120 | | { |
| 0 | 121 | | if (request.Address.ToString().Contains(TestCommon.Configuration.BatchManagementUrl)) |
| 0 | 122 | | { |
| 0 | 123 | | return true; |
| 0 | 124 | | } |
| 0 | 125 | | } |
| 0 | 126 | | return sslPolicyErrors == SslPolicyErrors.None; //use the default validation for all other certi |
| 0 | 127 | | }; |
| | 128 | |
|
| | 129 | | //If the account doesn't already have auto storage enabled, enable it |
| 0 | 130 | | BatchAccount batchAccount = await managementClient.BatchAccount.GetAsync(TestCommon.Configuration.BatchA |
| 0 | 131 | | if (batchAccount.AutoStorage == null) |
| | 132 | | { |
| | 133 | | const string classicStorageAccountGroup = "Microsoft.ClassicStorage"; |
| | 134 | | const string storageAccountGroup = "Microsoft.Storage"; |
| 0 | 135 | | string resourceFormatString = $"/subscriptions/{TestCommon.Configuration.BatchSubscription}/resource |
| 0 | 136 | | $"/storageAccounts/{TestCommon.Configuration.StorageAccountName}"; |
| | 137 | |
|
| 0 | 138 | | string classicStorageAccountFullResourceId = string.Format(resourceFormatString, classicStorageAccou |
| 0 | 139 | | string storageAccountFullResourceId = string.Format(resourceFormatString, storageAccountGroup); |
| | 140 | |
|
| 0 | 141 | | var updateParameters = new BatchAccountUpdateParameters() |
| 0 | 142 | | { |
| 0 | 143 | | AutoStorage = new AutoStorageBaseProperties |
| 0 | 144 | | { |
| 0 | 145 | | StorageAccountId = classicStorageAccountFullResourceId |
| 0 | 146 | | } |
| 0 | 147 | | }; |
| | 148 | | try |
| | 149 | | { |
| 0 | 150 | | await managementClient.BatchAccount.UpdateAsync(TestCommon.Configuration.BatchAccountResourceGro |
| 0 | 151 | | } |
| 0 | 152 | | catch (Exception e) when (e.Message.Contains("The specified storage account could not be found")) |
| | 153 | | { |
| | 154 | | //If the storage account could not be found, it might be because we looked in "Classic" -- in th |
| | 155 | | //the exception. |
| 0 | 156 | | } |
| | 157 | |
|
| 0 | 158 | | updateParameters.AutoStorage.StorageAccountId = storageAccountFullResourceId; |
| 0 | 159 | | await managementClient.BatchAccount.UpdateAsync(TestCommon.Configuration.BatchAccountResourceGroup, |
| 0 | 160 | | } |
| 0 | 161 | | } |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | public static async Task UploadTestApplicationAsync(string storageUrl) |
| | 165 | | { |
| 0 | 166 | | CloudBlockBlob blob = new CloudBlockBlob(new Uri(storageUrl)); |
| | 167 | |
|
| | 168 | | const string dummyPackageContentFile = @"TestApplicationPackage.zip"; |
| | 169 | |
|
| 0 | 170 | | using (MemoryStream fakeApplicationPackageZip = new MemoryStream()) |
| | 171 | | { |
| 0 | 172 | | using (ZipArchive zip = new ZipArchive(fakeApplicationPackageZip, ZipArchiveMode.Create, leaveOpen: true |
| | 173 | | { |
| 0 | 174 | | ZipArchiveEntry entry = zip.CreateEntry(dummyPackageContentFile); |
| 0 | 175 | | using (var s = entry.Open()) |
| | 176 | | { |
| 0 | 177 | | byte[] bytes = Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog."); |
| 0 | 178 | | s.Write(bytes, 0, bytes.Length); |
| 0 | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| 0 | 182 | | fakeApplicationPackageZip.Seek(0, SeekOrigin.Begin); |
| | 183 | |
|
| 0 | 184 | | await blob.UploadFromStreamAsync(fakeApplicationPackageZip).ConfigureAwait(false); |
| 0 | 185 | | } |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | public static string GetTemporaryCertificateFilePath(string fileName) |
| | 189 | | { |
| 8 | 190 | | string certificateFolderPath = Path.Combine(Path.GetTempPath(), @"BatchTestCertificates"); |
| | 191 | |
|
| 8 | 192 | | Directory.CreateDirectory(certificateFolderPath); |
| | 193 | |
|
| 8 | 194 | | return Path.Combine(certificateFolderPath, fileName); |
| | 195 | | } |
| | 196 | | } |
| | 197 | | } |