Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ MigrationBackup/

# Config Json file
**/config.json
**/config.jsonc

# Generated Milestone PR metadata files
.milestone-prs/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,63 @@
using System.IO;
using Newtonsoft.Json;

#nullable enable

namespace Microsoft.Data.SqlClient.TestUtilities
{
public class Config
{
public string TCPConnectionString = null;
public string NPConnectionString = null;
public string TCPConnectionStringHGSVBS = null;
public string TCPConnectionStringNoneVBS = null;
public string TCPConnectionStringAASSGX = null;
public string AADAuthorityURL = null;
public string AADPasswordConnectionString = null;
public string AADServicePrincipalId = null;
public string AADServicePrincipalSecret = null;
public string AzureKeyVaultURL = null;
public string AzureKeyVaultTenantId = null;
public string LocalDbAppName = null;
public string LocalDbSharedInstanceName = null;
public string? TCPConnectionString = null;
public string? NPConnectionString = null;
public string? TCPConnectionStringHGSVBS = null;
public string? TCPConnectionStringNoneVBS = null;
public string? TCPConnectionStringAASSGX = null;
public bool EnclaveEnabled = false;
public bool TracingEnabled = false;
public string? AADAuthorityURL = null;
public string? AADPasswordConnectionString = null;
public string? AADServicePrincipalId = null;
public string? AADServicePrincipalSecret = null;
public string? AzureKeyVaultURL = null;
public string? AzureKeyVaultTenantId = null;
public bool SupportsIntegratedSecurity = false;
public bool ManagedIdentitySupported = true;
public string FileStreamDirectory = null;
public string? LocalDbAppName = null;
public string? LocalDbSharedInstanceName = null;
public string? FileStreamDirectory = null;
public bool UseManagedSNIOnWindows = false;
public string DNSCachingConnString = null;
public string DNSCachingServerCR = null; // this is for the control ring
public string DNSCachingServerTR = null; // this is for the tenant ring
public bool IsAzureSynapse = false; // True for Azure Data Warehouse/Synapse
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was removed as it is not used.

public string? DNSCachingConnString = null;
public string? DNSCachingServerCR = null; // this is for the control ring
public string? DNSCachingServerTR = null; // this is for the tenant ring
public bool IsDNSCachingSupportedCR = false; // this is for the control ring
public bool IsDNSCachingSupportedTR = false; // this is for the tenant ring
public string EnclaveAzureDatabaseConnString = null;
public string UserManagedIdentityClientId = null;
public string PowerShellPath = null;
public string KerberosDomainPassword = null;
public string KerberosDomainUser = null;
public string? EnclaveAzureDatabaseConnString = null;
public bool ManagedIdentitySupported = true;
public string? UserManagedIdentityClientId = null;
public string? PowerShellPath = null;
public string? AliasName = null;
public string? KerberosDomainPassword = null;
public string? KerberosDomainUser = null;
public bool IsManagedInstance = false;
public string AliasName = null;

public static Config Load(string configPath = @"config.json")
public static Config Load(string configPath)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added as an escape hatch for preserving the behavior of ExtUtilities project. I have plans to remove the ExtUtilities project, so this method will hopefully go away someday.

{
// Allow an override of the config path via an environment variable.
configPath = Environment.GetEnvironmentVariable("TEST_MDS_CONFIG") ?? configPath;
Config config = LoadInternal(Environment.GetEnvironmentVariable("TEST_MDS_CONFIG")) ??
LoadInternal(configPath) ??
throw new FileNotFoundException("Could not find test configuration file.");

Comment thread
benrr101 marked this conversation as resolved.
Config config;
try
{
using (StreamReader r = new StreamReader(configPath))
{
config = JsonConvert.DeserializeObject<Config>(r.ReadToEnd())
?? throw new InvalidOperationException($"Failed to deserialize config from '{configPath}'.");
}
}
catch
{
throw;
}
// Allow environment variables to override individual config values.
SetFromEnv("MDS_TCPConnectionString", ref config.TCPConnectionString);

static void SetFromEnv(string envVar, ref string configValue)
{
string envValue = Environment.GetEnvironmentVariable(envVar);
if (!string.IsNullOrEmpty(envValue))
{
configValue = envValue;
}
}
return config;
}

public static Config Load()
{
// Load config from environment variable first, jsonc file second, json file last.
Config config = LoadInternal(Environment.GetEnvironmentVariable("TEST_MDS_CONFIG")) ??
LoadInternal("config.jsonc") ??
LoadInternal("config.json") ??
throw new FileNotFoundException("Could not find test configuration file.");

// Allow environment variables to override individual config values.
SetFromEnv("MDS_TCPConnectionString", ref config.TCPConnectionString);
Expand All @@ -82,5 +75,34 @@ public static void UpdateConfig(Config updatedConfig, string configPath = @"conf
string config = JsonConvert.SerializeObject(updatedConfig);
File.WriteAllText(configPath, config);
}

private static Config? LoadInternal(string? configPath)
{
if (configPath is null)
{
return null;
}

Comment thread
benrr101 marked this conversation as resolved.
try
{
using StreamReader sr = new StreamReader(configPath);
return JsonConvert.DeserializeObject<Config>(sr.ReadToEnd()) ??
Comment thread
benrr101 marked this conversation as resolved.
Outdated
throw new InvalidOperationException($"Failed to deserialize config from '{configPath}'");
}
catch (FileNotFoundException)
{
// File did not exist at the path given. We will try a different location.
Comment thread
benrr101 marked this conversation as resolved.
Comment thread
benrr101 marked this conversation as resolved.
Comment thread
benrr101 marked this conversation as resolved.
return null;
}
}

private static void SetFromEnv(string envVar, ref string? configValue)
{
string? envValue = Environment.GetEnvironmentVariable(envVar);
if (!string.IsNullOrEmpty(envValue))
{
configValue = envValue;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<Target Name="CopyConfig" BeforeTargets="Compile">
<Copy SourceFiles="config.default.json" DestinationFiles="config.json" Condition="!Exists('config.json')" />
<Copy SourceFiles="config.default.jsonc" DestinationFiles="config.jsonc" Condition="!Exists('config.jsonc')" />
</Target>
<ItemGroup>
<None Update="config.json">
<None Update="config.jsonc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Comment thread
benrr101 marked this conversation as resolved.
Outdated
<PackageReference Include="Newtonsoft.Json" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
"SupportsIntegratedSecurity": true,
"LocalDbAppName": "",
"LocalDbSharedInstanceName": "",
"SupportsFileStream": false,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field did not exist in the Config class.

"FileStreamDirectory": "",
"UseManagedSNIOnWindows": false,
"DNSCachingConnString": "",
"DNSCachingServerCR": "",
"DNSCachingServerTR": "",
"IsDNSCachingSupportedCR": false,
"IsDNSCachingSupportedTR": false,
"IsAzureSynapse": false,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field existed in the Config class, but was removed since it's unused.

"EnclaveAzureDatabaseConnString": "",
"ManagedIdentitySupported": true,
"UserManagedIdentityClientId": "",
"PowerShellPath": "",
"AliasName": "",
"WorkloadIdentityFederationServiceConnectionId": ""
"WorkloadIdentityFederationServiceConnectionId": "",
"KerberosDomainPassword": "",
"KerberosDomainuser": "",
"IsManagedInstance": false
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These fields exist in Config class, but did not in the json file.

}
Loading