forked from microsoft/ApplicationInsights-dotnet-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NugetAudit.ps1
317 lines (243 loc) · 10.6 KB
/
NugetAudit.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# This script checks the Authoring Requirements listed here:
# Full Requirements: https://microsoft.sharepoint.com/teams/NuGet/MicrosoftWiki/MicrosoftPackages.aspx
# Authoring Requirements: https://microsoft.sharepoint.com/teams/NuGet/MicrosoftWiki/AuthoringRequirements.aspx
# Signing Requirements: https://microsoft.sharepoint.com/teams/NuGet/MicrosoftWiki/SigningMicrosoftPackages.aspx
Param(
[Parameter(Mandatory=$true,HelpMessage="Path to Artifact files (nupkg):")]
[string]
$path,
[Parameter(Mandatory=$true,HelpMessage="Full Log?:")] #Include Pass with Fail output?
[bool]
$verboseLog
)
$requiredCopyright = "$([char]0x00A9) Microsoft Corporation. All rights reserved.";#"© Microsoft Corporation. All rights reserved.";
$expectedProjectUrl = "https://go.microsoft.com/fwlink/?LinkId=392727"; # Application Insights Project Url
$expectedLicense = "MIT"; # MIT license SPDX ID
$expectedOwner = "AppInsightsSdk"; # Application Insights Nuget Account
$expectedTags = @("Azure","Monitoring");
$sb = [System.Text.StringBuilder]::new();
$script:isValid = $true;
# Get the latest Nuget.exe from here:
if (!(Test-Path ".\Nuget.exe")) {
Write-Host "Nuget.exe not found. Attempting download...";
Write-Host "Start time:" (Get-Date -Format G);
$downloadNugetUrl = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe";
$saveFile = "$PSScriptRoot\Nuget.exe";
(New-Object System.Net.WebClient).DownloadFile($downloadNugetUrl, $saveFile);
Write-Host "Finish time:" (Get-Date -Format G);
if (!(Test-Path ".\Nuget.exe")) {
throw "Error: Nuget.exe not found! Please download latest from: https://www.nuget.org/downloads";
}
}
function Write-Break() {
$displayMessage = "========== ========== ========== ========== ==========";
Write-Host "";
Write-Host $displayMessage;
$null = $sb.AppendLine();
$null = $sb.AppendLine($displayMessage);
}
function Write-Name([string]$message) {
Write-Host $message;
$null = $sb.AppendLine($message);
}
function Write-IsValid ([string]$message) {
if ($verboseLog) {
$displayMessage = "`tPass:`t$message";
Write-Host $displayMessage -ForegroundColor Green;
$null = $sb.AppendLine($displayMessage);
}
}
function Write-SemiValid ([string]$message, [string]$recommendedDescription) {
$displayMessage = "`tReview:`t$message [Recommended: $recommendedDescription]";
Write-Host $displayMessage -ForegroundColor Yellow;
$null = $sb.AppendLine($displayMessage);
}
function Write-NotValid ([string]$message, [string]$requiredDescription) {
$displayMessage = "`tFail:`t$message [Required: $requiredDescription]";
Write-Host $displayMessage -ForegroundColor Red;
$null = $sb.AppendLine($displayMessage);
$script:isValid = $false;
}
function Test-Condition ([bool]$condition, [string]$message, [string]$requiredDescription) {
if ($condition) {
Write-IsValid $message
} else {
Write-NotValid $message $requiredDescription
}
}
function Test-MultiCondition ([bool]$requiredCondition, [bool]$recommendedCondition, [string]$message, [string]$requiredDescription, [string]$recommendedDescription) {
if ($requiredCondition) {
if($recommendedCondition) {
Write-IsValid $message
} else {
Write-SemiValid $message $recommendedDescription
}
} else {
Write-NotValid $message $requiredDescription
}
}
function Get-IsPackageSigned([string]$nupkgPath) {
$verifyOutput = "";
$null = .\Nuget.exe verify -signature -CertificateFingerprint 3F9001EA83C560D712C24CF213C3D312CB3BFF51EE89435D3430BD06B5D0EECE $nupkgPath -verbosity detailed 2>&1 | Tee-Object -Variable verifyOutput
#TEST OUTPUT
Write-Host $verifyOutput
$output = $verifyOutput[$verifyOutput.Length-1]
$success = ($output -like "Successfully verified*");
if (!$success){
$verifyOutput | Where-Object { $_ -like "NU*" -and $_ -notlike "NUGET*"} | ForEach-Object {
$output = "$output $_" -replace "`n","" -replace "`r","";
}
}
$message = "Is Signed: $output";
$requirement = "Must be signed."
Test-Condition ($success) $message $requirement;
}
function Get-IsDllSigned ([string]$dllPath) {
$test = Get-AuthenticodeSignature $dllPath; # this is equivalent to signtool.exe verify /pa
$SignedStatus = $test.Status;
$SignatureValid = [System.Management.Automation.SignatureStatus]::Valid;
$message = "Is Signed: $SignedStatus";
$requirement = "Must be signed."
Test-Condition ($SignedStatus -eq $SignatureValid) $message $requirement;
}
function Get-DoesXmlDocExist ([string]$dllPath) {
# CONFIRM .XML DOCUMENTATION FILE EXISTS WITH EACH DLL
[string]$docFile = $dllPath -replace ".dll", ".xml";
$message = "XML Documentation:";
$requirement = "Must exist."
Test-Condition (Test-Path $docFile) $message $requirement;
}
function Get-IsValidPackageId([xml]$nuspecXml) {
$id = $nuspecXml.package.metadata.id;
$message = "Package Id: $id";
$requirement = "Must begin with 'Microsoft.'"
Test-Condition ($id.StartsWith("Microsoft.")) $message $requirement;
}
function Get-IsValidAuthors([xml]$nuspecXml) {
$authors = $nuspecXml.package.metadata.authors;
$message = "Authors: $authors";
$requirement = "Microsoft must be the only author.";
Test-Condition ($authors -eq "Microsoft") $message $requirement;
}
function Get-IsValidOwners([xml]$nuspecXml) {
$owners = $nuspecXml.package.metadata.owners;
$ownersList = $owners -split ',';
$message = "Owners: $owners";
$requirement = "Must include Microsoft."
$recommendation = "Should include nuget owner."
Test-MultiCondition ($ownersList -contains "Microsoft") ($ownersList -contains $expectedOwner -and $ownersList.Length -eq 2) $message $requirement $recommendation;
}
function Get-IsValidProjectUrl([xml]$nuspecXml) {
$projectUrl = $nuspecXml.package.metadata.projectUrl;
$message = "Project Url: $projectUrl";
$requirement = "Must match expected."
Test-Condition ($projectUrl -eq $expectedProjectUrl) $message $requirement;
}
function Get-IsValidLicense([xml]$nuspecXml) {
$license = $nuspecXml.package.metadata.license.InnerText;
$message = "License Url: $license";
$requirement = "Must match expected."
Test-Condition ($license -eq $expectedLicense) $message $requirement;
}
function Get-IsValidLicenseAcceptance([xml]$nuspecXml) {
$requireLicenseAcceptance = $nuspecXml.package.metadata.requireLicenseAcceptance;
$message = "Require License Acceptance: $requireLicenseAcceptance";
$requirement = "Not mandatory requirement."
$recommendation = "Should require.";
Test-MultiCondition ($true) ($requireLicenseAcceptance -eq $true) $message $requirement $recommendation;
}
function Get-IsValidCopyright([xml]$nuspecXml) {
$copyright = $nuspecXml.package.metadata.copyright;
$message = "Copyright: $copyright";
$requirement = "Must match '$requiredCopyright'";
Test-Condition ($copyright -eq $requiredCopyright) $message $requirement;
}
function Get-IsValidDescription([xml]$nuspecXml) {
$description = $nuspecXml.package.metadata.description;
$hasDescription = !([System.String]::IsNullOrEmpty($description));
$message = "Description: $description";
$requirement = "Must have a description."
Test-Condition $hasDescription $message $requirement;
}
function Get-IsValidTags([xml]$nuspecXml) {
$tags = $nuspecXml.package.metadata.tags;
$hasTags = !([System.String]::IsNullOrEmpty($tags));
$message = "Tags: $tags";
$requirement = "Must have tags."
Test-Condition $hasTags $message $requirement;
$tagsArray = @();
if($hasTags) {
$tagsArray = $tags -split " ";
}
$expectedTags | ForEach-Object {
$hasTag = $tagsArray.Contains($_);
$requirement = "Must include tag: $_";
Test-Condition $hasTag $message $requirement;
}
}
function Get-IsValidLogoUrl([xml]$nuspecXml, $path) {
$logoUrl = $nuspecXml.package.metadata.iconUrl;
$isEmpty = [System.String]::IsNullOrEmpty($logoUrl);
$dimension = "";
try {
$filePath = Join-Path $path "logo.png";
$wc = New-Object System.Net.WebClient;
$wc.DownloadFile($logoUrl, $filePath);
add-type -AssemblyName System.Drawing
$png = New-Object System.Drawing.Bitmap $filePath
$dimension = "$($png.Height)x$($png.Width)";
# Release lock on png file
Remove-Variable png;
Remove-Variable wc;
} catch [System.SystemException] {
$_.Exception.Message;
}
[string[]]$expectedDimensions = ("32x32","48x48","64x64","128x128");
$message = "Logo Url: $logoUrl Dimensions: $dimension";
$requirement = "Must have a logo."
$recommendation = "Should be one of these sizes: $expectedDimensions";
$isExpected = ($expectedDimensions -contains $dimension);
Test-MultiCondition (!$isEmpty) ($isExpected) $message $requirement $recommendation;
}
function Invoke-UnZip([string]$zipfile, [string]$outpath) {
Write-Verbose "Unzip - source: $zipfile"
Write-Verbose "Unzip - target: $outpath"
Add-Type -assembly "system.io.compression.filesystem"
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
function Start-EvaluateNupkg ($nupkgPath) {
Write-Break;
Write-Name $nupkgPath;
Get-IsPackageSigned $nupkgPath;
$unzipPath = $nupkgPath+"_unzip";
Remove-Item $unzipPath -Recurse -ErrorAction Ignore
$null = Invoke-UnZip $nupkgPath $unzipPath;
# LOOK FOR ALL NUSPEC WITHIN NUPKG
Get-ChildItem -Path $unzipPath -Recurse -Filter *.nuspec | ForEach-Object {
Write-Name $_.FullName;
[xml]$nuspecXml = Get-Content $_.FullName
Get-IsValidPackageId $nuspecXml;
Get-IsValidAuthors $nuspecXml;
Get-IsValidOwners $nuspecXml;
Get-IsValidProjectUrl $nuspecXml;
Get-IsValidLicense $nuspecXml;
Get-IsValidLicenseAcceptance $nuspecXml;
Get-IsValidCopyright $nuspecXml;
Get-IsValidLogoUrl $nuspecXml $unzipPath;
Get-IsValidDescription $nuspecXml;
Get-IsValidTags $nuspecXml;
}
# LOOK FOR ALL DLL WITHIN NUPKG
Get-ChildItem -Path $unzipPath -Recurse -Filter *.dll | ForEach-Object {
Write-Name $_.FullName;
Get-IsDllSigned $_.FullName;
Get-DoesXmlDocExist $_.FullName;
}
}
# LOOK FOR ALL NUPKG IN A DIRECTORY.
Get-ChildItem -Path $path -Recurse -Filter *.nupkg -Exclude *.symbols.nupkg |
ForEach-Object { Start-EvaluateNupkg $_.FullName }
$sb.ToString() | Add-Content (Join-Path $path "log.txt");
if (!$script:isValid){
throw "NUPKG or DLL is not valid. Please review log...";
}