8000 Support user@host:port syntax for SSH Transport by SteveL-MSFT · Pull Request #6558 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Support user@host:port syntax for SSH Transport #6558

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

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,42 @@ internal static void ValidateSpecifiedAuthentication(PSCredential credential, st

#endregion

/// <summary>
/// Parse a hostname used with SSH Transport to get embedded
/// username and/or port.
/// </summary>
/// <param name="hostname">host name to parse</param>
/// <param name="host">resolved target host</param>
/// <param name="userName">resolved target user name</param>
/// <param name="port">resolved target port</param>
protected void ParseSshHostName(string hostname, out string host, out string userName, out int port)
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be a static method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Need access to this so can't be static

{
host = hostname;
userName = this.UserName;
port = this.Port;
try
{
Uri uri = new System.Uri("ssh://" + hostname);
host = ResolveComputerName(uri.Host);
ValidateComputerName(new string[]{host});
if (uri.UserInfo != String.Empty)
{
userName = uri.UserInfo;
}
if (uri.Port != -1)
{
port = uri.Port;
}
}
catch (UriFormatException)
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException(PSRemotingErrorInvariants.FormatResourceString(
RemotingErrorIdStrings.InvalidComputerName)), "PSSessionInvalidComputerName",
ErrorCategory.InvalidArgument, hostname));
}
}

/// <summary>
/// Parse the Connection parameter HashTable array.
/// </summary>
Expand Down Expand Up @@ -856,8 +892,16 @@ internal SSHConnection[] ParseSSHConnectionHashTable()
if (paramName.Equals(ComputerNameParameter, StringComparison.OrdinalIgnoreCase) || paramName.Equals(HostNameAlias, StringComparison.OrdinalIgnoreCase))
{
var resolvedComputerName = ResolveComputerName(GetSSHConnectionStringParameter(item[paramName]));
ValidateComputerName(new string[] { resolvedComputerName });
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to no longer validate the host name?

Copy link
Member Author

Choose a reason for hiding this comment

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

I suppose I can call ValidateComputerName after extracting the hostname

connectionInfo.ComputerName = resolvedComputerName;
ParseSshHostName(resolvedComputerName, out string host, out string userName, out int port);
connectionInfo.ComputerName = host;
if (userName != String.Empty)
{
connectionInfo.UserName = userName;
}
if (port != -1)
{
connectionInfo.Port = port;
}
}
else if (paramName.Equals(UserNameParameter, StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -1337,11 +1381,11 @@ protected virtual void CreateHelpersForSpecifiedComputerNames()
/// </summary>
protected void CreateHelpersForSpecifiedSSHComputerNames()
{
ValidateComputerName(ResolvedComputerNames);

foreach (string computerName in ResolvedComputerNames)
{
var sshConnectionInfo = new SSHConnectionInfo(this.UserName, computerName, this.KeyFilePath, this.Port);
ParseSshHostName(computerName, out string host, out string userName, out int port);

var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port);
var typeTable = TypeTable.LoadDefaultTypeFiles();
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
var pipeline = CreatePipeline(remoteRunspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,8 @@ private RemoteRunspace GetRunspaceForContainerSession()
/// </summary>
private RemoteRunspace GetRunspaceForSSHSession()
{
var sshConnectionInfo = new SSHConnectionInfo(this.UserName, ResolveComputerName(HostName), this.KeyFilePath, this.Port);
ParseSshHostName(HostName, out string host, out string userName, out int port);
var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port);
var typeTable = TypeTable.LoadDefaultTypeFiles();

// Use the class _tempRunspace field while the runspace is being opened so that StopProcessing can be handled at that time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1067,18 +1067,20 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostParameterSet()
{
// Resolve all the machine names
String[] resolvedComputerNames;

ResolveComputerNames(HostName, out resolvedComputerNames);
ValidateComputerName(resolvedComputerNames);

var remoteRunspaces = new List<RemoteRunspace>();
int index = 0;
foreach (var computerName in resolvedComputerNames)
{
ParseSshHostName(computerName, out string host, out string userName, out int port);

var sshConnectionInfo = new SSHConnectionInfo(
this.UserName,
computerName,
userName,
host,
this.KeyFilePath,
this.Port);
port);
var typeTable = TypeTable.LoadDefaultTypeFiles();
string rsName = GetRunspaceName(index, out int rsIdUnused);
index++;
Expand Down
0