|
| 1 | +# Sign a single file |
| 2 | +function Invoke-SignFile { |
| 3 | + [OutputType([Boolean])] |
| 4 | + Param( |
| 5 | + # The path to the file to sign |
| 6 | + [Parameter(Mandatory=$true)] |
| 7 | + [string]$SourcePath, |
| 8 | + # The path to the signed file |
| 9 | + [Parameter(Mandatory=$true)] |
| 10 | + [string]$TargetPath, |
| 11 | + # The name of the unsigned AWS bucket |
| 12 | + [Parameter(Mandatory=$true)] |
| 13 | + [string]$AwsUnsignedBucket, |
| 14 | + [Parameter(Mandatory=$true)] |
| 15 | + # The name of the signed AWS bucket |
| 16 | + [string]$AwsSignedBucket, |
| 17 | + # The name of the AWS key |
| 18 | + [Parameter(Mandatory=$true)] |
| 19 | + [string]$AwsKey, |
| 20 | + [Parameter(Mandatory=$false)] |
| 21 | + [bool]$AsMockResponse=$false |
| 22 | + ) |
| 23 | + |
| 24 | + Write-Host "Signing is enabled. Will attempt to sign" |
| 25 | + |
| 26 | + # Remember to install 'jq' dependency before |
| 27 | + if ($AsMockResponse) { |
| 28 | + Copy-Item $SourcePath $TargetPath |
| 29 | + return $true |
| 30 | + } |
| 31 | + |
| 32 | + # Upload unsigned .msi to S3 Bucket |
| 33 | + Write-Host "Obtaining version id and uploading unsigned .msi to S3 Bucket" |
| 34 | + $versionId = $( aws s3api put-object --bucket $AwsUnsignedBucket --key $AwsKey --body $SourcePath --acl bucket-owner-full-control | jq '.VersionId' ) |
| 35 | + $jobId = "" |
| 36 | + |
| 37 | + if ([string]::IsNullOrEmpty($versionId)) { |
| 38 | + Write-Host "Failed to PUT unsigned file in bucket." |
| 39 | + return $false |
| 40 | + } |
| 41 | + |
| 42 | + # Attempt to get Job ID from bucket tagging, will retry up to 3 times before exiting with a failure code. |
| 43 | + # Will sleep for 5 seconds between retries. |
| 44 | + Write-Host "Attempt to get Job ID from bucket tagging, will retry up to 3 times before exiting with a failure code." |
| 45 | + for ( $i = 0; $i -lt 3; $i++ ) { |
| 46 | + # Get job ID |
| 47 | + $id=$( aws s3api get-object-tagging --bucket $AwsUnsignedBucket --key $AwsKey --version-id $versionId | jq -r '.TagSet[0].Value' ) |
| 48 | + if ( $id -ne "null" ) { |
| 49 | + $jobId = $id |
| 50 | + break |
| 51 | + } |
| 52 | + |
| 53 | + Write-Host "Will sleep for 5 seconds between retries." |
| 54 | + Start-Sleep -Seconds 5 |
| 55 | + } |
| 56 | + |
| 57 | + if ( $jobId -eq "" ) { |
| 58 | + Write-Host "Exiting because unable to retrieve job ID" |
| 59 | + return $false |
| 60 | + } |
| 61 | + |
| 62 | + # Poll signed S3 bucket to see if the signed artifact is there |
| 63 | + Write-Host "Poll signed S3 bucket to see if the signed artifact is there" |
| 64 | + aws s3api wait object-exists --bucket $AwsSignedBucket --key $AwsKey-$jobId |
| 65 | + |
| 66 | + # Get signed msi from S3 |
| 67 | + Write-Host "Get signed msi from S3 to $TargetPath" |
| 68 | + aws s3api get-object --bucket $AwsSignedBucket --key $AwsKey-$jobId $TargetPath |
| 69 | + |
| 70 | + Write-Host "Signing completed" |
| 71 | + return $true |
| 72 | +} |
| 73 | + |
| 74 | +# Sign the installer file for architecture and ODBC version |
| 75 | +function Invoke-SignInstaller { |
| 76 | + [OutputType([Boolean])] |
| 77 | + Param( |
| 78 | + # The path to the build directory. |
| 79 | + [Parameter(Mandatory=$true)] |
| 80 | + [string]$BuildDir, |
| 81 | + # The architecture name. |
| 82 | + [Parameter(Mandatory=$true)] |
| 83 | + [string]$Architecture, |
| 84 | + # The ODBC version. |
| 85 | + [Parameter(Mandatory=$true)] |
| 86 | + [string]$OdbcVersion, |
| 87 | + # The name of the unsigned AWS bucket |
| 88 | + [Parameter(Mandatory=$true)] |
| 89 | + [string]$AwsUnsignedBucket, |
| 90 | + [Parameter(Mandatory=$true)] |
| 91 | + # The name of the signed AWS bucket |
| 92 | + [string]$AwsSignedBucket, |
| 93 | + # The name of the AWS key |
| 94 | + [Parameter(Mandatory=$true)] |
| 95 | + [string]$AwsKey, |
| 96 | + [Parameter(Mandatory=$false)] |
| 97 | + [bool]$AsMockResponse=$false |
| 98 | + ) |
| 99 | + |
| 100 | + $unsignedInstallerPath=$(Join-Path $BuildDir "aws-mysql-odbc-$OdbcVersion-$Architecture.msi") |
| 101 | + $signedInstallerPath=$(Join-Path $BuildDir "aws-mysql-odbc-$OdbcVersion-$Architecture-signed.msi") |
| 102 | + |
| 103 | + Write-Host "unsignedInstallerPath=${unsignedInstallerPath}" |
| 104 | + Write-Host "signedInstallerPath=${signedInstallerPath}" |
| 105 | + |
| 106 | + # Sign the installer |
| 107 | + Write-Host "Signing the installer." |
| 108 | + if ( !(Invoke-SignFile $unsignedInstallerPath $signedInstallerPath $AwsUnsignedBucket $AwsSignedBucket $AwsKey -AsMockResponse $AsMockResponse) ) { |
| 109 | + Write-Host "Failed to sign installer file." |
| 110 | + return $false |
| 111 | + } |
| 112 | + |
| 113 | + # Remove unsigned installer and remove "-signed" in signed installer name |
| 114 | + Write-Host "Removing unsigned executable." |
| 115 | + Remove-Item -Path $unsignedInstallerPath |
| 116 | + Rename-Item -Path $signedInstallerPath -NewName "awsmysql-odbc-$OdbcVersion-$Architecture.msi" |
| 117 | + |
| 118 | + return $true |
| 119 | +} |
0 commit comments