forked from mtusi2026/mtusi2026lab3
37 lines
1.6 KiB
PowerShell
37 lines
1.6 KiB
PowerShell
# Windows PowerShell 5.1+ version of scripts/init-secrets.sh.
|
|
# Creates local synthetic secrets in secrets\ and .env (both git-ignored).
|
|
# Idempotent: existing files are NOT overwritten. Values are never printed.
|
|
$ErrorActionPreference = 'Stop'
|
|
$root = Split-Path -Parent $PSScriptRoot
|
|
Set-Location $root
|
|
New-Item -ItemType Directory -Force -Path (Join-Path $root 'secrets'), (Join-Path $root 'backups') | Out-Null
|
|
$ascii = New-Object System.Text.ASCIIEncoding
|
|
|
|
function New-Hex {
|
|
$bytes = New-Object byte[] 24
|
|
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
|
$rng.GetBytes($bytes)
|
|
($bytes | ForEach-Object { $_.ToString('x2') }) -join ''
|
|
}
|
|
function Set-SecretFile([string]$name, [string]$value) {
|
|
$path = Join-Path (Join-Path $root 'secrets') $name
|
|
if (-not (Test-Path $path)) {
|
|
# No BOM, no trailing CR/LF: the value is read verbatim inside Linux containers.
|
|
[System.IO.File]::WriteAllText($path, $value, $ascii)
|
|
}
|
|
}
|
|
function Get-SecretFile([string]$name) {
|
|
([System.IO.File]::ReadAllText((Join-Path (Join-Path $root 'secrets') $name))).Trim()
|
|
}
|
|
|
|
Set-SecretFile 'postgres_password' (New-Hex)
|
|
Set-SecretFile 'redis_password' (New-Hex)
|
|
$pg = Get-SecretFile 'postgres_password'
|
|
$rd = Get-SecretFile 'redis_password'
|
|
Set-SecretFile 'database_url' ("postgresql://messenger:" + $pg + "@postgres:5432/messenger")
|
|
Set-SecretFile 'cache_url' ("redis://:" + $rd + "@redis:6379/0")
|
|
if (-not (Test-Path (Join-Path $root '.env'))) {
|
|
Copy-Item (Join-Path $root '.env.example') (Join-Path $root '.env')
|
|
}
|
|
Write-Output 'secrets\ and .env are ready (values not shown).'
|