89 lines
4.2 KiB
PowerShell
89 lines
4.2 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")
|
|
# S3 (SeaweedFS): two identities. admin - only for bucket bootstrap (s3-init service);
|
|
# messenger-api - Read/Write/List ONLY on the attachments bucket (cannot create/delete buckets).
|
|
function New-KeyId {
|
|
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
|
$bytes = New-Object byte[] 20
|
|
[System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
|
|
($bytes | ForEach-Object { $chars[$_ % $chars.Length] }) -join ''
|
|
}
|
|
Set-SecretFile 's3_admin_access_key_id' (New-KeyId)
|
|
Set-SecretFile 's3_admin_secret_access_key' (New-Hex)
|
|
Set-SecretFile 's3_access_key_id' (New-KeyId)
|
|
Set-SecretFile 's3_secret_access_key' (New-Hex)
|
|
$bucket = 'messenger-attachments'
|
|
$envPath = Join-Path $root '.env'
|
|
if (Test-Path $envPath) {
|
|
$line = Select-String -Path $envPath -Pattern '^S3_BUCKET=(.+)$' | Select-Object -First 1
|
|
if ($line) { $bucket = $line.Matches[0].Groups[1].Value.Trim() }
|
|
}
|
|
$cfgPath = Join-Path (Join-Path $root 'secrets') 's3_config.json'
|
|
if (-not (Test-Path $cfgPath)) {
|
|
$cfg = [ordered]@{ identities = @(
|
|
[ordered]@{ name = 'admin'; credentials = @([ordered]@{ accessKey = (Get-SecretFile 's3_admin_access_key_id'); secretKey = (Get-SecretFile 's3_admin_secret_access_key') });
|
|
actions = @('Admin', 'Read', 'List', 'Tagging', 'Write') },
|
|
[ordered]@{ name = 'messenger-api'; credentials = @([ordered]@{ accessKey = (Get-SecretFile 's3_access_key_id'); secretKey = (Get-SecretFile 's3_secret_access_key') });
|
|
actions = @(('Read:' + $bucket), ('Write:' + $bucket), ('List:' + $bucket)) }
|
|
) }
|
|
[System.IO.File]::WriteAllText($cfgPath, ($cfg | ConvertTo-Json -Depth 6), $ascii)
|
|
}
|
|
# NATS JetStream: service password (API, worker, bootstrap) and the server config that holds it.
|
|
Set-SecretFile 'nats_password' (New-Hex)
|
|
$np = Get-SecretFile 'nats_password'
|
|
Set-SecretFile 'broker_url' ('nats://messenger:' + $np + '@nats:4222')
|
|
$natsConf = @(
|
|
'# nats-server (lab5): JetStream on disk with fsync on every write, password auth,',
|
|
'# monitoring (healthcheck) only on 127.0.0.1 inside the container.',
|
|
'server_name: nats-lab6',
|
|
'listen: "0.0.0.0:4222"',
|
|
'http: "127.0.0.1:8222"',
|
|
'max_payload: 65536',
|
|
'jetstream {',
|
|
' store_dir: "/data"',
|
|
' max_memory_store: 64MB',
|
|
' max_file_store: 2GB',
|
|
' sync_interval: always',
|
|
'}',
|
|
'authorization {',
|
|
(' users = [ { user: "messenger", password: "' + $np + '" } ]'),
|
|
'}'
|
|
) -join "`n"
|
|
Set-SecretFile 'nats.conf' ($natsConf + "`n")
|
|
# lab6: Grafana admin password (UI on 127.0.0.1:3000; tests read it locally and never print it).
|
|
Set-SecretFile 'grafana_admin_password' (New-Hex)
|
|
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).'
|