From e293d7e365ab79e61b2898023fccab9ffeac79d6 Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Thu, 23 Dec 2021 14:24:17 -0500 Subject: [PATCH 1/2] 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 From 05d2016ff0b401958e7e23543e8ed64ce6bb5bf0 Mon Sep 17 00:00:00 2001 From: Brian Wuchner Date: Thu, 23 Dec 2021 19:15:22 -0500 Subject: [PATCH 2/2] Update SaltStackConfig.psm1 Minor update to add examples of the new functionality to the help in the function. Signed-off-by: Brian Wuchner --- Modules/SaltStackConfig/SaltStackConfig.psm1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Modules/SaltStackConfig/SaltStackConfig.psm1 b/Modules/SaltStackConfig/SaltStackConfig.psm1 index 0842470..cf2d5f8 100644 --- a/Modules/SaltStackConfig/SaltStackConfig.psm1 +++ b/Modules/SaltStackConfig/SaltStackConfig.psm1 @@ -22,6 +22,13 @@ Function Connect-SscServer { .EXAMPLE PS C:\> Connect-SscServer -Server 'salt.example.com' -Username 'bwuchner' -Password 'MyPassword1!' -AuthSource 'LAB Directory' This will use the 'Lab Directory' LDAP authentication source. + .EXAMPLE + PS C:\> Connect-SscServer -Server 'salt.example.com' + This will prompt for credentials + .EXAMPLE + $creds = Get-Credential + PS C:\> Connect-SscServer -Server 'salt.example.com' -Credential $creds -AuthSource 'LAB Directory' + This will connect to the 'LAB Directory' LDAP authentication source using a specified credential. #> param( [Parameter(Mandatory=$true, Position=0)][string]$server,