diff --git a/RemoteFile/RemoteCertificateStore.cs b/RemoteFile/RemoteCertificateStore.cs index 3a8dd18..ce0538f 100644 --- a/RemoteFile/RemoteCertificateStore.cs +++ b/RemoteFile/RemoteCertificateStore.cs @@ -454,8 +454,6 @@ internal void Initialize(string sudoImpersonatedUser) else RemoteHandler = new WinRMHandler(Server, ServerId, ServerPassword, treatAsLocal); - RemoteHandler.Initialize(); - logger.MethodExit(LogLevel.Debug); } diff --git a/RemoteFile/RemoteHandlers/BaseRemoteHandler.cs b/RemoteFile/RemoteHandlers/BaseRemoteHandler.cs index 179e929..f8a0ca6 100644 --- a/RemoteFile/RemoteHandlers/BaseRemoteHandler.cs +++ b/RemoteFile/RemoteHandlers/BaseRemoteHandler.cs @@ -33,8 +33,6 @@ public static void AreLinuxPermissionsValid(string permissions) throw new RemoteFileException($"Invalid format for Linux file permissions. This value must be exactly 3 digits long with each digit between 0-7 but found {permissions} instead."); } - public abstract void Initialize(); - public abstract void Terminate(); public abstract string RunCommand(string commandText, object[] arguments, bool withSudo, string[] passwordsToMaskInLog); diff --git a/RemoteFile/RemoteHandlers/IRemoteHandler.cs b/RemoteFile/RemoteHandlers/IRemoteHandler.cs index 34f93a9..619e3b9 100644 --- a/RemoteFile/RemoteHandlers/IRemoteHandler.cs +++ b/RemoteFile/RemoteHandlers/IRemoteHandler.cs @@ -13,8 +13,6 @@ namespace Keyfactor.Extensions.Orchestrator.RemoteFile.RemoteHandlers /// interface IRemoteHandler { - void Initialize(); - void Terminate(); string RunCommand(string commandText, object[] arguments, bool withSudo, string[] passwordsToMaskInLog); diff --git a/RemoteFile/RemoteHandlers/LinuxLocalHandler.cs b/RemoteFile/RemoteHandlers/LinuxLocalHandler.cs index 8195ad4..f7b45ac 100644 --- a/RemoteFile/RemoteHandlers/LinuxLocalHandler.cs +++ b/RemoteFile/RemoteHandlers/LinuxLocalHandler.cs @@ -27,12 +27,6 @@ class LinuxLocalHandler : BaseRemoteHandler private Command BaseCommand { get; set; } internal LinuxLocalHandler() - { - _logger.MethodEntry(LogLevel.Debug); - _logger.MethodExit(LogLevel.Debug); - } - - public override void Initialize() { _logger.MethodEntry(LogLevel.Debug); diff --git a/RemoteFile/RemoteHandlers/SSHHandler.cs b/RemoteFile/RemoteHandlers/SSHHandler.cs index ab95c5c..c21fbd3 100644 --- a/RemoteFile/RemoteHandlers/SSHHandler.cs +++ b/RemoteFile/RemoteHandlers/SSHHandler.cs @@ -20,6 +20,7 @@ using Keyfactor.PKI.PEM; using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext; using Renci.SshNet.Common; +using Org.BouncyCastle.Bcpg; namespace Keyfactor.Extensions.Orchestrator.RemoteFile.RemoteHandlers { @@ -28,6 +29,7 @@ class SSHHandler : BaseRemoteHandler private ConnectionInfo Connection { get; set; } private string SudoImpersonatedUser { get; set; } private bool IsStoreServerLinux { get; set; } + private string UserId { get; set; } private string Password { get; set; } private SshClient sshClient; @@ -38,16 +40,25 @@ internal SSHHandler(string server, string serverLogin, string serverPassword, bo Server = server; SudoImpersonatedUser = sudoImpersonatedUser; IsStoreServerLinux = isStoreServerLinux; + UserId = serverLogin; Password = serverPassword; - List authenticationMethods = new List(); if (serverPassword.Length < PASSWORD_LENGTH_MAX) { - authenticationMethods.Add(new PasswordAuthenticationMethod(serverLogin, serverPassword)); - - KeyboardInteractiveAuthenticationMethod keyboardAuthentication = new KeyboardInteractiveAuthenticationMethod(UserId); - keyboardAuthentication.AuthenticationPrompt += KeyboardAuthentication_AuthenticationPrompt; - authenticationMethods.Add(keyboardAuthentication); + try + { + Connection = new ConnectionInfo(server, serverLogin, new PasswordAuthenticationMethod(serverLogin, serverPassword)); + SshClient tempSshClient = new SshClient(Connection); + tempSshClient.Connect(); + tempSshClient.Disconnect(); + tempSshClient.Dispose(); + } + catch (Exception ex) + { + KeyboardInteractiveAuthenticationMethod keyboardAuthentication = new KeyboardInteractiveAuthenticationMethod(UserId); + keyboardAuthentication.AuthenticationPrompt += KeyboardAuthentication_AuthenticationPrompt; + Connection = new ConnectionInfo(server, serverLogin, keyboardAuthentication); + } } else { @@ -68,18 +79,9 @@ internal SSHHandler(string server, string serverLogin, string serverPassword, bo } } - authenticationMethods.Add(new PrivateKeyAuthenticationMethod(serverLogin, privateKeyFile)); + Connection = new ConnectionInfo(server, serverLogin, new PrivateKeyAuthenticationMethod(serverLogin, privateKeyFile)); } - Connection = new ConnectionInfo(server, serverLogin, authenticationMethods.ToArray()); - - _logger.MethodExit(LogLevel.Debug); - } - - public override void Initialize() - { - _logger.MethodEntry(LogLevel.Debug); - try { sshClient = new SshClient(Connection); diff --git a/RemoteFile/RemoteHandlers/WinRMHandler.cs b/RemoteFile/RemoteHandlers/WinRMHandler.cs index c884283..1d1a041 100644 --- a/RemoteFile/RemoteHandlers/WinRMHandler.cs +++ b/RemoteFile/RemoteHandlers/WinRMHandler.cs @@ -43,13 +43,6 @@ internal WinRMHandler(string server, string serverLogin, string serverPassword, } } - _logger.MethodExit(LogLevel.Debug); - } - - public override void Initialize() - { - _logger.MethodEntry(LogLevel.Debug); - try { if (RunLocal) @@ -69,8 +62,8 @@ public override void Initialize() catch (Exception ex) { - _logger.LogError($"Exception during Initialize...{RemoteFileException.FlattenExceptionMessages(ex, ex.Message)}"); - throw ex; + _logger.LogError($"Exception attempting to connect to server...{RemoteFileException.FlattenExceptionMessages(ex, ex.Message)}"); + throw; } _logger.MethodExit(LogLevel.Debug);