|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Downloads the CodeCov.io uploader tool and returns the path to it. |
| 4 | +.PARAMETER AllowSkipVerify |
| 5 | + Allows skipping signature verification of the downloaded tool if gpg is not installed. |
| 6 | +#> |
| 7 | +[CmdletBinding()] |
| 8 | +Param( |
| 9 | + [switch]$AllowSkipVerify |
| 10 | +) |
| 11 | + |
| 12 | +if ($IsMacOS) { |
| 13 | + $codeCovUrl = "https://uploader.codecov.io/latest/macos/codecov" |
| 14 | + $toolName = 'codecov' |
| 15 | +} |
| 16 | +elseif ($IsLinux) { |
| 17 | + $codeCovUrl = "https://uploader.codecov.io/latest/linux/codecov" |
| 18 | + $toolName = 'codecov' |
| 19 | +} |
| 20 | +else { |
| 21 | + $codeCovUrl = "https://uploader.codecov.io/latest/windows/codecov.exe" |
| 22 | + $toolName = 'codecov.exe' |
| 23 | +} |
| 24 | + |
| 25 | +$shaSuffix = ".SHA256SUM" |
| 26 | +$sigSuffix = $shaSuffix + ".sig" |
| 27 | + |
| 28 | +Function Get-FileFromWeb([Uri]$Uri, $OutDir) { |
| 29 | + $OutFile = Join-Path $OutDir $Uri.Segments[-1] |
| 30 | + if (!(Test-Path $OutFile)) { |
| 31 | + Write-Verbose "Downloading $Uri..." |
| 32 | + if (!(Test-Path $OutDir)) { New-Item -ItemType Directory -Path $OutDir | Out-Null } |
| 33 | + try { |
| 34 | + (New-Object System.Net.WebClient).DownloadFile($Uri, $OutFile) |
| 35 | + } finally { |
| 36 | + # This try/finally causes the script to abort |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + $OutFile |
| 41 | +} |
| 42 | + |
| 43 | +$toolsPath = & "$PSScriptRoot\Get-TempToolsPath.ps1" |
| 44 | +$binaryToolsPath = Join-Path $toolsPath codecov |
| 45 | +$testingPath = Join-Path $binaryToolsPath unverified |
| 46 | +$finalToolPath = Join-Path $binaryToolsPath $toolName |
| 47 | + |
| 48 | +if (!(Test-Path $finalToolPath)) { |
| 49 | + if (Test-Path $testingPath) { |
| 50 | + Remove-Item -Recurse -Force $testingPath # ensure we download all matching files |
| 51 | + } |
| 52 | + $tool = Get-FileFromWeb $codeCovUrl $testingPath |
| 53 | + $sha = Get-FileFromWeb "$codeCovUrl$shaSuffix" $testingPath |
| 54 | + $sig = Get-FileFromWeb "$codeCovUrl$sigSuffix" $testingPath |
| 55 | + $key = Get-FileFromWeb https://keybase.io/codecovsecurity/pgp_keys.asc $testingPath |
| 56 | + |
| 57 | + if ((Get-Command gpg -ErrorAction SilentlyContinue)) { |
| 58 | + Write-Host "Importing codecov key" -ForegroundColor Yellow |
| 59 | + gpg --import $key |
| 60 | + Write-Host "Verifying signature on codecov hash" -ForegroundColor Yellow |
| 61 | + gpg --verify $sig $sha |
| 62 | + } else { |
| 63 | + if ($AllowSkipVerify) { |
| 64 | + Write-Warning "gpg not found. Unable to verify hash signature." |
| 65 | + } else { |
| 66 | + throw "gpg not found. Unable to verify hash signature. Install gpg or add -AllowSkipVerify to override." |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Write-Host "Verifying hash on downloaded tool" -ForegroundColor Yellow |
| 71 | + $actualHash = (Get-FileHash -Path $tool -Algorithm SHA256).Hash |
| 72 | + $expectedHash = (Get-Content $sha).Split()[0] |
| 73 | + if ($actualHash -ne $expectedHash) { |
| 74 | + # Validation failed. Delete the tool so we can't execute it. |
| 75 | + #Remove-Item $codeCovPath |
| 76 | + throw "codecov uploader tool failed signature validation." |
| 77 | + } |
| 78 | + |
| 79 | + Copy-Item $tool $finalToolPath |
| 80 | + |
| 81 | + if ($IsMacOS -or $IsLinux) { |
| 82 | + chmod u+x $finalToolPath |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +return $finalToolPath |
0 commit comments