Print a warning when token and envvar are set

Fixes GH-7206
This commit is contained in:
Seth Vargo 2016-04-08 10:16:10 -04:00
parent 42c5d98f60
commit 2d0943b0ad
2 changed files with 25 additions and 1 deletions

View File

@ -1,9 +1,12 @@
require "rest_client" require "rest_client"
require "vagrant/util/downloader" require "vagrant/util/downloader"
require "vagrant/util/presence"
module VagrantPlugins module VagrantPlugins
module LoginCommand module LoginCommand
class Client class Client
include Vagrant::Util::Presence
# Initializes a login client with the given Vagrant::Environment. # Initializes a login client with the given Vagrant::Environment.
# #
# @param [Vagrant::Environment] env # @param [Vagrant::Environment] env
@ -88,7 +91,21 @@ module VagrantPlugins
# #
# @return [String] # @return [String]
def token 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") @logger.debug("Using authentication token from environment variable")
return ENV["ATLAS_TOKEN"] return ENV["ATLAS_TOKEN"]
end end

View File

@ -112,6 +112,13 @@ describe VagrantPlugins::LoginCommand::Client do
expect(subject.token).to eq("ABCD1234") expect(subject.token).to eq("ABCD1234")
end 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 it "returns nil if there's no token set" do
expect(subject.token).to be(nil) expect(subject.token).to be(nil)
end end