Files
mtusi2026lab4/scripts/init-local.ps1

37 lines
1.5 KiB
PowerShell

$ErrorActionPreference = 'Stop'
$repoRoot = Split-Path -Parent $PSScriptRoot
$envPath = Join-Path $repoRoot '.env'
if (-not (Test-Path -LiteralPath $envPath)) {
Copy-Item -LiteralPath (Join-Path $repoRoot '.env.example') -Destination $envPath
Write-Output 'Local .env created (non-secret session TTL).'
} else {
Write-Output 'Existing .env preserved.'
}
$secretsDirectory = Join-Path $repoRoot 'secrets'
New-Item -ItemType Directory -Path $secretsDirectory -Force | Out-Null
function Ensure-LocalSecret([string] $name, [string] $prefix = '', [int] $byteCount = 32) {
$secretPath = Join-Path $secretsDirectory "$name.txt"
if (Test-Path -LiteralPath $secretPath) {
if ([string]::IsNullOrWhiteSpace([IO.File]::ReadAllText($secretPath))) {
throw "Existing secret file is empty: $name"
}
Write-Output "Existing secret preserved: $name"
return
}
$randomBytes = New-Object byte[] $byteCount
$generator = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try { $generator.GetBytes($randomBytes) } finally { $generator.Dispose() }
$secretValue = $prefix + ([BitConverter]::ToString($randomBytes).Replace('-', '').ToLowerInvariant())
[IO.File]::WriteAllText($secretPath, $secretValue, [Text.Encoding]::ASCII)
Write-Output "Local secret created: $name (value not printed)."
}
Ensure-LocalSecret 'postgres_password'
Ensure-LocalSecret 's3_root_user' 'root' 8
Ensure-LocalSecret 's3_root_password'
Ensure-LocalSecret 's3_access_key_id' 'api-' 8
Ensure-LocalSecret 's3_secret_access_key'