From 4ddd7c140cdf2103ff823e95e1c969ed58b45e93 Mon Sep 17 00:00:00 2001 From: John Klann Date: Mon, 25 Mar 2019 11:07:58 -0400 Subject: [PATCH 1/2] #160 - Configuration Commands - Updated New, Update, Get KPDBConfig functions to support a new -Default parameter. - Added pester tests for new functionality. --- PoShKeePass.psd1 | Bin 9242 -> 9242 bytes Test/PSKeePassUsage.Tests.ps1 | 62 +++++++++++++++++- .../Get-KeePassDatabaseConfiguration.ps1 | 16 ++++- .../New-KeePassDatabaseConfiguration.ps1 | 17 +++++ .../Update-KeePassDatabaseConfiguration.ps1 | 17 +++++ 5 files changed, 108 insertions(+), 4 deletions(-) diff --git a/PoShKeePass.psd1 b/PoShKeePass.psd1 index a8c8d26835ee6abce4e3eda98bfe27e24f283f34..3da87309d2418641c05fd82bb18fea2b2c2fea2d 100644 GIT binary patch delta 18 ZcmbQ`G0S5^3=^Z}WJhN4%_U6B6ahTL1?vC+ delta 16 XcmbQ`G0S5^4AW!>W{J%uOv@AjG}Z;N diff --git a/Test/PSKeePassUsage.Tests.ps1 b/Test/PSKeePassUsage.Tests.ps1 index 6f26999..5c0a848 100644 --- a/Test/PSKeePassUsage.Tests.ps1 +++ b/Test/PSKeePassUsage.Tests.ps1 @@ -119,8 +119,8 @@ InModuleScope "PoShKeePass" { $DatabaseConfiguration.UseNetworkAccount | Should Be $false $DatabaseConfiguration.UseMasterKey | Should Be $false $DatabaseConfiguration.AuthenticationType | Should Be 'Key' + $DatabaseConfiguration.Default | Should Be $false } - break } Context "Example 2: Create a new KeePass Database Configuration Profile - MasterKey" { @@ -198,6 +198,28 @@ InModuleScope "PoShKeePass" { $DatabaseConfiguration.AuthenticationType | Should Be 'Network' } } + + Context "Example 5: Create a new KeePass Database Configuration Profile - Default DB" { + + New-KPConfigurationFile -Force + + It "Example 5.1: Database Configuration Profile - Default - Valid with PassThru" { + $DatabaseConfiguration = New-KeePassDatabaseConfiguration -DatabaseProfileName 'DefaultTestPassThru' -DatabasePath "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" -UseNetworkAccount -Default -PassThru + + $DatabaseConfiguration.Name | Should Be 'DefaultTestPassThru' + $DatabaseConfiguration.DatabasePath | Should Be "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" + $DatabaseConfiguration.KeyPath | Should Be '' + $DatabaseConfiguration.UseNetworkAccount | Should Be $True + $DatabaseConfiguration.UseMasterKey | Should Be $false + $DatabaseConfiguration.AuthenticationType | Should Be 'Network' + $DatabaseConfiguration.Default | Should Be $true + } + + It "Example 5.2: Database Configuration Profile - Default - Invalid Already Default" { + {New-KeePassDatabaseConfiguration -DatabaseProfileName 'DefaultTestFailPassThru' -DatabasePath "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" -UseNetworkAccount -Default -PassThru } | should Throw + # $Error[0].exception.message -ilike 'DefaultTestPassThru profile is already set to the default*' | should be $true + } + } } Describe "Get-KeePassDatabaseConfiguration - UnitTest" -Tag UnitTest { @@ -228,6 +250,20 @@ InModuleScope "PoShKeePass" { $DatabaseConfiguration.UseMasterKey | Should Be 'False' $DatabaseConfiguration.AuthenticationType | Should Be 'Network' } + + It "Example 1.3: Get Database Configuration Profile - Valid - By Default" { + New-KeePassDatabaseConfiguration -DatabaseProfileName 'SampleProfile1' -DatabasePath "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" -UseNetworkAccount -Default | Should Be $null + + $DatabaseConfiguration = Get-KeePassDatabaseConfiguration -Default + + $DatabaseConfiguration.Name | Should Be 'SampleProfile1' + $DatabaseConfiguration.DatabasePath | Should Be "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" + $DatabaseConfiguration.KeyPath | Should Be '' + $DatabaseConfiguration.UseNetworkAccount | Should Be 'True' + $DatabaseConfiguration.UseMasterKey | Should Be 'False' + $DatabaseConfiguration.AuthenticationType | Should Be 'Network' + $DatabaseConfiguration.Default | Should Be $true + } } New-KPConfigurationFile -Force @@ -252,6 +288,30 @@ InModuleScope "PoShKeePass" { } } + Context "Example 2: Update a KeePass Database Configuration Profile - Default DB" { + + New-KPConfigurationFile -Force + + It "Example 2.1: Update Database Configuration Profile - Valid - DefaultDB" { + New-KeePassDatabaseConfiguration -DatabaseProfileName 'SampleProfile' -DatabasePath "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" -UseNetworkAccount | Should Be $null + + $DatabaseConfiguration = Update-KeePassDatabaseConfiguration -DatabaseProfileName 'SampleProfile' -NewDatabaseProfileName 'Updated' -Default -PassThru + + $DatabaseConfiguration.Name | Should Be 'Updated' + $DatabaseConfiguration.DatabasePath | Should Be "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" + $DatabaseConfiguration.KeyPath | Should Be '' + $DatabaseConfiguration.UseNetworkAccount | Should Be 'True' + $DatabaseConfiguration.UseMasterKey | Should Be 'False' + $DatabaseConfiguration.AuthenticationType | Should Be 'Network' + $DatabaseConfiguration.Default | Should Be $true + } + + It "Example 2.2: Update Database Configuration Profile - Valid - Invalid Already Default" { + {New-KeePassDatabaseConfiguration -DatabaseProfileName 'SampleProfile1' -DatabasePath "$($PSScriptRoot)\Includes\AuthenticationDatabases\MasterKey.kdbx" -UseNetworkAccount -Default -PassThru } | should Throw + # $Error[0].exception.message -ilike 'DefaultTestPassThru profile is already set to the default*' | should be $true + } + } + New-KPConfigurationFile -Force } diff --git a/functions/Get-KeePassDatabaseConfiguration.ps1 b/functions/Get-KeePassDatabaseConfiguration.ps1 index 00b01b7..af99332 100644 --- a/functions/Get-KeePassDatabaseConfiguration.ps1 +++ b/functions/Get-KeePassDatabaseConfiguration.ps1 @@ -20,14 +20,18 @@ function Get-KeePassDatabaseConfiguration .OUTPUTS PSObject #> - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = '__None')] param ( - [Parameter(Position = 0)] + [Parameter(Position = 0, ParameterSetName = '__Profile')] [ValidateNotNullOrEmpty()] [String] $DatabaseProfileName, - [Parameter(Position = 1)] + [Parameter(Position = 1, ParameterSetName = '__DefaultDB')] + [ValidateNotNullOrEmpty()] + [Switch] $Default, + + [Parameter(Position = 2)] [Switch] $Stop ) process @@ -41,6 +45,10 @@ function Get-KeePassDatabaseConfiguration { $ProfileResults = $XML.Settings.DatabaseProfiles.Profile | Where-Object { $_.Name -ilike $DatabaseProfileName } } + elseif($Default) + { + $ProfileResults = $XML.Settings.DatabaseProfiles.Profile | Where-Object { $_.Default -ieq 'true' } + } else { $ProfileResults = $XML.Settings.DatabaseProfiles.Profile @@ -55,6 +63,7 @@ function Get-KeePassDatabaseConfiguration { $UseNetworkAccount = if($ProfileResult.UseNetworkAccount -eq 'True'){$true}else{$false} $UseMasterKey = if($ProfileResult.UseMasterKey -eq 'True'){$true}else{$false} + $ProfileDefault = if($ProfileResult.Default -eq 'True'){$true}else{$false} [hashtable] $ProfileObject = [ordered]@{ 'Name' = $ProfileResult.Name; @@ -63,6 +72,7 @@ function Get-KeePassDatabaseConfiguration 'UseMasterKey' = $UseMasterKey; 'UseNetworkAccount' = $UseNetworkAccount; 'AuthenticationType' = $ProfileResult.AuthenticationType; + 'Default' = $ProfileDefault; } New-Object -TypeName PSObject -Property $ProfileObject diff --git a/functions/New-KeePassDatabaseConfiguration.ps1 b/functions/New-KeePassDatabaseConfiguration.ps1 index 71a2bb2..10c6f07 100644 --- a/functions/New-KeePassDatabaseConfiguration.ps1 +++ b/functions/New-KeePassDatabaseConfiguration.ps1 @@ -65,6 +65,9 @@ function New-KeePassDatabaseConfiguration [Switch] $UseMasterKey, [Parameter(Position = 5)] + [Switch] $Default, + + [Parameter(Position = 6)] [Switch] $PassThru ) begin @@ -97,6 +100,16 @@ function New-KeePassDatabaseConfiguration { try { + if($Default) + { + $defaultProfile = Get-KeePassDatabaseConfiguration -Default + + if($defaultProfile) + { + throw ('{0} profile is already set to the default, if you would like to overwrite it as the default please use the Update-KeePassDatabaseConfiguration function and remove the default flag.' -f $defaultProfile.Name) + } + } + [Xml] $XML = New-Object -TypeName System.Xml.XmlDocument $XML.Load($Global:KeePassConfigurationFile) ## Create New Profile Element with Name of the new profile @@ -126,6 +139,10 @@ function New-KeePassDatabaseConfiguration $AuthenticationTypeNode.InnerText = $PSCmdlet.ParameterSetName $DatabaseProfile.AppendChild($AuthenticationTypeNode) | Out-Null + $DefaultNode = $XML.CreateNode('element', 'Default', '') + $DefaultNode.InnerText = $Default + $DatabaseProfile.AppendChild($DefaultNode) | Out-Null + $XML.SelectSingleNode('/Settings/DatabaseProfiles').AppendChild($DatabaseProfile) | Out-Null $XML.Save($Global:KeePassConfigurationFile) diff --git a/functions/Update-KeePassDatabaseConfiguration.ps1 b/functions/Update-KeePassDatabaseConfiguration.ps1 index 6358143..3098c19 100644 --- a/functions/Update-KeePassDatabaseConfiguration.ps1 +++ b/functions/Update-KeePassDatabaseConfiguration.ps1 @@ -69,6 +69,9 @@ function Update-KeePassDatabaseConfiguration [Switch] $UseMasterKey, [Parameter(Position = 6)] + [Switch] $Default, + + [Parameter(Position = 7)] [Switch] $PassThru ) begin @@ -101,6 +104,16 @@ function Update-KeePassDatabaseConfiguration } else { + if($Default) + { + $defaultProfile = Get-KeePassDatabaseConfiguration -Default + + if($defaultProfile -and $defaultProfile.Name -ine $DatabaseProfileName) + { + throw ('{0} profile is already set to the default, if you would like to overwrite it as the default please use the Update-KeePassDatabaseConfiguration function and remove the default flag.' -f $defaultProfile.Name) + } + } + try { [Xml] $XML = New-Object -TypeName System.Xml.XmlDocument @@ -142,6 +155,10 @@ function Update-KeePassDatabaseConfiguration $AuthenticationTypeNode.InnerText = $_AuthenticationType $DatabaseProfile.AppendChild($AuthenticationTypeNode) | Out-Null + $DefaultNode = $XML.CreateNode('element', 'Default', '') + $DefaultNode.InnerText = $Default + $DatabaseProfile.AppendChild($DefaultNode) | Out-Null + $XML.SelectSingleNode('/Settings/DatabaseProfiles').ReplaceChild($DatabaseProfile, $OldProfile) | Out-Null $XML.Save($Global:KeePassConfigurationFile) From be614864b282df002ae084a8974e64650ac46610 Mon Sep 17 00:00:00 2001 From: John Klann Date: Mon, 25 Mar 2019 12:11:12 -0400 Subject: [PATCH 2/2] #160 - Updated core commands to support default config - fixed error handling when no default profile and -stop parameter passed - added support to New-KPConnection for default profile - Updated exported commands to no longer require -DatabaseProfileName parameter. - Added tests for all changes - Updated documentation --- PoShKeePass.psd1 | Bin 9242 -> 9242 bytes Test/PSKeePassUsage.Tests.ps1 | 141 ++++++++++++++---- changelog.md | 22 +++ .../Get-KeePassDatabaseConfiguration.ps1 | 5 + functions/Get-KeePassEntry.ps1 | 2 +- functions/Get-KeePassGroup.ps1 | 2 +- functions/New-KeePassEntry.ps1 | 2 +- functions/New-KeePassGroup.ps1 | 2 +- functions/Remove-KeePassEntry.ps1 | 2 +- functions/Remove-KeePassGroup.ps1 | 2 +- functions/Update-KeePassEntry.ps1 | 2 +- functions/Update-KeePassGroup.ps1 | 2 +- internal/New-KPConnection.ps1 | 15 +- readme.md | 41 ++--- 14 files changed, 183 insertions(+), 57 deletions(-) diff --git a/PoShKeePass.psd1 b/PoShKeePass.psd1 index 3da87309d2418641c05fd82bb18fea2b2c2fea2d..d25a232eec5753d55ed924e90a62b3c768e16101 100644 GIT binary patch delta 22 dcmbQ`G0S5^1QV+ - [CmdletBinding(DefaultParameterSetName = 'Profile')] + [CmdletBinding(DefaultParameterSetName = '__None')] param ( - [Parameter(Position = 0, Mandatory, ParameterSetName = 'Profile')] - [ValidateNotNullOrEmpty()] + [Parameter(Position = 0, ParameterSetName = 'Profile')] + [AllowNull()] [String] $DatabaseProfileName, [Parameter(Position = 0, Mandatory, ParameterSetName = 'CompositeKey')] @@ -57,9 +57,14 @@ function New-KPConnection Write-Error -Message ('[PROCESS] The MasterKey of type: ({0}). Is not Supported Please supply a MasterKey of Types (SecureString or PSCredential).' -f $($MasterKey.GetType().Name)) -Category InvalidType -TargetObject $MasterKey -RecommendedAction 'Provide a MasterKey of Type PSCredential or SecureString' } - if($PSCmdlet.ParameterSetName -eq 'Profile') + if($PSCmdlet.ParameterSetName -eq 'Profile' -or $PSCmdlet.ParameterSetName -eq '__None') { - $KeepassConfigurationObject = Get-KeePassDatabaseConfiguration -DatabaseProfileName $DatabaseProfileName -Stop + ## if not passing a profile name, attempt to get the default db + $getKeePassDatabaseConfigurationSplat = @{ Stop = $true } + if($DatabaseProfileName){ $getKeePassDatabaseConfigurationSplat.DatabaseProfileName = $DatabaseProfileName } + else{ $getKeePassDatabaseConfigurationSplat.Default = $true } + + $KeepassConfigurationObject = Get-KeePassDatabaseConfiguration @getKeePassDatabaseConfigurationSplat $Database = $KeepassConfigurationObject.DatabasePath if(-not [string]::IsNullOrEmpty($KeepassConfigurationObject.KeyPath)){ $KeyPath = $KeepassConfigurationObject.KeyPath } diff --git a/readme.md b/readme.md index 27238e9..65df6ee 100644 --- a/readme.md +++ b/readme.md @@ -31,6 +31,28 @@ Please check out our [Getting Started](https://github.com/PSKeePass/PoShKeePass/ Please review the [changelog document](https://github.com/PSKeePass/PoShKeePass/blob/master/changelog.md) for a full history. +## v.2.1.3.0 + +* Added [#160](https://github.com/PSKeePass/PoShKeePass/issues/160) - Default Database Configuration Profile. + * When set, the `-DatabaseProfileName` parameter is optional, and if not passed it will grab the default profile from the config. + * To Set it up on an existing profile simply use the update command: + + ```powershell + Update-KeePassDatabaseConfigurationProfile -DatabaseProfileName 'name' -Default + ``` + + * To Create a new profile as default use the new command: + + ```powershell + New-KeePassDatabaseConfigurationProfile -DatabaseProfileName 'name' -Default -DatabasePath '' other options + ``` + + * This allows for calls to the main module functions without the `-DatabaseProfileName` parameter such as: + + ```powershell + Get-KeePassEntry -UserName 'aUser' + ``` + ## v.2.1.2.8 * Added - [#84](https://github.com/PSKeePass/PoShKeePass/issues/84) - Manage Notes properties on KPGroup Objects. @@ -95,25 +117,6 @@ Please review the [changelog document](https://github.com/PSKeePass/PoShKeePass/ * **Breaking Change** Since this has been implemeneted the `-AsPsCredential` parameter has been removed. The new method is better as it allows for multiple entries to be returned with thier cred objects instead of limiting it to 1 entry. * **Breaking Change** - `ConvertTo-KPPSObject` and all returned objects the `.FullPath` property now returns the true full path of the object. The `ParentGroup` property still exists and can be used as an alteranative data source for any lost functionality. -### v2.0.5.6 - -* Update-KeePassEntry no longer creates a new entry, Entry history is retained, UUID is never changed, All time modificiation fields are now updated when appropriate. - * [#127](https://github.com/PSKeePass/PoShKeePass/issues/127) - * [#123](https://github.com/PSKeePass/PoShKeePass/issues/123) - * [#120](https://github.com/PSKeePass/PoShKeePass/issues/120) -* Code clean up in the internal functions - * Removed unecessary comments. - * Simplified parameter attributes, and formatting. - * Updated error handling to use write-error and simplified handling. -* Normalized repetative checks to their own `Test-X` functions and moved error\null handling inside. - * Test-KPConnection - Checks to see if connection exists and and is open. - * Test-KPPasswordValue - Correctly checks for supported types and moved error handling inside. -* Fixed Dev Tool AutoVersioning Script, now updates psd1 version again. -* Simplified `Import-KPLibrary` function. -* Updated `ConvertTo-KPPSObject` to be construct PSObject differently and gained 86% speed performance improvement. -* Created a `build.ps1` script to build the module for use and publishing to gallery -* Updated `New-KPConnection` to prompt for user MasterKey (keepass passsword) via console prompt `Read-host` instead of `$Host.ui.PromptForCredential()`, this is much faster than loading the gui. - ## Known Issues See the [Known-Issue](https://github.com/PSKeePass/PoShKeePass/issues?q=is%3Aissue+is%3Aopen+label%3AKnown-Issue) tag to get a list of known issues and their status.