Repository navigation
Closed
Labels
Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Size-DaysWG-RemotingPSRP issues with any transport layer
Milestone
Description
When I try running New-PSSession or Enter-PSSession using a public key, powershell either:
- When passed a valid KeyFilePath, complains that it doesn't exist and continues gracefully to password-based authentication.
- 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
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
Metadata
Metadata
Assignees
Labels
Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.Size-DaysWG-RemotingPSRP issues with any transport layer

Activity
PaulHigin commentedon Aug 4, 2017
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
PaulHigin commentedon Aug 4, 2017
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.
luzlab commentedon Aug 7, 2017
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#L2047It's as if the test at line 2044 is inverting the logic of whether a keyfile was detected at a given path.
RemotingErrorIdStrings.KeyFileNotFoundstring.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).
luzlab commentedon Aug 7, 2017
Also, there's no spaces in the KeyFilePath I'm using.
PaulHigin commentedon Aug 7, 2017
The
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.
PaulHigin commentedon Aug 8, 2017
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
luzlab commentedon Aug 8, 2017
Thank you! I'll test this out tomorrow.
luzlab commentedon Aug 9, 2017
Weird... I've gotten different results the first 2 times I tried running the command.



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.luzlab commentedon Aug 9, 2017
By the way, I was able to workaround this. I added the following entry to my
~/.ssh/config:Since this allows me to log in programmatically, this issue isn't blocking me at the moment.
PaulHigin commentedon Aug 9, 2017
The warning message you are getting:
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.
luzlab commentedon Aug 9, 2017
It worked - I checked out your repo, but hadn't switched to the correct branch.
Thank you!
PaulHigin commentedon Aug 9, 2017
Great to hear. Thanks!
PaulHigin commentedon Aug 15, 2017
Fixed with PR #4529