Skip to content

Tests | Fix RemoteCertificateNameMismatchErrorTest (ActiveIssue 31754) #3059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,44 @@ steps:
displayName: 'Setup SQL Alias [Win]'
condition: ${{parameters.condition }}

- powershell: |
# Create Certificate
$computerDnsName = [System.Net.Dns]::Resolve($null).HostName
$certificate = New-SelfSignedCertificate -DnsName $computerDnsName,localhost -CertStoreLocation cert:\LocalMachine\My -FriendlyName test99 -KeySpec KeyExchange

# Get path to Private key (used later)
$keyPath = $certificate.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
$machineKeyPath = "$env:ProgramData\Microsoft\Crypto\RSA\MachineKeys\$keyPath"

# Add certificate to trusted roots
$store = new-object System.Security.Cryptography.X509Certificates.X509Store(
[System.Security.Cryptography.X509Certificates.StoreName]::Root,
"localmachine"
)

$store.open("MaxAllowed")
$store.add($certificate)
$store.close()
Comment on lines +213 to +215
Copy link
Contributor

Choose a reason for hiding this comment

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

We're going to add a new certificate to the Computer and to the Trusted Root Certificate Authorities certificate stores on every CI run. Depending how often the test agents are reprovisioned, we might bump into KB2801679.

Another approach might be to iterate over the list of instances, then lazily generate a computer certificate if one is missing, then add the certificate to the Root store, then to add permissions over its machine key path.


# Get SQL Server instances and add the Certificate
$instances = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL'
foreach ($instance in $instances){
$instance | ForEach-Object {
$_.PSObject.Properties | Where-Object { $_.Name -notmatch '^PS.*' } | ForEach-Object {
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$($_.Value)\MSSQLServer\SuperSocketNetLib" -Name Certificate -Value $certificate.Thumbprint.ToLower()

# Grant read access to Private Key for SQL Service Account
if ($($_.Name) -eq "MSSQLSERVER") {
icacls $machineKeyPath /grant "NT Service\MSSQLSERVER:R"
} else {
icacls $machineKeyPath /grant "NT Service\MSSQL`$$($_.Name):R"
}
Comment on lines +222 to +229
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$($_.Value)\MSSQLServer\SuperSocketNetLib" -Name Certificate -Value $certificate.Thumbprint.ToLower()
# Grant read access to Private Key for SQL Service Account
if ($($_.Name) -eq "MSSQLSERVER") {
icacls $machineKeyPath /grant "NT Service\MSSQLSERVER:R"
} else {
icacls $machineKeyPath /grant "NT Service\MSSQL`$$($_.Name):R"
}
$serviceAccount = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\$($_.Name)" -Name ObjectName).ObjectName
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$($_.Value)\MSSQLServer\SuperSocketNetLib" -Name Certificate -Value $certificate.Thumbprint.ToLower()
# Grant read access to Private Key for SQL Service Account
icacls $machineKeyPath /grant "$serviceAccount:R"

This will handle the case where SQL Server runs under a non-default account (CONTOSO\srv-mssql, etc.)

It'd be a little more PowerShell-centric to use the built-in Get-Acl and Set-Acl rather than shelling out to icacls, but that doesn't do anything different - the approach is fine.

}
}
}
displayName: 'Add SQL Certificate [Win]'
condition: ${{parameters.condition }}

- powershell: |
# You need to restart SQL Server for the change to persist
# -Force takes care of any dependent services, like SQL Agent.
Expand All @@ -210,6 +248,7 @@ steps:
}

Restart-Service -Name "$serviceName" -Force
Restart-Service -Name MSSQLSERVER* -Force

displayName: 'Restart SQL Server [Win]'
condition: ${{parameters.condition }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public void OpeningConnectionWitHNICTest()
}
}

[ActiveIssue("31754")]
[ConditionalFact(nameof(AreConnStringsSetup), nameof(UseManagedSNIOnWindows), nameof(IsNotAzureServer), nameof(IsLocalHost))]
[PlatformSpecific(TestPlatforms.Windows)]
public void RemoteCertificateNameMismatchErrorTest()
Expand All @@ -175,6 +174,7 @@ public void RemoteCertificateNameMismatchErrorTest()
{
DataSource = GetLocalIpAddress(),
Encrypt = SqlConnectionEncryptOption.Mandatory,
TrustServerCertificate = false,
HostNameInCertificate = "BadHostName"
};
using SqlConnection connection = new(builder.ConnectionString);
Expand All @@ -183,7 +183,6 @@ public void RemoteCertificateNameMismatchErrorTest()
Assert.Equal(20, exception.Class);
Assert.IsType<AuthenticationException>(exception.InnerException);
Assert.StartsWith("Certificate name mismatch. The provided 'DataSource' or 'HostNameInCertificate' does not match the name in the certificate.", exception.InnerException.Message);
Console.WriteLine(exception.Message);
}

private static void CreateValidCertificate(string script)
Expand Down
Loading