From e934d20cedcf07f9c411d308d97f79e32c016647 Mon Sep 17 00:00:00 2001 From: Petri Asikainen Date: Sun, 22 Aug 2021 22:09:34 +0300 Subject: [PATCH] Keep it simple ... --- .../Private/Test-SnipeitPSConnection.ps1 | 17 ++++ SnipeitPS/Public/Connect-Snipeit.ps1 | 73 ++++++++------- SnipeitPS/Public/Connect-SnipeitPS.ps1 | 90 +++++++++++++++++++ 3 files changed, 142 insertions(+), 38 deletions(-) create mode 100644 SnipeitPS/Private/Test-SnipeitPSConnection.ps1 create mode 100644 SnipeitPS/Public/Connect-SnipeitPS.ps1 diff --git a/SnipeitPS/Private/Test-SnipeitPSConnection.ps1 b/SnipeitPS/Private/Test-SnipeitPSConnection.ps1 new file mode 100644 index 0000000..63e5064 --- /dev/null +++ b/SnipeitPS/Private/Test-SnipeitPSConnection.ps1 @@ -0,0 +1,17 @@ +function Test-SnipeitPSConnection { + #test api connection + $Parameters = @{ + Api = '/api/v1/statuslabels' + Method = 'Get' + GetParameters = @{'limit'=1} + } + Write-Verbose "Testing connection to $url." + + $contest = Invoke-SnipeitMethod @Parameters + if ( $contest) { + Write-Verbose "Connection to $url tested succesfully." + return $true + } else { + return $false + } +} diff --git a/SnipeitPS/Public/Connect-Snipeit.ps1 b/SnipeitPS/Public/Connect-Snipeit.ps1 index 99f9c98..81505a3 100644 --- a/SnipeitPS/Public/Connect-Snipeit.ps1 +++ b/SnipeitPS/Public/Connect-Snipeit.ps1 @@ -18,73 +18,70 @@ .EXAMPLE Connect-SnipeitPS -Url $url -apiKey $myapikey - Connect to Snipe it api and stores connection information. + Connect to Snipe it api. .EXAMPLE - Connect-SnipeitPS -Url $url -apiKey $myapikey -DontStore - Just connects to Snipe it api, connection information is not stored. + Connect-SnipeitPS -Url $url -SecureApiKey $myapikey + Connects to Snipe it api with apikey stored to securestring .EXAMPLE - Connect-SnipeitPS -Url $url - Connects existing Snipe It Url with stored apiKey + Connect-SnipeitPS -siteCred (Get-Credential -message "Use site url as username and apikey as password") + Connect to Snipe It with PSCredential object .EXAMPLE - Connect-SnipeitPS - Connects last used Snipe It Url with stored apikey + Build credential with apiakay value from secret vault (Microsoft.PowerShell.SecretManagement) + $siteurl = "https://mysnipeitsite.url" + $apikey = Get-SecretInfo -Name SnipeItApiKey + $siteCred = New-Object -Type PSCredential -Argumentlist $siteurl,$spikey + Connect-SnipeitPS -siteCred $siteCred + #> function Connect-SnipeitPS { [CmdletBinding( - DefaultParameterSetName = 'Connect to existing connection' + DefaultParameterSetName = 'Connect with url and apikey' )] [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')] param ( - [Parameter(ParameterSetName='Setup new connection',Mandatory=$true)] - [Parameter(ParameterSetName='Connect to existing connection',Mandatory=$false)] + [Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$true)] + [Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$true)] [Uri]$url, - [Parameter(ParameterSetName='Setup new connection',Mandatory=$true)] + [Parameter(ParameterSetName='Connect with url and apikey',Mandatory=$true)] [String]$apiKey, - [Parameter(ParameterSetName='Setup new connection')] - [switch]$DontStore + [Parameter(ParameterSetName='Connect with url and secure apikey',Mandatory=$true)] + [SecureString]$SecureApiKey, + + + [Parameter(ParameterSetName='Connect with credential',Mandatory=$true)] + [PSCredertial]$siteCred + ) PROCESS { switch ($PsCmdlet.ParameterSetName) { - 'Setup new connection' { - try { + 'Connect with url and apikey' { $SnipeitPSSession.url = $url - $SnipeitPSSession.apiKey = $apiKey - - #test connection - $Parameters = @{ - Api = '/api/v1/statuslabels' - Method = 'Get' - GetParameters = @{'limit'=1} - } - Write-Verbose "Testin connection to $url." - - $contest = Invoke-SnipeitMethod @Parameters - if ( $contest) { - Write-Verbose "Connection to $url tested succesfully." - } - - } - catch { - throw "Cannot setup connection to $url. To start troubleshooting, check your url, certificates and apiKey" - } - # TODO: Save connection information safely on disk - + $SnipeitPSSession.apiKey = $apiKey | ConvertTo-SecureString -AsPlainText } - 'Connect to existing connection' { - # TODO: everything + 'Connect with url and secure apikey' { + $SnipeitPSSession.url = $url + $SnipeitPSSession.apiKey = $secureApiKey } + + 'Connect with credential' { + $SnipeitPSSession.url = $siteCred.Username + $SnipeitPSSession.apiKey = $siteCred.GetNetworkCredential().SecurePassword + } + } + if (-not (Test-SnipeitPSConnection)) { + throw "Cannot verify connection to snipe it. For the start check url and provided apikey" } } } diff --git a/SnipeitPS/Public/Connect-SnipeitPS.ps1 b/SnipeitPS/Public/Connect-SnipeitPS.ps1 new file mode 100644 index 0000000..99f9c98 --- /dev/null +++ b/SnipeitPS/Public/Connect-SnipeitPS.ps1 @@ -0,0 +1,90 @@ +<# + .SYNOPSIS + Sets authetication information + + .DESCRIPTION + Set and stores apikey and url user to connect Snipe-It system. + Based on Set-SnipeitInfo command, that's now just combatipility wrapper + and calls Connect-SnipeitPS + + .PARAMETER url + URL of Snipeit system. + + .PARAMETER apiKey + User's API Key for Snipeit. + + .PARAMETER DontStore + Don't store connection information just connect to Url + + .EXAMPLE + Connect-SnipeitPS -Url $url -apiKey $myapikey + Connect to Snipe it api and stores connection information. + + .EXAMPLE + Connect-SnipeitPS -Url $url -apiKey $myapikey -DontStore + Just connects to Snipe it api, connection information is not stored. + + .EXAMPLE + Connect-SnipeitPS -Url $url + Connects existing Snipe It Url with stored apiKey + + .EXAMPLE + Connect-SnipeitPS + Connects last used Snipe It Url with stored apikey + + +#> +function Connect-SnipeitPS { + [CmdletBinding( + DefaultParameterSetName = 'Connect to existing connection' + )] + [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')] + + param ( + + [Parameter(ParameterSetName='Setup new connection',Mandatory=$true)] + [Parameter(ParameterSetName='Connect to existing connection',Mandatory=$false)] + [Uri]$url, + + [Parameter(ParameterSetName='Setup new connection',Mandatory=$true)] + [String]$apiKey, + + [Parameter(ParameterSetName='Setup new connection')] + [switch]$DontStore + ) + + + PROCESS { + switch ($PsCmdlet.ParameterSetName) { + 'Setup new connection' { + try { + $SnipeitPSSession.url = $url + $SnipeitPSSession.apiKey = $apiKey + + #test connection + $Parameters = @{ + Api = '/api/v1/statuslabels' + Method = 'Get' + GetParameters = @{'limit'=1} + } + Write-Verbose "Testin connection to $url." + + $contest = Invoke-SnipeitMethod @Parameters + if ( $contest) { + Write-Verbose "Connection to $url tested succesfully." + } + + } + catch { + throw "Cannot setup connection to $url. To start troubleshooting, check your url, certificates and apiKey" + } + # TODO: Save connection information safely on disk + + } + + 'Connect to existing connection' { + # TODO: everything + } + } + } +}