EvotecIT / PSPGP

PSPGP is a PowerShell module that provides PGP functionality in PowerShell. It allows encrypting and decrypting files/folders and strings using PGP.
MIT License
59 stars 22 forks source link

Protect-PGP does not create an encrypted file by specifying the path with -OutFilePath #6

Closed aikiox closed 2 years ago

aikiox commented 2 years ago

Hello, Since version 0.1.7, I have a different behavior with the Protect-PGP command. I use the -OutFilePath parameter to create the encrypted file in a different directory than the -FilePath to separate them. As this is an output file, the file path does not exist yet and I get a warning message WARNING: Protect-PGP - Can't encrypt file : Cannot find path 'C:\Project\File.txt.pgp' because it does not exist.

I noticed, on line 65, the use of Resolve-Path which returns this error because the path does not exist. Usually, an output path is given for the file in order to create it so the file does not exist. I think we should remove this control or use it to add a warning that the output file already exists and that it will be rewritten asking for confirmation. In this case, add a -Force parameter to apply this change.

Here is an example with the -Force parameter and a check if the file does not already exist.

        [Parameter(Mandatory, ParameterSetName = 'File')][string] $FilePath,
        [Parameter(ParameterSetName = 'File')][string] $OutFilePath,
        [Parameter(ParameterSetName = 'File')][switch]$Force,
....
    elseif ($FilePath) {
        try {
            $ResolvedFilePath = Resolve-Path -Path $FilePath
            if ($OutFilePath) {
                $TestOutFilePath = Test-Path -Path $OutFilePath
                if ($TestOutFilePath -and -not $Force)
                {
                    Write-Warning -Message "Protect-PGP - Can't encrypt file $($File.FuleName): The file already exists. Please use -Force to overwrite."
                    return    
                }
                $PGP.EncryptFile($ResolvedFilePath.Path, "$($OutFilePath)")
            }
            else { $PGP.EncryptFile($ResolvedFilePath.Path, "$($ResolvedFilePath.Path).pgp") }
        }
        catch {
            if ($PSBoundParameters.ErrorAction -eq 'Stop') { throw } else {
                Write-Warning -Message "Protect-PGP - Can't encrypt file $($File.FuleName): $($_.Exception.Message)"
                return
            }
        }
    }

Have a nice day, Romain

PrzemyslawKlys commented 2 years ago

I guess we need to replace it with something else

$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("C:\Win")

This seems to work

image

Based on: https://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist it can resolve PSdrive paths as well.

Sorry for that.

PrzemyslawKlys commented 2 years ago

I believe this is now fixed.