Skip to content

Commit

Permalink
Merge pull request #45 from fluxcd/artifact-integrity-check
Browse files Browse the repository at this point in the history
git: add archive integrity check
  • Loading branch information
stefanprodan authored Apr 28, 2020
2 parents 8b98573 + 9540efe commit c71cfa3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
}
defer unlock()

// archive artifact
err = r.Storage.Archive(artifact, tmpGit, "")
// archive artifact and check integrity
err = r.Storage.Archive(artifact, tmpGit, "", true)
if err != nil {
err = fmt.Errorf("storage archive error: %w", err)
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err
Expand Down
19 changes: 18 additions & 1 deletion controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
}

// Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string) error {
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error {
if excludes == "" {
excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
}
Expand All @@ -129,6 +129,23 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes strin
if err != nil {
return fmt.Errorf("command '%s' failed: %w", cmd, err)
}

if integrityCheck {
cmd = fmt.Sprintf("gunzip -t %s", artifact.Path)
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
err = command.Run()
if err != nil {
return fmt.Errorf("gzip integrity check failed")
}

cmd = fmt.Sprintf("tar -tzf %s >/dev/null", artifact.Path)
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
err = command.Run()
if err != nil {
return fmt.Errorf("tar integrity check failed")
}
}

return nil
}

Expand Down

0 comments on commit c71cfa3

Please sign in to comment.