|   |  | 1 |  | // Copyright (c) Microsoft Corporation. All rights reserved. | 
|   |  | 2 |  | // Licensed under the MIT License. | 
|   |  | 3 |  |  | 
|   |  | 4 |  | using System; | 
|   |  | 5 |  | using System.Collections.Generic; | 
|   |  | 6 |  | using System.Security.Cryptography; | 
|   |  | 7 |  | using System.IO; | 
|   |  | 8 |  | using System.Reflection; | 
|   |  | 9 |  | using System.Text.Json; | 
|   |  | 10 |  | using System.Threading; | 
|   |  | 11 |  | using System.Threading.Tasks; | 
|   |  | 12 |  | using Azure.Identity; | 
|   |  | 13 |  |  | 
|   |  | 14 |  | namespace Azure.Core.TestFramework | 
|   |  | 15 |  | { | 
|   |  | 16 |  |     /// <summary> | 
|   |  | 17 |  |     ///   Represents the ambient environment in which the test suite is | 
|   |  | 18 |  |     ///   being run. | 
|   |  | 19 |  |     /// </summary> | 
|   |  | 20 |  |     public abstract class TestEnvironment | 
|   |  | 21 |  |     { | 
|   |  | 22 |  |         private static readonly string RepositoryRoot; | 
|   |  | 23 |  |         private readonly string _prefix; | 
|   |  | 24 |  |  | 
|   |  | 25 |  |         private TokenCredential _credential; | 
|   |  | 26 |  |         private TestRecording _recording; | 
|   |  | 27 |  |  | 
|   | 682 | 28 |  |         private readonly Dictionary<string, string> _environmentFile = new Dictionary<string, string>(StringComparer.Ord | 
|   |  | 29 |  |  | 
|   | 682 | 30 |  |         protected TestEnvironment(string serviceName) | 
|   |  | 31 |  |         { | 
|   | 682 | 32 |  |             _prefix = serviceName.ToUpperInvariant() + "_"; | 
|   | 682 | 33 |  |             if (RepositoryRoot == null) | 
|   |  | 34 |  |             { | 
|   | 0 | 35 |  |                 throw new InvalidOperationException("Unexpected error, repository root not found"); | 
|   |  | 36 |  |             } | 
|   |  | 37 |  |  | 
|   | 682 | 38 |  |             var sdkDirectory = Path.Combine(RepositoryRoot, "sdk", serviceName); | 
|   | 682 | 39 |  |             if (!Directory.Exists(sdkDirectory)) | 
|   |  | 40 |  |             { | 
|   | 0 | 41 |  |                 throw new InvalidOperationException($"SDK directory {sdkDirectory} not found"); | 
|   |  | 42 |  |             } | 
|   |  | 43 |  |  | 
|   | 682 | 44 |  |             var testEnvironmentFile = Path.Combine(RepositoryRoot, "sdk", serviceName, "test-resources.json.env"); | 
|   | 682 | 45 |  |             if (File.Exists(testEnvironmentFile)) | 
|   |  | 46 |  |             { | 
|   | 0 | 47 |  |                 var json = JsonDocument.Parse( | 
|   | 0 | 48 |  |                     ProtectedData.Unprotect(File.ReadAllBytes(testEnvironmentFile), null, DataProtectionScope.CurrentUse | 
|   | 0 | 49 |  |                 ); | 
|   |  | 50 |  |  | 
|   | 0 | 51 |  |                 foreach (var property in json.RootElement.EnumerateObject()) | 
|   |  | 52 |  |                 { | 
|   | 0 | 53 |  |                     _environmentFile[property.Name] = property.Value.GetString(); | 
|   |  | 54 |  |                 } | 
|   |  | 55 |  |             } | 
|   | 682 | 56 |  |         } | 
|   |  | 57 |  |  | 
|   |  | 58 |  |         static TestEnvironment() | 
|   |  | 59 |  |         { | 
|   |  | 60 |  |             // Traverse parent directories until we find an "artifacts" directory | 
|   |  | 61 |  |             // parent of that would become a repo root for test environment resolution purposes | 
|   | 57 | 62 |  |             var directoryInfo = new DirectoryInfo(Assembly.GetExecutingAssembly().Location); | 
|   |  | 63 |  |  | 
|   | 342 | 64 |  |             while (directoryInfo.Name != "artifacts") | 
|   |  | 65 |  |             { | 
|   | 285 | 66 |  |                 if (directoryInfo.Parent == null) | 
|   |  | 67 |  |                 { | 
|   | 0 | 68 |  |                     return; | 
|   |  | 69 |  |                 } | 
|   |  | 70 |  |  | 
|   | 285 | 71 |  |                 directoryInfo = directoryInfo.Parent; | 
|   |  | 72 |  |             } | 
|   |  | 73 |  |  | 
|   | 57 | 74 |  |             RepositoryRoot = directoryInfo?.Parent?.FullName; | 
|   | 57 | 75 |  |         } | 
|   |  | 76 |  |  | 
|   | 12743 | 77 |  |         internal RecordedTestMode? Mode { get; set; } | 
|   |  | 78 |  |  | 
|   |  | 79 |  |         /// <summary> | 
|   |  | 80 |  |         ///   The name of the Azure subscription containing the resource group to be used for Live tests. Recorded. | 
|   |  | 81 |  |         /// </summary> | 
|   | 4240 | 82 |  |         public string SubscriptionId => GetRecordedVariable("SUBSCRIPTION_ID"); | 
|   |  | 83 |  |  | 
|   |  | 84 |  |         /// <summary> | 
|   |  | 85 |  |         ///   The name of the Azure resource group to be used for Live tests. Recorded. | 
|   |  | 86 |  |         /// </summary> | 
|   | 12 | 87 |  |         public string ResourceGroup => GetRecordedVariable("RESOURCE_GROUP"); | 
|   |  | 88 |  |  | 
|   |  | 89 |  |         /// <summary> | 
|   |  | 90 |  |         ///   The location of the Azure resource group to be used for Live tests (e.g. westus2). Recorded. | 
|   |  | 91 |  |         /// </summary> | 
|   | 64 | 92 |  |         public string Location => GetRecordedVariable("LOCATION"); | 
|   |  | 93 |  |  | 
|   |  | 94 |  |         /// <summary> | 
|   |  | 95 |  |         ///   The environment of the Azure resource group to be used for Live tests (e.g. AzureCloud). Recorded. | 
|   |  | 96 |  |         /// </summary> | 
|   | 0 | 97 |  |         public string AzureEnvironment => GetRecordedVariable("ENVIRONMENT"); | 
|   |  | 98 |  |  | 
|   |  | 99 |  |         /// <summary> | 
|   |  | 100 |  |         ///   The name of the Azure Active Directory tenant that holds the service principal to use during Live tests. R | 
|   |  | 101 |  |         /// </summary> | 
|   | 24 | 102 |  |         public string TenantId => GetRecordedVariable("TENANT_ID"); | 
|   |  | 103 |  |  | 
|   |  | 104 |  |         /// <summary> | 
|   |  | 105 |  |         ///   The URL of the Azure Resource Manager to be used for management plane operations. Recorded. | 
|   |  | 106 |  |         /// </summary> | 
|   | 0 | 107 |  |         public string ResourceManagerUrl => GetRecordedOptionalVariable("RESOURCE_MANAGER_URL"); | 
|   |  | 108 |  |  | 
|   |  | 109 |  |         /// <summary> | 
|   |  | 110 |  |         ///   The URL of the Azure Service Management endpoint to be used for management plane authentication. Recorded. | 
|   |  | 111 |  |         /// </summary> | 
|   | 0 | 112 |  |         public string ServiceManagementUrl => GetRecordedOptionalVariable("SERVICE_MANAGEMENT_URL"); | 
|   |  | 113 |  |  | 
|   |  | 114 |  |         /// <summary> | 
|   |  | 115 |  |         ///   The URL of the Azure Authority host to be used for authentication. Recorded. | 
|   |  | 116 |  |         /// </summary> | 
|   | 0 | 117 |  |         public string AuthorityHostUrl => GetRecordedOptionalVariable("AZURE_AUTHORITY_HOST"); | 
|   |  | 118 |  |  | 
|   |  | 119 |  |         /// <summary> | 
|   |  | 120 |  |         ///   The suffix for Azure Storage accounts for the active cloud environment, such as "core.windows.net".  Recor | 
|   |  | 121 |  |         /// </summary> | 
|   | 0 | 122 |  |         public string StorageEndpointSuffix => GetRecordedOptionalVariable("STORAGE_ENDPOINT_SUFFIX"); | 
|   |  | 123 |  |  | 
|   |  | 124 |  |         /// <summary> | 
|   |  | 125 |  |         ///   The client id of the Azure Active Directory service principal to use during Live tests. Recorded. | 
|   |  | 126 |  |         /// </summary> | 
|   | 8 | 127 |  |         public string ClientId => GetRecordedVariable("CLIENT_ID"); | 
|   |  | 128 |  |  | 
|   |  | 129 |  |         /// <summary> | 
|   |  | 130 |  |         ///   The client secret of the Azure Active Directory service principal to use during Live tests. Not recorded. | 
|   |  | 131 |  |         /// </summary> | 
|   | 0 | 132 |  |         public string ClientSecret => GetVariable("CLIENT_SECRET"); | 
|   |  | 133 |  |  | 
|   |  | 134 |  |         public TokenCredential Credential | 
|   |  | 135 |  |         { | 
|   |  | 136 |  |             get | 
|   |  | 137 |  |             { | 
|   | 4836 | 138 |  |                 if (_credential != null) | 
|   |  | 139 |  |                 { | 
|   | 2938 | 140 |  |                     return _credential; | 
|   |  | 141 |  |                 } | 
|   |  | 142 |  |  | 
|   | 1898 | 143 |  |                 if (Mode == RecordedTestMode.Playback) | 
|   |  | 144 |  |                 { | 
|   | 1898 | 145 |  |                     _credential = new TestCredential(); | 
|   |  | 146 |  |                 } | 
|   |  | 147 |  |                 else | 
|   |  | 148 |  |                 { | 
|   | 0 | 149 |  |                     _credential = new ClientSecretCredential( | 
|   | 0 | 150 |  |                         GetVariable("TENANT_ID"), | 
|   | 0 | 151 |  |                         GetVariable("CLIENT_ID"), | 
|   | 0 | 152 |  |                         GetVariable("CLIENT_SECRET") | 
|   | 0 | 153 |  |                     ); | 
|   |  | 154 |  |                 } | 
|   |  | 155 |  |  | 
|   | 1898 | 156 |  |                 return _credential; | 
|   |  | 157 |  |             } | 
|   |  | 158 |  |         } | 
|   |  | 159 |  |  | 
|   |  | 160 |  |         /// <summary> | 
|   |  | 161 |  |         /// Returns and records an environment variable value when running live or recorded value during playback. | 
|   |  | 162 |  |         /// </summary> | 
|   |  | 163 |  |         protected string GetRecordedOptionalVariable(string name) | 
|   |  | 164 |  |         { | 
|   | 10161 | 165 |  |             if (Mode == RecordedTestMode.Playback) | 
|   |  | 166 |  |             { | 
|   | 10147 | 167 |  |                 return GetRecordedValue(name); | 
|   |  | 168 |  |             } | 
|   |  | 169 |  |  | 
|   | 14 | 170 |  |             string value = GetOptionalVariable(name); | 
|   |  | 171 |  |  | 
|   | 14 | 172 |  |             SetRecordedValue(name, value); | 
|   |  | 173 |  |  | 
|   | 2 | 174 |  |             return value; | 
|   |  | 175 |  |         } | 
|   |  | 176 |  |  | 
|   |  | 177 |  |         /// <summary> | 
|   |  | 178 |  |         /// Returns and records an environment variable value when running live or recorded value during playback. | 
|   |  | 179 |  |         /// Throws when variable is not found. | 
|   |  | 180 |  |         /// </summary> | 
|   |  | 181 |  |         protected string GetRecordedVariable(string name) | 
|   |  | 182 |  |         { | 
|   | 9945 | 183 |  |             var value = GetRecordedOptionalVariable(name); | 
|   | 9929 | 184 |  |             EnsureValue(name, value); | 
|   | 9929 | 185 |  |             return value; | 
|   |  | 186 |  |         } | 
|   |  | 187 |  |  | 
|   |  | 188 |  |         /// <summary> | 
|   |  | 189 |  |         /// Returns an environment variable value or null when variable is not found. | 
|   |  | 190 |  |         /// </summary> | 
|   |  | 191 |  |         protected string GetOptionalVariable(string name) | 
|   |  | 192 |  |         { | 
|   | 252 | 193 |  |             var prefixedName = _prefix + name; | 
|   |  | 194 |  |  | 
|   |  | 195 |  |             // Environment variables override the environment file | 
|   | 252 | 196 |  |             var value = Environment.GetEnvironmentVariable(prefixedName) ?? | 
|   | 252 | 197 |  |                         Environment.GetEnvironmentVariable(name); | 
|   |  | 198 |  |  | 
|   | 252 | 199 |  |             if (value == null) | 
|   |  | 200 |  |             { | 
|   | 220 | 201 |  |                 _environmentFile.TryGetValue(prefixedName, out value); | 
|   |  | 202 |  |             } | 
|   |  | 203 |  |  | 
|   | 252 | 204 |  |             if (value == null) | 
|   |  | 205 |  |             { | 
|   | 220 | 206 |  |                 _environmentFile.TryGetValue(name, out value); | 
|   |  | 207 |  |             } | 
|   |  | 208 |  |  | 
|   | 252 | 209 |  |             return value; | 
|   |  | 210 |  |         } | 
|   |  | 211 |  |  | 
|   |  | 212 |  |         /// <summary> | 
|   |  | 213 |  |         /// Returns an environment variable value. | 
|   |  | 214 |  |         /// Throws when variable is not found. | 
|   |  | 215 |  |         /// </summary> | 
|   |  | 216 |  |         protected string GetVariable(string name) | 
|   |  | 217 |  |         { | 
|   | 18 | 218 |  |             var value = GetOptionalVariable(name); | 
|   | 18 | 219 |  |             EnsureValue(name, value); | 
|   | 18 | 220 |  |             return value; | 
|   |  | 221 |  |         } | 
|   |  | 222 |  |  | 
|   |  | 223 |  |         private void EnsureValue(string name, string value) | 
|   |  | 224 |  |         { | 
|   | 9947 | 225 |  |             if (value == null) | 
|   |  | 226 |  |             { | 
|   | 0 | 227 |  |                 var prefixedName = _prefix + name; | 
|   | 0 | 228 |  |                 throw new InvalidOperationException( | 
|   | 0 | 229 |  |                     $"Unable to find environment variable {prefixedName} or {name} required by test." + Environment.NewL | 
|   | 0 | 230 |  |                     "Make sure the test environment was initialized using eng/common/TestResources/New-TestResources.ps1 | 
|   |  | 231 |  |             } | 
|   | 9947 | 232 |  |         } | 
|   |  | 233 |  |  | 
|   |  | 234 |  |         public void SetRecording(TestRecording recording) | 
|   |  | 235 |  |         { | 
|   | 3692 | 236 |  |             _credential = null; | 
|   | 3692 | 237 |  |             _recording = recording; | 
|   | 3692 | 238 |  |         } | 
|   |  | 239 |  |  | 
|   |  | 240 |  |         private string GetRecordedValue(string name) | 
|   |  | 241 |  |         { | 
|   | 10147 | 242 |  |             if (_recording == null) | 
|   |  | 243 |  |             { | 
|   | 4 | 244 |  |                 throw new InvalidOperationException("Recorded value should not be retrieved outside the test method invo | 
|   |  | 245 |  |             } | 
|   |  | 246 |  |  | 
|   | 10143 | 247 |  |             return _recording.GetVariable(name, null); | 
|   |  | 248 |  |         } | 
|   |  | 249 |  |  | 
|   |  | 250 |  |         private void SetRecordedValue(string name, string value) | 
|   |  | 251 |  |         { | 
|   | 14 | 252 |  |             if (!Mode.HasValue) | 
|   |  | 253 |  |             { | 
|   | 2 | 254 |  |                 return; | 
|   |  | 255 |  |             } | 
|   |  | 256 |  |  | 
|   | 12 | 257 |  |             if (_recording == null) | 
|   |  | 258 |  |             { | 
|   | 12 | 259 |  |                 throw new InvalidOperationException("Recorded value should not be set outside the test method invocation | 
|   |  | 260 |  |             } | 
|   |  | 261 |  |  | 
|   | 0 | 262 |  |             _recording?.SetVariable(name, value); | 
|   | 0 | 263 |  |         } | 
|   |  | 264 |  |  | 
|   |  | 265 |  |         private class TestCredential : TokenCredential | 
|   |  | 266 |  |         { | 
|   |  | 267 |  |             public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken c | 
|   |  | 268 |  |             { | 
|   | 1724 | 269 |  |                 return new ValueTask<AccessToken>(GetToken(requestContext, cancellationToken)); | 
|   |  | 270 |  |             } | 
|   |  | 271 |  |  | 
|   |  | 272 |  |             public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken | 
|   |  | 273 |  |             { | 
|   | 3452 | 274 |  |                 return new AccessToken("TEST TOKEN " + string.Join(" ", requestContext.Scopes), DateTimeOffset.MaxValue) | 
|   |  | 275 |  |             } | 
|   |  | 276 |  |         } | 
|   |  | 277 |  |     } | 
|   |  | 278 |  | } |