-
Notifications
You must be signed in to change notification settings - Fork 311
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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() | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
# 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
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 |
||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||||
|
@@ -210,6 +248,7 @@ steps: | |||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Restart-Service -Name "$serviceName" -Force | ||||||||||||||||||||||||||||
Restart-Service -Name MSSQLSERVER* -Force | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
displayName: 'Restart SQL Server [Win]' | ||||||||||||||||||||||||||||
condition: ${{parameters.condition }} | ||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
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.