From a656a2bb29e13c6fb1808fc99211e302c861cc39 Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Fri, 7 Jun 2019 11:21:25 +0530 Subject: [PATCH 1/6] Fix wrong error message when root is used as default user when user is not provided and if the root user don't have ssh permissions Signed-off-by: Vasu1105 --- lib/train/extras/command_wrapper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/train/extras/command_wrapper.rb b/lib/train/extras/command_wrapper.rb index abda3845..3a45a8ed 100644 --- a/lib/train/extras/command_wrapper.rb +++ b/lib/train/extras/command_wrapper.rb @@ -162,7 +162,10 @@ class CommandWrapper include_options WindowsCommand def self.load(transport, options) - if transport.os.unix? + msg = transport.run_command("whoami").stdout + if msg.match?(/Please login as the user/) + raise Train::UserError.new("Sudo failed: #{msg}") + elsif transport.os.unix? return nil unless LinuxCommand.active?(options) res = LinuxCommand.new(transport, options) verification_res = res.verify From f466a5385d0994ff1bbbe102e504a75e35233b63 Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Fri, 7 Jun 2019 14:32:58 +0530 Subject: [PATCH 2/6] Created connected? method so that code is reusable Signed-off-by: Vasu1105 --- lib/train/extras/command_wrapper.rb | 9 ++++++--- lib/train/transports/ssh_connection.rb | 5 +++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/train/extras/command_wrapper.rb b/lib/train/extras/command_wrapper.rb index 3a45a8ed..1efc59c8 100644 --- a/lib/train/extras/command_wrapper.rb +++ b/lib/train/extras/command_wrapper.rb @@ -162,9 +162,12 @@ class CommandWrapper include_options WindowsCommand def self.load(transport, options) - msg = transport.run_command("whoami").stdout - if msg.match?(/Please login as the user/) - raise Train::UserError.new("Sudo failed: #{msg}") + if !transport.connected? + # Need to call run_command here to get the message and show it to user while raising exception + msg = transport.run_command("whoami").stdout + # this will throw the error as + # Train::UserError: Ssh failed: Please login as the user "username" rather than the user "root". + raise Train::UserError.new("Ssh failed: #{msg}") elsif transport.os.unix? return nil unless LinuxCommand.active?(options) res = LinuxCommand.new(transport, options) diff --git a/lib/train/transports/ssh_connection.rb b/lib/train/transports/ssh_connection.rb index 112c7f63..6f6be68a 100644 --- a/lib/train/transports/ssh_connection.rb +++ b/lib/train/transports/ssh_connection.rb @@ -101,6 +101,11 @@ def login_command LoginCommand.new("ssh", args) end + def connected? + return false if session.nil? + session.exec!("whoami").match?(/Please login as the user/) ? false : true + end + # (see Base::Connection#upload) def upload(locals, remote) waits = [] From ff4688703b6a2b4263444260647a35516220fce6 Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Fri, 7 Jun 2019 15:09:00 +0530 Subject: [PATCH 3/6] Not required on local connection Signed-off-by: Vasu1105 --- lib/train/extras/command_wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/train/extras/command_wrapper.rb b/lib/train/extras/command_wrapper.rb index 1efc59c8..978d1590 100644 --- a/lib/train/extras/command_wrapper.rb +++ b/lib/train/extras/command_wrapper.rb @@ -162,7 +162,7 @@ class CommandWrapper include_options WindowsCommand def self.load(transport, options) - if !transport.connected? + if transport.class != Train::Transports::Local::Connection && !transport.connected? # Need to call run_command here to get the message and show it to user while raising exception msg = transport.run_command("whoami").stdout # this will throw the error as From 4625a8ba30eed72f937a7043ff8fd89808ae709a Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Mon, 10 Jun 2019 19:14:36 +0530 Subject: [PATCH 4/6] Updated condition Signed-off-by: Vasu1105 --- lib/train/extras/command_wrapper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/train/extras/command_wrapper.rb b/lib/train/extras/command_wrapper.rb index 978d1590..223d0542 100644 --- a/lib/train/extras/command_wrapper.rb +++ b/lib/train/extras/command_wrapper.rb @@ -162,7 +162,7 @@ class CommandWrapper include_options WindowsCommand def self.load(transport, options) - if transport.class != Train::Transports::Local::Connection && !transport.connected? + if transport.class == Train::Transports::SSH::Connection && !transport.connected? # Need to call run_command here to get the message and show it to user while raising exception msg = transport.run_command("whoami").stdout # this will throw the error as From e36d9a87a09ce60381f6951a67c3c26a9c095efa Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Tue, 11 Jun 2019 13:38:43 +0530 Subject: [PATCH 5/6] Instead of fixing the issue in where platform detection is happening fixed it in command execution as this is the origin Signed-off-by: Vasu1105 --- lib/train/extras/command_wrapper.rb | 12 +++--------- lib/train/platforms/detect/helpers/os_common.rb | 6 ++++++ lib/train/transports/ssh_connection.rb | 5 ----- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/train/extras/command_wrapper.rb b/lib/train/extras/command_wrapper.rb index 223d0542..d7054228 100644 --- a/lib/train/extras/command_wrapper.rb +++ b/lib/train/extras/command_wrapper.rb @@ -162,22 +162,16 @@ class CommandWrapper include_options WindowsCommand def self.load(transport, options) - if transport.class == Train::Transports::SSH::Connection && !transport.connected? - # Need to call run_command here to get the message and show it to user while raising exception - msg = transport.run_command("whoami").stdout - # this will throw the error as - # Train::UserError: Ssh failed: Please login as the user "username" rather than the user "root". - raise Train::UserError.new("Ssh failed: #{msg}") - elsif transport.os.unix? + if transport.platform.unix? return nil unless LinuxCommand.active?(options) res = LinuxCommand.new(transport, options) verification_res = res.verify if verification_res msg, reason = verification_res - raise Train::UserError.new("Sudo failed: #{msg}", reason) + raise Train::UserError, "Sudo failed: #{msg}", reason end res - elsif transport.os.windows? + elsif transport.platform.windows? res = WindowsCommand.new(transport, options) res end diff --git a/lib/train/platforms/detect/helpers/os_common.rb b/lib/train/platforms/detect/helpers/os_common.rb index 88af6dad..f2126054 100644 --- a/lib/train/platforms/detect/helpers/os_common.rb +++ b/lib/train/platforms/detect/helpers/os_common.rb @@ -31,6 +31,12 @@ def unix_file_exist?(path) def command_output(cmd) res = @backend.run_command(cmd).stdout + # When you try to execute command using ssh connction as root user and you have provided ssh user identity file + # it gives standard output to login as authorised user other than root. To show this standard ouput as an error + # to user we are matching the string of stdout and raising the error here so that user gets exact information. + if @backend.class == Train::Transports::SSH::Connection && res =~ /Please login as the user/ + raise Train::UserError, "SSHFailed: #{res}" + end res.strip! unless res.nil? res end diff --git a/lib/train/transports/ssh_connection.rb b/lib/train/transports/ssh_connection.rb index 6f6be68a..112c7f63 100644 --- a/lib/train/transports/ssh_connection.rb +++ b/lib/train/transports/ssh_connection.rb @@ -101,11 +101,6 @@ def login_command LoginCommand.new("ssh", args) end - def connected? - return false if session.nil? - session.exec!("whoami").match?(/Please login as the user/) ? false : true - end - # (see Base::Connection#upload) def upload(locals, remote) waits = [] From 0521bfe3c29e9c2da046c43fe5849e06ec714023 Mon Sep 17 00:00:00 2001 From: Vasu1105 Date: Tue, 11 Jun 2019 14:25:14 +0530 Subject: [PATCH 6/6] Fix uninitialized constant error Signed-off-by: Vasu1105 --- lib/train/platforms/detect/helpers/os_common.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/train/platforms/detect/helpers/os_common.rb b/lib/train/platforms/detect/helpers/os_common.rb index f2126054..7fb070e6 100644 --- a/lib/train/platforms/detect/helpers/os_common.rb +++ b/lib/train/platforms/detect/helpers/os_common.rb @@ -34,7 +34,7 @@ def command_output(cmd) # When you try to execute command using ssh connction as root user and you have provided ssh user identity file # it gives standard output to login as authorised user other than root. To show this standard ouput as an error # to user we are matching the string of stdout and raising the error here so that user gets exact information. - if @backend.class == Train::Transports::SSH::Connection && res =~ /Please login as the user/ + if @backend.class.to_s == "Train::Transports::SSH::Connection" && res =~ /Please login as the user/ raise Train::UserError, "SSHFailed: #{res}" end res.strip! unless res.nil?