diff --git a/commands/upload_product.go b/commands/upload_product.go index 20bad3a0b..6a76f6415 100644 --- a/commands/upload_product.go +++ b/commands/upload_product.go @@ -13,10 +13,10 @@ type UploadProduct struct { multipart multipart logger logger service uploadProductService - Options struct { + Options struct { Product string `long:"product" short:"p" required:"true" description:"path to product"` PollingInterval int `long:"polling-interval" short:"pi" description:"interval (in seconds) at which to print status" default:"1"` - Shasum string `long:"shasum" short:"s" description:"shasum of the provided product file to be used for validation"` + Shasum string `long:"shasum" short:"sha" description:"shasum of the provided product file to be used for validation"` } metadataExtractor metadataExtractor } diff --git a/commands/upload_product_test.go b/commands/upload_product_test.go index d30ec020a..d03c6f4df 100644 --- a/commands/upload_product_test.go +++ b/commands/upload_product_test.go @@ -152,17 +152,6 @@ var _ = Describe("UploadProduct", func() { file.WriteString("testing-shasum") command := commands.NewUploadProduct(multipart, metadataExtractor, fakeService, logger) - metadataExtractor.ExtractMetadataReturns(extractor.Metadata{ - Name: "cf", - Version: "1.5.0", - }, nil) - fakeService.CheckProductAvailabilityStub = func(name, version string) (bool, error) { - if name == "cf" && version == "1.5.0" { - return true, nil - } - return false, errors.New("unknown") - } - err = command.Execute([]string{ "--product", file.Name(), "--shasum", "not-the-correct-shasum", diff --git a/commands/upload_stemcell.go b/commands/upload_stemcell.go index d138ab575..e48a493b2 100644 --- a/commands/upload_stemcell.go +++ b/commands/upload_stemcell.go @@ -7,6 +7,8 @@ import ( "github.com/pivotal-cf/jhanda" "github.com/pivotal-cf/om/api" "github.com/pivotal-cf/om/formcontent" + "github.com/pivotal-cf/om/validator" + "strconv" ) @@ -14,10 +16,11 @@ type UploadStemcell struct { multipart multipart logger logger service uploadStemcellService - Options struct { + Options struct { Stemcell string `long:"stemcell" short:"s" required:"true" description:"path to stemcell"` Force bool `long:"force" short:"f" description:"upload stemcell even if it already exists on the target Ops Manager"` Floating bool `long:"floating" default:"true" description:"assigns the stemcell to all compatible products "` + Shasum string `long:"shasum" short:"sha" description:"shasum of the provided stemcell file to be used for validation"` } } @@ -55,6 +58,21 @@ func (us UploadStemcell) Execute(args []string) error { return fmt.Errorf("could not parse upload-stemcell flags: %s", err) } + if us.Options.Shasum != "" { + shaValidator := validator.NewSHA256Calculator() + shasum, err := shaValidator.Checksum(us.Options.Stemcell) + + if err != nil { + return err + } + + if shasum != us.Options.Shasum { + return fmt.Errorf("expected shasum %s does not match file shasum %s", us.Options.Shasum, shasum) + } + + us.logger.Printf("expected shasum matches stemcell shasum.") + } + if !us.Options.Force { us.logger.Printf("processing stemcell") report, err := us.service.GetDiagnosticReport() diff --git a/commands/upload_stemcell_test.go b/commands/upload_stemcell_test.go index c6b24c8ef..2d4e8f22a 100644 --- a/commands/upload_stemcell_test.go +++ b/commands/upload_stemcell_test.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "io/ioutil" + "os" "strings" "github.com/pivotal-cf/jhanda" @@ -185,6 +186,63 @@ var _ = Describe("UploadStemcell", func() { }) }) + Context("when the --shasum flag is defined", func() { + It("proceeds normally when the sha sums match", func() { + file, err := ioutil.TempFile("", "test-file.tgz") + Expect(err).ToNot(HaveOccurred()) + + file.Close() + defer os.Remove(file.Name()) + + file.WriteString("testing-shasum") + + submission := formcontent.ContentSubmission{ + Length: 10, + Content: ioutil.NopCloser(strings.NewReader("")), + ContentType: "some content-type", + } + multipart.FinalizeReturns(submission, nil) + + fakeService.GetDiagnosticReportReturns(api.DiagnosticReport{Stemcells: []string{}}, nil) + + command := commands.NewUploadStemcell(multipart, fakeService, logger) + err = command.Execute([]string{ + "--stemcell", file.Name(), + "--shasum", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + }) + Expect(err).NotTo(HaveOccurred()) + format, v := logger.PrintfArgsForCall(0) + Expect(fmt.Sprintf(format, v...)).To(ContainSubstring("expected shasum matches stemcell shasum.")) + }) + + It("returns an error when the sha sums don't match", func() { + file, err := ioutil.TempFile("", "test-file.tgz") + Expect(err).ToNot(HaveOccurred()) + + file.Close() + defer os.Remove(file.Name()) + + file.WriteString("testing-shasum") + + command := commands.NewUploadStemcell(multipart, fakeService, logger) + err = command.Execute([]string{ + "--stemcell", file.Name(), + "--shasum", "not-the-correct-shasum", + }) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError("expected shasum not-the-correct-shasum does not match file shasum e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")) + }) + It("fails when the file can not calculate a shasum", func() { + command := commands.NewUploadStemcell(multipart, fakeService, logger) + err := command.Execute([]string{ + "--stemcell", "/path/to/testing.tgz", + "--shasum", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + }) + Expect(err).To(HaveOccurred()) + Expect(err).To(MatchError("open /path/to/testing.tgz: no such file or directory")) + }) + }) + Context("when the diagnostic report is unavailable", func() { It("uploads the stemcell", func() { submission := formcontent.ContentSubmission{