Merge pull request #5130 from mitchellh/sethvargo/validate_pushes
Actually validate push configurations
This commit is contained in:
commit
adfe3caafb
|
@ -7,11 +7,13 @@ module VagrantPlugins
|
||||||
#
|
#
|
||||||
# @param [Vagrant::Environment] env
|
# @param [Vagrant::Environment] env
|
||||||
def initialize(env)
|
def initialize(env)
|
||||||
@env = env
|
@logger = Log4r::Logger.new("vagrant::login::client")
|
||||||
|
@env = env
|
||||||
end
|
end
|
||||||
|
|
||||||
# Removes the token, effectively logging the user out.
|
# Removes the token, effectively logging the user out.
|
||||||
def clear_token
|
def clear_token
|
||||||
|
@logger.info("Clearing token")
|
||||||
token_path.delete if token_path.file?
|
token_path.delete if token_path.file?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,6 +40,8 @@ module VagrantPlugins
|
||||||
# @param [String] pass
|
# @param [String] pass
|
||||||
# @return [String] token The access token, or nil if auth failed.
|
# @return [String] token The access token, or nil if auth failed.
|
||||||
def login(user, pass)
|
def login(user, pass)
|
||||||
|
@logger.info("Logging in '#{user}'")
|
||||||
|
|
||||||
with_error_handling do
|
with_error_handling do
|
||||||
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
url = "#{Vagrant.server_url}/api/v1/authenticate"
|
||||||
request = { "user" => { "login" => user, "password" => pass } }
|
request = { "user" => { "login" => user, "password" => pass } }
|
||||||
|
@ -52,9 +56,12 @@ module VagrantPlugins
|
||||||
#
|
#
|
||||||
# @param [String] token
|
# @param [String] token
|
||||||
def store_token(token)
|
def store_token(token)
|
||||||
|
@logger.info("Storing token in #{token_path}")
|
||||||
|
|
||||||
token_path.open("w") do |f|
|
token_path.open("w") do |f|
|
||||||
f.write(token)
|
f.write(token)
|
||||||
end
|
end
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,13 +72,17 @@ module VagrantPlugins
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def token
|
def token
|
||||||
if ENV["ATLAS_TOKEN"] && !ENV["ATLAS_TOKEN"].empty?
|
if ENV["ATLAS_TOKEN"] && !ENV["ATLAS_TOKEN"].empty?
|
||||||
|
@logger.debug("Using authentication token from environment variable")
|
||||||
return ENV["ATLAS_TOKEN"]
|
return ENV["ATLAS_TOKEN"]
|
||||||
end
|
end
|
||||||
|
|
||||||
if token_path.exist?
|
if token_path.exist?
|
||||||
|
@logger.debug("Using authentication token from disk at #{token_path}")
|
||||||
return token_path.read.strip
|
return token_path.read.strip
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@logger.debug("No authentication token in environment or #{token_path}")
|
||||||
|
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -80,8 +91,13 @@ module VagrantPlugins
|
||||||
def with_error_handling(&block)
|
def with_error_handling(&block)
|
||||||
yield
|
yield
|
||||||
rescue RestClient::Unauthorized
|
rescue RestClient::Unauthorized
|
||||||
|
@logger.debug("Unauthorized!")
|
||||||
false
|
false
|
||||||
rescue RestClient::NotAcceptable => e
|
rescue RestClient::NotAcceptable => e
|
||||||
|
@logger.debug("Got unacceptable response:")
|
||||||
|
@logger.debug(e.message)
|
||||||
|
@logger.debug(e.backtrace.join("\n"))
|
||||||
|
|
||||||
begin
|
begin
|
||||||
errors = JSON.parse(e.response)["errors"].join("\n")
|
errors = JSON.parse(e.response)["errors"].join("\n")
|
||||||
raise Errors::ServerError, errors: errors
|
raise Errors::ServerError, errors: errors
|
||||||
|
@ -89,6 +105,7 @@ module VagrantPlugins
|
||||||
|
|
||||||
raise "An unexpected error occurred: #{e.inspect}"
|
raise "An unexpected error occurred: #{e.inspect}"
|
||||||
rescue SocketError
|
rescue SocketError
|
||||||
|
@logger.info("Socket error")
|
||||||
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
|
raise Errors::ServerUnreachable, url: Vagrant.server_url.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -115,6 +115,22 @@ module VagrantPlugins
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Validate all pushes
|
||||||
|
def validate(machine)
|
||||||
|
errors = { "push" => _detected_errors }
|
||||||
|
|
||||||
|
__compiled_pushes.each do |_, push|
|
||||||
|
config = push[1]
|
||||||
|
push_errors = config.validate(machine)
|
||||||
|
|
||||||
|
if push_errors
|
||||||
|
errors = Vagrant::Config::V2::Util.merge_errors(errors, push_errors)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
errors
|
||||||
|
end
|
||||||
|
|
||||||
# This returns the list of compiled pushes as a hash by name.
|
# This returns the list of compiled pushes as a hash by name.
|
||||||
#
|
#
|
||||||
# @return [Hash<Symbol, Array<Class, Object>>]
|
# @return [Hash<Symbol, Array<Class, Object>>]
|
||||||
|
|
|
@ -9,10 +9,10 @@ describe VagrantPlugins::CommandPush::Command do
|
||||||
let(:iso_env) { isolated_environment }
|
let(:iso_env) { isolated_environment }
|
||||||
let(:env) do
|
let(:env) do
|
||||||
iso_env.vagrantfile(<<-VF)
|
iso_env.vagrantfile(<<-VF)
|
||||||
Vagrant.configure("2") do |config|
|
Vagrant.configure("2") do |config|
|
||||||
config.vm.box = "nope"
|
config.vm.box = "hashicorp/precise64"
|
||||||
end
|
end
|
||||||
VF
|
VF
|
||||||
iso_env.create_vagrant_env
|
iso_env.create_vagrant_env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,20 +38,29 @@ VF
|
||||||
subject.execute
|
subject.execute
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "delegates to Environment#push" do
|
||||||
|
expect(env).to receive(:push).once
|
||||||
|
subject.execute
|
||||||
|
end
|
||||||
|
|
||||||
it "validates the configuration" do
|
it "validates the configuration" do
|
||||||
iso_env.vagrantfile("")
|
iso_env.vagrantfile <<-EOH
|
||||||
|
Vagrant.configure("2") do |config|
|
||||||
|
config.vm.box = "hashicorp/precise64"
|
||||||
|
|
||||||
|
config.push.define "noop" do |push|
|
||||||
|
push.bad = "ham"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
EOH
|
||||||
|
|
||||||
subject = described_class.new(argv, iso_env.create_vagrant_env)
|
subject = described_class.new(argv, iso_env.create_vagrant_env)
|
||||||
allow(subject).to receive(:validate_pushes!)
|
allow(subject).to receive(:validate_pushes!)
|
||||||
.and_return(:noop)
|
.and_return(:noop)
|
||||||
|
|
||||||
expect { subject.execute }.to raise_error(
|
expect { subject.execute }.to raise_error(Vagrant::Errors::ConfigInvalid) { |err|
|
||||||
Vagrant::Errors::ConfigInvalid)
|
expect(err.message).to include("The following settings shouldn't exist: bad")
|
||||||
end
|
}
|
||||||
|
|
||||||
it "delegates to Environment#push" do
|
|
||||||
expect(env).to receive(:push).once
|
|
||||||
subject.execute
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ require_relative "../../../base"
|
||||||
|
|
||||||
require Vagrant.source_root.join("plugins/provisioners/chef/omnibus")
|
require Vagrant.source_root.join("plugins/provisioners/chef/omnibus")
|
||||||
|
|
||||||
describe VagrantPlugins::Chef::Omnibus, :focus do
|
describe VagrantPlugins::Chef::Omnibus do
|
||||||
let(:prefix) { "curl -sL #{described_class.const_get(:OMNITRUCK)}" }
|
let(:prefix) { "curl -sL #{described_class.const_get(:OMNITRUCK)}" }
|
||||||
|
|
||||||
let(:version) { :latest }
|
let(:version) { :latest }
|
||||||
|
|
|
@ -991,7 +991,7 @@ VF
|
||||||
end
|
end
|
||||||
|
|
||||||
def push
|
def push
|
||||||
!!self.class.class_variable_set(:@@pushed, true)
|
self.class.class_variable_set(:@@pushed, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue