Skip to content

Commit

Permalink
tuf: fix on-disk cache when writing targets in subfolders (#729)
Browse files Browse the repository at this point in the history
Added a test, patched cosign locally, and can confirm this now works correctly with the v5 root changes.

Note: no old clients will break from this change. It's forward compatible.

It's in anticipation of a root update that we'll push to a separate repository as to not break clients who are still using this version. Clients won't break until the current repository loses lifetime: in late January next year.

Signed-off-by: Asra Ali <asraa@google.com>
  • Loading branch information
asraa committed Oct 6, 2022
1 parent bba7507 commit a862909
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pkg/tuf/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,10 +677,12 @@ func (d *diskCache) Set(p string, b []byte) error {
if err := d.memory.Set(p, b); err != nil {
return err
}
if err := os.MkdirAll(d.base, 0o700); err != nil {

fp := filepath.FromSlash(filepath.Join(d.base, p))
if err := os.MkdirAll(filepath.Dir(fp), 0o700); err != nil {
return fmt.Errorf("creating targets dir: %w", err)
}
fp := filepath.FromSlash(filepath.Join(d.base, p))

return os.WriteFile(fp, b, 0o600)
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/tuf/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,43 @@ func TestKeyFormatMigration(t *testing.T) {
}
checkTargetsAndMeta(t, tuf, []string{"fulcio.crt.pem"})
}

// Test to validate that sigstore TUF client can cache targets that
// are located in sub-folders.
func TestTargetsSubfolder(t *testing.T) {
ctx := context.Background()
// Create a remote repository.
td := t.TempDir()
remote, r := newTufCustomRepo(t, td, "foo")
newTarget := "subfolder/fooNew.txt"
addNewCustomTarget(t, td, r, map[string]string{newTarget: "newdata"})

// Serve remote repository.
s := httptest.NewServer(http.FileServer(http.Dir(filepath.Join(td, "repository"))))
defer s.Close()

// Initialize with custom root.
tufRoot := t.TempDir()
// Set the TUF_ROOT so we don't interact with other tests and local TUF roots.
t.Setenv("TUF_ROOT", tufRoot)
meta, err := remote.GetMeta()
if err != nil {
t.Error(err)
}
rootBytes, ok := meta["root.json"]
if !ok {
t.Error(err)
}

if err := Initialize(ctx, s.URL, rootBytes); err != nil {
t.Error(err)
}

defer resetForTests()

tuf, err := NewFromEnv(ctx)
if err != nil {
t.Fatal(err)
}
checkTargetsAndMeta(t, tuf, []string{newTarget})
}

0 comments on commit a862909

Please sign in to comment.