Skip to content

Commit c71cfa3

Browse files
authored
Merge pull request #45 from fluxcd/artifact-integrity-check
git: add archive integrity check
2 parents 8b98573 + 9540efe commit c71cfa3

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

controllers/gitrepository_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
341341
}
342342
defer unlock()
343343

344-
// archive artifact
345-
err = r.Storage.Archive(artifact, tmpGit, "")
344+
// archive artifact and check integrity
345+
err = r.Storage.Archive(artifact, tmpGit, "", true)
346346
if err != nil {
347347
err = fmt.Errorf("storage archive error: %w", err)
348348
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err

controllers/storage.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
113113
}
114114

115115
// Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions
116-
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string) error {
116+
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error {
117117
if excludes == "" {
118118
excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
119119
}
@@ -129,6 +129,23 @@ func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes strin
129129
if err != nil {
130130
return fmt.Errorf("command '%s' failed: %w", cmd, err)
131131
}
132+
133+
if integrityCheck {
134+
cmd = fmt.Sprintf("gunzip -t %s", artifact.Path)
135+
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
136+
err = command.Run()
137+
if err != nil {
138+
return fmt.Errorf("gzip integrity check failed")
139+
}
140+
141+
cmd = fmt.Sprintf("tar -tzf %s >/dev/null", artifact.Path)
142+
command = exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
143+
err = command.Run()
144+
if err != nil {
145+
return fmt.Errorf("tar integrity check failed")
146+
}
147+
}
148+
132149
return nil
133150
}
134151

0 commit comments

Comments
 (0)