From 65651178cdd6825abcecda482f008d1c148d4b22 Mon Sep 17 00:00:00 2001 From: Brian Cain Date: Fri, 19 Oct 2018 13:17:41 -0700 Subject: [PATCH] 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. --- plugins/synced_folders/smb/synced_folder.rb | 23 ++++++++++---- .../synced_folders/smb/synced_folder_test.rb | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/plugins/synced_folders/smb/synced_folder.rb b/plugins/synced_folders/smb/synced_folder.rb index 4a95a0da5..f382c36c9 100644 --- a/plugins/synced_folders/smb/synced_folder.rb +++ b/plugins/synced_folders/smb/synced_folder.rb @@ -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 diff --git a/test/unit/plugins/synced_folders/smb/synced_folder_test.rb b/test/unit/plugins/synced_folders/smb/synced_folder_test.rb index 0b1e9c85d..493a427f5 100644 --- a/test/unit/plugins/synced_folders/smb/synced_folder_test.rb +++ b/test/unit/plugins/synced_folders/smb/synced_folder_test.rb @@ -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)