Skip to content

Commit 1cae704

Browse files
committed
(#281) Prevents Creation Of Unrequired Self-Signed Certificate
The Chocolatey-Management-Service package generates a self-signed certificate to use if no thumbprint is specified as a parameter. Regardless of if a certificate was specified, we were installing it without a thumbprint and then setting the certificate later. This resulted in an unused self-signed certificate being generated and stored. This change ensures the package gets the appropriate parameter in order to not generate unneeded certificate(s).
1 parent 15db143 commit 1cae704

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

Start-C4bCcmSetup.ps1

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ param(
1717
[System.Management.Automation.PSCredential]
1818
$DatabaseCredential = (Get-Credential -Username ChocoUser -Message 'Create a credential for the ChocolateyManagement DB user (document this somewhere)'),
1919

20-
#Certificate to use for CCM service
20+
# Certificate to use for CCM service
2121
[Parameter()]
22+
[Alias('Thumbprint')]
2223
[String]
2324
$CertificateThumbprint
2425
)
@@ -116,31 +117,27 @@ process {
116117
$hostName = [System.Net.Dns]::GetHostName()
117118
$domainName = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().DomainName
118119

119-
if(-Not $hostName.endswith($domainName)) {
120+
if (-not $hostName.EndsWith($domainName)) {
120121
$hostName += "." + $domainName
121122
}
122123

123124
Write-Host "Installing Chocolatey Central Management Service"
124-
if($CertificateThumbprint){
125+
$chocoArgs = @('install', 'chocolatey-management-service', "--source='ChocolateyInternal'", '-y', "--package-parameters-sensitive=`"/ConnectionString:'Server=Localhost\SQLEXPRESS;Database=ChocolateyManagement;User ID=$DatabaseUser;Password=$DatabaseUserPw;'`"", '--no-progress')
126+
if ($CertificateThumbprint) {
125127
Write-Verbose "Validating certificate is in LocalMachine\TrustedPeople Store"
126-
if($CertificateThumbprint -notin (Get-ChildItem Cert:\LocalMachine\TrustedPeople | Select-Object -Expand Thumbprint)){
128+
if (-not (Get-Item Cert:\LocalMachine\TrustedPeople\$CertificateThumbprint -EA 0) -and -not (Get-Item Cert:\LocalMachine\My\$CertificateThumbprint -EA 0)) {
127129
Write-Warning "You specified $CertificateThumbprint for use with CCM service, but the certificate is not in the required LocalMachine\TrustedPeople store!"
128130
Write-Warning "Please place certificate with thumbprint: $CertificateThumbprint in the LocalMachine\TrustedPeople store and re-run this step"
129-
throw "Certificate not in correct location....exiting."
130-
}
131-
else {
131+
throw "Certificate not in correct location... exiting."
132+
} elseif ($MyCertificate = Get-Item Cert:\LocalMachine\My\$CertificateThumbprint -EA 0) {
133+
Write-Verbose "Copying certificate from 'Personal' store to 'TrustedPeople'"
134+
Copy-CertToStore $MyCertificate
135+
} else {
132136
Write-Verbose "Certificate has been successfully found in correct store"
133-
$chocoArgs = @('install', 'chocolatey-management-service', '-y', "--package-parameters-sensitive='/ConnectionString:Server=Localhost\SQLEXPRESS;Database=ChocolateyManagement;User Id=$DatabaseUser;Password=$DatabaseUserPw'")
134-
& Invoke-Choco @chocoArgs
135-
136-
Set-CcmCertificate -CertificateThumbprint $CertificateThumbprint
137137
}
138+
$chocoArgs += @("--package-parameters='/CertificateThumbprint=$CertificateThumbprint'")
138139
}
139-
140-
else {
141-
$chocoArgs = @('install', 'chocolatey-management-service', "--source='ChocolateyInternal'", '-y', "--package-parameters-sensitive=`"/ConnectionString:'Server=Localhost\SQLEXPRESS;Database=ChocolateyManagement;User ID=$DatabaseUser;Password=$DatabaseUserPw;'`"", '--no-progress')
142-
& Invoke-Choco @chocoArgs
143-
}
140+
& Invoke-Choco @chocoArgs
144141

145142
Write-Host "Installing Chocolatey Central Management Website"
146143
$chocoArgs = @('install', 'chocolatey-management-web', "--source='ChocolateyInternal'", '-y', "--package-parameters-sensitive=""'/ConnectionString:Server=Localhost\SQLEXPRESS;Database=ChocolateyManagement;User ID=$DatabaseUser;Password=$DatabaseUserPw;'""", '--no-progress')

Start-C4bSetup.ps1

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,14 @@ try {
150150

151151
# Kick off unattended running of remaining setup scripts.
152152
if ($Unattend) {
153+
$Certificate = @{}
154+
if ($Thumbprint) {$Certificate.Thumbprint = $Thumbprint}
155+
153156
Set-Location "$env:SystemDrive\choco-setup\files"
154157
.\Start-C4BNexusSetup.ps1
155-
.\Start-C4bCcmSetup.ps1 -DatabaseCredential $DatabaseCredential
158+
.\Start-C4bCcmSetup.ps1 @Certificate -DatabaseCredential $DatabaseCredential
156159
.\Start-C4bJenkinsSetup.ps1
157-
if ($Thumbprint) {
158-
.\Set-SslSecurity.ps1 -Thumbprint $Thumbprint
159-
}
160-
else {
161-
.\Set-SslSecurity.ps1
162-
}
160+
.\Set-SslSecurity.ps1 @Certificate
163161
}
164162
} finally {
165163
$ErrorActionPreference = $DefaultEap

0 commit comments

Comments
 (0)