diff options
author | smriti <sgarg@msystechnologies.com> | 2021-07-28 21:51:10 +0530 |
---|---|---|
committer | smriti <sgarg@msystechnologies.com> | 2021-07-28 21:51:28 +0530 |
commit | 5aff1581584e89630340690ed7a294678b01d196 (patch) | |
tree | 31729c803e5d06a3f1a25e924d70231e7ff06cfd | |
parent | ec33c4b9b46f6905e14aef7deda2cd9637af5078 (diff) | |
download | chef-5aff1581584e89630340690ed7a294678b01d196.tar.gz |
Changes for displaying proper error message to user for missing terminal on sudo error-s
-rw-r--r-- | knife/lib/chef/knife/bootstrap.rb | 3 | ||||
-rw-r--r-- | knife/spec/unit/knife/bootstrap_spec.rb | 13 |
2 files changed, 16 insertions, 0 deletions
diff --git a/knife/lib/chef/knife/bootstrap.rb b/knife/lib/chef/knife/bootstrap.rb index d57614cb3d..d6687d8161 100644 --- a/knife/lib/chef/knife/bootstrap.rb +++ b/knife/lib/chef/knife/bootstrap.rb @@ -20,6 +20,7 @@ require_relative "../knife" require_relative "data_bag_secret_options" require "chef-utils/dist" unless defined?(ChefUtils::Dist) require "license_acceptance/cli_flags/mixlib_cli" + module LicenseAcceptance autoload :Acceptor, "license_acceptance/acceptor" end @@ -705,6 +706,8 @@ class Chef ui.warn("#{e.message} - trying with pty request") conn_options[:pty] = true # ensure we can talk to systems with requiretty set true in sshd config retry + elsif e.reason == :sudo_missing_terminal + ui.error "Sudo password is required for this operation. Please enter password using -P or --ssh-password option" elsif config[:use_sudo_password] && (e.reason == :sudo_password_required || e.reason == :bad_sudo_password) && limit < 3 ui.warn("Failed to authenticate #{conn_options[:user]} to #{server_name} - #{e.message} \n sudo: #{limit} incorrect password attempt") sudo_password = ui.ask("Enter sudo password for #{conn_options[:user]}@#{server_name}:", echo: false) diff --git a/knife/spec/unit/knife/bootstrap_spec.rb b/knife/spec/unit/knife/bootstrap_spec.rb index 30878edf39..c5b2f945e0 100644 --- a/knife/spec/unit/knife/bootstrap_spec.rb +++ b/knife/spec/unit/knife/bootstrap_spec.rb @@ -2050,6 +2050,19 @@ describe Chef::Knife::Bootstrap do expect { knife.do_connect({}) }.to raise_error(expected_error) end end + + context "when a train sudo error is thrown for missing terminal" do + let(:ui_error_msg) { "Sudo password is required for this operation. Please enter password using -P or --ssh-password option" } + let(:expected_error) { Train::UserError.new(ui_error_msg, :sudo_missing_terminal) } + before do + allow(connection).to receive(:connect!).and_raise(expected_error) + end + it "outputs user friendly error message" do + expect { knife.do_connect({}) }.not_to raise_error + expect(stderr.string).to include(ui_error_msg) + end + end + end describe "validate_winrm_transport_opts!" do |