Skip to content

Commit 35a1554

Browse files
authored
Sign on server (#2531)
1 parent ccc7ff9 commit 35a1554

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

azure-pipelines-publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
trigger: none
2+
3+
variables:
4+
- group: signing
5+
6+
jobs:
7+
- job: build
8+
workspace:
9+
clean: all
10+
pool:
11+
vmImage: windows-2022
12+
timeoutInMinutes: 60
13+
steps:
14+
- task: AzureCLI@2
15+
displayName: Publish
16+
inputs:
17+
azureSubscription: CodeSigner
18+
scriptType: ps
19+
scriptLocation: inlineScript
20+
inlineScript: |
21+
.\publish\release.ps1 -PsGalleryApiKey '$(PsGalleryApiKey)' -NugetApiKey '$(NugetApiKey)' -ChocolateyApiKey '$(ChocolateyApiKey)' -TenantId '$(TenantId)' -VaultUrl '$(SigningVaultURL)' -CertificateName '$(SigningCertName)'

publish/release.ps1

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ param (
44
[String] $PsGalleryApiKey,
55
[String] $NugetApiKey,
66
[String] $ChocolateyApiKey,
7-
[String] $CertificateThumbprint = '2FCC9148EC2C9AB951C6F9654C0D2ED16AF27738',
7+
[String] $TenantId,
8+
[String] $VaultUrl,
9+
[String] $CertificateName,
810
[Switch] $Force
911
)
1012

@@ -52,7 +54,7 @@ if ((Get-Item $bin/Pester.psm1).Length -lt 50KB) {
5254
throw "Module is too small, are you publishing non-inlined module?"
5355
}
5456

55-
& "$PSScriptRoot/signModule.ps1" -Thumbprint $CertificateThumbprint -Path $bin
57+
& "$PSScriptRoot/signModule.ps1" -VaultUrl $VaultUrl -TenantId $TenantId -CertificateName $CertificateName -Path $bin
5658

5759
$files = @(
5860
'Pester.ps1'
@@ -128,8 +130,23 @@ Get-ChildItem -Path $bin -Filter *.dll -Recurse | ForEach-Object {
128130
}
129131

130132
& nuget pack "$PSScriptRoot/Pester.nuspec" -OutputDirectory $nugetDir -NoPackageAnalysis -version $version
131-
$nupkg = (Join-Path $nugetDir "Pester.$version.nupkg")
132-
& nuget sign $nupkg -CertificateFingerprint $CertificateThumbprint -Timestamper "http://timestamp.digicert.com"
133+
[string] $nupkg = (Join-Path $nugetDir "Pester.$version.nupkg")
134+
135+
dotnet tool install --global NuGetKeyVaultSignTool
136+
if (0 -ne $LASTEXITCODE) {
137+
throw "Failed to install NuGetKeyVaultSignTool"
138+
}
139+
140+
Write-Host "Nuget path: $nupkg"
141+
NuGetKeyVaultSignTool sign -kvu $VaultUrl -kvm -kvc $CertificateName -kvt $TenantId -own "nohwnd,fflaten" -tr "http://timestamp.digicert.com" $nupkg
142+
if (0 -ne $LASTEXITCODE) {
143+
throw "Failed to sign nupkg"
144+
}
145+
146+
NuGetKeyVaultSignTool verify $nupkg
147+
if (0 -ne $LASTEXITCODE) {
148+
throw "Failed to verify nupkg"
149+
}
133150

134151
Publish-Module -Path $psGalleryDir -NuGetApiKey $PsGalleryApiKey -Verbose -Force
135152

publish/signModule.ps1

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
param($Thumbprint, $Path)
1+
param(
2+
[String] $TenantId,
3+
[String] $VaultUrl,
4+
[String] $CertificateName,
5+
[String] $Path
6+
)
27
$ErrorActionPreference = 'Stop'
38

4-
$cert = Get-ChildItem Cert:\CurrentUser\My |
5-
Where-Object Thumbprint -eq $Thumbprint
6-
7-
if ($null -eq $cert) {
8-
throw "No certificate was found."
9-
}
10-
11-
if (@($cert).Length -gt 1) {
12-
throw "More than one cerfificate with the given thumbprint was found."
13-
}
14-
159
"Signing Files"
1610
$files = Get-ChildItem -Recurse -ErrorAction SilentlyContinue $Path |
1711
Where-Object { $_.Extension -in ".ps1", ".psm1", ".psd1", ".ps1xml", ".dll" } |
@@ -28,14 +22,16 @@ if (-not @($filesToSign)) {
2822
return "There are no files to sign, all the files in the repository are already signed."
2923
}
3024

31-
$results = $filesToSign |
32-
ForEach-Object {
33-
$r = Set-AuthenticodeSignature $_ -Certificate $cert -TimestampServer 'http://timestamp.digicert.com' -ErrorAction Stop
34-
$r | Out-String | Write-Host
35-
$r
36-
}
25+
dotnet tool install --global AzureSignTool
26+
if (0 -ne $LASTEXITCODE) {
27+
throw "Failed to install AzureSignTool"
28+
}
29+
azuresigntool sign -kvu $VaultUrl -kvm -kvc $CertificateName -kvt $TenantId -du "https://pester.dev" -tr "http://timestamp.digicert.com" -v $filesToSign
30+
if (0 -ne $LASTEXITCODE) {
31+
throw "Failed to sign files"
32+
}
3733

38-
$failed = $results | Where-Object { $_.Status -ne "Valid" }
34+
$failed = $filesToSign | Get-AuthenticodeSignature | Where-Object { $_.Status -ne "Valid" }
3935

4036
if ($failed) {
4137
throw "Failed signing $($failed.Path -join "`n")"

0 commit comments

Comments
 (0)