internal/configurationvalidation/secret.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Register-PSFConfigValidation -Name "secret" -ScriptBlock {
    param (
        $Value
    )
    
    $Result = New-Object PSObject -Property @{
        Success = $True
        Value   = $null
        Message = ""
    }
    
    if ($null -eq $Value) {
        $Result.Message = "Secrets cannot be empty!"
        $Result.Success = $False
        return $Result
    }
    
    if ($Value.GetType() -notin [string], [System.Security.SecureString], [System.Management.Automation.PSCredential]) {
        $Result.Message = "Secrets must be either a string, a securestring or a pscredential object!"
        $Result.Success = $False
        return $Result
    }
    
    if ($Value -is [string]) {
        $Value = $Value | ConvertTo-SecureString -AsPlainText -Force
    }
    if ($Value -is [System.Security.SecureString]) {
        $Value = New-Object System.Management.Automation.PSCredential('<none>', $Value)
    }
    
    $Result.Value = $Value
    
    return $Result
}