-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathConfig.cs
More file actions
282 lines (254 loc) · 11.9 KB
/
Config.cs
File metadata and controls
282 lines (254 loc) · 11.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
using System.Text.Json;
using Microsoft.Data.SqlClient.TestUtilities;
namespace Microsoft.Data.SqlClient.Extensions.Azure.Test;
/// <summary>
/// This class reads configuration information from environment variables and
/// the config.jsonc file for use by our tests.
///
/// Environment variables take precedence over config.jsonc settings. Note that
/// variable names are case-sensitive on non-Windows platforms.
///
/// The following variables are supported:
///
/// ADO_POOL:
/// When defined, indicates that tests are running in an ADO-CI pool.
///
/// SYSTEM_ACCESSTOKEN:
/// The Azure Pipelines $(System.AccessToken) to use for workload identity
/// federation.
///
/// TEST_DEBUG_EMIT:
/// When defined, enables debug output of configuration values.
///
/// TEST_MDS_CONFIG:
/// The path to the config file to use instead of the default. If not
/// supplied, the config file is assumed to be located next to the test
/// assembly and is named config.jsonc.
/// </summary>
internal static class Config
{
#region Config Properties
internal static bool AdoPool { get; } = false;
internal static bool DebugEmit { get; } = false;
internal static bool IntegratedSecuritySupported { get; } = false;
internal static bool ManagedIdentitySupported { get; } = false;
internal static string PasswordConnectionString { get; } = string.Empty;
internal static string ServicePrincipalId { get; } = string.Empty;
internal static string ServicePrincipalSecret { get; } = string.Empty;
internal static string SystemAccessToken { get; } = string.Empty;
internal static bool SystemAssignedManagedIdentitySupported { get; } = false;
internal static string TcpConnectionString { get; } = string.Empty;
internal static string TenantId { get; } = string.Empty;
internal static bool UseManagedSniOnWindows { get; } = false;
internal static string UserManagedIdentityClientId { get; } = string.Empty;
internal static string WorkloadIdentityFederationServiceConnectionId { get; } = string.Empty;
#endregion
#region Conditional Fact/Theory Helpers
internal static bool HasPasswordConnectionString() => !PasswordConnectionString.IsEmpty();
internal static bool HasServicePrincipal() => !ServicePrincipalId.IsEmpty() && !ServicePrincipalSecret.IsEmpty();
internal static bool HasSystemAccessToken() => !SystemAccessToken.IsEmpty();
internal static bool HasTcpConnectionString() => !TcpConnectionString.IsEmpty();
internal static bool HasTenantId() => !TenantId.IsEmpty();
internal static bool HasUserManagedIdentityClientId() => !UserManagedIdentityClientId.IsEmpty();
internal static bool HasWorkloadIdentityFederationServiceConnectionId() => !WorkloadIdentityFederationServiceConnectionId.IsEmpty();
internal static bool IsAzureSqlServer() =>
Utils.IsAzureSqlServer(new SqlConnectionStringBuilder(TcpConnectionString).DataSource);
internal static bool OnAdoPool() => AdoPool;
internal static bool OnLinux() => RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
internal static bool OnMacOS() => RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
internal static bool OnWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
internal static bool OnUnix() => OnLinux() || OnMacOS();
internal static bool SupportsIntegratedSecurity() => IntegratedSecuritySupported;
internal static bool SupportsManagedIdentity() => ManagedIdentitySupported;
internal static bool SupportsSystemAssignedManagedIdentity() => SystemAssignedManagedIdentitySupported;
#endregion
#region Static Construction
/// <summary>
/// Static construction reads configuration settings from the config file
/// and environment variables.
/// </summary>
static Config()
{
// Read from the config.jsonc file. If the TEST_MDS_CONFIG environment
// variable is set, use it. Otherwise, assume the config file is in the
// working directory and named config.jsonc.
string configPath = GetEnvVar("TEST_MDS_CONFIG");
if (configPath.IsEmpty())
{
configPath = "config.jsonc";
}
try
{
using JsonDocument doc =
JsonDocument.Parse(
File.ReadAllText(configPath),
new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true
});
JsonElement root = doc.RootElement;
// See the sample config file for information about these settings:
//
// src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/config.default.jsonc
//
// The sample file is copied to the build output directory as
// config.jsonc by the TestUtilities project file.
//
IntegratedSecuritySupported = GetBool(root, "SupportsIntegratedSecurity");
ManagedIdentitySupported = GetBool(root, "ManagedIdentitySupported");
PasswordConnectionString = GetString(root, "AADPasswordConnectionString");
ServicePrincipalId = GetString(root, "AADServicePrincipalId");
ServicePrincipalSecret = GetString(root, "AADServicePrincipalSecret");
SystemAssignedManagedIdentitySupported =
GetBool(root, "SupportsSystemAssignedManagedIdentity");
TcpConnectionString = GetString(root, "TCPConnectionString");
TenantId = GetString(root, "AzureKeyVaultTenantId");
UseManagedSniOnWindows = GetBool(root, "UseManagedSNIOnWindows");
UserManagedIdentityClientId = GetString(root, "UserManagedIdentityClientId");
WorkloadIdentityFederationServiceConnectionId =
GetString(root, "WorkloadIdentityFederationServiceConnectionId");
}
catch (Exception ex)
{
Console.WriteLine(
$"Config: Failed to read config file={configPath}: {ex}");
throw;
}
// Apply environment variable overrides.
//
// Note that environment variables are case-sensitive on non-Windows
// platforms.
AdoPool = GetEnvFlag("ADO_POOL");
DebugEmit = GetEnvFlag("TEST_DEBUG_EMIT");
SystemAccessToken = GetEnvVar("SYSTEM_ACCESSTOKEN");
// Circumvent pipeline masking of things it considers secrets by base64 encoding them. We
// will emit them on their own line without labels.
string Base64Encode(string s)
{
return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(s));
}
// Emit debug information if requested.
if (DebugEmit)
{
Console.WriteLine("Config:");
Console.WriteLine(
$" AdoPool: {AdoPool}");
Console.WriteLine(
$" DebugEmit: {DebugEmit}");
Console.WriteLine(
$" IntegratedSecuritySupported: {IntegratedSecuritySupported}");
Console.WriteLine(
$" ManagedIdentitySupported: {ManagedIdentitySupported}");
Console.WriteLine(
$" PasswordConnectionString: {PasswordConnectionString}");
Console.WriteLine(
$" {Base64Encode(PasswordConnectionString)}");
Console.WriteLine(
$" ServicePrincipalId: {ServicePrincipalId}");
Console.WriteLine(
$" ServicePrincipalSecret: {ServicePrincipalSecret}");
Console.WriteLine(
$" {Base64Encode(ServicePrincipalSecret)}");
Console.WriteLine(
$" SystemAccessToken: {SystemAccessToken}");
Console.WriteLine(
$" SystemAssignedManagedIdentitySupported: {SystemAssignedManagedIdentitySupported}");
Console.WriteLine(
$" TcpConnectionString: {TcpConnectionString}");
Console.WriteLine(
$" {Base64Encode(TcpConnectionString)}");
Console.WriteLine(
$" TenantId: {TenantId}");
Console.WriteLine(
$" UseManagedSniOnWindows: {UseManagedSniOnWindows}");
Console.WriteLine(
$" UserManagedIdentityClientId: {UserManagedIdentityClientId}");
Console.WriteLine(
$" {Base64Encode(UserManagedIdentityClientId)}");
Console.WriteLine(
" WorkloadIdentityFederationServiceConnectionId: " +
WorkloadIdentityFederationServiceConnectionId);
}
// Apply the SNI flag, if necessary. This must occur before any MDS
// APIs are used.
if (UseManagedSniOnWindows)
{
AppContext.SetSwitch(
"Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows",
true);
}
}
#endregion
#region Private Methods
/// <summary>
/// Get a string property from a JSON element.
/// </summary>
/// <param name="element">The JSON element to read from.</param>
/// <param name="name">The name of the property to read.</param>
/// <returns>The string value of the property, or an empty string if not found or invalid.</returns>
private static string GetString(JsonElement element, string name)
{
if (element.TryGetProperty(name, out var property))
{
try
{
var value = property.GetString();
if (value is not null)
{
return value;
}
}
catch (InvalidOperationException)
{
// Ignore invalid values.
}
}
return string.Empty;
}
/// <summary>
/// Get a boolean property from a JSON element.
/// </summary>
/// <param name="element">The JSON element to read from.</param>
/// <param name="name">The name of the property to read.</param>
/// <returns>The boolean value of the property, or false if not found or invalid.</returns>
private static bool GetBool(JsonElement element, string name)
{
if (element.TryGetProperty(name, out var property))
{
try
{
return property.GetBoolean();
}
catch (InvalidOperationException)
{
// Ignore invalid values.
}
}
return false;
}
/// <summary>
/// Get a boolean flag from an environment variable. The variable's value
/// is not examined; only its presence is checked.
/// </summary>
/// <param name="name">The name of the environment variable.</param>
/// <returns>True if the environment variable is set; false otherwise.</returns>
private static bool GetEnvFlag(string name)
{
return Environment.GetEnvironmentVariable(name) is not null;
}
/// <summary>
/// Get a string value from an environment variable.
/// </summary>
/// <param name="name">The name of the environment variable.</param>
/// <returns>The value of the environment variable, or an empty string if not set.</returns>
private static string GetEnvVar(string name)
{
return Environment.GetEnvironmentVariable(name) ?? string.Empty;
}
#endregion
}