$ErrorActionPreference = 'Stop' Set-Location (Join-Path $PSScriptRoot '..') if (!(Test-Path -LiteralPath '.env')) { Copy-Item -LiteralPath '.env.example' -Destination '.env' } New-Item -ItemType Directory -Path 'secrets' -Force | Out-Null function New-SecretFile([string]$Name, [int]$Length) { $path = Join-Path (Get-Location) "secrets/$Name" if (!(Test-Path -LiteralPath $path)) { $bytes = [byte[]]::new($Length) $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create() try { $rng.GetBytes($bytes) } finally { $rng.Dispose() } $value = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant() [IO.File]::WriteAllText($path, $value, [Text.Encoding]::ASCII) } } New-SecretFile 'db_password' 24 New-SecretFile 's3_root_access' 10 New-SecretFile 's3_root_secret' 20 New-SecretFile 's3_access_key_id' 10 New-SecretFile 's3_secret_access_key' 20 New-SecretFile 'broker_password' 24 $brokerPassword = (Get-Content -LiteralPath 'secrets/broker_password' -Raw).Trim() [IO.File]::WriteAllText((Join-Path (Get-Location) 'secrets/broker_url'), "amqp://messenger:${brokerPassword}@broker:5672/%2F", [Text.Encoding]::ASCII) [IO.File]::WriteAllText((Join-Path (Get-Location) 'secrets/rabbitmq_config'), "default_user = messenger`ndefault_pass = ${brokerPassword}`n", [Text.Encoding]::ASCII) if (!(Test-Path -LiteralPath 'secrets/database_url')) { $password = (Get-Content -LiteralPath 'secrets/db_password' -Raw).TrimEnd([char]10) $encoded = [Uri]::EscapeDataString($password) $url = "postgresql://messenger:${encoded}@db:5432/messenger" [IO.File]::WriteAllText((Join-Path (Get-Location) 'secrets/database_url'), $url, [Text.Encoding]::ASCII) } Write-Host 'Local secrets are ready.'