Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create extension for keepass-plugin-* pkgs #222

Closed
dtgm opened this issue Jun 13, 2016 · 6 comments
Closed

Create extension for keepass-plugin-* pkgs #222

dtgm opened this issue Jun 13, 2016 · 6 comments

Comments

@dtgm
Copy link
Owner

dtgm commented Jun 13, 2016

Note

>=2.34, plugins are only loaded from application directory or Plugins sub-directory.
http://keepass.info/news/n160611_2.34.html

@dtgm
Copy link
Owner Author

dtgm commented Jun 13, 2016

example code currently in use

chocolateyInstall.ps1

# powershell v2 compatibility
$psVer = $PSVersionTable.PSVersion.Major
if ($psver -ge 3) {
  function Get-ChildItemDir {Get-ChildItem -Directory $args}
} else {
  function Get-ChildItemDir {Get-ChildItem $args}
}

$packageName = 'keepass-plugin-keepasshttp'
$typName = 'KeePassHttp.plgx'
$packageSearch = 'KeePass Password Safe'
$url = 'https://github.com/pfn/keepasshttp/raw/master/KeePassHttp.plgx'
$checksum = '3f3358fa0536d70f22d290907e79d450f0b22022'
$checksumType = 'sha1'

try {
Write-Verbose "Searching registry for installed KeePass..."
$regPath = Get-ItemProperty -Path @('HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
                                    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
                                    'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') `
                            -ErrorAction:SilentlyContinue `
           | Where-Object {$_.DisplayName -like "$packageSearch*" `
                           -and `
                           $_.DisplayVersion -ge 2.17 `
                           -and `
                           $_.DisplayVersion -lt 3.0 } `
           | ForEach-Object {$_.InstallLocation}
$installPath = $regPath
if (! $installPath) {
  Write-Verbose "Searching $env:ChocolateyBinRoot for portable install..."
  $binRoot = Get-BinRoot
  $portPath = Join-Path $binRoot "keepass"
  $installPath = Get-ChildItemDir $portPath* -ErrorAction SilentlyContinue
}
if (! $installPath) {
  Write-Verbose "Searching $env:Path for unregistered install..."
  $installFullName = (Get-Command keepass -ErrorAction SilentlyContinue).Path
  if (! $installFullName) {
    $installPath = [io.path]::GetDirectoryName($installFullName)
  }
}
if (! $installPath) {
  Write-Warning "$($packageSearch) not found."
  throw
}
Write-Verbose "`t...found."

Write-Verbose "Searching for plugin directory..."
$pluginPath = (Get-ChildItemDir $installPath\Plugin*).FullName
if ($pluginPath.Count -eq 0) {
  $pluginPath = Join-Path $installPath "Plugins"
  [System.IO.Directory]::CreateDirectory($pluginPath)
}
$installFile = Join-Path $pluginPath "$($packageName).plgx"
Get-ChocolateyWebFile -PackageName "$packageName" `
                      -FileFullPath "$installFile" `
                      -Url "$url" `
                      -Checksum "$checksum" `
                      -ChecksumType "$checksumType"

if ( Get-Process -Name "KeePass" `
                 -ErrorAction SilentlyContinue ) {
  Write-Warning "$($packageSearch) is currently running. Plugin will be available at next restart of $($packageSearch)."
} else {
  Write-Host "$($packageName) will be loaded the next time KeePass is started."
  Write-Host "Please note this plugin may require additional configuration. Look for a new entry in KeePass' Menu>Tools"
}} catch {
  throw $_.Exception
}

chocolateyUninstall.ps1

# powershell v2 compatibility
$psVer = $PSVersionTable.PSVersion.Major
if ($psver -ge 3) {
  function Get-ChildItemDir {Get-ChildItem -Directory $args}
} else {
  function Get-ChildItemDir {Get-ChildItem $args}
}

$packageName = 'keepass-plugin-keepasshttp'
$typName = 'KeePassHttp.plgx'
$packageSearch = 'KeePass Password Safe'

try {
Write-Verbose "Checking KeePass is not running..."
if (Get-Process -Name "KeePass" `
                -ErrorAction SilentlyContinue) {
  Write-Warning "$($packageSearch) is running. Please save any opened databases and close $($packageSearch) before attempting to uninstall KeePass plugins."
  throw
}

Write-Verbose "Searching registry for installed KeePass..."
$regPath = Get-ItemProperty -Path @('HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
                                    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
                                    'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') `
                            -ErrorAction:SilentlyContinue `
           | Where-Object {$_.DisplayName -like "$packageSearch*"} `
           | ForEach-Object {$_.InstallLocation}
$installPath = $regPath
if (! $installPath) {
  Write-Verbose "Searching $env:ChocolateyBinRoot for portable install..."
  $binRoot = Get-BinRoot
  $portPath = Join-Path $binRoot "keepass"
  $installPath = Get-ChildItemDir $portPath* -ErrorAction SilentlyContinue
}
if (! $installPath) {
  Write-Verbose "Searching $env:Path for unregistered install..."
  $installFullName = (Get-Command keepass -ErrorAction SilentlyContinue).Path
  if (! $installFullName) {
    $installPath = [io.path]::GetDirectoryName($installFullName)
  }
}
if (! $installPath) {
  Write-Warning "$($packageSearch) not found."
  throw
}
Write-Verbose "`t...found."

Write-Verbose "Searching for plugin directory..."
$pluginPath = (Get-ChildItemDir $installPath\Plugin*).FullName
if ($pluginPath.Count -eq 0) {
  throw "Plugins directory not found."
}
$pluginTypFile = Join-Path $pluginPath $typName
$pluginChocoFile = Join-Path $pluginPath "$($packageName).plgx"
Remove-Item -Path $pluginTypFile `
            -Force `
            -ErrorAction SilentlyContinue
Remove-Item -Path $pluginChocoFile `
            -Force `
            -ErrorAction Continue
} catch {
  throw $_.Exception
}

