|
| 1 | +# Generates a strong random SQL Server password and exposes it as the |
| 2 | +# job-scoped runtime variable SQL_PASSWORD (secret-masked in logs). |
| 3 | +# |
| 4 | +# Idempotent within a single job: a non-secret marker variable |
| 5 | +# SQL_PASSWORD_GENERATED is set on first run and inspected on subsequent |
| 6 | +# invocations, so including this template from multiple step-templates |
| 7 | +# in the same job will only generate the password once. |
| 8 | +# |
| 9 | +# Subsequent steps in the job consume the value via the standard |
| 10 | +# `$(SQL_PASSWORD)` macro syntax — no caller changes required. |
| 11 | + |
| 12 | +parameters: |
| 13 | + - name: osType |
| 14 | + type: string |
| 15 | + default: Linux |
| 16 | + values: |
| 17 | + - Windows |
| 18 | + - Linux |
| 19 | + - MacOS |
| 20 | + |
| 21 | +steps: |
| 22 | + - ${{ if eq(parameters.osType, 'Windows') }}: |
| 23 | + - task: PowerShell@2 |
| 24 | + displayName: 'Generate SQL_PASSWORD (job-scoped)' |
| 25 | + inputs: |
| 26 | + targetType: 'inline' |
| 27 | + script: | |
| 28 | + $ErrorActionPreference = 'Stop' |
| 29 | + if ($env:SQL_PASSWORD_GENERATED -eq '1') { |
| 30 | + Write-Host 'SQL_PASSWORD already generated for this job; skipping' |
| 31 | + exit 0 |
| 32 | + } |
| 33 | + $bytes = New-Object byte[] 24 |
| 34 | + $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() |
| 35 | + try { |
| 36 | + $rng.GetBytes($bytes) |
| 37 | + $rand = ([Convert]::ToBase64String($bytes)) -replace '[+/=]','' |
| 38 | + while ($rand.Length -lt 22) { |
| 39 | + $rng.GetBytes($bytes) |
| 40 | + $rand = $rand + (([Convert]::ToBase64String($bytes)) -replace '[+/=]','') |
| 41 | + } |
| 42 | + } finally { |
| 43 | + $rng.Dispose() |
| 44 | + } |
| 45 | + $rand = $rand.Substring(0, 22) |
| 46 | + # Prepend a known mix to guarantee SQL Server password-policy classes. |
| 47 | + $pwd = 'Aa1!' + $rand |
| 48 | + Write-Host "##vso[task.setvariable variable=SQL_PASSWORD;issecret=true]$pwd" |
| 49 | + Write-Host "##vso[task.setvariable variable=SQL_PASSWORD_GENERATED]1" |
| 50 | + Write-Host "Generated SQL_PASSWORD (length=$($pwd.Length))" |
| 51 | +
|
| 52 | + - ${{ if ne(parameters.osType, 'Windows') }}: |
| 53 | + - bash: | |
| 54 | + set -euo pipefail |
| 55 | + if [ "${SQL_PASSWORD_GENERATED:-}" = "1" ]; then |
| 56 | + echo "SQL_PASSWORD already generated for this job; skipping" |
| 57 | + exit 0 |
| 58 | + fi |
| 59 | + rand=$(openssl rand -base64 24 | tr -d '=+/' | head -c 22) |
| 60 | + while [ "${#rand}" -lt 22 ]; do |
| 61 | + rand="${rand}$(openssl rand -base64 24 | tr -d '=+/' | head -c 4)" |
| 62 | + done |
| 63 | + rand="${rand:0:22}" |
| 64 | + # Prepend a known mix to guarantee SQL Server password-policy classes. |
| 65 | + pwd="Aa1!${rand}" |
| 66 | + echo "##vso[task.setvariable variable=SQL_PASSWORD;issecret=true]${pwd}" |
| 67 | + echo "##vso[task.setvariable variable=SQL_PASSWORD_GENERATED]1" |
| 68 | + echo "Generated SQL_PASSWORD (length=${#pwd})" |
| 69 | + displayName: 'Generate SQL_PASSWORD (job-scoped)' |
0 commit comments