diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index e00fac2d9..41b63b292 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -58,6 +58,7 @@ module Vagrant # @param [Pathname] directory The directory where this box exists on # disk. # @param [String] metadata_url Metadata URL for box + # @param [Hook] hook A hook to apply to the box downloader, for example, for authentication def initialize(name, provider, version, directory, metadata_url: nil, hook: nil) @name = name @version = version diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index a8a0968c4..eee9744e7 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -59,7 +59,7 @@ module Vagrant @ca_cert = options[:ca_cert] @ca_path = options[:ca_path] @continue = options[:continue] - @headers = options[:headers] + @headers = Array(options[:headers]) @insecure = options[:insecure] @ui = options[:ui] @client_cert = options[:client_cert] diff --git a/plugins/commands/cloud/auth/middleware/add_downloader_authentication.rb b/plugins/commands/cloud/auth/middleware/add_downloader_authentication.rb index 2c7237c36..ae00302a7 100644 --- a/plugins/commands/cloud/auth/middleware/add_downloader_authentication.rb +++ b/plugins/commands/cloud/auth/middleware/add_downloader_authentication.rb @@ -13,6 +13,8 @@ module VagrantPlugins module CloudCommand class AddDownloaderAuthentication < AddAuthentication + @@logger = Log4r::Logger.new("vagrant::clout::add_download_authentication") + def call(env) client = Client.new(env[:env]) token = client.token @@ -38,8 +40,9 @@ module VagrantPlugins self.class.custom_host_notified! end - if !Array(env[:downloader].headers).any? { |h| h.include?("Authorization") } - env[:downloader].headers ||= [] + if Array(env[:downloader].headers).any? { |h| h.include?("Authorization") } + @@logger.info("Not adding an authentication header, one already found") + else env[:downloader].headers << "Authorization: Bearer #{token}" end end diff --git a/test/unit/plugins/commands/cloud/auth/middleware/add_downloader_authentication_test.rb b/test/unit/plugins/commands/cloud/auth/middleware/add_downloader_authentication_test.rb index 9def49ff1..517d789c6 100644 --- a/test/unit/plugins/commands/cloud/auth/middleware/add_downloader_authentication_test.rb +++ b/test/unit/plugins/commands/cloud/auth/middleware/add_downloader_authentication_test.rb @@ -36,13 +36,13 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do env[:downloader] = dwnloader subject.call(env) - expect(env[:downloader].headers.nil?).to eq(true) + expect(env[:downloader].headers.empty?).to eq(true) end it "does nothing if we aren't logged in" do env[:downloader] = dwnloader subject.call(env) - expect(env[:downloader].headers.nil?).to eq(true) + expect(env[:downloader].headers.empty?).to eq(true) end end @@ -117,7 +117,7 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do subject.call(env) expect(env[:downloader].source).to eq(file_path) - expect(env[:downloader].headers).to eq(nil) + expect(env[:downloader].headers.empty?).to eq(true) end end @@ -149,7 +149,7 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token) env[:downloader] = dwnloader subject.call(env) - expect(env[:downloader].headers).to eq(nil) + expect(env[:downloader].headers.empty?).to eq(true) end end end