Skip to content

Commit 7663524

Browse files
authored
Tests | Fix RemoteCertificateNameMismatchErrorTest (ActiveIssue 31754) (#3059)
* Install SQL Certificate & Trust It Do not automatically trust the Certificate in the Test * Remove Console output * Restart default isntance as well if installed
1 parent 0eec7e2 commit 7663524

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

eng/pipelines/common/templates/steps/configure-sql-server-win-step.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,44 @@ steps:
191191
displayName: 'Setup SQL Alias [Win]'
192192
condition: ${{parameters.condition }}
193193

194+
- powershell: |
195+
# Create Certificate
196+
$computerDnsName = [System.Net.Dns]::Resolve($null).HostName
197+
$certificate = New-SelfSignedCertificate -DnsName $computerDnsName,localhost -CertStoreLocation cert:\LocalMachine\My -FriendlyName test99 -KeySpec KeyExchange
198+
199+
# Get path to Private key (used later)
200+
$keyPath = $certificate.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
201+
$machineKeyPath = "$env:ProgramData\Microsoft\Crypto\RSA\MachineKeys\$keyPath"
202+
203+
# Add certificate to trusted roots
204+
$store = new-object System.Security.Cryptography.X509Certificates.X509Store(
205+
[System.Security.Cryptography.X509Certificates.StoreName]::Root,
206+
"localmachine"
207+
)
208+
209+
$store.open("MaxAllowed")
210+
$store.add($certificate)
211+
$store.close()
212+
213+
# Get SQL Server instances and add the Certificate
214+
$instances = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL'
215+
foreach ($instance in $instances){
216+
$instance | ForEach-Object {
217+
$_.PSObject.Properties | Where-Object { $_.Name -notmatch '^PS.*' } | ForEach-Object {
218+
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$($_.Value)\MSSQLServer\SuperSocketNetLib" -Name Certificate -Value $certificate.Thumbprint.ToLower()
219+
220+
# Grant read access to Private Key for SQL Service Account
221+
if ($($_.Name) -eq "MSSQLSERVER") {
222+
icacls $machineKeyPath /grant "NT Service\MSSQLSERVER:R"
223+
} else {
224+
icacls $machineKeyPath /grant "NT Service\MSSQL`$$($_.Name):R"
225+
}
226+
}
227+
}
228+
}
229+
displayName: 'Add SQL Certificate [Win]'
230+
condition: ${{parameters.condition }}
231+
194232
- powershell: |
195233
# You need to restart SQL Server for the change to persist
196234
# -Force takes care of any dependent services, like SQL Agent.
@@ -206,6 +244,7 @@ steps:
206244
}
207245
208246
Restart-Service -Name "$serviceName" -Force
247+
Restart-Service -Name MSSQLSERVER* -Force
209248
210249
displayName: 'Restart SQL Server [Win]'
211250
condition: ${{parameters.condition }}

src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ConnectionTestWithSSLCert/CertificateTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ public void OpeningConnectionWitHNICTest()
166166
}
167167
}
168168

169-
[ActiveIssue("31754")]
170169
[ConditionalFact(nameof(AreConnStringsSetup), nameof(UseManagedSNIOnWindows), nameof(IsNotAzureServer), nameof(IsLocalHost))]
171170
[PlatformSpecific(TestPlatforms.Windows)]
172171
public void RemoteCertificateNameMismatchErrorTest()
@@ -175,6 +174,7 @@ public void RemoteCertificateNameMismatchErrorTest()
175174
{
176175
DataSource = GetLocalIpAddress(),
177176
Encrypt = SqlConnectionEncryptOption.Mandatory,
177+
TrustServerCertificate = false,
178178
HostNameInCertificate = "BadHostName"
179179
};
180180
using SqlConnection connection = new(builder.ConnectionString);
@@ -183,7 +183,6 @@ public void RemoteCertificateNameMismatchErrorTest()
183183
Assert.Equal(20, exception.Class);
184184
Assert.IsType<AuthenticationException>(exception.InnerException);
185185
Assert.StartsWith("Certificate name mismatch. The provided 'DataSource' or 'HostNameInCertificate' does not match the name in the certificate.", exception.InnerException.Message);
186-
Console.WriteLine(exception.Message);
187186
}
188187

189188
private static void CreateValidCertificate(string script)

0 commit comments

Comments
 (0)