forked from mtusi2026/mtusi2026lab5
Complete Lab 5 grade 4 async attachment pipeline
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$repo = Split-Path -Parent $PSScriptRoot
|
||||
$secretDir = Join-Path $repo 'secrets'
|
||||
New-Item -ItemType Directory -Path $secretDir -Force | Out-Null
|
||||
function New-Secret {
|
||||
$bytes = New-Object byte[] 32
|
||||
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
|
||||
return [Convert]::ToBase64String($bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_')
|
||||
}
|
||||
|
||||
function New-AccessKey {
|
||||
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
|
||||
$bytes = New-Object byte[] 20
|
||||
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
|
||||
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
|
||||
$value = -join ($bytes | ForEach-Object { $alphabet[($_ % $alphabet.Length)] })
|
||||
return $value
|
||||
}
|
||||
|
||||
function Read-Secret([string]$name) {
|
||||
$path = Join-Path $secretDir $name
|
||||
if (-not (Test-Path -LiteralPath $path)) { return $null }
|
||||
return [System.IO.File]::ReadAllText($path).Trim()
|
||||
}
|
||||
|
||||
function Write-Secret([string]$name, [string]$value) {
|
||||
$path = Join-Path $secretDir $name
|
||||
[System.IO.File]::WriteAllText($path, $value, [System.Text.UTF8Encoding]::new($false))
|
||||
}
|
||||
|
||||
function Ensure-Pair([string]$first, [string]$second, [scriptblock]$newFirst, [scriptblock]$newSecond) {
|
||||
$left = Read-Secret $first
|
||||
$right = Read-Secret $second
|
||||
if (($null -eq $left) -ne ($null -eq $right)) {
|
||||
throw "Only one file of credential pair '$first'/'$second' exists. Resolve it manually; credentials are never rotated automatically."
|
||||
}
|
||||
if ($null -eq $left) {
|
||||
$left = & $newFirst
|
||||
$right = & $newSecond $left
|
||||
Write-Secret $first $left
|
||||
Write-Secret $second $right
|
||||
}
|
||||
return @($left, $right)
|
||||
}
|
||||
|
||||
$pg = Ensure-Pair 'postgres_password' 'database_url' { New-Secret } {
|
||||
param($password)
|
||||
"postgresql+psycopg://messenger:$password@db:5432/messenger"
|
||||
}
|
||||
$redis = Ensure-Pair 'redis_password' 'cache_url' { New-Secret } {
|
||||
param($password)
|
||||
"redis://:$password@redis:6379/0"
|
||||
}
|
||||
$broker = Ensure-Pair 'broker_password' 'broker_url' { New-Secret } {
|
||||
param($password)
|
||||
"redis://:$password@broker:6379/0"
|
||||
}
|
||||
$s3root = Ensure-Pair 's3_root_access_key_id' 's3_root_secret_access_key' { New-AccessKey } { New-Secret }
|
||||
$s3api = Ensure-Pair 's3_access_key_id' 's3_secret_access_key' { New-AccessKey } { New-Secret }
|
||||
Write-Output 'Local Postgres, Redis sessions, Redis Streams broker, RustFS root and limited API credentials are ready under secrets/. Values were not displayed.'
|
||||
Reference in New Issue
Block a user