From 8453bf659e8a61a211dc897db079e95a465c8e15 Mon Sep 17 00:00:00 2001 From: Zack Newman Date: Wed, 23 Mar 2022 13:20:17 -0400 Subject: [PATCH] Allow commit without adding targets (#238) By making `init` write an empty `targets.json`. --- repo.go | 13 ++++++++----- repo_test.go | 12 +++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/repo.go b/repo.go index c436195e..cb6922ba 100644 --- a/repo.go +++ b/repo.go @@ -74,11 +74,14 @@ func (r *Repo) Init(consistentSnapshot bool) error { root.ConsistentSnapshot = consistentSnapshot // Set root version to 1 for a new root. root.Version = 1 - err = r.setTopLevelMeta("root.json", root) - if err == nil { - fmt.Println("Repository initialized") + if err = r.setTopLevelMeta("root.json", root); err != nil { + return err } - return err + if err = r.writeTargetWithExpires(t, data.DefaultExpires("targets")); err != nil { + return err + } + fmt.Println("Repository initialized") + return nil } func (r *Repo) topLevelKeysDB() (*verify.DB, error) { @@ -738,7 +741,7 @@ func (r *Repo) writeTargetWithExpires(t *data.Targets, expires time.Time) error } err := r.setTopLevelMeta("targets.json", t) - if err == nil { + if err == nil && len(t.Targets) > 0 { fmt.Println("Added/staged targets:") for k := range t.Targets { fmt.Println("*", k) diff --git a/repo_test.go b/repo_test.go index 1e46147c..eb9356ef 100644 --- a/repo_test.go +++ b/repo_test.go @@ -687,13 +687,15 @@ func (rs *RepoSuite) TestCommit(c *C) { // commit without root.json c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"root.json"}) - // commit without targets.json + // Init should create targets.json, but not signed yet + r.Init(false) + c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"snapshot.json"}) + genKey(c, r, "root") - c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"targets.json"}) // commit without snapshot.json genKey(c, r, "targets") - c.Assert(r.AddTarget("foo.txt", nil), IsNil) + c.Assert(r.Sign("targets.json"), IsNil) c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"snapshot.json"}) // commit without timestamp.json @@ -714,12 +716,12 @@ func (rs *RepoSuite) TestCommit(c *C) { // commit with an invalid root hash in snapshot.json due to new key creation genKey(c, r, "targets") c.Assert(r.Sign("targets.json"), IsNil) - c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 511 got 725")) + c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 338 got 552")) // commit with an invalid targets hash in snapshot.json c.Assert(r.Snapshot(), IsNil) c.Assert(r.AddTarget("bar.txt", nil), IsNil) - c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 725 got 899")) + c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 552 got 725")) // commit with an invalid timestamp c.Assert(r.Snapshot(), IsNil)