core: box checksums just work properly again for direct box adds
This commit is contained in:
parent
7926f7f051
commit
ce350fe872
|
@ -124,7 +124,10 @@ module Vagrant
|
||||||
"0",
|
"0",
|
||||||
provider,
|
provider,
|
||||||
nil,
|
nil,
|
||||||
env)
|
env,
|
||||||
|
checksum: env[:box_checksum],
|
||||||
|
checksum_type: env[:box_checksum_type],
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Adds a box given that the URL is a metadata document.
|
# Adds a box given that the URL is a metadata document.
|
||||||
|
@ -265,33 +268,6 @@ module Vagrant
|
||||||
end
|
end
|
||||||
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
|
if checksum_klass
|
||||||
@logger.info("Validating checksum with #{checksum_klass}")
|
@logger.info("Validating checksum with #{checksum_klass}")
|
||||||
@logger.info("Expected checksum: #{checksum}")
|
@logger.info("Expected checksum: #{checksum}")
|
||||||
|
@ -354,6 +330,11 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if opts[:checksum] && opts[:checksum_type]
|
||||||
|
validate_checksum(
|
||||||
|
opts[:checksum_type], opts[:checksum], box_url)
|
||||||
|
end
|
||||||
|
|
||||||
# Add the box!
|
# Add the box!
|
||||||
box = env[:box_collection].add(
|
box = env[:box_collection].add(
|
||||||
box_url, name, version,
|
box_url, name, version,
|
||||||
|
@ -490,6 +471,30 @@ module Vagrant
|
||||||
return false if !match
|
return false if !match
|
||||||
match.last.chomp == "application/json"
|
match.last.chomp == "application/json"
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -149,6 +149,21 @@ describe Vagrant::Action::Builtin::BoxAdd do
|
||||||
to raise_error(Vagrant::Errors::BoxAlreadyExists)
|
to raise_error(Vagrant::Errors::BoxAlreadyExists)
|
||||||
end
|
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
|
it "force adds if exists and specified" do
|
||||||
box_path = iso_env.box2_file(:virtualbox)
|
box_path = iso_env.box2_file(:virtualbox)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue