summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2021-08-02 10:55:46 -0700
committerGitHub <noreply@github.com>2021-08-02 10:55:46 -0700
commit170f6fb6530c520cb13f1c9eab33cbea2a06790c (patch)
tree43a3ddbe67653c822497a725c952bcad25e09926
parentff653b97cd269ad03ab7486aefabe46cd4c34a12 (diff)
parent5aff1581584e89630340690ed7a294678b01d196 (diff)
downloadchef-170f6fb6530c520cb13f1c9eab33cbea2a06790c.tar.gz
Merge pull request #11654 from MsysTechnologiesllc/smriti/11359_knife_bootstrap_sudo_password_broken
Bootstrap: Fix for performing sudo operations once sudo password enteā€¦
-rw-r--r--knife/lib/chef/knife/bootstrap.rb3
-rw-r--r--knife/spec/unit/knife/bootstrap_spec.rb13
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 bf5b17cc42..dc5e589c52 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