diff --git a/Functions/Helpers.ps1 b/Functions/Helpers.ps1 index 5a259d7..bad7235 100644 --- a/Functions/Helpers.ps1 +++ b/Functions/Helpers.ps1 @@ -286,7 +286,7 @@ function InvokeNetboxRequest { [switch]$Raw ) - $creds = Get-NetboxCredentials + $creds = Get-NetboxCredential $Headers.Authorization = "Token {0}" -f $creds.GetNetworkCredential().Password diff --git a/Functions/Setup.ps1 b/Functions/Setup.ps1 index 38de303..c3d6a23 100644 --- a/Functions/Setup.ps1 +++ b/Functions/Setup.ps1 @@ -36,7 +36,7 @@ function GetNetboxConfigVariable { } function Set-NetboxHostName { - [CmdletBinding(ConfirmImpact = 'Medium', + [CmdletBinding(ConfirmImpact = 'Low', SupportsShouldProcess = $true)] [OutputType([string])] param @@ -45,8 +45,10 @@ function Set-NetboxHostName { [string]$Hostname ) - $script:NetboxConfig.Hostname = $Hostname.Trim() - $script:NetboxConfig.Hostname + if ($PSCmdlet.ShouldProcess('Netbox Hostname', 'Set')) { + $script:NetboxConfig.Hostname = $Hostname.Trim() + $script:NetboxConfig.Hostname + } } function Get-NetboxHostname { @@ -61,50 +63,49 @@ function Get-NetboxHostname { $script:NetboxConfig.Hostname } -function Set-NetboxCredentials { +function Set-NetboxCredential { [CmdletBinding(DefaultParameterSetName = 'CredsObject', - ConfirmImpact = 'Medium', + ConfirmImpact = 'Low', SupportsShouldProcess = $true)] - [OutputType([pscredential], ParameterSetName = 'CredsObject')] - [OutputType([pscredential], ParameterSetName = 'UserPass')] [OutputType([pscredential])] param ( [Parameter(ParameterSetName = 'CredsObject', Mandatory = $true)] - [pscredential]$Credentials, + [pscredential]$Credential, [Parameter(ParameterSetName = 'UserPass', Mandatory = $true)] - [string]$Token + [securestring]$Token ) - switch ($PsCmdlet.ParameterSetName) { - 'CredsObject' { - $script:NetboxConfig.Credentials = $Credentials - break + if ($PSCmdlet.ShouldProcess('Netbox Credentials', 'Set')) { + switch ($PsCmdlet.ParameterSetName) { + 'CredsObject' { + $script:NetboxConfig.Credential = $Credential + break + } + + 'UserPass' { + $script:NetboxConfig.Credential = [System.Management.Automation.PSCredential]::new('notapplicable', $Token) + break + } } - 'UserPass' { - $securePW = ConvertTo-SecureString $Token -AsPlainText -Force - $script:NetboxConfig.Credentials = [System.Management.Automation.PSCredential]::new('notapplicable', $securePW) - break - } + $script:NetboxConfig.Credential } - - $script:NetboxConfig.Credentials } -function Get-NetboxCredentials { +function Get-NetboxCredential { [CmdletBinding()] [OutputType([pscredential])] param () - if (-not $script:NetboxConfig.Credentials) { - throw "Netbox Credentials not set! You may set with Set-NetboxCredentials" + if (-not $script:NetboxConfig.Credential) { + throw "Netbox Credentials not set! You may set with Set-NetboxCredential" } - $script:NetboxConfig.Credentials + $script:NetboxConfig.Credential } function VerifyAPIConnectivity { @@ -153,7 +154,7 @@ function Connect-NetboxAPI { if (-not $Credentials) { try { - $Credentials = Get-NetboxCredentials -ErrorAction Stop + $Credentials = Get-NetboxCredential -ErrorAction Stop } catch { # Credentials are not set... Try to obtain from the user if (-not ($Credentials = Get-Credential -UserName 'username-not-applicable' -Message "Enter token for Netbox")) { @@ -163,7 +164,7 @@ function Connect-NetboxAPI { } $null = Set-NetboxHostName -Hostname $Hostname - $null = Set-NetboxCredentials -Credentials $Credentials + $null = Set-NetboxCredential -Credential $Credentials try { Write-Verbose "Verifying API connectivity..." diff --git a/Tests/Helpers.Tests.ps1 b/Tests/Helpers.Tests.ps1 index 331da0f..7195980 100644 --- a/Tests/Helpers.Tests.ps1 +++ b/Tests/Helpers.Tests.ps1 @@ -162,7 +162,7 @@ Describe "Helpers tests" -Tag 'Core', 'Helpers' -Fixture { } } - Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith { + Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith { return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) } diff --git a/Tests/IPAM.Tests.ps1 b/Tests/IPAM.Tests.ps1 index fd21b2d..942ed56 100644 --- a/Tests/IPAM.Tests.ps1 +++ b/Tests/IPAM.Tests.ps1 @@ -38,7 +38,7 @@ Describe -Name "IPAM tests" -Tag 'Ipam' -Fixture { } } - Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith { + Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith { return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) } diff --git a/Tests/Setup.Tests.ps1 b/Tests/Setup.Tests.ps1 index 72143b3..664f3aa 100644 --- a/Tests/Setup.Tests.ps1 +++ b/Tests/Setup.Tests.ps1 @@ -34,17 +34,17 @@ Describe "Setup tests" -Tag 'Core', 'Setup' -Fixture { } It "Throws an error for empty credentials" { - { Get-NetboxCredentials } | Should -Throw + { Get-NetboxCredential } | Should -Throw } Context "Plain text credentials" { It "Sets the credentials using plain text" { - $Creds = Set-NetboxCredentials -Token "faketoken" | Should -BeOfType [pscredential] + Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText) | Should -BeOfType [pscredential] } It "Checks the set credentials" { - $Creds = Set-NetboxCredentials -Token "faketoken" - (Get-NetboxCredentials).GetNetworkCredential().Password | Should -BeExactly "faketoken" + $Creds = Set-NetboxCredential -Token (ConvertTo-SecureString -String "faketoken" -Force -AsPlainText) + (Get-NetboxCredential).GetNetworkCredential().Password | Should -BeExactly "faketoken" } } @@ -52,11 +52,11 @@ Describe "Setup tests" -Tag 'Core', 'Setup' -Fixture { $Creds = [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) It "Sets the credentials using [pscredential]" { - Set-NetboxCredentials -Credentials $Creds | Should -BeOfType [pscredential] + Set-NetboxCredential -Credential $Creds | Should -BeOfType [pscredential] } It "Checks the set credentials" { - (Get-NetboxCredentials).GetNetworkCredential().Password | Should -BeExactly 'faketoken' + (Get-NetboxCredential).GetNetworkCredential().Password | Should -BeExactly 'faketoken' } } diff --git a/Tests/Virtualization.Tests.ps1 b/Tests/Virtualization.Tests.ps1 index 83b587a..bdfff70 100644 --- a/Tests/Virtualization.Tests.ps1 +++ b/Tests/Virtualization.Tests.ps1 @@ -37,7 +37,7 @@ Describe -Name "Virtualization tests" -Tag 'Virtualization' -Fixture { } } - Mock -CommandName 'Get-NetboxCredentials' -Verifiable -ModuleName 'NetboxPS' -MockWith { + Mock -CommandName 'Get-NetboxCredential' -Verifiable -ModuleName 'NetboxPS' -MockWith { return [PSCredential]::new('notapplicable', (ConvertTo-SecureString -String "faketoken" -AsPlainText -Force)) }