core: box checksums just work properly again for direct box adds

This commit is contained in:
Mitchell Hashimoto 2014-03-01 11:54:37 +01:00
parent 7926f7f051
commit ce350fe872
2 changed files with 48 additions and 28 deletions

View File

@ -124,7 +124,10 @@ module Vagrant
"0",
provider,
nil,
env)
env,
checksum: env[:box_checksum],
checksum_type: env[:box_checksum_type],
)
end
# Adds a box given that the URL is a metadata document.
@ -265,33 +268,6 @@ module Vagrant
end
end
# Go through each URL and attempt to download it
download_error = nil
download_url = nil
urls = env[:box_url]
urls = [env[:box_url]] if !urls.is_a?(Array)
urls.each do |url|
begin
@temp_path = download_box_url(url, env)
download_error = nil
download_url = url
rescue Errors::DownloaderError => e
env[:ui].error(I18n.t(
"vagrant.actions.box.download.download_failed"))
download_error = e
end
# If we were interrupted during this download, then just return
# at this point, we don't need to try anymore.
if @download_interrupted
@logger.warn("Download interrupted, not trying any more box URLs.")
return
end
end
# If all the URLs failed, then raise an exception
raise download_error if download_error
if checksum_klass
@logger.info("Validating checksum with #{checksum_klass}")
@logger.info("Expected checksum: #{checksum}")
@ -354,6 +330,11 @@ module Vagrant
end
end
if opts[:checksum] && opts[:checksum_type]
validate_checksum(
opts[:checksum_type], opts[:checksum], box_url)
end
# Add the box!
box = env[:box_collection].add(
box_url, name, version,
@ -490,6 +471,30 @@ module Vagrant
return false if !match
match.last.chomp == "application/json"
end
def validate_checksum(checksum_type, checksum, path)
checksum_klass = case checksum_type.to_sym
when :md5
Digest::MD5
when :sha1
Digest::SHA1
when :sha256
Digest::SHA2
else
raise Errors::BoxChecksumInvalidType,
type: env[:box_checksum_type].to_s
end
@logger.info("Validating checksum with #{checksum_klass}")
@logger.info("Expected checksum: #{checksum}")
actual = FileChecksum.new(path, checksum_klass).checksum
if actual != checksum
raise Errors::BoxChecksumMismatch,
actual: actual,
expected: checksum
end
end
end
end
end

View File

@ -149,6 +149,21 @@ describe Vagrant::Action::Builtin::BoxAdd do
to raise_error(Vagrant::Errors::BoxAlreadyExists)
end
it "raises an error if checksum specified and doesn't match" do
box_path = iso_env.box2_file(:virtualbox)
env[:box_name] = "foo"
env[:box_url] = box_path.to_s
env[:box_checksum] = checksum(box_path) + "A"
env[:box_checksum_type] = "sha1"
box_collection.should_receive(:add).never
app.should_receive(:call).never
expect { subject.call(env) }.
to raise_error(Vagrant::Errors::BoxChecksumMismatch)
end
it "force adds if exists and specified" do
box_path = iso_env.box2_file(:virtualbox)