Skip to content

Commit

Permalink
Fix for removing credentials, issue #211
Browse files Browse the repository at this point in the history
  • Loading branch information
ahaydon committed Nov 1, 2023
1 parent 88af203 commit 24f0b09
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
16 changes: 13 additions & 3 deletions resources/dataconnection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/dataconnection.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24f0b09

Please sign in to comment.