-
Notifications
You must be signed in to change notification settings - Fork 70
feat(compute/hashfiles): add hash-files subcommand #943
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a461e8d
docs(manifest): annotate File fields
Integralist fa40697
feat(compute/hashfiles): add hash-files subcommand
Integralist abde95f
docs(compute/hashfiles): annotate methods and ignore gosec warning
Integralist b3a4d0a
fix: replace compute.PackageSizeLimit with MaxPackageSize
Integralist 2667215
fix(compute/hashfiles): ignore gosec
Integralist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| package compute | ||
|
|
||
| import ( | ||
| "archive/tar" | ||
| "bytes" | ||
| "compress/gzip" | ||
| "crypto/sha512" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "sort" | ||
|
|
||
| "github.com/kennygrant/sanitize" | ||
|
|
||
| "github.com/fastly/cli/pkg/cmd" | ||
| "github.com/fastly/cli/pkg/global" | ||
| "github.com/fastly/cli/pkg/manifest" | ||
| "github.com/fastly/cli/pkg/text" | ||
| ) | ||
|
|
||
| // NOTE: This is variable not a constant for the sake of test manipulations. | ||
| // https://developer.fastly.com/learning/compute/#limitations-and-constraints | ||
| var MaxPackageSize int64 = 100000000 // 100MB in bytes | ||
|
|
||
| // HashFilesCommand produces a deployable artifact from files on the local disk. | ||
| type HashFilesCommand struct { | ||
| cmd.Base | ||
|
|
||
| buildCmd *BuildCommand | ||
| Manifest manifest.Data | ||
| Package string | ||
| SkipBuild bool | ||
| } | ||
|
|
||
| // NewHashFilesCommand returns a usable command registered under the parent. | ||
| func NewHashFilesCommand(parent cmd.Registerer, g *global.Data, build *BuildCommand, m manifest.Data) *HashFilesCommand { | ||
| var c HashFilesCommand | ||
| c.buildCmd = build | ||
| c.Globals = g | ||
| c.Manifest = m | ||
| c.CmdClause = parent.Command("hash-files", "Generate a SHA512 digest from the contents of the Compute@Edge package") | ||
| c.CmdClause.Flag("package", "Path to a package tar.gz").Short('p').StringVar(&c.Package) | ||
| c.CmdClause.Flag("skip-build", "Skip the build step").BoolVar(&c.SkipBuild) | ||
| return &c | ||
| } | ||
|
|
||
| // Exec implements the command interface. | ||
| func (c *HashFilesCommand) Exec(in io.Reader, out io.Writer) (err error) { | ||
| if !c.SkipBuild { | ||
| err = c.Build(in, out) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| pkgName := fmt.Sprintf("%s.tar.gz", sanitize.BaseName(c.Globals.Manifest.File.Name)) | ||
| pkg := filepath.Join("pkg", pkgName) | ||
|
|
||
| if c.Package != "" { | ||
| pkg, err = filepath.Abs(c.Package) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to locate package path '%s': %w", c.Package, err) | ||
| } | ||
| } | ||
|
|
||
| var r io.Reader | ||
| // G304 (CWE-22): Potential file inclusion via variable | ||
| // #nosec | ||
| r, err = os.Open(pkg) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open package '%s': %w", pkg, err) | ||
| } | ||
|
|
||
| zr, err := gzip.NewReader(r) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create a gzip reader: %w", err) | ||
| } | ||
|
|
||
| files, err := c.ReadFilesFromPackage(tar.NewReader(zr)) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read files within the package: %w", err) | ||
| } | ||
|
|
||
| hash, err := c.GetFilesHash(files) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to generate hash from package files: %w", err) | ||
| } | ||
|
|
||
| text.Output(out, hash) | ||
| return nil | ||
| } | ||
|
|
||
| // Build constructs and executes the build logic. | ||
| func (c *HashFilesCommand) Build(in io.Reader, out io.Writer) error { | ||
| output := out | ||
| if !c.Globals.Verbose() { | ||
| output = io.Discard | ||
| } | ||
| return c.buildCmd.Exec(in, output) | ||
| } | ||
|
|
||
| // ReadFilesFromPackage reads all files within the provided package tar and | ||
| // generates a map data structure where the key is the filename and the value is | ||
| // the file contents. | ||
| func (c *HashFilesCommand) ReadFilesFromPackage(tr *tar.Reader) (map[string]*bytes.Buffer, error) { | ||
| // Store the content of every file within the package. | ||
| contents := make(map[string]*bytes.Buffer) | ||
|
|
||
| // Track overall package size. | ||
| var pkgSize int64 | ||
|
|
||
| for { | ||
| hdr, err := tr.Next() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Avoids G110: Potential DoS vulnerability via decompression bomb (gosec). | ||
| pkgSize += hdr.Size | ||
| if pkgSize > MaxPackageSize { | ||
| return nil, errors.New("package size exceeded 100MB limit") | ||
| } | ||
|
|
||
| if hdr.Typeflag != tar.TypeReg { | ||
| continue | ||
| } | ||
|
|
||
| contents[hdr.Name] = &bytes.Buffer{} | ||
|
|
||
| _, err = io.CopyN(contents[hdr.Name], tr, hdr.Size) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return contents, nil | ||
| } | ||
|
|
||
| // GetFilesHash returns a hash of all the filecontent in sorted filename order. | ||
| func (c *HashFilesCommand) GetFilesHash(contents map[string]*bytes.Buffer) (string, error) { | ||
| keys := make([]string, 0, len(contents)) | ||
| for k := range contents { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(keys) | ||
| h := sha512.New() | ||
| for _, fname := range keys { | ||
| if _, err := io.Copy(h, contents[fname]); err != nil { | ||
| return "", err | ||
| } | ||
| } | ||
| return fmt.Sprintf("%x", h.Sum(nil)), nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