diff --git a/cmd/tuf/server/main.go b/cmd/tuf/server/main.go index 8923d521a..84936a995 100644 --- a/cmd/tuf/server/main.go +++ b/cmd/tuf/server/main.go @@ -107,7 +107,7 @@ func main() { // Secrets have 1MiB and the repository as tested goes to about ~3k, so no // worries here. var compressed bytes.Buffer - if err := repo.Compress(dir, &compressed); err != nil { + if err := repo.CompressFS(os.DirFS(dir), &compressed, map[string]bool{"keys": true, "staged": true}); err != nil { logging.FromContext(ctx).Fatalf("Failed to compress the repo: %v", err) } data["repository"] = compressed.Bytes() diff --git a/pkg/repo/repo.go b/pkg/repo/repo.go index 027601faa..742e47633 100644 --- a/pkg/repo/repo.go +++ b/pkg/repo/repo.go @@ -108,55 +108,6 @@ func writeStagedTarget(dir, path string, data []byte) error { return nil } -// Compress archives a TUF repository so that it can be written to Secret -// for later use. -func Compress(src string, buf io.Writer) error { - wd, err := os.Getwd() - if err != nil { - return fmt.Errorf("failed to get current directory: %w", err) - } - defer os.Chdir(wd) - if err := os.Chdir(src); err != nil { - return fmt.Errorf("failed to change to dst dir %s: %w", src, err) - } - - // tar > gzip > buf - zr := gzip.NewWriter(buf) - tw := tar.NewWriter(zr) - - filepath.Walk(".", func(file string, fi os.FileInfo, err error) error { - // Skip the 'keys' and 'staged' directory - if fi.IsDir() && (fi.Name() == "keys" || fi.Name() == "staged") { - return filepath.SkipDir - } - - header, err := tar.FileInfoHeader(fi, file) - if err != nil { - return err - } - header.Name = filepath.ToSlash(file) - if err := tw.WriteHeader(header); err != nil { - return err - } - // For files, write the contents. - if !fi.IsDir() { - data, err := os.Open(file) - if err != nil { - return err - } - if _, err := io.Copy(tw, data); err != nil { - return err - } - } - return nil - }) - - if err := tw.Close(); err != nil { - return err - } - return zr.Close() -} - // CompressFS archives a TUF repository so that it can be written to Secret // for later use. func CompressFS(fsys fs.FS, buf io.Writer, skipDirs map[string]bool) error { diff --git a/pkg/repo/repo_test.go b/pkg/repo/repo_test.go index fd7394248..447717154 100644 --- a/pkg/repo/repo_test.go +++ b/pkg/repo/repo_test.go @@ -76,53 +76,6 @@ func TestCreateRepo(t *testing.T) { t.Logf("Got repo meta as: %+v", meta) } -func TestCompressUncompress(t *testing.T) { - files := map[string][]byte{ - "fulcio_v1.crt.pem": []byte(fulcioRootCert), - "ctfe.pub": []byte(ctlogPublicKey), - "rekor.pub": []byte(rekorPublicKey), - } - repo, dir, err := CreateRepo(context.Background(), files) - if err != nil { - t.Fatalf("Failed to CreateRepo: %s", err) - } - defer os.RemoveAll(dir) - - var buf bytes.Buffer - if err = Compress(dir, &buf); err != nil { - t.Fatalf("Failed to compress: %v", err) - } - dstDir := t.TempDir() - if err = Uncompress(&buf, dstDir); err != nil { - t.Fatalf("Failed to uncompress: %v", err) - } - // Then check that files have been uncompressed there. - meta, err := repo.GetMeta() - if err != nil { - t.Errorf("Failed to GetMeta: %s", err) - } - root := meta["root.json"] - - // This should have roundtripped to the new directory. - rtRoot, err := os.ReadFile(filepath.Join(dstDir, "repository", "root.json")) - if err != nil { - t.Errorf("Failed to read the roundtripped root %v", err) - } - if bytes.Compare(root, rtRoot) != 0 { - t.Errorf("Roundtripped root differs:\n%s\n%s", string(root), string(rtRoot)) - } - - // As well as, say rekor.pub under targets dir - rtRekor, err := os.ReadFile(filepath.Join(dstDir, "repository", "targets", "rekor.pub")) - if err != nil { - t.Errorf("Failed to read the roundtripped rekor %v", err) - } - if bytes.Compare(files["rekor.pub"], rtRekor) != 0 { - t.Errorf("Roundtripped rekor differs:\n%s\n%s", rekorPublicKey, string(rtRekor)) - } - -} - func TestCompressUncompressFS(t *testing.T) { files := map[string][]byte{ "fulcio_v1.crt.pem": []byte(fulcioRootCert),