Skip to content

Commit

Permalink
Allow commit without adding targets (#238)
Browse files Browse the repository at this point in the history
By making `init` write an empty `targets.json`.
  • Loading branch information
znewman01 authored Mar 23, 2022
1 parent 506b95a commit 8453bf6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
13 changes: 8 additions & 5 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8453bf6

Please sign in to comment.