summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2019-09-09 19:39:40 -0700
committerGitHub <noreply@github.com>2019-09-09 19:39:40 -0700
commit683b1284e18afa9a0dac508afb5b590aa233b09c (patch)
tree782932c8ff41d3bd51f6b1dbe19d9b78f9d112b9
parenta5854d7ae6100a4f4a125d15dbef8fa69ca1cfad (diff)
parentb95561376295a74b04664004071844a3ac52d849 (diff)
downloadchef-683b1284e18afa9a0dac508afb5b590aa233b09c.tar.gz
Merge pull request #8816 from MsysTechnologiesllc/VSingh/MSYS-1063-retry-with-pty-true
Bootstrap: Set pty true only if required
-rw-r--r--lib/chef/knife/bootstrap.rb9
-rw-r--r--spec/unit/knife/bootstrap_spec.rb18
2 files changed, 21 insertions, 6 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb
index e892f1f2c9..efa1515858 100644
--- a/lib/chef/knife/bootstrap.rb
+++ b/lib/chef/knife/bootstrap.rb
@@ -672,6 +672,14 @@ class Chef
def do_connect(conn_options)
@connection = TrainConnector.new(host_descriptor, connection_protocol, conn_options)
connection.connect!
+ rescue Train::UserError => e
+ if !conn_options.key?(:pty) && e.reason == :sudo_no_tty
+ 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
+ else
+ raise
+ end
end
# Fail if both first_boot_attributes and first_boot_attributes_from_file
@@ -895,7 +903,6 @@ class Chef
opts = {}
return opts if winrm?
- opts[:pty] = true # ensure we can talk to systems with requiretty set true in sshd config
opts[:non_interactive] = true # Prevent password prompts from underlying net/ssh
opts[:forward_agent] = (config_value(:ssh_forward_agent) === true)
opts[:connection_timeout] = session_timeout
diff --git a/spec/unit/knife/bootstrap_spec.rb b/spec/unit/knife/bootstrap_spec.rb
index 5f4be8dfa2..752fcff1e3 100644
--- a/spec/unit/knife/bootstrap_spec.rb
+++ b/spec/unit/knife/bootstrap_spec.rb
@@ -1021,7 +1021,6 @@ describe Chef::Knife::Bootstrap do
verify_host_key: nil,
port: 9999,
non_interactive: true,
- pty: true,
}
end
@@ -1076,7 +1075,6 @@ describe Chef::Knife::Bootstrap do
verify_host_key: nil, # Config
port: 12, # cli
non_interactive: true,
- pty: true,
}
end
@@ -1128,7 +1126,6 @@ describe Chef::Knife::Bootstrap do
sudo_password: "blah",
verify_host_key: true,
non_interactive: true,
- pty: true,
}
end
it "generates a config hash using the CLI options and pulling nothing from Chef::Config" do
@@ -1152,7 +1149,6 @@ describe Chef::Knife::Bootstrap do
sudo: false,
verify_host_key: "always",
non_interactive: true,
- pty: true,
connection_timeout: 60,
}
end
@@ -1504,7 +1500,6 @@ describe Chef::Knife::Bootstrap do
let(:default_opts) do
{
non_interactive: true,
- pty: true,
forward_agent: false,
connection_timeout: 60,
}
@@ -2003,6 +1998,19 @@ describe Chef::Knife::Bootstrap do
expect(connection).to receive(:connect!)
knife.do_connect({})
end
+
+ context "when sshd confgiured with requiretty" do
+ let(:pty_err_msg) { "Sudo requires a TTY. Please see the README on how to configure sudo to allow for non-interactive usage." }
+ let(:expected_error) { Train::UserError.new(pty_err_msg, :sudo_no_tty) }
+ before do
+ allow(connection).to receive(:connect!).and_raise(expected_error)
+ end
+ it "retry with pty true request option" do
+ expect(Chef::Knife::Bootstrap::TrainConnector).to receive(:new).and_return(connection).exactly(2).times
+ expect(knife.ui).to receive(:warn).with("#{pty_err_msg} - trying with pty request")
+ expect { knife.do_connect({}) }.to raise_error(expected_error)
+ end
+ end
end
describe "validate_winrm_transport_opts!" do