Files
mtusi2026lab6/scripts/init-secrets.ps1
T

33 lines
2.0 KiB
PowerShell

# Creates the same local secret files as init-secrets.sh on Windows.
# Existing values are preserved and never printed.
$ErrorActionPreference = 'Stop'
$secretDirectory = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '../infra/secrets'))
[IO.Directory]::CreateDirectory($secretDirectory) | Out-Null
function New-RandomHex {
$bytes = New-Object byte[] 24
$randomGenerator = [Security.Cryptography.RandomNumberGenerator]::Create()
try { $randomGenerator.GetBytes($bytes) } finally { $randomGenerator.Dispose() }
return ([BitConverter]::ToString($bytes)).Replace('-', '').ToLowerInvariant()
}
function Write-NewSecret([string]$Name, [string]$Value) {
$secretPath = Join-Path $secretDirectory $Name
if (Test-Path -LiteralPath $secretPath) { Write-Output "keep infra/secrets/$Name" }
else {
[IO.File]::WriteAllText($secretPath, $Value, (New-Object Text.UTF8Encoding $false))
Write-Output "create infra/secrets/$Name"
}
}
Write-NewSecret 'postgres_password.txt' (New-RandomHex)
$databasePassword = [IO.File]::ReadAllText((Join-Path $secretDirectory 'postgres_password.txt')).Trim()
Write-NewSecret 'database_url.txt' "postgresql://messenger:${databasePassword}@db:5432/messenger"
Write-NewSecret 'valkey_password.txt' (New-RandomHex)
$cachePassword = [IO.File]::ReadAllText((Join-Path $secretDirectory 'valkey_password.txt')).Trim()
Write-NewSecret 'cache_url.txt' "redis://:${cachePassword}@valkey:6379/0"
Write-NewSecret 's3_admin_access_key_id.txt' ('admin' + (New-RandomHex).Substring(0,15))
Write-NewSecret 's3_admin_secret_access_key.txt' ((New-RandomHex) + (New-RandomHex))
Write-NewSecret 's3_access_key_id.txt' ('api' + (New-RandomHex).Substring(0,17))
Write-NewSecret 's3_secret_access_key.txt' ((New-RandomHex) + (New-RandomHex))
Write-NewSecret 'broker_password.txt' (New-RandomHex)
$queuePassword = [IO.File]::ReadAllText((Join-Path $secretDirectory 'broker_password.txt')).Trim()
Write-NewSecret 'broker_url.txt' "amqp://messenger:${queuePassword}@broker:5672/"