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

fix: Check file format when uploading artifacts #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions client/deployments/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package deployments

import (
"archive/tar"
"bytes"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -296,6 +297,24 @@ func (c *Client) DirectUpload(
return errors.Wrap(err, "Cannot read artifact file stats")
}

tr := tar.NewReader(artifact)
versionH, err := tr.Next()
if err != nil {
return errors.Wrap(err, "Cannot find artifact")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we adjust the error message a little?
I found the current message quite confusing when I was testing the CLI.

Suggested change
return errors.Wrap(err, "Cannot find artifact")
return errors.Wrap(err, "error parsing artifact")

} else if versionH.Name != "version" {
return errors.New("Invalid artifact format")
}
v := struct {
Format string `json:"format"`
}{}
err = json.NewDecoder(tr).Decode(&v)
if err != nil || v.Format != "mender" {
return errors.New("Invalid artifact format")
}
_, err = artifact.Seek(0, io.SeekStart)
if err != nil || v.Format != "mender" {
return err
}
Comment on lines +300 to +317
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Can you refactor this artifact parsing part into it's own function and reuse it below?
Duplicating code often leads to forgetting about the existence about the other instance when touching the code later.

var req *http.Request
if !noProgress {
// create progress bar
Expand Down Expand Up @@ -367,6 +386,24 @@ func (c *Client) UploadArtifact(
return errors.Wrap(err, "Cannot read artifact file stats")
}

tr := tar.NewReader(artifact)
versionH, err := tr.Next()
if err != nil {
return errors.Wrap(err, "Cannot find artifact")
} else if versionH.Name != "version" {
return errors.New("Invalid artifact format")
}
v := struct {
Format string `json:"format"`
}{}
err = json.NewDecoder(tr).Decode(&v)
if err != nil || v.Format != "mender" {
return errors.New("Invalid artifact format")
}
_, err = artifact.Seek(0, io.SeekStart)
if err != nil || v.Format != "mender" {
return err
}
// create pipe
pR, pW := io.Pipe()

Expand Down