Update SaltStackConfig.psm1

Improve Connect-SscServer to accept credentials instead of just plaintext username/password values.

We will make the PlainText parameter set items mandatory, so if you use this parameter set both values need to be provided.
However, if you don't specify any credentials at all as arguments, we will default to the optional Credential parameter set.  When the credential parameter set is used but the credential value is null, we will prompt for credentials using Get-Credential.

Signed-off-by: Brian Wuchner <brian.wuchner@gmail.com>
This commit is contained in:
Brian Wuchner
2021-12-23 14:24:17 -05:00
parent 1d96b6a340
commit e293d7e365

View File

@@ -24,11 +24,18 @@ Function Connect-SscServer {
This will use the 'Lab Directory' LDAP authentication source.
#>
param(
[Parameter(Mandatory=$true)][string]$server,
[Parameter(Mandatory=$true)][string]$username,
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][string]$password,
[string]$AuthSource='internal'
[Parameter(Mandatory=$true, Position=0)][string]$server,
[Parameter(Mandatory=$true, ParameterSetName='PlainText', Position=1)][string]$username,
[Parameter(Mandatory=$true, ParameterSetName='PlainText', Position=2)][ValidateNotNullOrEmpty()][string]$password,
[Parameter(Mandatory=$false, Position=3)][string]$AuthSource='internal',
[Parameter(Mandatory=$false, ParameterSetName='Credential')][PSCredential]$Credential
)
if ($PSCmdlet.ParameterSetName -eq 'Credential' -AND $Credential -eq $null) { $Credential = Get-Credential}
if ($Credential) {
$username = $Credential.GetNetworkCredential().username
$password = $Credential.GetNetworkCredential().password
}
$loginBody = @{'username'=$username; 'password'=$password; 'config_name'=$AuthSource}
try {
@@ -37,7 +44,7 @@ Function Connect-SscServer {
$webRequest = Invoke-WebRequest -Uri "https://$server/account/login" -WebSession $ws -method POST -body (ConvertTo-Json $loginBody)
$webRequestJson = ConvertFrom-JSON $webRequest.Content
$global:DefaultSscConnection = New-Object psobject -property @{ 'SscWebSession'=$ws; 'Name'=$server; 'ConnectionDetail'=$webRequestJson;
'User'=$webRequestJson.attributes.config_driver +'\'+ $username; 'Authenticated'=$webRequestJson.authenticated; PSTypeName='SscConnection' }
'User'=$webRequestJson.attributes.config_name +'\'+ $username; 'Authenticated'=$webRequestJson.authenticated; PSTypeName='SscConnection' }
# Return the connection object
$global:DefaultSscConnection