forked from mtusi2026/mtusi2026lab2
34 lines
1.4 KiB
PowerShell
34 lines
1.4 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
$repo = Split-Path -Parent $PSScriptRoot
|
|
$secretDir = Join-Path $repo 'secrets'
|
|
New-Item -ItemType Directory -Path $secretDir -Force | Out-Null
|
|
$secretNames = @('postgres_password', 'redis_password', 'database_url', 'cache_url')
|
|
$existing = @($secretNames | Where-Object { Test-Path -LiteralPath (Join-Path $secretDir $_) })
|
|
if ($existing.Count -eq $secretNames.Count) {
|
|
Write-Output 'All local secret files already exist; they were left unchanged.'
|
|
return
|
|
}
|
|
if ($existing.Count -gt 0) {
|
|
throw 'Some but not all local secret files exist. Resolve them manually; this script will not rotate credentials.'
|
|
}
|
|
|
|
function New-Secret {
|
|
$bytes = New-Object byte[] 32
|
|
[System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes)
|
|
return [Convert]::ToBase64String($bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
|
|
}
|
|
|
|
$postgresPassword = New-Secret
|
|
$redisPassword = New-Secret
|
|
$files = @{
|
|
postgres_password = $postgresPassword
|
|
redis_password = $redisPassword
|
|
database_url = "postgresql+psycopg://messenger:$postgresPassword@db:5432/messenger"
|
|
cache_url = "redis://:$redisPassword@redis:6379/0"
|
|
}
|
|
foreach ($name in $secretNames) {
|
|
$path = Join-Path $secretDir $name
|
|
[System.IO.File]::WriteAllText($path, $files[$name], [System.Text.UTF8Encoding]::new($false))
|
|
}
|
|
Write-Output 'Local Postgres and Redis credentials created under secrets/. Values were not displayed.'
|