From ccc232914d78138f33250d2472a319cb57e24e82 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 14 Sep 2022 13:01:31 -0400 Subject: [PATCH 1/2] Raise error if required metadata.json box fields are not present --- lib/vagrant/box.rb | 15 +++++++++++++++ lib/vagrant/errors.rb | 4 ++++ templates/locales/en.yml | 6 ++++++ 3 files changed, 25 insertions(+) diff --git a/lib/vagrant/box.rb b/lib/vagrant/box.rb index e9a7a0667..90dc69d38 100644 --- a/lib/vagrant/box.rb +++ b/lib/vagrant/box.rb @@ -18,6 +18,8 @@ module Vagrant autoload :Remote, "vagrant/box/remote" + # The required fields in a boxes `metadata.json` file + REQUIRED_METADATA_FIELDS = ["provider"] # Number of seconds to wait between checks for box updates BOX_UPDATE_CHECK_INTERVAL = 3600 @@ -75,6 +77,7 @@ module Vagrant begin @metadata = JSON.parse(directory.join("metadata.json").read) + validate_metadata_json(@metadata) rescue JSON::ParserError raise Errors::BoxMetadataCorrupted, name: @name end @@ -82,6 +85,18 @@ module Vagrant @logger = Log4r::Logger.new("vagrant::box") end + def validate_metadata_json(metadata) + metatdata_fields = metadata.keys + REQUIRED_METADATA_FIELDS.each do |field| + if !metatdata_fields.include?(field) + raise Errors::BoxMetadataMissingRequiredFields, + name: @name, + required_field: field, + all_fields: REQUIRED_METADATA_FIELDS.join(", ") + end + end + end + # This deletes the box. This is NOT undoable. def destroy! # Delete the directory to delete the box. diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 1deff4346..b23295b85 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -168,6 +168,10 @@ module Vagrant error_key(:box_metadata_corrupted) end + class BoxMetadataMissingRequiredFields < VagrantError + error_key(:box_metadata_missing_required_fields) + end + class BoxMetadataDownloadError < VagrantError error_key(:box_metadata_download_error) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index a7ba58f30..b7c37613e 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -615,6 +615,12 @@ en: The metadata associated with the box '%{name}' appears corrupted. This is most often caused by a disk issue or system crash. Please remove the box, re-add it, and try again. + box_metadata_missing_required_fields: |- + The metadata associated with the box '%{name}' appears to be missing + the required field '%{required_field}'. Please ensure `metadata.json` + has all required fields. + + Required fields: %{all_fields} box_metadata_download_error: |- There was an error while downloading the metadata for this box. The error message is shown below: From 115bec2b05a96dfaa19b2e39c149ac54ea4ea218 Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 14 Sep 2022 14:05:27 -0400 Subject: [PATCH 2/2] Add test for validating box metadata.json --- test/unit/vagrant/box_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unit/vagrant/box_test.rb b/test/unit/vagrant/box_test.rb index d045005cc..b45e837d9 100644 --- a/test/unit/vagrant/box_test.rb +++ b/test/unit/vagrant/box_test.rb @@ -44,6 +44,19 @@ describe Vagrant::Box, :skip_windows do f.write(JSON.generate(data)) end + # Verify the metadata + expect { subject.metadata }. + to raise_error(Vagrant::Errors::BoxMetadataMissingRequiredFields) + end + + it "provides the metadata associated with a box" do + data = { "provider" => "bar" } + + # Write the metadata + directory.join("metadata.json").open("w") do |f| + f.write(JSON.generate(data)) + end + # Verify the metadata expect(subject.metadata).to eq(data) end