From 2d0943b0ad559611bb0028b9b0bf398a2e13eda3 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Fri, 8 Apr 2016 10:16:10 -0400 Subject: [PATCH] Print a warning when token and envvar are set Fixes GH-7206 --- plugins/commands/login/client.rb | 19 ++++++++++++++++++- .../plugins/commands/login/client_test.rb | 7 +++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/plugins/commands/login/client.rb b/plugins/commands/login/client.rb index 42639c2ab..04b6ee0e0 100644 --- a/plugins/commands/login/client.rb +++ b/plugins/commands/login/client.rb @@ -1,9 +1,12 @@ require "rest_client" require "vagrant/util/downloader" +require "vagrant/util/presence" module VagrantPlugins module LoginCommand class Client + include Vagrant::Util::Presence + # Initializes a login client with the given Vagrant::Environment. # # @param [Vagrant::Environment] env @@ -88,7 +91,21 @@ module VagrantPlugins # # @return [String] def token - if ENV["ATLAS_TOKEN"] && !ENV["ATLAS_TOKEN"].empty? + if present?(ENV["ATLAS_TOKEN"]) && token_path.exist? + @env.ui.warn <<-EOH.strip +Vagrant detected both the ATLAS_TOKEN environment variable and a Vagrant login +token are present on this system. The ATLAS_TOKEN environment variable takes +precedence over the locally stored token. To remove this error, either unset +the ATLAS_TOKEN environment variable or remove the login token stored on disk: + + ~/.vagrant.d/data/vagrant_login_token + +In general, the ATLAS_TOKEN is more preferred because it is respected by all +HashiCorp products. +EOH + end + + if present?(ENV["ATLAS_TOKEN"]) @logger.debug("Using authentication token from environment variable") return ENV["ATLAS_TOKEN"] end diff --git a/test/unit/plugins/commands/login/client_test.rb b/test/unit/plugins/commands/login/client_test.rb index 2f49ec6f5..d8d744ee5 100644 --- a/test/unit/plugins/commands/login/client_test.rb +++ b/test/unit/plugins/commands/login/client_test.rb @@ -112,6 +112,13 @@ describe VagrantPlugins::LoginCommand::Client do expect(subject.token).to eq("ABCD1234") end + it "prints a warning if the envvar and stored file are both present" do + stub_env("ATLAS_TOKEN" => "ABCD1234") + subject.store_token("EFGH5678") + expect(env.ui).to receive(:warn).with(/detected both/) + subject.token + end + it "returns nil if there's no token set" do expect(subject.token).to be(nil) end