From e293d7e365ab79e61b2898023fccab9ffeac79d6 Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Thu, 23 Dec 2021 14:24:17 -0500 Subject: [PATCH] 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 --- Modules/SaltStackConfig/SaltStackConfig.psm1 | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 index 007f74d..0842470 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psm1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -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