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

git: add archive integrity check #45

Merged
merged 1 commit into from
Apr 28, 2020
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
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