Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include fastly.toml in the hashsum as well #575

Merged
merged 1 commit into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions pkg/commands/compute/deploy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compute

import (
"bytes"
"crypto/sha512"
"errors"
"fmt"
Expand Down Expand Up @@ -414,13 +415,16 @@ func validatePackage(data manifest.Data, packageFlag string, errLog fsterr.LogIn
Remediation: fsterr.PackageSizeRemediation,
}
}
contents := map[string]*bytes.Buffer{
"fastly.toml": &bytes.Buffer{},
"main.wasm": &bytes.Buffer{},
}
if err := validate(pkgPath, func(f archiver.File) error {
if f.Name() == "main.wasm" {
h := sha512.New()
if _, err := io.Copy(h, f); err != nil {
return fmt.Errorf("error computing hashsum: %w", err)
switch fname := f.Name(); fname {
case "fastly.toml", "main.wasm":
if _, err := io.Copy(contents[fname], f); err != nil {
return fmt.Errorf("error reading %s: %w", fname, err)
}
hashSum = fmt.Sprintf("%x", h.Sum(nil))
}
return nil
}); err != nil {
Expand All @@ -430,6 +434,10 @@ func validatePackage(data manifest.Data, packageFlag string, errLog fsterr.LogIn
})
return pkgName, pkgPath, hashSum, err
}
hashSum, err = getHashSum(contents)
if err != nil {
return pkgName, pkgPath, hashSum, err
}
return pkgName, pkgPath, hashSum, nil
}

Expand Down Expand Up @@ -786,6 +794,17 @@ func pkgCompare(client api.Interface, serviceID string, version int, hashSum str
return true, nil
}

// getHashSum creates a SHA 512 hash from the given file contents in a specific order.
func getHashSum(contents map[string]*bytes.Buffer) (hash string, err error) {
h := sha512.New()
for _, fname := range []string{"fastly.toml", "main.wasm"} {
if _, err := io.Copy(h, contents[fname]); err != nil {
return "", err
}
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}

// pkgUpload uploads the package to the specified service and version.
func pkgUpload(progress text.Progress, client api.Interface, serviceID string, version int, path string) error {
progress.Step("Uploading package...")
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/compute/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ func getPackageIdentical(i *fastly.GetPackageInput) (*fastly.Package, error) {
ServiceID: i.ServiceID,
ServiceVersion: i.ServiceVersion,
Metadata: fastly.PackageMetadata{
HashSum: "84b2bbb700142d16ed7efad2058984ee01881654effca70dfe7f21ec8b451df12e49173569145d2920e6449745bf87ede2a68c73e2e165c2d1190d469f99046f",
HashSum: "bf634ccf8be5c8417cf562466ece47ea61056ddeb07273a3d861e8ad757ed3577bc182006d04093c301467cadfd2b1805eedebd1e7cfa0404c723680f2dbc01e",
},
}, nil
}
Expand Down