61 lines
1.8 KiB
PowerShell
61 lines
1.8 KiB
PowerShell
$ErrorActionPreference = 'Stop'
|
|
$root = Split-Path -Parent $PSScriptRoot
|
|
$secrets = Join-Path $root 'secrets'
|
|
$names = @(
|
|
'postgres_password',
|
|
'database_url',
|
|
'minio_root_user',
|
|
'minio_root_password',
|
|
's3_access_key_id',
|
|
's3_secret_access_key'
|
|
)
|
|
|
|
if (-not (Test-Path (Join-Path $root '.env'))) {
|
|
Copy-Item (Join-Path $root '.env.example') (Join-Path $root '.env')
|
|
}
|
|
|
|
$existing = @($names | Where-Object { Test-Path (Join-Path $secrets $_) })
|
|
if ($existing.Count -gt 0) {
|
|
if ($existing.Count -eq $names.Count) {
|
|
Write-Output 'Local secrets already exist; leaving them unchanged.'
|
|
exit 0
|
|
}
|
|
throw 'The local secret set is incomplete. Restore it as a set; do not rotate credentials against existing volumes.'
|
|
}
|
|
|
|
New-Item -ItemType Directory -Force $secrets | Out-Null
|
|
function New-HexSecret([int]$byteCount) {
|
|
$bytes = New-Object byte[] $byteCount
|
|
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
|
try {
|
|
$rng.GetBytes($bytes)
|
|
}
|
|
finally {
|
|
$rng.Dispose()
|
|
}
|
|
$builder = New-Object System.Text.StringBuilder
|
|
foreach ($byte in $bytes) {
|
|
[void]$builder.Append($byte.ToString('x2'))
|
|
}
|
|
return $builder.ToString()
|
|
}
|
|
|
|
$databasePassword = New-HexSecret 32
|
|
$values = @{
|
|
'postgres_password' = $databasePassword
|
|
'database_url' = "postgresql://messenger:$databasePassword@postgres:5432/messenger"
|
|
'minio_root_user' = 'root-' + (New-HexSecret 6)
|
|
'minio_root_password' = New-HexSecret 32
|
|
's3_access_key_id' = New-HexSecret 10
|
|
's3_secret_access_key' = New-HexSecret 32
|
|
}
|
|
foreach ($name in $names) {
|
|
$path = Join-Path $secrets $name
|
|
[System.IO.File]::WriteAllText(
|
|
$path,
|
|
$values[$name],
|
|
[System.Text.Encoding]::ASCII
|
|
)
|
|
}
|
|
Write-Output 'Created local Compose secrets. Their values were not printed.'
|