Skip to content

SSH KeyFilePath parameter not working  #4475

Closed

Description

@luzlab

When I try running New-PSSession or Enter-PSSession using a public key, powershell either:

  1. When passed a valid KeyFilePath, complains that it doesn't exist and continues gracefully to password-based authentication.
  2. When passed a bogus KeyFilePath, throws an ItemNotFoundException

Steps to reproduce

> New-PSSession -UserName "xx" -KeyFilePath "/fakepath" -Hostname " xx.xx.xx.xx"
New-PSSession : Cannot find path 'doesntexist' because it does not exist.
At line:1 char:1
+ New-PSSession -UserName "Administrator" -KeyFilePath "doesntexist" -H ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (doesntexist:String) [New-PSSession], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.NewPSSessionCommand

> New-PSSession -UserName "xx" -KeyFilePath "~/.ssh/id_rsa" -Hostname " xx.xx.xx.xx"
Warning: Identity file "/Users/cquinonez/.ssh/workgangkey" not accessible: No such file or directory.

Expected behavior

>  New-PSSession -UserName "xx" -KeyFilePath "/fakepath" -Hostname " xx.xx.xx.xx"
Warning: Identity file "/Users/cquinonez/.ssh/workgangkey" not accessible: No such file or directory.
>  New-PSSession -UserName "xx" -KeyFilePath "<validKeyFilePath>" -Hostname " xx.xx.xx.xx"

 Id Name            ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            ------------    ------------    -----         -----------------     ------------
  1 SSH1            xx.xx.xx.xx     RemoteMachine   Opened        DefaultShell             Available

Actual behavior

untitled

I've verified that Test-Path also correctly detects the key file as well.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.4
OS                             Darwin 16.7.0 Darwin Kernel Version 16.7.0: Thu Jun 15 17:36:27 PDT 2017; root:xnu-3789.70.16~2/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    WG-Remoting
    on Aug 3, 2017
  2. added this to the milestone on Aug 3, 2017
  3. PaulHigin commented on Aug 4, 2017

    @PaulHigin
    Contributor

    I am able to repro this. This looks to be a bug in how the ssh -i parameter argument is being passed to the ssh process after creation. The KeyFile (-i parameter) path is surrounded in quotes to handle file paths with spaces. But it is somehow causing ssh to emit this error. If I remove the quotes then it works correctly.

    Also I notice that the argument parser in the ssh process creation code is not parsing paths with spaces correctly.

    https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs#L2050

  4. added
    Issue-Bug
    on Aug 4, 2017
  5. PaulHigin commented on Aug 4, 2017

    @PaulHigin
    Contributor

    As a workaround, if you put the private key in a location that ssh looks for it (such as '/home//.ssh/userKey') then ssh will automatically find it for that user and the KeyFilePath parameter is not needed.

  6. luzlab commented on Aug 7, 2017

    @luzlab
    Author

    Isn't the Warning: Identity file "xxx" not accessible: No such file or directory. error thrown at line 2047? https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs#L2047

    It's as if the test at line 2044 is inverting the logic of whether a keyfile was detected at a given path.

    • non-existent keyfiles (my "/fakepath" example) pass the test. No error is thrown and powershell tries to open up an SSH session with a bogus path (which throws an unhandled exception) at line 2050.
    • real keyfiles seem to fail the test, so powershell throws an error with the RemotingErrorIdStrings.KeyFileNotFound string.

    Regarding the suggested workaround - how would I tell powershell to use a keyfile without providing a -KeyFilePath parameter? I tried adding the key to my ssh-agent, but that didn't work. I also tried saving my keyfile at the default location (~/.ssh/id_rsa) but that doesn't work via the New-PSSession command. If I try using SSH directly, I'm able to use my ssh key (as well as keys loaded into the ssh-agent).

    image

  7. luzlab commented on Aug 7, 2017

    @luzlab
    Author

    Also, there's no spaces in the KeyFilePath I'm using.

  8. PaulHigin commented on Aug 7, 2017

    @PaulHigin
    Contributor

    The

    Warning: Identity file "/Users/cquinonez/.ssh/workgangkey" not accessible: No such file or directory.
    

    error is coming from ssh. This is due to an error in PowerShell in how it passes the key path to the ssh child process. That is why the keypath works with ssh by itself but not with PowerShell using ssh. To use ssh automatic user key location you need to set that up. For example the public key needs to go into a .ssh/authorized_keys file if sshd_config is set up for it. See ssh user authentication key documentation for more information.

    I just noticed that PowerShell's ssh argument parsing didn't handle paths with spaces correctly while investigating this issue. I didn't mean to imply that your case was affected by it. But this will also need to be fixed. KeyFilePath works correctly on Windows so this appears to only be broken for Linux platforms.

  9. PaulHigin commented on Aug 8, 2017

    @PaulHigin
    Contributor

    Carlo Q (@luzlab)
    I have a PR for a fix for this. If you would like to experiment with it you can find it here:
    https://github.com/PaulHigin/powershell/tree/Fix-KeyFilePath-Parameter

  10. luzlab commented on Aug 8, 2017

    @luzlab
    Author

    Thank you! I'll test this out tomorrow.

  11. luzlab commented on Aug 9, 2017

    @luzlab
    Author

    Weird... I've gotten different results the first 2 times I tried running the command.
    image
    image
    The only thing I did was rerun /build.shin between the two runs. I double-checked the permissions on the keyfile and they're set at 600. I can successfully SSH inside of Powershell to the windows box.
    image

  12. luzlab commented on Aug 9, 2017

    @luzlab
    Author

    By the way, I was able to workaround this. I added the following entry to my ~/.ssh/config:

    Host 34.208.13.94
      HostName 34.208.13.94
      User Administrator
      IdentityFile ~/.ssh/workgangkey
    

    Since this allows me to log in programmatically, this issue isn't blocking me at the moment.

  13. PaulHigin commented on Aug 9, 2017

    @PaulHigin
    Contributor

    The warning message you are getting:

    Warning: Identity file "/users/cquinonez/.ssh/workgangkey" not accessible: No such file or directory.
    

    indicates that you do not have the fix, since this is the specific error that it addresses.
    Are you sure you are building the right branch?

    BTW I didn't have any trouble while testing this change on Linux (Ubuntu 16.04), however I did run into access issues when testing on Windows 10 (with Win32-OpenSSH). I was able to resolve the issues with:
    https://github.com/PowerShell/Win32-OpenSSH/wiki/Security-protection-of-various-files-in-win32-openssh
    User identity keys are essentially user credentials and I am sure Linux has similar restrictions. But since your key file works directly with SSH it sounds like you don't have any access problems.

  14. luzlab commented on Aug 9, 2017

    @luzlab
    Author

    It worked - I checked out your repo, but hadn't switched to the correct branch.

    image

    Thank you!

  15. PaulHigin commented on Aug 9, 2017

    @PaulHigin
    Contributor

    Great to hear. Thanks!

  16. PaulHigin commented on Aug 15, 2017

    @PaulHigin
    Contributor

    Fixed with PR #4529

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-BugResolution-FixedSize-DaysWG-Remoting

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions