FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use type conversion in `SSHConnection` hashtables when value doesn't match expected type by SeeminglyScience · Pull Request #10720 · PowerShell/PowerShell · GitHub

Use type conversion in SSHConnection hashtables when value doesn't match expected type - #10720

Merged
Ilya (iSazonov) merged 2 commits into
PowerShell:masterfrom
SeeminglyScience:fix-ssh-connection-hashtable-psobject
Oct 15, 2019
Merged

Use type conversion in SSHConnection hashtables when value doesn't match expected type#10720
Ilya (iSazonov) merged 2 commits into
PowerShell:masterfrom
SeeminglyScience:fix-ssh-connection-hashtable-psobject

Conversation

Copy link
Copy Markdown
Contributor

PR Summary

Use LanguagePrimitives.ConvertTo when reading a hashtable provided to the SSHConnection parameter.

PR Context

Currently if the hashtable value does not match the expected type exactly, an exception is thrown stating that the value was null.

Fixes #10687

PR Checklist

Use LanguagePrimitives.ConvertTo in GetSSHConnectionStringParameter and
GetSSHConnectionIntParameter.

Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please add tests.

private static string GetSSHConnectionStringParameter(object param)
{
if (param is string paramValue && !string.IsNullOrEmpty(paramValue))
string paramValue = LanguagePrimitives.ConvertTo<string>(param);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

ConvertTo() can throw PSInvalidCastException but we should keep PSArgumentException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Is there a circumstance where converting to string will throw?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Yes, it is not common but possible (if someone creates ToString() method which throw).

}

throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter);
return LanguagePrimitives.ConvertTo<int>(param);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The same.

Copy link
Copy Markdown
Contributor Author

Please add tests.

Any thoughts on that? If I try to test that the conversion works then we'll hit the interactive SSH prompt. I could give it a bad port and/or hostname, but that'll add ~5 seconds per test. If that's acceptable I'll add a few of those.

Ilya (iSazonov) commented Oct 7, 2019
edited
Loading

Copy link
Copy Markdown
Collaborator

I'd expect 2 tests for these two code paths modified.
Please use Feature and Slow tags in the tests.

Copy link
Copy Markdown
Contributor Author

Ilya (@iSazonov) I've added tests and normalized the exception type. In the tests I used port 49151 since it's reserved and shouldn't be in use, but that was the result of a quick search so I'm open to suggestions.


It "<testName>" -TestCases $TestCasesSSHConnection {
param($scriptBlock)
{ & $scriptBlock } | Should -Throw -ErrorId '2100,PSSessionOpenFailed'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It is out of the PR but I'd prefer to see more useful message.

Ilya (iSazonov) self-assigned this Oct 14, 2019
Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Oct 14, 2019
Ilya (iSazonov) added this to the 7.0.0-preview.5 milestone Oct 14, 2019
Ilya (iSazonov) merged commit beb8b44 into PowerShell:master Oct 15, 2019

Copy link
Copy Markdown
Collaborator

Patrick Meinecke (@SeeminglyScience) Thanks for your contribution!

Patrick Meinecke (SeeminglyScience) deleted the fix-ssh-connection-hashtable-psobject branch October 15, 2019 18:29

Copy link
Copy Markdown

🎉v7.0.0-preview.5 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
…match expected type (PowerShell#10720)

Before the change if the hashtable value does not match the expected type exactly, an exception is thrown stating that the value was null.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-Command -SSHConnection breaks if the HostName string is [psobject]-wrapped

5 participants


Back | FazBrowse Home | New Git URL