Skip to content
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

(#155) Keep directory structure for signed PowerShell scripts #156

Merged
merged 2 commits into from
May 22, 2024
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
21 changes: 18 additions & 3 deletions Chocolatey.Cake.Recipe/Content/sign-powershell.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Param(
[String]
$OutputFolder,

[Parameter()]
[string]
$RootFolder,

[Parameter()]
[String]
$TimeStampServer,
Expand Down Expand Up @@ -61,21 +65,32 @@ if ($Cert) {
'Cert' = $Cert
}

if (!$RootFolder) {
$RootFolder = Resolve-Path "./"
}

Push-Location $RootFolder

foreach ($Script in $ScriptsToSign) {
$ExistingSig = Get-AuthenticodeSignature -FilePath $Script

if ($ExistingSig.Status -ne 'Valid' -or $ExistingSig.SignerCertificate.Issuer -notmatch 'DigiCert' -or $ExistingSig.SignerCertificate.NotAfter -lt [datetime]::Now) {
$relativePath = (Resolve-Path -Relative -LiteralPath $Script).TrimStart('.', '/', '\')
$destinationPath = Join-Path $OutputFolder $relativePath
$destinationFolder = Split-Path -Parent $destinationPath
$NewSig = Set-AuthenticodeSignature -FilePath $Script @CommonSignParams
Write-Host "Script file '$Script' signed with status: $($NewSig.Status)"

if (!(Test-Path -Path $OutputFolder)) {
$null = New-Item -Path $OutputFolder -Type Directory
if (!(Test-Path -Path $destinationFolder)) {
$null = New-Item -Path $destinationFolder -Type Directory -Force
}
Copy-Item -Path $Script -Destination $OutputFolder
Copy-Item -Path $Script -Destination $destinationPath
} else {
Write-Host "Script file '$Script' does not need signing, current signature is valid."
}
}

Pop-Location
} else {
Write-Warning "Skipping script signing, no currently valid DigiCert issued Authenticode signing certificate matching '$($CertificateSubjectName)' was found."
}
9 changes: 5 additions & 4 deletions Chocolatey.Cake.Recipe/Content/sign.cake
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ BuildParameters.Tasks.SignPowerShellScriptsTask = Task("Sign-PowerShellScripts")
});
}

foreach (var signedFile in GetFiles(BuildParameters.Paths.Directories.SignedFiles + "/**/*"))
{
BuildParameters.BuildProvider.UploadArtifact(signedFile);
}
var files = GetFiles(BuildParameters.Paths.Directories.SignedFiles + "/**/*") - GetFiles(BuildParameters.Paths.Directories.SignedFiles + "/**/*.zip");
var destination = BuildParameters.Paths.Directories.SignedFiles.CombineWithFilePath("SignedFiles.zip");
Zip(BuildParameters.Paths.Directories.SignedFiles, destination, files);

BuildParameters.BuildProvider.UploadArtifact(destination);
}
else
{
Expand Down