-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Adds Port parameter for SSH PSSessions #3499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
925c564
125769f
2d10b56
b525b70
aa658fb
5ff6445
6f7057e
6a6e6d8
e4fc798
dc7a4f3
de296a4
153ca56
0ccd8f2
7a6535c
c8f1a10
4795020
70f3448
97f070a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -275,6 +275,7 @@ internal struct SSHConnection | |
| public string ComputerName; | ||
| public string UserName; | ||
| public string KeyFilePath; | ||
| public int Port; | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -561,6 +562,7 @@ public virtual PSCredential Credential | |
| /// to override the policy setting | ||
| /// </remarks> | ||
| [Parameter(ParameterSetName = PSRemotingBaseCmdlet.ComputerNameParameterSet)] | ||
| [Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)] | ||
| [ValidateRange((Int32)1, (Int32)UInt16.MaxValue)] | ||
| public virtual Int32 Port { get; set; } | ||
|
|
||
|
|
@@ -820,6 +822,7 @@ internal static void ValidateSpecifiedAuthentication(PSCredential credential, st | |
| private const string UserNameParameter = "UserName"; | ||
| private const string KeyFilePathParameter = "KeyFilePath"; | ||
| private const string IdentityFilePathAlias = "IdentityFilePath"; | ||
| private const string PortParameter = "Port"; | ||
|
|
||
| #endregion | ||
|
|
||
|
|
@@ -851,25 +854,23 @@ internal SSHConnection[] ParseSSHConnectionHashTable() | |
| throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter); | ||
| } | ||
|
|
||
| string paramValue = item[paramName] as string; | ||
| if (string.IsNullOrEmpty(paramValue)) | ||
| { | ||
| throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter); | ||
| } | ||
|
|
||
| if (paramName.Equals(ComputerNameParameter, StringComparison.OrdinalIgnoreCase) || paramName.Equals(HostNameAlias, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| var resolvedComputerName = ResolveComputerName(paramValue); | ||
| var resolvedComputerName = ResolveComputerName(GetSSHConnectionStringParameter(item[paramName])); | ||
| ValidateComputerName(new string[] { resolvedComputerName }); | ||
| connectionInfo.ComputerName = resolvedComputerName; | ||
| } | ||
| else if (paramName.Equals(UserNameParameter, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| connectionInfo.UserName = paramValue; | ||
| connectionInfo.UserName = GetSSHConnectionStringParameter(item[paramName]); | ||
| } | ||
| else if (paramName.Equals(KeyFilePathParameter, StringComparison.OrdinalIgnoreCase) || paramName.Equals(IdentityFilePathAlias, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| connectionInfo.KeyFilePath = paramValue; | ||
| connectionInfo.KeyFilePath = GetSSHConnectionStringParameter(item[paramName]); | ||
| } | ||
| else if (paramName.Equals(PortParameter, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| connectionInfo.Port = GetSSHConnectionIntParameter(item[paramName]); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -981,6 +982,37 @@ protected void ValidateComputerName(String[] computerNames) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates parameter value and returns as string | ||
| /// </summary> | ||
| /// <param name="param">Parameter value to be validated</param> | ||
| /// <returns>Parameter value as string</returns> | ||
| private static string GetSSHConnectionStringParameter(object param) | ||
| { | ||
| if (param is string paramValue && !string.IsNullOrEmpty(paramValue)) | ||
| { | ||
| return paramValue; | ||
| } | ||
|
|
||
| throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter); | ||
| } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Validates parameter value and returns as integer | ||
| /// </summary> | ||
| /// <param name="param">Parameter value to be validated</param> | ||
| /// <returns>Parameter value as integer</returns> | ||
| private static int GetSSHConnectionIntParameter(object param) | ||
| { | ||
| if (param is int paramValue) | ||
| { | ||
| return paramValue; | ||
| } | ||
|
|
||
| throw new PSArgumentException(RemotingErrorIdStrings.InvalidSSHConnectionParameter); | ||
| } | ||
|
|
||
| #endregion Private Methods | ||
|
|
||
| #region Overrides | ||
|
|
@@ -1312,7 +1344,7 @@ protected void CreateHelpersForSpecifiedSSHComputerNames() | |
|
|
||
| foreach (string computerName in ResolvedComputerNames) | ||
| { | ||
| var sshConnectionInfo = new SSHConnectionInfo(this.UserName, computerName, this.KeyFilePath); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These creation helper functions are used by Invoke-Command, and this cmdlet will also have to be updated to support the new "Port" parameter. All cmdlets that currently support SSH parameter sets are New-PSSession, Enter-PSSession, Invoke-Command. You covered the first two but missed the last one. |
||
| var sshConnectionInfo = new SSHConnectionInfo(this.UserName, computerName, this.KeyFilePath, this.Port); | ||
| var typeTable = TypeTable.LoadDefaultTypeFiles(); | ||
| var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace; | ||
| var pipeline = CreatePipeline(remoteRunspace); | ||
|
|
@@ -1334,7 +1366,8 @@ protected void CreateHelpersForSpecifiedSSHHashComputerNames() | |
| var sshConnectionInfo = new SSHConnectionInfo( | ||
| sshConnection.UserName, | ||
| sshConnection.ComputerName, | ||
| sshConnection.KeyFilePath); | ||
| sshConnection.KeyFilePath, | ||
| sshConnection.Port); | ||
| var typeTable = TypeTable.LoadDefaultTypeFiles(); | ||
| var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace; | ||
| var pipeline = CreatePipeline(remoteRunspace); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a little worried about sharing parameters between WinRM and SSH parameter sets. My worry is that PowerShell parameter resolution might resolve to the wrong parameter set for positional parameters (e.g., New-PSSession localhost -Port 8080). Please verify that parameter set resolution does not change (i.e. causes a breaking change to existing behavior).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this turns out to be a problem we can think about adding a new SSH specific parameter set port parameter (SSHPort), but hopefully we won't have to.
In reply to: 111206290 [](ancestors = 111206290)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tested this, confirmed to default to ComputerNameParameterSet:
Not sure whether DefaultParameterSetName would help here to remove any uncertainty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like we are Ok. I assume that this works and doesn't result in a "cannot resolve parameter set" error?
Also please check this again when you implement Invoke-Command SSH parameter sets
I think this will also be Ok but it does have more complex parameter sets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter set looks to be resolving correctly:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've confirmed this to be resolving correctly following implementation for Invoke-Command:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great news. I am looking forward to the next commit.