Skip to content

Commit d9a4507

Browse files
committed
addressed more comments. Set the rootVersion in loadAndVerifyLocalRootMeta. Fixed a buggy test.
1 parent 2fc418a commit d9a4507

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

client/client.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,25 +245,21 @@ func (c *Client) updateRoots() error {
245245
// Prepare for 5.3.11: If the timestamp and / or snapshot keys have been rotated,
246246
// then delete the trusted timestamp and snapshot metadata files.
247247
getKeyInfo := func(role string) KeyInfo {
248-
//keyIDs := make([]string, 0, len(c.db.GetRole(role).KeyIDs))
249248
keyIDs := make(map[string]bool)
250249
for k := range c.db.GetRole(role).KeyIDs {
251-
//keyIDs = append(keyIDs, k)
252250
keyIDs[k] = true
253251
}
254-
//sort.Strings(keyIDs)
255252
return KeyInfo{keyIDs, c.db.GetRole(role).Threshold}
256253
}
257254

258-
// The manifest looks like this:
255+
// The nonRootKeyInfo looks like this:
259256
// {
260-
// "timestamp": ["KEYID1", "KEYID2"],
261-
// "snapshot": ["KEYID3"],
262-
// "targets": ["KEYID4", "KEYID5", "KEYID6"]
257+
// "timestamp": {KeyIDs={"KEYID1": true, "KEYID2": true}, Threshold=2},
258+
// "snapshot": {KeyIDs={"KEYID3": true}, Threshold=1},
259+
// "targets": {KeyIDs={"KEYID4": true, "KEYID5": true, "KEYID6": true}, Threshold=1}
263260
// }
264261

265262
nonRootKeyInfo := map[string]KeyInfo{"timestamp": {}, "snapshot": {}, "targets": {}}
266-
//nonRootThreshold := map[string]int{"timestamp": 1, "snapshot": 1, "targets": 1}
267263
for k := range nonRootKeyInfo {
268264
nonRootKeyInfo[k] = getKeyInfo(k)
269265
}
@@ -299,9 +295,8 @@ func (c *Client) updateRoots() error {
299295
if _, ok := err.(ErrMissingRemoteMetadata); ok {
300296
// stop when the next root can't be downloaded
301297
break
302-
} else {
303-
return err
304298
}
299+
return err
305300
}
306301

307302
// 5.3.4 Check for an arbitrary software attack.
@@ -369,7 +364,11 @@ func (c *Client) updateRoots() error {
369364
// timestamp -> delete timestamp.json
370365
// snapshot -> delete timestamp.json and snapshot.json
371366
// targets -> delete snapshot.json and targets.json
367+
//
368+
// nonRootKeyInfo contains the keys and thresholds from root.json
369+
// that were on disk before the root update process begins.
372370
for topLevelRolename := range nonRootKeyInfo {
371+
// ki contains the keys and thresholds from the latest downloaded root.json.
373372
ki := getKeyInfo(topLevelRolename)
374373
if countDeleted(nonRootKeyInfo[topLevelRolename].KeyIDs, ki.KeyIDs) >= nonRootKeyInfo[topLevelRolename].Threshold {
375374
deleteMeta := map[string][]string{
@@ -479,6 +478,7 @@ func (c *Client) loadAndVerifyLocalRootMeta() error {
479478
return err
480479
}
481480
c.consistentSnapshot = root.ConsistentSnapshot
481+
c.rootVer = root.Version
482482
c.db = ndb
483483
return nil
484484
}

client/client_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,15 +726,20 @@ func (s *ClientSuite) TestUpdateLocalRootExpired(c *C) {
726726

727727
// add soon to expire root.json to local storage
728728
s.genKeyExpired(c, "timestamp")
729+
c.Assert(s.repo.Snapshot(tuf.CompressionTypeNone), IsNil)
729730
c.Assert(s.repo.Timestamp(), IsNil)
731+
c.Assert(s.repo.Commit(), IsNil)
730732
s.syncLocal(c)
731733

732734
// add far expiring root.json to remote storage
733735
s.genKey(c, "timestamp")
734736
s.addRemoteTarget(c, "bar.txt")
737+
c.Assert(s.repo.Snapshot(tuf.CompressionTypeNone), IsNil)
738+
c.Assert(s.repo.Timestamp(), IsNil)
739+
c.Assert(s.repo.Commit(), IsNil)
735740
s.syncRemote(c)
736741

737-
const expectedRootVersion = 2
742+
const expectedRootVersion = 3
738743

739744
// check the update downloads the non expired remote root.json and
740745
// restarts itself, thus successfully updating
@@ -761,7 +766,9 @@ func (s *ClientSuite) TestUpdateRemoteExpired(c *C) {
761766
})
762767

763768
c.Assert(s.repo.SnapshotWithExpires(tuf.CompressionTypeNone, s.expiredTime), IsNil)
769+
c.Assert(s.repo.Snapshot(tuf.CompressionTypeNone), IsNil)
764770
c.Assert(s.repo.Timestamp(), IsNil)
771+
c.Assert(s.repo.Commit(), IsNil)
765772
s.syncRemote(c)
766773
s.withMetaExpired(func() {
767774
_, err := client.Update()
@@ -794,7 +801,9 @@ func (s *ClientSuite) TestUpdateLocalRootExpiredKeyChange(c *C) {
794801

795802
// add soon to expire root.json to local storage
796803
s.genKeyExpired(c, "timestamp")
804+
c.Assert(s.repo.Snapshot(tuf.CompressionTypeNone), IsNil)
797805
c.Assert(s.repo.Timestamp(), IsNil)
806+
c.Assert(s.repo.Commit(), IsNil)
798807
s.syncLocal(c)
799808

800809
// replace all keys

0 commit comments

Comments
 (0)