Skip to content

Commit

Permalink
remove parallel dir based compress, update to newer version of cosign…
Browse files Browse the repository at this point in the history
… for tests.

Signed-off-by: Ville Aikas <vaikas@chainguard.dev>
  • Loading branch information
vaikas committed Sep 27, 2022
1 parent 82d9002 commit d7eaf57
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 97 deletions.
2 changes: 1 addition & 1 deletion cmd/tuf/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
49 changes: 0 additions & 49 deletions pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
47 changes: 0 additions & 47 deletions pkg/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit d7eaf57

Please sign in to comment.