Skip to content

Commit

Permalink
Merge pull request #162 from PSKeePass/default_db
Browse files Browse the repository at this point in the history
Default db
  • Loading branch information
jkdba authored Mar 25, 2019
2 parents e3aa808 + be61486 commit 655bc4e
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 61 deletions.
Binary file modified PoShKeePass.psd1
Binary file not shown.
203 changes: 177 additions & 26 deletions Test/PSKeePassUsage.Tests.ps1

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
# Changelog

## 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.
Expand Down
21 changes: 18 additions & 3 deletions functions/Get-KeePassDatabaseConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +45,15 @@ 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' }

if($Stop -and -not $ProfileResults)
{
throw 'Unable to find a default KeePass Configuration, please specify a database profile name or set a default profile.'
}
}
else
{
$ProfileResults = $XML.Settings.DatabaseProfiles.Profile
Expand All @@ -55,6 +68,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;
Expand All @@ -63,6 +77,7 @@ function Get-KeePassDatabaseConfiguration
'UseMasterKey' = $UseMasterKey;
'UseNetworkAccount' = $UseNetworkAccount;
'AuthenticationType' = $ProfileResult.AuthenticationType;
'Default' = $ProfileDefault;
}

New-Object -TypeName PSObject -Property $ProfileObject
Expand Down
2 changes: 1 addition & 1 deletion functions/Get-KeePassEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function Get-KeePassEntry
[Alias('AsPSCredential')]
[Switch] $WithCredential,

[Parameter(Position = 5, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 5, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
2 changes: 1 addition & 1 deletion functions/Get-KeePassGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function Get-KeePassGroup
[Parameter(Position = 1)]
[Switch] $AsPlainText,

[Parameter(Position = 2, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 2, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
17 changes: 17 additions & 0 deletions functions/New-KeePassDatabaseConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ function New-KeePassDatabaseConfiguration
[Switch] $UseMasterKey,

[Parameter(Position = 5)]
[Switch] $Default,

[Parameter(Position = 6)]
[Switch] $PassThru
)
begin
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion functions/New-KeePassEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function New-KeePassEntry
[Parameter(Position = 8)]
[DateTime] $ExpiryTime,

[Parameter(Position = 9, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 9, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
2 changes: 1 addition & 1 deletion functions/New-KeePassGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function New-KeePassGroup
[Parameter(Position = 5)]
[DateTime] $ExpiryTime,

[Parameter(Position = 6, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 6, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
2 changes: 1 addition & 1 deletion functions/Remove-KeePassEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Remove-KeePassEntry
[Parameter(Position = 2)]
[Switch] $Force,

[Parameter(Position = 3, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 3, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
2 changes: 1 addition & 1 deletion functions/Remove-KeePassGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Remove-KeePassGroup
[Parameter(Position = 2)]
[Switch] $Force,

[Parameter(Position = 3, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 3, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
17 changes: 17 additions & 0 deletions functions/Update-KeePassDatabaseConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ function Update-KeePassDatabaseConfiguration
[Switch] $UseMasterKey,

[Parameter(Position = 6)]
[Switch] $Default,

[Parameter(Position = 7)]
[Switch] $PassThru
)
begin
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion functions/Update-KeePassEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function Update-KeePassEntry
[Parameter(Position = 9)]
[DateTime] $ExpiryTime,

[Parameter(Position = 10, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 10, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
2 changes: 1 addition & 1 deletion functions/Update-KeePassGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function Update-KeePassGroup
[Parameter(Position = 4)]
[DateTime] $ExpiryTime,

[Parameter(Position = 5, Mandatory, ValueFromPipelineByPropertyName)]
[Parameter(Position = 5, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[string] $DatabaseProfileName,

Expand Down
15 changes: 10 additions & 5 deletions internal/New-KPConnection.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function New-KPConnection
.PARAMETER UseWindowsAccount
Use the current windows account as an authentication method
#>
[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')]
Expand Down Expand Up @@ -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 }
Expand Down
41 changes: 22 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 655bc4e

Please sign in to comment.