From 1c5273a5e99042696c32c56b06b19517acb64f2b Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Thu, 17 Dec 2020 19:26:44 -0800 Subject: [PATCH 1/5] Change SSHCommand from const to string to make it configurable --- .../engine/remoting/common/RunspaceConnectionInfo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index 9debc89ce76..9783f9e52b9 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -2050,9 +2050,9 @@ internal int StartSSHProcess( { string filePath = string.Empty; #if UNIX - const string sshCommand = "ssh"; + string sshCommand = "ssh"; #else - const string sshCommand = "ssh.exe"; + string sshCommand = "ssh.exe"; #endif var context = Runspaces.LocalPipeline.GetExecutionContextFromTLS(); if (context != null) From 198b9135eb3d84b7a5fa03eea298db02e02bc5a1 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Thu, 17 Dec 2020 19:27:21 -0800 Subject: [PATCH 2/5] Add SSHTransportName parameter --- .../remoting/commands/InvokeCommandCommand.cs | 13 +++++++++++++ .../engine/remoting/commands/PSRemotingCmdlet.cs | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index b60f3131f24..874fb77ce09 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -753,6 +753,19 @@ public override SwitchParameter SSHTransport set { base.SSHTransport = value; } } + /// + /// This parameter allows you to specify a custom executable name to use for the SSH transport. The executable must be on the path. + /// + [Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)] + [Parameter(ParameterSetName = InvokeCommandCommand.FilePathSSHHostParameterSet)] + [ValidateSet("true")] + public override string SSHTransportName + { + get { return base.SSHTransportName; } + + set { base.SSHTransportName = value; } + } + /// /// Hashtable array containing SSH connection parameters for each remote target /// ComputerName (Alias: HostName) (required) diff --git a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs index bbb2056189c..1b249af9a16 100644 --- a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs +++ b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs @@ -769,6 +769,17 @@ public virtual SwitchParameter SSHTransport set; } + /// + /// This parameter allows you to specify a custom executable name to use for the SSH transport. The executable must be on the path. + /// + [Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)] + [ValidateSet("true")] + public virtual string SSHTransportName + { + get; + set; + } + /// /// Hashtable array containing SSH connection parameters for each remote target /// ComputerName (Alias: HostName) (required) From f8c09537e094a847522cd19bf6d42bebce52c0d3 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Fri, 18 Dec 2020 15:24:06 -0800 Subject: [PATCH 3/5] Add SSHTransportPath, related constructor, and update runspace Copy function --- .../remoting/commands/InvokeCommandCommand.cs | 7 ++- .../remoting/commands/PSRemotingCmdlet.cs | 3 +- .../remoting/commands/PushRunspaceCommand.cs | 2 +- .../remoting/common/RunspaceConnectionInfo.cs | 46 ++++++++++++++++--- 4 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs index 874fb77ce09..29de22a0536 100644 --- a/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/InvokeCommandCommand.cs @@ -758,12 +758,11 @@ public override SwitchParameter SSHTransport /// [Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)] [Parameter(ParameterSetName = InvokeCommandCommand.FilePathSSHHostParameterSet)] - [ValidateSet("true")] - public override string SSHTransportName + public override string SSHTransportPath { - get { return base.SSHTransportName; } + get { return base.SSHTransportPath; } - set { base.SSHTransportName = value; } + set { base.SSHTransportPath = value; } } /// diff --git a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs index 1b249af9a16..45942e2c9dc 100644 --- a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs +++ b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs @@ -773,8 +773,7 @@ public virtual SwitchParameter SSHTransport /// This parameter allows you to specify a custom executable name to use for the SSH transport. The executable must be on the path. /// [Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)] - [ValidateSet("true")] - public virtual string SSHTransportName + public virtual string SSHTransportPath { get; set; diff --git a/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs b/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs index cbc5f82742b..acdcabed688 100644 --- a/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs @@ -1262,7 +1262,7 @@ private RemoteRunspace GetRunspaceForContainerSession() private RemoteRunspace GetRunspaceForSSHSession() { ParseSshHostName(HostName, out string host, out string userName, out int port); - var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port, this.Subsystem); + var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port, this.Subsystem, base.SSHTransportPath); var typeTable = TypeTable.LoadDefaultTypeFiles(); // Use the class _tempRunspace field while the runspace is being opened so that StopProcessing can be handled at that time. diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index 9783f9e52b9..d709f7d9bc4 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -1892,6 +1892,15 @@ private string Subsystem set; } + /// + /// Name of the ssh executable to use + /// + private string sshPath + { + get; + set; + } + #endregion #region Constructors @@ -1961,6 +1970,27 @@ public SSHConnectionInfo( this.Subsystem = (string.IsNullOrEmpty(subsystem)) ? DefaultSubsystem : subsystem; } + + /// + /// Constructor. + /// + /// User Name. + /// Computer Name. + /// Key File Path. + /// Port number for connection (default 22). + /// Subsystem to use (default 'powershell'). + /// Name or path to ssh executable (default 'ssh' or 'ssh.exe') + public SSHConnectionInfo( + string userName, + string computerName, + string keyFilePath, + int port, + string subsystem, + string sshPath) : this(userName, computerName, keyFilePath, port, subsystem) + { + this.sshPath = sshPath; + } + #endregion #region Overrides @@ -2016,6 +2046,7 @@ internal override RunspaceConnectionInfo InternalCopy() newCopy.KeyFilePath = this.KeyFilePath; newCopy.Port = this.Port; newCopy.Subsystem = this.Subsystem; + newCopy.sshPath = this.sshPath; return newCopy; } @@ -2049,15 +2080,18 @@ internal int StartSSHProcess( out StreamReader stdErrReaderVar) { string filePath = string.Empty; -#if UNIX - string sshCommand = "ssh"; -#else - string sshCommand = "ssh.exe"; -#endif + if (sshPath == null) { + #if UNIX + sshPath = "ssh"; + #else + sshPath = "ssh.exe"; + #endif + } + var context = Runspaces.LocalPipeline.GetExecutionContextFromTLS(); if (context != null) { - var cmdInfo = context.CommandDiscovery.LookupCommandInfo(sshCommand, CommandOrigin.Internal) as ApplicationInfo; + var cmdInfo = context.CommandDiscovery.LookupCommandInfo(sshPath, CommandOrigin.Internal) as ApplicationInfo; if (cmdInfo != null) { filePath = cmdInfo.Path; From ee9de8efeb04b8aa132afe8a64229cd6b22d9485 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Fri, 18 Dec 2020 15:44:12 -0800 Subject: [PATCH 4/5] CodeFactor Linting Cleanup --- .../remoting/commands/PSRemotingCmdlet.cs | 2 +- .../remoting/commands/PushRunspaceCommand.cs | 2 +- .../remoting/common/RunspaceConnectionInfo.cs | 24 +++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs index 45942e2c9dc..48a9a182af1 100644 --- a/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs +++ b/src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs @@ -770,7 +770,7 @@ public virtual SwitchParameter SSHTransport } /// - /// This parameter allows you to specify a custom executable name to use for the SSH transport. The executable must be on the path. + /// Gets or sets a custom executable name to use for the SSH transport. The executable must be on the path. /// [Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)] public virtual string SSHTransportPath diff --git a/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs b/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs index acdcabed688..b2b041eba41 100644 --- a/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs +++ b/src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs @@ -1262,7 +1262,7 @@ private RemoteRunspace GetRunspaceForContainerSession() private RemoteRunspace GetRunspaceForSSHSession() { ParseSshHostName(HostName, out string host, out string userName, out int port); - var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port, this.Subsystem, base.SSHTransportPath); + var sshConnectionInfo = new SSHConnectionInfo(userName, host, KeyFilePath, port, Subsystem, SSHTransportPath); var typeTable = TypeTable.LoadDefaultTypeFiles(); // Use the class _tempRunspace field while the runspace is being opened so that StopProcessing can be handled at that time. diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index d709f7d9bc4..ee7226f81a9 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -1893,9 +1893,9 @@ private string Subsystem } /// - /// Name of the ssh executable to use + /// Gets or sets theName of the ssh executable to use /// - private string sshPath + private string SshPath { get; set; @@ -1969,26 +1969,25 @@ public SSHConnectionInfo( this.Port = port; this.Subsystem = (string.IsNullOrEmpty(subsystem)) ? DefaultSubsystem : subsystem; } - /// - /// Constructor. + /// Initializes a new instance of the SSHConnectionInfo class. /// /// User Name. /// Computer Name. /// Key File Path. /// Port number for connection (default 22). /// Subsystem to use (default 'powershell'). - /// Name or path to ssh executable (default 'ssh' or 'ssh.exe') + /// Name or path to ssh executable (default 'ssh' or 'ssh.exe')'. public SSHConnectionInfo( string userName, string computerName, string keyFilePath, int port, string subsystem, - string sshPath) : this(userName, computerName, keyFilePath, port, subsystem) + string SshPath) : this(userName, computerName, keyFilePath, port, subsystem) { - this.sshPath = sshPath; + this.SshPath = SshPath; } #endregion @@ -2046,7 +2045,7 @@ internal override RunspaceConnectionInfo InternalCopy() newCopy.KeyFilePath = this.KeyFilePath; newCopy.Port = this.Port; newCopy.Subsystem = this.Subsystem; - newCopy.sshPath = this.sshPath; + newCopy.SshPath = this.SshPath; return newCopy; } @@ -2080,18 +2079,19 @@ internal int StartSSHProcess( out StreamReader stdErrReaderVar) { string filePath = string.Empty; - if (sshPath == null) { + if (SshPath == null) + { #if UNIX - sshPath = "ssh"; + SshPath = "ssh"; #else - sshPath = "ssh.exe"; + SshPath = "ssh.exe"; #endif } var context = Runspaces.LocalPipeline.GetExecutionContextFromTLS(); if (context != null) { - var cmdInfo = context.CommandDiscovery.LookupCommandInfo(sshPath, CommandOrigin.Internal) as ApplicationInfo; + var cmdInfo = context.CommandDiscovery.LookupCommandInfo(SshPath, CommandOrigin.Internal) as ApplicationInfo; if (cmdInfo != null) { filePath = cmdInfo.Path; From 5f7ed89de9f32eb0fa4f25f0f53ecae913c04ca7 Mon Sep 17 00:00:00 2001 From: Justin Grote Date: Fri, 18 Dec 2020 15:46:07 -0800 Subject: [PATCH 5/5] Further codefactor cleanup --- .../engine/remoting/common/RunspaceConnectionInfo.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index ee7226f81a9..21131208073 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -1893,7 +1893,7 @@ private string Subsystem } /// - /// Gets or sets theName of the ssh executable to use + /// Gets or sets the name of the ssh executable to use. /// private string SshPath { @@ -1978,16 +1978,16 @@ public SSHConnectionInfo( /// Key File Path. /// Port number for connection (default 22). /// Subsystem to use (default 'powershell'). - /// Name or path to ssh executable (default 'ssh' or 'ssh.exe')'. + /// Name or path to ssh executable (default 'ssh' or 'ssh.exe')'. public SSHConnectionInfo( string userName, string computerName, string keyFilePath, int port, string subsystem, - string SshPath) : this(userName, computerName, keyFilePath, port, subsystem) + string sshPath) : this(userName, computerName, keyFilePath, port, subsystem) { - this.SshPath = SshPath; + this.SshPath = sshPath; } #endregion