chocolatey / chocolatey-licensed-issues

Issues for Licensed Editions of Chocolatey
19 stars 13 forks source link

Central Management - Service - Error during installation when providing existing certificate: Cannot index into a null array #143

Closed 2Deep2Dive closed 4 years ago

2Deep2Dive commented 4 years ago

Hi Everyone,

There is an error in the code which throws this Cannot index into a null array the issues is produced when starting the installation while providing an existing certificate thumbprint

Here the thumbprint is checked and the value is assigned to the variable, the issue is with the else part, at this point the $existingCertificatesCoun is null since there was no values assigned to it, then a switch is called checking the value of this variable, in this case the switch fails since the value is null

I believe the Else should include the switch for the logic to function properly

If ($pp['CertificateThumbprint']) { $certificateHash = $pp['CertificateThumbprint'] } Else { $existingCertificates = (Get-ChildItem -Path Cert:\LocalMachine\TrustedPeople, Cert:\LocalMachine\My | Where-Object { $_.Subject -like "CN=$certificateDnsName" } | Sort-Object -Property NotBefore | Select-Object ) $existingCertificatesCount = ($existingCertificates | Measure-Object).Count } Switch ($existingCertificatesCount) { 0 { Write-Output "Creating self signed certificate"

# you can only generate a new certificate in 'My'
# necessary to branch based on PowerShell version, since not all parameters are supported in earlier versions
If($PSVersionTable.PSVersion.Major -le 4) {
  $newCert = New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\My -DnsName $certificateDnsName
} Else {
  $newCert = New-SelfSignedCertificate -CertStoreLocation cert:\LocalMachine\My -DnsName $certificateDnsName -KeyUsage KeyEncipherment,DigitalSignature -NotAfter (Get-Date).AddYears(10)
}
# move the certificate to 'TrustedPeople'
$certPath = Get-ChildItem -Path 'Cert:\\LocalMachine\\My' | Where-Object subject -like "*$certificateDnsName"
$null = Move-Item -Path $certPath.PsPath -Destination 'Cert:\\LocalMachine\\TrustedPeople'
$certificateHash = $newCert.Thumbprint

} 1 { Write-Warning "A single self-signed Certificate matching Subject $certificateDnsName was found. Using this to continue installation" $certificateHash = $existingCertificates[0].Thumbprint } default { Write-Warning "Multiple self-signed Certificates were found matching Subject $certificateDnsName. Using the oldest Certificate to continue installation." $certificateHash = $existingCertificates[0].Thumbprint Write-Warning "Using $certificateHash" } }

Cheers, Mohamed

References

robweber commented 4 years ago

I had this some problem, I can confirm that putting the switch statement within the ELSE block as suggested will fix it.

ferventcoder commented 4 years ago

Howdy folks, just moved this over to https://github.com/chocolatey/chocolatey-licensed-issues/issues/143 as it is not a problem with the wiki. Updated the issue name as well. Thanks for finding this.

steviecoaster commented 4 years ago

Internal Issue

ferventcoder commented 4 years ago

This has been fixed for Chocolatey Central Management v0.2.0. Thanks for letting us know about the issue!