Fixes #9591: Allow for 'default' smb_username if set

Prior to this commit, Vagrant would prompt for smb username and password
every time, even if only smb_username was defined. This commit changes
that by allowing a "default" username from the Vagrantfile, with the
option of overriding it.
This commit is contained in:
Brian Cain 2018-10-19 13:17:41 -07:00
parent 28c4940969
commit 65651178cd
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 48 additions and 5 deletions

View File

@ -39,19 +39,27 @@ module VagrantPlugins
# If we need auth information, then ask the user.
have_auth = false
folders.each do |id, data|
if data[:smb_username] && data[:smb_password]
smb_username = data[:smb_username]
smb_password = data[:smb_password]
smb_username = data[:smb_username] if data[:smb_username]
smb_password = data[:smb_password] if data[:smb_password]
if smb_username && smb_password
have_auth = true
break
end
end
modify_username = false
if !have_auth
machine.ui.detail(I18n.t("vagrant_sf_smb.warning_password") + "\n ")
retries = 0
while retries < CREDENTIAL_RETRY_MAX do
smb_username = machine.ui.ask("Username: ")
if smb_username
username = machine.ui.ask("Username (#{smb_username}): ")
smb_username = username if username != ""
modify_username = true
else
smb_username = machine.ui.ask("Username: ")
end
smb_password = machine.ui.ask("Password (will be hidden): ", echo: false)
auth_success = true
@ -77,7 +85,12 @@ module VagrantPlugins
end
folders.each do |id, data|
data[:smb_username] ||= smb_username
if modify_username
# Only override original username if user requests to
data[:smb_username] = smb_username
else
data[:smb_username] ||= smb_username
end
data[:smb_password] ||= smb_password
# Register password as sensitive

View File

@ -69,6 +69,36 @@ describe VagrantPlugins::SyncedFolderSMB::SyncedFolder do
describe ".prepare" do
let(:host_caps){ [:smb_start, :smb_prepare] }
context "with username credentials provided" do
let(:folders){ {'/first/path' => {smb_username: 'smbuser'}} }
it "should prompt for credentials" do
expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('username').at_least(1)
expect(machine.env.ui).to receive(:ask).with(/word/, any_args).and_return('password').at_least(1)
subject.prepare(machine, folders, options)
end
it "should set credential information into all folder options and override username" do
expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('username').at_least(1)
expect(machine.env.ui).to receive(:ask).with(/word/, any_args).and_return('password').at_least(1)
subject.prepare(machine, folders, options)
expect(folders['/first/path'][:smb_username]).to eq('username')
expect(folders['/first/path'][:smb_password]).to eq('password')
end
it "will use configured default with no input" do
expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('').at_least(1)
expect(machine.env.ui).to receive(:ask).with(/word/, any_args).and_return('password').at_least(1)
subject.prepare(machine, folders, options)
expect(folders['/first/path'][:smb_username]).to eq('smbuser')
expect(folders['/first/path'][:smb_password]).to eq('password')
end
end
context "without credentials provided" do
before do
expect(machine.env.ui).to receive(:ask).with(/name/, any_args).and_return('username').at_least(1)