From 24f0b097fd7241a94d5f654ff59a8986f8f24af1 Mon Sep 17 00:00:00 2001 From: Adam Haydon Date: Wed, 1 Nov 2023 11:31:50 +0000 Subject: [PATCH] Fix for removing credentials, issue #211 --- resources/dataconnection.ps1 | 16 ++++++++++++--- tests/unit/dataconnection.tests.ps1 | 30 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/resources/dataconnection.ps1 b/resources/dataconnection.ps1 index 88f5d4c..50252a1 100644 --- a/resources/dataconnection.ps1 +++ b/resources/dataconnection.ps1 @@ -135,13 +135,23 @@ function Update-QlikDataConnection { If ($PSBoundParameters.ContainsKey("ConnectionString")) { $qdc.connectionstring = $ConnectionString } - if ( $Credential ) { + if ($PSBoundParameters.ContainsKey("Credential")) { + if ($null -eq $Credential) { + $Credential = [System.Management.Automation.PSCredential]::Empty + } + if ($Credential.Password -is [System.Security.SecureString]) { + $password = $Credential.GetNetworkCredential().Password + } + else { + $password = '' + } + $qdc.username = $Credential.Username if ($qdc.psobject.Properties.name -contains "password") { - $qdc.password = $Credential.GetNetworkCredential().Password + $qdc.password = $password } else { - $qdc | Add-Member -MemberType NoteProperty -Name "password" -Value $($Credential.GetNetworkCredential().Password) + $qdc | Add-Member -MemberType NoteProperty -Name "password" -Value $password } } if ($PSBoundParameters.ContainsKey("customProperties")) { $qdc.customProperties = @(GetCustomProperties $customProperties $qdc.customProperties) } diff --git a/tests/unit/dataconnection.tests.ps1 b/tests/unit/dataconnection.tests.ps1 index b60021c..1a9a8f5 100644 --- a/tests/unit/dataconnection.tests.ps1 +++ b/tests/unit/dataconnection.tests.ps1 @@ -122,6 +122,36 @@ Describe "Update-QlikDataConnection" { "@ | ConvertFrom-Json } + Context 'Credential' { + It 'should be removed when a null value is provided' { + $dc = Update-QlikDataConnection ` + -id '158e743b-c59f-490e-900c-b57e66cf8185' ` + -Credential $null + + $dc.PSObject.Properties.Name | Should -Contain username + $dc.PSObject.Properties.Name | Should -Contain password + $dc.username | Should -BeNullOrEmpty + $dc.password | Should -BeNullOrEmpty + + Assert-VerifiableMock + } + + It 'should be removed when an empty credential is provided' { + $password = New-Object System.Security.SecureString + $credential = [System.Management.Automation.PSCredential]::Empty + $dc = Update-QlikDataConnection ` + -id '158e743b-c59f-490e-900c-b57e66cf8185' ` + -Credential $credential + + $dc.PSObject.Properties.Name | Should -Contain username + $dc.PSObject.Properties.Name | Should -Contain password + $dc.username | Should -BeNullOrEmpty + $dc.password | Should -BeNullOrEmpty + + Assert-VerifiableMock + } + } + Context 'Password' { It 'should be updated when a credential is provided' { $password = ConvertTo-SecureString -String 'password' -AsPlainText -Force