8000 Use type conversion in `SSHConnection` hashtables when value doesn't match expected type by SeeminglyScience · Pull Request #10720 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Conversation

@SeeminglyScience
Copy link
Collaborator

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.
Copy link
Collaborator
@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

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
Collaborator

Choose a reason for hiding this comment

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

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there a circumstance where converting to string will throw?

Copy link
Collaborator

Choose a reason for hiding this comment

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

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
Collaborator

Choose a reason for hiding this comment

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

The same.

@SeeminglyScience
Copy link
Collaborator 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.

@iSazonov
Copy link
Collaborator
iSazonov commented Oct 7, 2019

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

@SeeminglyScience
Copy link
Collaborator Author

@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
Collaborator

Choose a reason for hiding this comment

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

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

@iSazonov iSazonov self-assigned this Oct 14, 2019
@iSazonov 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
@iSazonov iSazonov added this to the 7.0.0-preview.5 milestone Oct 14, 2019
@iSazonov iSazonov merged commit beb8b44 into PowerShell:master Oct 15, 2019
@iSazonov
Copy link
Collaborator

@SeeminglyScience Thanks for your contribution!

@SeeminglyScience SeeminglyScience deleted the fix-ssh-connection-hashtable-psobject branch October 15, 2019 18:29
@ghost
Copy link
ghost commented Oct 23, 2019

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

Handy links:

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

0