From f811e995da12e6556eccab79f4985ad7a000bf27 Mon Sep 17 00:00:00 2001 From: sophia Date: Mon, 26 Sep 2022 13:10:17 -0400 Subject: [PATCH 1/2] Return errors when box metadata is unavailable or invalid --- internal/core/box.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/core/box.go b/internal/core/box.go index 18c6fac36..cdad64b91 100644 --- a/internal/core/box.go +++ b/internal/core/box.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "path/filepath" + "strings" "sync" "time" @@ -18,6 +19,7 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/vagrant-plugin-sdk/core" "github.com/hashicorp/vagrant-plugin-sdk/helper/path" + "github.com/hashicorp/vagrant-plugin-sdk/localizer" "github.com/hashicorp/vagrant-plugin-sdk/proto/vagrant_plugin_sdk" "github.com/hashicorp/vagrant/internal/server/proto/vagrant_server" "github.com/mitchellh/mapstructure" @@ -28,6 +30,8 @@ import ( // Number of seconds to wait between checks for box updates const BoxUpdateCheckInterval = 3600 +var RequiredMetadataFields = []string{"provider"} + type Box struct { basis *Basis box *vagrant_server.Box @@ -58,7 +62,10 @@ func NewBox(opts ...BoxOption) (b *Box, err error) { metadataFile := filepath.Join(b.box.Directory, "metadata.json") if _, err := os.Stat(metadataFile); err != nil { - return nil, err + return nil, localizer.LocalizeErr( + "box_does_not_have_metadata_json_file", + map[string]string{"BoxName": b.box.Name}, + ) } data, err := os.ReadFile(metadataFile) @@ -69,6 +76,19 @@ func NewBox(opts ...BoxOption) (b *Box, err error) { if err := json.Unmarshal(data, &metadata); err != nil { return nil, err } + for _, field := range RequiredMetadataFields { + // If the metadata does not have a required field + if _, ok := metadata[field]; !ok { + return nil, localizer.LocalizeErr( + "box_metadata_missing_required_fields", + map[string]string{ + "BoxName": b.box.Name, + "RequiredField": field, + "RequiredFields": strings.Join(RequiredMetadataFields, " ,"), + }, + ) + } + } b.box.Metadata, err = structpb.NewStruct(metadata) if err != nil { return nil, err From aa4e263cff34bdd7890f8b65d7c2b51c8d51d59b Mon Sep 17 00:00:00 2001 From: sophia Date: Wed, 28 Sep 2022 16:53:21 -0400 Subject: [PATCH 2/2] Bump sdk --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index ce89e6245..7b06b85b4 100644 --- a/go.mod +++ b/go.mod @@ -25,7 +25,7 @@ require ( github.com/hashicorp/go-version v1.3.0 github.com/hashicorp/hcl/v2 v2.11.1 github.com/hashicorp/nomad/api v0.0.0-20200814140818-42de70466a9d - github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204300-c0e4b14e08c5 + github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204555-798e860a8593 github.com/imdario/mergo v0.3.11 github.com/improbable-eng/grpc-web v0.13.0 github.com/kr/text v0.2.0 diff --git a/go.sum b/go.sum index 16b3d5df3..44c0ffc33 100644 --- a/go.sum +++ b/go.sum @@ -369,6 +369,8 @@ github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220919180735-d47bfe003e94 h1:CG github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220919180735-d47bfe003e94/go.mod h1:zA5vDskG3gH306C+obL+yURiUiLMAlx52yqO8MC2r9w= github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204300-c0e4b14e08c5 h1:xxwRPE6ISOz4CFFJlk3DmDD+4ZBt7iO9YiGwk4W/CYY= github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204300-c0e4b14e08c5/go.mod h1:zA5vDskG3gH306C+obL+yURiUiLMAlx52yqO8MC2r9w= +github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204555-798e860a8593 h1:A75xYKrvyA/fNB6nSLBosbcrEmGTZSyMvuFHH7agscY= +github.com/hashicorp/vagrant-plugin-sdk v0.0.0-20220928204555-798e860a8593/go.mod h1:zA5vDskG3gH306C+obL+yURiUiLMAlx52yqO8MC2r9w= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce h1:7UnVY3T/ZnHUrfviiAgIUjg2PXxsQfs5bphsG8F7Keo= github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=