mirror of
https://github.com/containers/podman.git
synced 2025-08-06 19:44:14 +08:00
Add passwd validate and generate steps
Add generate helper function. Also, add a troubleshooting try/catch block in case we get more flakes during Set-LocalUser step in Windows powershell. Resolves: https://github.com/containers/podman/issues/23468 Signed-off-by: Nicola Sella <nsella@redhat.com>
This commit is contained in:
@ -2,24 +2,52 @@
|
|||||||
# tunnels the output such that WSL operations will complete
|
# tunnels the output such that WSL operations will complete
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
|
$syms = [char[]]([char]'a'..[char]'z') +
|
||||||
|
[char[]]([char]'A'..[char]'Z') +
|
||||||
|
[char[]]([char]'0'..[char]'9')
|
||||||
|
|
||||||
if ($Args.Length -lt 1) {
|
if ($Args.Length -lt 1) {
|
||||||
Write-Object "Usage: " + $MyInvocation.MyCommand.Name + " <script>"
|
Write-Object "Usage: " + $MyInvocation.MyCommand.Name + " <script>"
|
||||||
Exit 1;
|
Exit 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function RegenPassword {
|
|
||||||
param($username)
|
# Shuffle a 32 character password from $syms
|
||||||
$syms = [char[]]([char]'a'..[char]'z' `
|
function GenerateRandomPassword {
|
||||||
+ [char]'A'..[char]'Z' `
|
param(
|
||||||
+ [char]'0'..[char]'9')
|
[int]$length = 32
|
||||||
$rnd = [byte[]]::new(32)
|
)
|
||||||
|
|
||||||
|
$rnd = [byte[]]::new($length)
|
||||||
[System.Security.Cryptography.RandomNumberGenerator]::create().getBytes($rnd)
|
[System.Security.Cryptography.RandomNumberGenerator]::create().getBytes($rnd)
|
||||||
$password = ($rnd | % { $syms[$_ % $syms.length] }) -join ''
|
$password = ($rnd | % { $syms[$_ % $syms.length] }) -join ''
|
||||||
$encPass = ConvertTo-SecureString $password -AsPlainText -Force
|
|
||||||
Set-LocalUser -Name $username -Password $encPass
|
|
||||||
return $password
|
return $password
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function RegenPassword {
|
||||||
|
param($username)
|
||||||
|
|
||||||
|
$maxAttempts = 3
|
||||||
|
for ($attempts = 0; $attempts -lt $maxAttempts; $attempts++) {
|
||||||
|
$password = GenerateRandomPassword
|
||||||
|
|
||||||
|
try{
|
||||||
|
$encPass = ConvertTo-SecureString $password -AsPlainText -Force
|
||||||
|
Set-LocalUser -Name $username -Password $encPass
|
||||||
|
return $password
|
||||||
|
} catch {
|
||||||
|
# In case we catch a flake like here
|
||||||
|
# https://github.com/containers/podman/issues/23468
|
||||||
|
# we want to know what happened and retry.
|
||||||
|
Write-Host "Error generating password."
|
||||||
|
Write-Host "Username: $username"
|
||||||
|
Write-Host "Generated Password: $password"
|
||||||
|
Write-Host "Error Message: $_"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$runScript = $Args[0]
|
$runScript = $Args[0]
|
||||||
$nil > tmpout
|
$nil > tmpout
|
||||||
$cwd = Get-Location
|
$cwd = Get-Location
|
||||||
|
Reference in New Issue
Block a user