Files
homelabstack/scripts/new-registry-auth.ps1
T

38 lines
1.3 KiB
PowerShell

[CmdletBinding()]
param(
[string]$OutputDirectory = (Join-Path $env:TEMP "valhalla-registry-auth"),
[string]$PushUser = "registry_push",
[string]$PullUser = "registry_pull"
)
$ErrorActionPreference = "Stop"
function New-Secret {
$bytes = New-Object byte[] 32
[System.Security.Cryptography.RandomNumberGenerator]::Fill($bytes)
return [Convert]::ToBase64String($bytes).TrimEnd("=")
}
New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null
$pushPassword = New-Secret
$pullPassword = New-Secret
$htpasswdPath = Join-Path $OutputDirectory "htpasswd"
$envPath = Join-Path $OutputDirectory "registry.env"
$pushLine = docker run --rm --entrypoint htpasswd httpd:2 -Bbn $PushUser $pushPassword
$pullLine = docker run --rm --entrypoint htpasswd httpd:2 -Bbn $PullUser $pullPassword
Set-Content -LiteralPath $htpasswdPath -NoNewline -Value ($pushLine + "`n" + $pullLine + "`n")
Set-Content -LiteralPath $envPath -NoNewline -Value @"
REGISTRY_PUSH_USERNAME=$PushUser
REGISTRY_PUSH_PASSWORD=$pushPassword
REGISTRY_PULL_USERNAME=$PullUser
REGISTRY_PULL_PASSWORD=$pullPassword
"@
Write-Host "Created:"
Write-Host " $htpasswdPath"
Write-Host " $envPath"
Write-Host "Append registry.env values to .env, then copy htpasswd to valhalla:/config/registry/auth/htpasswd."