From d3fffd65d254a3d40276a4984786473866ef9c1a Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 15 Mar 2021 15:21:07 -0700 Subject: [PATCH 1/4] Prevent notifications on redirect to default store This also checks if the redirect notification has been displayed before inspecting the source and location to prevent repeat checks after the notification has been sent. --- lib/vagrant/util/curl_helper.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/vagrant/util/curl_helper.rb b/lib/vagrant/util/curl_helper.rb index 8c3d205d9..07e0a5dbf 100644 --- a/lib/vagrant/util/curl_helper.rb +++ b/lib/vagrant/util/curl_helper.rb @@ -4,6 +4,7 @@ module Vagrant # Hosts that do not require notification on redirect SILENCED_HOSTS = [ + "vagrantcloud-files-production.s3-accelerate.amazonaws.com".freeze, "vagrantcloud.com".freeze, "vagrantup.com".freeze ].freeze @@ -21,6 +22,7 @@ module Vagrant # Accumulate progress_data progress_data << data + redirect_notify = false while true # If the download has been redirected and we are no longer downloading # from the original host, notify the user that the target host has @@ -30,16 +32,15 @@ module Vagrant if !location.empty? location_uri = URI.parse(location) - unless location_uri.host.nil? - redirect_notify = false + if !location_uri.host.nil? && !redirect_notify logger.info("download redirected to #{location}") source_uri = URI.parse(source) source_host = source_uri.host.to_s.split(".", 2).last location_host = location_uri.host.to_s.split(".", 2).last - if !redirect_notify && location_host != source_host && !SILENCED_HOSTS.include?(location_host) - ui.rewriting do |ui| - ui.clear_line - ui.detail "Download redirected to host: #{location_uri.host}" + if location_host != source_host && !SILENCED_HOSTS.include?(location_host) && !SILENCED_HOSTS.include?(location_uri.host.to_s) + ui.rewriting do |_ui| + _ui.clear_line + _ui.detail "Download redirected to host: #{location_uri.host}" end end redirect_notify = true From d580fbbee2bf33bbe9a5f4ec9555882f3e2fd74b Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 15 Mar 2021 15:23:14 -0700 Subject: [PATCH 2/4] Use the server url helper method within the client Some client setup locations where not using the custom helper method for the vagrant server URL value so they have been updated. The api path is also appended if it is not set for custom server URLs --- plugins/commands/cloud/client/client.rb | 13 ++++++++++--- plugins/commands/cloud/locales/en.yml | 2 +- plugins/commands/cloud/util.rb | 12 ++++++++++-- test/unit/plugins/commands/cloud/client_test.rb | 4 ++-- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/plugins/commands/cloud/client/client.rb b/plugins/commands/cloud/client/client.rb index 0e9452e1d..365973706 100644 --- a/plugins/commands/cloud/client/client.rb +++ b/plugins/commands/cloud/client/client.rb @@ -18,6 +18,7 @@ module VagrantPlugins ###################################################################### APP = "app".freeze + include Util include Vagrant::Util::Presence attr_accessor :client @@ -32,7 +33,10 @@ module VagrantPlugins def initialize(env) @logger = Log4r::Logger.new("vagrant::cloud::client") @env = env - @client = VagrantCloud::Client.new(access_token: token) + @client = VagrantCloud::Client.new( + access_token: token, + url_base: api_server_url + ) end # Removes the token, effectively logging the user out. @@ -72,7 +76,10 @@ module VagrantPlugins password: password, description: description, code: code) Vagrant::Util::CredentialScrubber.sensitive(r[:token]) - @client = VagrantCloud::Client.new(access_token: r[:token]) + @client = VagrantCloud::Client.new( + access_token: r[:token], + url_base: api_server_url + ) r[:token] end end @@ -106,7 +113,7 @@ module VagrantPlugins end # Reset after we store the token since this is now _our_ token - @client = VagrantCloud::Client.new(access_token: token) + @client = VagrantCloud::Client.new(access_token: token, url_base: api_server_url) nil end diff --git a/plugins/commands/cloud/locales/en.yml b/plugins/commands/cloud/locales/en.yml index 451bd8dbe..713282261 100644 --- a/plugins/commands/cloud/locales/en.yml +++ b/plugins/commands/cloud/locales/en.yml @@ -112,7 +112,7 @@ en: Failed to update box %{org}/%{box_name} whoami: read_error: |- - Failed to read organization %{org} + Failed to locate account information provider: create_fail: |- Failed to create provider %{provider} on box %{org}/%{box_name} for version %{version} diff --git a/plugins/commands/cloud/util.rb b/plugins/commands/cloud/util.rb index 355af731d..36856c5ba 100644 --- a/plugins/commands/cloud/util.rb +++ b/plugins/commands/cloud/util.rb @@ -5,8 +5,16 @@ module VagrantPlugins def api_server_url if Vagrant.server_url == Vagrant::DEFAULT_SERVER_URL return "#{Vagrant.server_url}/api/v1" - else - return Vagrant.server_url + end + begin + addr = URI.parse(Vagrant.server_url.to_s) + if addr.path.empty? || addr.path.to_s == "/" + addr.path = "/api/v1" + end + + addr.to_s + rescue URI::Error + Vagrant.server_url end end diff --git a/test/unit/plugins/commands/cloud/client_test.rb b/test/unit/plugins/commands/cloud/client_test.rb index 843d4e93b..c6be8d842 100644 --- a/test/unit/plugins/commands/cloud/client_test.rb +++ b/test/unit/plugins/commands/cloud/client_test.rb @@ -101,7 +101,7 @@ describe VagrantPlugins::CloudCommand::Client do end it "should create a new internal client" do - expect(VagrantCloud::Client).to receive(:new).with(access_token: new_token) + expect(VagrantCloud::Client).to receive(:new).with(access_token: new_token, url_base: anything) subject.login end @@ -174,7 +174,7 @@ describe VagrantPlugins::CloudCommand::Client do end it "should create a new internal client with token" do - expect(VagrantCloud::Client).to receive(:new).with(access_token: new_token) + expect(VagrantCloud::Client).to receive(:new).with(access_token: new_token, url_base: anything) subject.store_token(new_token) end From 3316098bb63d1cc5b0e743c254f11c9b643df7ac Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 17 Mar 2021 13:08:30 -0700 Subject: [PATCH 3/4] White space trim --- .../add_downloader_authentication_test.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 517d789c6..0ddb463de 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 @@ -53,16 +53,16 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do it "warns when adding token to custom server" do server_url = "https://surprise.com" allow(Vagrant).to receive(:server_url).and_return(server_url) - + token = "foobarbaz" VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token) - + expect(subject).to receive(:sleep).once expect(ui).to receive(:warn).once - + env[:downloader] = dwnloader subject.call(env) - + expect(env[:downloader].headers).to eq(["Authorization: Bearer #{token}"]) end end @@ -97,11 +97,11 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do let(:auth_header) { "Authorization Bearer: token" } let(:other_header) {"some: thing"} let(:dwnloader) { Vagrant::Util::Downloader.new(server_url, "/some/path", {headers: [other_header]}) } - + it "appends the auth header" do token = "foobarbaz" VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token) - + env[:downloader] = dwnloader subject.call(env) @@ -115,7 +115,7 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do it "returns original urls when not modified" do env[:downloader] = dwnloader subject.call(env) - + expect(env[:downloader].source).to eq(file_path) expect(env[:downloader].headers.empty?).to eq(true) end @@ -125,7 +125,7 @@ describe VagrantPlugins::CloudCommand::AddDownloaderAuthentication do dwnloader.headers << auth_header token = "foobarbaz" VagrantPlugins::CloudCommand::Client.new(iso_env).store_token(token) - + env[:downloader] = dwnloader subject.call(env) From 16e91e4ba2bf15940a85ef484981e6373d33892f Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 17 Mar 2021 13:08:43 -0700 Subject: [PATCH 4/4] Do not convert value to allow nil value to pass through --- plugins/commands/cloud/util.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/commands/cloud/util.rb b/plugins/commands/cloud/util.rb index 36856c5ba..525762113 100644 --- a/plugins/commands/cloud/util.rb +++ b/plugins/commands/cloud/util.rb @@ -7,7 +7,7 @@ module VagrantPlugins return "#{Vagrant.server_url}/api/v1" end begin - addr = URI.parse(Vagrant.server_url.to_s) + addr = URI.parse(Vagrant.server_url) if addr.path.empty? || addr.path.to_s == "/" addr.path = "/api/v1" end