@dtgm
Copy link
Owner Author

dtgm commented Jun 15, 2016

Comments/improvements to install script; open to suggestion/feedback.

Double hash ## is meta-comment on following code. Single hash # with no space is what I intend to deprecate with next line(s) replacing it.

chocolateyInstall.ps1

# powershell v2 compatibility
$psVer = $PSVersionTable.PSVersion.Major
if ($psver -ge 3) {
  function Get-ChildItemDir {Get-ChildItem -Directory $args}
} else {
  ## why didn't I pipe to Where-Object {$_.PsIsContainer} ?
  function Get-ChildItemDir {Get-ChildItem $args}
}

## we rename the PLGX to this
$packageName = 'keepass-plugin-keepasshttp'

## use to search for existing plugins? should leave off extension to search on keyword (case-insensitive)
#$typName = 'KeePassHttp.plgx'
$typName = 'KeePassHttp'

## String as stored in registry key of installed KeePass; used to find InstallLocation
# (consistency) Rename variable to match function parameter name
#$packageSearch = 'KeePass Password Safe'
$softwareName = 'KeePass Password Safe'

## download info
$url = 'https://github.com/pfn/keepasshttp/raw/master/KeePassHttp.plgx'
$checksum = '3f3358fa0536d70f22d290907e79d450f0b22022'
$checksumType = 'sha1'

## Remove superfluous try{} and also catch{} at end of script
#try {

Write-Verbose "Searching registry for installed KeePass..."
### Replace next block with Get-InstallRegistryKey
<#
$regPath = Get-ItemProperty -Path @('HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
                                    'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
                                    'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*') `
                            -ErrorAction:SilentlyContinue `
           | Where-Object {$_.DisplayName -like "$packageSearch*" `
                           -and `
                           $_.DisplayVersion -ge 2.17 `
                           -and `
                           $_.DisplayVersion -lt 3.0 } `
           | ForEach-Object {$_.InstallLocation}
#>
[array]$key = Get-InstallRegistryKey -SoftwareName $softwareName
<# ^^^
(question) Is there ever a situation where this returns more than one key? 
(question) Is there a reason to specify search for version? 
  Again, there shouldn't be multiple keys returned and nuspec already specifies 
  this search so it appears redundant to include the DisplayVersion check.
(todo) Test how keepass 1.x gets installed and stored in registry...
  It could be they were the same name so we needed to only select registry key of version 2.x
#>

## Use consistent variable (as in other packages)
#$installPath = $regPath
$installPath = $key.InstallLocation

## above will only use first key, we should introduce check to warn about this
if ($key.Count -gt 1) {
  Write-Warning "`"$($key.Count)`" registry keys were found."
  Write-Warning "Installing to `"$installPath`""
}

<# ref code below
(comment) We can't install plugins into other package folders in 
  "$env:ChocolateyInstallLocation\lib\keepass.portable\" so it is 
  required `keepass.portable` would have to installed with Get-BinTools
(todo) but !!! keepass.portable doesn't install to 
   $env:ChocolateyBinRoot/$env:ChocolateyToolsLocation
(todo) fix install method of https://chocolatey.org/packages/keepass.portable/2.34
#>
if (! $installPath) {
  Write-Verbose "Searching `"$env:ChocolateyBinRoot`" for keepass.portable install..."
  $binRoot = Get-BinRoot
  $portPath = Join-Path $binRoot "keepass"
  $installPath = Get-ChildItemDir $portPath* -ErrorAction SilentlyContinue
}
if (! $installPath) {
  Write-Verbose "Searching $env:Path for unregistered install..."
  $installFullName = (Get-Command keepass -ErrorAction SilentlyContinue).Path
  if (! $installFullName) {
    $installPath = [io.path]::GetDirectoryName($installFullName)
  }
}
if (! $installPath) {
  Write-Warning "$($packageSearch) not found."
  throw
}
Write-Verbose "`t...found."

