Skip to content

Commit 8453bf6

Browse files
authored
Allow commit without adding targets (#238)
By making `init` write an empty `targets.json`.
1 parent 506b95a commit 8453bf6

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

repo.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ func (r *Repo) Init(consistentSnapshot bool) error {
7474
root.ConsistentSnapshot = consistentSnapshot
7575
// Set root version to 1 for a new root.
7676
root.Version = 1
77-
err = r.setTopLevelMeta("root.json", root)
78-
if err == nil {
79-
fmt.Println("Repository initialized")
77+
if err = r.setTopLevelMeta("root.json", root); err != nil {
78+
return err
8079
}
81-
return err
80+
if err = r.writeTargetWithExpires(t, data.DefaultExpires("targets")); err != nil {
81+
return err
82+
}
83+
fmt.Println("Repository initialized")
84+
return nil
8285
}
8386

8487
func (r *Repo) topLevelKeysDB() (*verify.DB, error) {
@@ -738,7 +741,7 @@ func (r *Repo) writeTargetWithExpires(t *data.Targets, expires time.Time) error
738741
}
739742

740743
err := r.setTopLevelMeta("targets.json", t)
741-
if err == nil {
744+
if err == nil && len(t.Targets) > 0 {
742745
fmt.Println("Added/staged targets:")
743746
for k := range t.Targets {
744747
fmt.Println("*", k)

repo_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,15 @@ func (rs *RepoSuite) TestCommit(c *C) {
687687
// commit without root.json
688688
c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"root.json"})
689689

690-
// commit without targets.json
690+
// Init should create targets.json, but not signed yet
691+
r.Init(false)
692+
c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"snapshot.json"})
693+
691694
genKey(c, r, "root")
692-
c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"targets.json"})
693695

694696
// commit without snapshot.json
695697
genKey(c, r, "targets")
696-
c.Assert(r.AddTarget("foo.txt", nil), IsNil)
698+
c.Assert(r.Sign("targets.json"), IsNil)
697699
c.Assert(r.Commit(), DeepEquals, ErrMissingMetadata{"snapshot.json"})
698700

699701
// commit without timestamp.json
@@ -714,12 +716,12 @@ func (rs *RepoSuite) TestCommit(c *C) {
714716
// commit with an invalid root hash in snapshot.json due to new key creation
715717
genKey(c, r, "targets")
716718
c.Assert(r.Sign("targets.json"), IsNil)
717-
c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 511 got 725"))
719+
c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 338 got 552"))
718720

719721
// commit with an invalid targets hash in snapshot.json
720722
c.Assert(r.Snapshot(), IsNil)
721723
c.Assert(r.AddTarget("bar.txt", nil), IsNil)
722-
c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 725 got 899"))
724+
c.Assert(r.Commit(), DeepEquals, errors.New("tuf: invalid targets.json in snapshot.json: wrong length, expected 552 got 725"))
723725

724726
// commit with an invalid timestamp
725727
c.Assert(r.Snapshot(), IsNil)

0 commit comments

Comments
 (0)