From f8a3f93a84cef50e69971c87ecdd57b33f55a878 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Wed, 15 Nov 2023 13:11:46 +0900 Subject: [PATCH 1/2] fix(update-checksum): copy an asset to a temporal file to calculate the checksum correctly Fixed a bug that the calculated checksum is wrong. - https://github.com/aquaproj/aqua/issues/2467 --- pkg/controller/updatechecksum/update.go | 34 +++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/pkg/controller/updatechecksum/update.go b/pkg/controller/updatechecksum/update.go index 26e0238d2..05fc6ccb7 100644 --- a/pkg/controller/updatechecksum/update.go +++ b/pkg/controller/updatechecksum/update.go @@ -15,6 +15,7 @@ import ( "github.com/aquaproj/aqua/v2/pkg/download" "github.com/aquaproj/aqua/v2/pkg/runtime" "github.com/sirupsen/logrus" + "github.com/spf13/afero" "github.com/suzuki-shunsuke/logrus-error/logerr" ) @@ -291,7 +292,7 @@ func (c *Controller) getChecksum(ctx context.Context, logE *logrus.Entry, checks return nil } -func (c *Controller) dlAssetAndGetChecksum(ctx context.Context, logE *logrus.Entry, checksums *checksum.Checksums, pkg *config.Package, rt *runtime.Runtime) (gErr error) { +func (c *Controller) dlAssetAndGetChecksum(ctx context.Context, logE *logrus.Entry, checksums *checksum.Checksums, pkg *config.Package, rt *runtime.Runtime) (gErr error) { //nolint:cyclop checksumID, err := pkg.ChecksumID(rt) if err != nil { return fmt.Errorf("get a checksum id: %w", err) @@ -322,9 +323,26 @@ func (c *Controller) dlAssetAndGetChecksum(ctx context.Context, logE *logrus.Ent return fmt.Errorf("download an asset: %w", err) } defer file.Close() + + // Download a file to a temporal file. + // https://github.com/aquaproj/aqua/issues/2467 + tempFilePath, err := copyToTempFile(c.fs, file) + if err != nil { + return fmt.Errorf("copy an asset to a temporal file: %w", err) + } + defer func() { + if err := c.fs.Remove(tempFilePath); err != nil { + logE.WithError(err).Warn("remove a temporal file") + } + }() + tempFile, err := c.fs.Open(tempFilePath) + if err != nil { + return fmt.Errorf("open a temporal file to calculate the checksum: %w", err) + } + algorithm := "sha256" fields["algorithm"] = algorithm - chk, err := checksum.CalculateReader(file, algorithm) + chk, err := checksum.CalculateReader(tempFile, algorithm) if err != nil { return fmt.Errorf("calculate an asset: %w", err) } @@ -335,3 +353,15 @@ func (c *Controller) dlAssetAndGetChecksum(ctx context.Context, logE *logrus.Ent }) return nil } + +func copyToTempFile(fs afero.Fs, file io.Reader) (string, error) { + tempFile, err := afero.TempFile(fs, "", "") + if err != nil { + return "", fmt.Errorf("create a temporal file: %w", err) + } + if _, err := io.Copy(tempFile, file); err != nil { + return "", fmt.Errorf("download an asset to a temporal file: %w", err) + } + tempFile.Close() + return tempFile.Name(), nil +} From c893e361107deb7c5ecf5ae9c98963cb102cab83 Mon Sep 17 00:00:00 2001 From: Shunsuke Suzuki Date: Wed, 15 Nov 2023 13:15:08 +0900 Subject: [PATCH 2/2] fix: close a file --- pkg/controller/updatechecksum/update.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/controller/updatechecksum/update.go b/pkg/controller/updatechecksum/update.go index 05fc6ccb7..dfdfb397d 100644 --- a/pkg/controller/updatechecksum/update.go +++ b/pkg/controller/updatechecksum/update.go @@ -339,6 +339,7 @@ func (c *Controller) dlAssetAndGetChecksum(ctx context.Context, logE *logrus.Ent if err != nil { return fmt.Errorf("open a temporal file to calculate the checksum: %w", err) } + defer tempFile.Close() algorithm := "sha256" fields["algorithm"] = algorithm