## As mentioned https://github.com/dtgm/chocolatey-packages/issues/222#issue-160020640
## >=2.34, plugins are only loaded from application directory or Plugins sub-directory
## (question) Best way to handle this with <2.34 and going forward?
Write-Verbose "Searching for plugin directory..."
$pluginPath = (Get-ChildItemDir $installPath\Plugin*).FullName

if ($pluginPath.Count -eq 0) {
  $pluginPath = Join-Path $installPath "Plugins"
  [System.IO.Directory]::CreateDirectory($pluginPath)
}
$installFile = Join-Path $pluginPath "$($packageName).plgx"

Get-ChocolateyWebFile -PackageName "$packageName" `
                      -FileFullPath "$installFile" `
                      -Url "$url" `
                      -Checksum "$checksum" `
                      -ChecksumType "$checksumType"

if ( Get-Process -Name "KeePass" `
                 -ErrorAction SilentlyContinue ) {
  Write-Warning "$softwareName is currently running. Plugin will be loaded and available at next restart of $packageSearch."
} else {
  Write-Host "This plugin, $packageName, will be loaded the next time KeePass is started."
}

Write-Host @"
After loading, this plugin may require additional configuration in KeePass. Look for new options in the following locations:
`tMenubar > View/Tools
`tContext-menu (right-click) on Groups or Entries. After doing so, additional lines may be revealed by holding down `"Shift`" key.
`tNew tabs or fields when creating/editing Entries
"@

## Remove superfluous try{} (above) and catch{} (here)
#}} catch {
#  throw $_.Exception
#}

@dtgm
Copy link
Owner Author

dtgm commented Jun 15, 2016

  1. I think the best course of action is to stop searching for Plugin* directory and force using Plugins since that is now default behavior 2.34
  2. Rather than auto-migrate, warn the user about possible duplicates.
    • previous versions: simple filename search in $installPath using $typName = 'KeePassHttp' ... change $typName to $defaultName and use best guess of name. This check would leave the duplicate present, but just warn about possible dupe.
    • same version: since most plugins are installed by directly downloading the .plgx I could use this info already present and stored in $checksum to compare sum of all .plgx files for exact duplicates. I would want to get list of all .plgx files, and foreach them to Get-ChecksumValid
$plugins = gci $installPath -Include "*.plgx" -Exclude "$packageName.plgx" -Recurse -File
foreach ($plugin in $plugins) {
  if (Get-ChecksumValid -File $plugin.FullName -Checksum $checksum -ChecksumType $checksumType) {
    Write-Warning "This plugin is already installed in KeePass. Removing `"$($plugin.FullName)`""
    rm $plugin.FullName -force
  }

This wouldn't work for zip files since the checksum is auto-built from the download file

  • Verifying checksums may be a bit unnecessary for someone with many plugins installed.
  • What if user wanted exact duplicate plugins for some reason? ...possibly dev debug testing to prevent issues? Probably better to just warn then and let user handle it manually?

For previous two points, probably default to do nothing and provide pkg parameters. It stands to reason it would only be of real benefit once when migrating to choco to manage keepass plugins and indicate conflicts. As long as choco is used for install/uninstall of plugins, there shouldn't be a reason to verify checksums at every pkg upgrade.
3. As far as Keepass' handling of plugins, every plugin found is loaded, regardless of being a duplicate or not. Loading a plugin more than once will most likely not create an immediate problem or crash, but options for the program will be duplicated. If this is seen, you should review installed plugins from Tools > Plugins

Loading is done alphabetically in precedence of AppDir, then AppDir\Plugins, and recursively in all sub-directories of AppDir\Plugins thereafter.

@Bennyboy84
Copy link

KeePass Plugin KeeAutoExec 2.2
ERROR: Specified cast is not valid.

I can't see the suggested changes in the chocolateyInstall.ps1

@tunisiano187
Copy link
Contributor

@gep13 , this kind of packages are handled by @pauby
So you can close this issue to

@gep13
Copy link
Collaborator

gep13 commented Jul 19, 2020

@tunisiano187 sounds good to me.

@gep13 gep13 closed this as completed Jul 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants