|
| 1 | +[CmdletBinding(PositionalBinding=$False)] |
| 2 | +param( |
| 3 | + [Parameter(Mandatory=$true, Position=0)][string] $InputPath, |
| 4 | + [Parameter(Mandatory=$true)][string] $BinlogToolVersion, |
| 5 | + [Parameter(Mandatory=$false)][string] $DotnetPath, |
| 6 | + [Parameter(Mandatory=$false)][string] $PackageFeed = 'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json', |
| 7 | + # File with strings to redact - separated by newlines. |
| 8 | + # For comments start the line with '# ' - such lines are ignored |
| 9 | + [Parameter(Mandatory=$false)][string] $TokensFilePath, |
| 10 | + [Parameter(ValueFromRemainingArguments=$true)][String[]]$TokensToRedact |
| 11 | +) |
| 12 | + |
| 13 | +try { |
| 14 | + . $PSScriptRoot\post-build-utils.ps1 |
| 15 | + |
| 16 | + $packageName = 'binlogtool' |
| 17 | + |
| 18 | + $dotnet = $DotnetPath |
| 19 | + |
| 20 | + if (!$dotnet) { |
| 21 | + $dotnetRoot = InitializeDotNetCli -install:$true |
| 22 | + $dotnet = "$dotnetRoot\dotnet.exe" |
| 23 | + } |
| 24 | + |
| 25 | + $toolList = & "$dotnet" tool list -g |
| 26 | + |
| 27 | + if ($toolList -like "*$packageName*") { |
| 28 | + & "$dotnet" tool uninstall $packageName -g |
| 29 | + } |
| 30 | + |
| 31 | + $toolPath = "$PSScriptRoot\..\..\..\.tools" |
| 32 | + $verbosity = 'minimal' |
| 33 | + |
| 34 | + New-Item -ItemType Directory -Force -Path $toolPath |
| 35 | + |
| 36 | + Push-Location -Path $toolPath |
| 37 | + |
| 38 | + try { |
| 39 | + Write-Host "Installing Binlog redactor CLI..." |
| 40 | + Write-Host "'$dotnet' new tool-manifest" |
| 41 | + & "$dotnet" new tool-manifest |
| 42 | + Write-Host "'$dotnet' tool install $packageName --local --add-source '$PackageFeed' -v $verbosity --version $BinlogToolVersion" |
| 43 | + & "$dotnet" tool install $packageName --local --add-source "$PackageFeed" -v $verbosity --version $BinlogToolVersion |
| 44 | + |
| 45 | + if (Test-Path $TokensFilePath) { |
| 46 | + Write-Host "Adding additional sensitive data for redaction from file: " $TokensFilePath |
| 47 | + $TokensToRedact += Get-Content -Path $TokensFilePath | Foreach {$_.Trim()} | Where { $_ -notmatch "^# " } |
| 48 | + } |
| 49 | + |
| 50 | + $optionalParams = [System.Collections.ArrayList]::new() |
| 51 | + |
| 52 | + Foreach ($p in $TokensToRedact) |
| 53 | + { |
| 54 | + if($p -match '^\$\(.*\)$') |
| 55 | + { |
| 56 | + Write-Host ("Ignoring token {0} as it is probably unexpanded AzDO variable" -f $p) |
| 57 | + } |
| 58 | + elseif($p) |
| 59 | + { |
| 60 | + $optionalParams.Add("-p:" + $p) | Out-Null |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + & $dotnet binlogtool redact --input:$InputPath --recurse --in-place ` |
| 65 | + @optionalParams |
| 66 | + |
| 67 | + if ($LastExitCode -ne 0) { |
| 68 | + Write-PipelineTelemetryError -Category 'Redactor' -Type 'warning' -Message "Problems using Redactor tool (exit code: $LastExitCode). But ignoring them now." |
| 69 | + } |
| 70 | + } |
| 71 | + finally { |
| 72 | + Pop-Location |
| 73 | + } |
| 74 | + |
| 75 | + Write-Host 'done.' |
| 76 | +} |
| 77 | +catch { |
| 78 | + Write-Host $_ |
| 79 | + Write-PipelineTelemetryError -Category 'Redactor' -Message "There was an error while trying to redact logs. Error: $_" |
| 80 | + ExitWithExitCode 1 |
| 81 | +} |
0 commit comments