Skip to content

Commit 507e038

Browse files
authored
user int64 for version (#240)
feat!: use int64 instead of int for metadata version
1 parent a747dcc commit 507e038

File tree

13 files changed

+109
-109
lines changed

13 files changed

+109
-109
lines changed

client/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ type Client struct {
6969

7070
// The following four fields represent the versions of metatdata either
7171
// from local storage or from recently downloaded metadata
72-
rootVer int
73-
targetsVer int
74-
snapshotVer int
75-
timestampVer int
72+
rootVer int64
73+
targetsVer int64
74+
snapshotVer int64
75+
timestampVer int64
7676

7777
// targets is the list of available targets, either from local storage
7878
// or from recently downloaded targets metadata
@@ -612,7 +612,7 @@ func (c *Client) downloadTarget(file string, get remoteGetFunc, hashes data.Hash
612612

613613
// downloadVersionedMeta downloads top-level metadata from remote storage and
614614
// verifies it using the given file metadata.
615-
func (c *Client) downloadMeta(name string, version int, m data.FileMeta) ([]byte, error) {
615+
func (c *Client) downloadMeta(name string, version int64, m data.FileMeta) ([]byte, error) {
616616
r, size, err := func() (io.ReadCloser, int64, error) {
617617
if c.consistentSnapshot {
618618
path := util.VersionedPath(name, version)

client/client_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -456,39 +456,39 @@ func (s *ClientSuite) TestUpdateRoots(c *C) {
456456
var tests = []struct {
457457
fixturePath string
458458
expectedError error
459-
expectedVersions map[string]int
459+
expectedVersions map[string]int64
460460
}{
461461
// Succeeds when there is no root update.
462-
{"testdata/Published1Time", nil, map[string]int{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
462+
{"testdata/Published1Time", nil, map[string]int64{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
463463
// Succeeds when client only has root.json
464-
{"testdata/Published1Time_client_root_only", nil, map[string]int{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
464+
{"testdata/Published1Time_client_root_only", nil, map[string]int64{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
465465
// Succeeds updating root from version 1 to version 2.
466-
{"testdata/Published2Times_keyrotated", nil, map[string]int{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
466+
{"testdata/Published2Times_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
467467
// Succeeds updating root from version 1 to version 2 when the client's initial root version is expired.
468-
{"testdata/Published2Times_keyrotated_initialrootexpired", nil, map[string]int{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
468+
{"testdata/Published2Times_keyrotated_initialrootexpired", nil, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
469469
// Succeeds updating root from version 1 to version 3 when versions 1 and 2 are expired.
470-
{"testdata/Published3Times_keyrotated_initialrootsexpired", nil, map[string]int{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
470+
{"testdata/Published3Times_keyrotated_initialrootsexpired", nil, map[string]int64{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
471471
// Succeeds updating root from version 2 to version 3.
472-
{"testdata/Published3Times_keyrotated_initialrootsexpired_clientversionis2", nil, map[string]int{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
472+
{"testdata/Published3Times_keyrotated_initialrootsexpired_clientversionis2", nil, map[string]int64{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
473473
// Fails updating root from version 1 to version 3 when versions 1 and 3 are expired but version 2 is not expired.
474-
{"testdata/Published3Times_keyrotated_latestrootexpired", ErrDecodeFailed{File: "root.json", Err: verify.ErrExpired{}}, map[string]int{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
474+
{"testdata/Published3Times_keyrotated_latestrootexpired", ErrDecodeFailed{File: "root.json", Err: verify.ErrExpired{}}, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
475475
// Fails updating root from version 1 to version 2 when old root 1 did not sign off on it (nth root didn't sign off n+1).
476-
{"testdata/Published2Times_keyrotated_invalidOldRootSignature", errors.New("tuf: signature verification failed"), map[string]int{}},
476+
{"testdata/Published2Times_keyrotated_invalidOldRootSignature", errors.New("tuf: signature verification failed"), map[string]int64{}},
477477
// Fails updating root from version 1 to version 2 when the new root 2 did not sign itself (n+1th root didn't sign off n+1)
478-
{"testdata/Published2Times_keyrotated_invalidNewRootSignature", errors.New("tuf: signature verification failed"), map[string]int{}},
478+
{"testdata/Published2Times_keyrotated_invalidNewRootSignature", errors.New("tuf: signature verification failed"), map[string]int64{}},
479479
// Fails updating root to 2.root.json when the value of the version field inside it is 1 (rollback attack prevention).
480-
{"testdata/Published1Time_backwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 1, Expected: 2}), map[string]int{}},
480+
{"testdata/Published1Time_backwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 1, Expected: 2}), map[string]int64{}},
481481
// Fails updating root to 2.root.json when the value of the version field inside it is 3 (rollforward attack prevention).
482-
{"testdata/Published3Times_keyrotated_forwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 3, Expected: 2}), map[string]int{}},
482+
{"testdata/Published3Times_keyrotated_forwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 3, Expected: 2}), map[string]int64{}},
483483
// Fails updating when there is no local trusted root.
484-
{"testdata/Published1Time_client_no_root", errors.New("tuf: no root keys found in local meta store"), map[string]int{}},
484+
{"testdata/Published1Time_client_no_root", errors.New("tuf: no root keys found in local meta store"), map[string]int64{}},
485485

486486
// snapshot role key rotation increase the snapshot and timestamp.
487-
{"testdata/Published2Times_snapshot_keyrotated", nil, map[string]int{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 1}},
487+
{"testdata/Published2Times_snapshot_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 1}},
488488
// targets role key rotation increase the snapshot, timestamp, and targets.
489-
{"testdata/Published2Times_targets_keyrotated", nil, map[string]int{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 2}},
489+
{"testdata/Published2Times_targets_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 2}},
490490
// timestamp role key rotation increase the timestamp.
491-
{"testdata/Published2Times_timestamp_keyrotated", nil, map[string]int{"root": 2, "timestamp": 2, "snapshot": 1, "targets": 1}},
491+
{"testdata/Published2Times_timestamp_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 1, "targets": 1}},
492492
}
493493

494494
for _, test := range tests {
@@ -498,7 +498,7 @@ func (s *ClientSuite) TestUpdateRoots(c *C) {
498498
c.Assert(err, IsNil)
499499
// Check if the root.json is being saved in non-volatile storage.
500500
tufClient.getLocalMeta()
501-
versionMethods := map[string]int{"root": tufClient.rootVer,
501+
versionMethods := map[string]int64{"root": tufClient.rootVer,
502502
"timestamp": tufClient.timestampVer,
503503
"snapshot": tufClient.snapshotVer,
504504
"targets": tufClient.targetsVer}
@@ -800,7 +800,7 @@ func (s *ClientSuite) TestUpdateLocalRootExpired(c *C) {
800800
c.Assert(s.repo.Commit(), IsNil)
801801
s.syncRemote(c)
802802

803-
const expectedRootVersion = 3
803+
const expectedRootVersion = int64(3)
804804

805805
// check the update downloads the non expired remote root.json and
806806
// restarts itself, thus successfully updating

client/delegations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func TestPersistedMeta(t *testing.T) {
129129

130130
type expectedTargets struct {
131131
name string
132-
version int
132+
version int64
133133
}
134134
var persistedTests = []struct {
135135
file string
@@ -241,7 +241,7 @@ func TestPersistedMeta(t *testing.T) {
241241
}
242242
}
243243

244-
func versionOfStoredTargets(name string, store map[string]json.RawMessage) (int, error) {
244+
func versionOfStoredTargets(name string, store map[string]json.RawMessage) (int64, error) {
245245
rawTargets, ok := store[name]
246246
if !ok {
247247
return 0, nil

client/errors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (e ErrDecodeFailed) Error() string {
4242
type ErrMaxDelegations struct {
4343
Target string
4444
MaxDelegations int
45-
SnapshotVersion int
45+
SnapshotVersion int64
4646
}
4747

4848
func (e ErrMaxDelegations) Error() string {
@@ -87,7 +87,7 @@ func (e ErrWrongSize) Error() string {
8787
}
8888

8989
type ErrLatestSnapshot struct {
90-
Version int
90+
Version int64
9191
}
9292

9393
func (e ErrLatestSnapshot) Error() string {
@@ -101,7 +101,7 @@ func IsLatestSnapshot(err error) bool {
101101

102102
type ErrUnknownTarget struct {
103103
Name string
104-
SnapshotVersion int
104+
SnapshotVersion int64
105105
}
106106

107107
func (e ErrUnknownTarget) Error() string {
@@ -128,7 +128,7 @@ func (e ErrInvalidURL) Error() string {
128128

129129
type ErrRoleNotInSnapshot struct {
130130
Role string
131-
SnapshotVersion int
131+
SnapshotVersion int64
132132
}
133133

134134
func (e ErrRoleNotInSnapshot) Error() string {

data/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func DefaultExpires(role string) time.Time {
9696
type Root struct {
9797
Type string `json:"_type"`
9898
SpecVersion string `json:"spec_version"`
99-
Version int `json:"version"`
99+
Version int64 `json:"version"`
100100
Expires time.Time `json:"expires"`
101101
Keys map[string]*PublicKey `json:"keys"`
102102
Roles map[string]*Role `json:"roles"`
@@ -167,15 +167,15 @@ func (f FileMeta) HashAlgorithms() []string {
167167

168168
type SnapshotFileMeta struct {
169169
FileMeta
170-
Version int `json:"version"`
170+
Version int64 `json:"version"`
171171
}
172172

173173
type SnapshotFiles map[string]SnapshotFileMeta
174174

175175
type Snapshot struct {
176176
Type string `json:"_type"`
177177
SpecVersion string `json:"spec_version"`
178-
Version int `json:"version"`
178+
Version int64 `json:"version"`
179179
Expires time.Time `json:"expires"`
180180
Meta SnapshotFiles `json:"meta"`
181181
Custom *json.RawMessage `json:"custom,omitempty"`
@@ -203,7 +203,7 @@ func (f TargetFileMeta) HashAlgorithms() []string {
203203
type Targets struct {
204204
Type string `json:"_type"`
205205
SpecVersion string `json:"spec_version"`
206-
Version int `json:"version"`
206+
Version int64 `json:"version"`
207207
Expires time.Time `json:"expires"`
208208
Targets TargetFiles `json:"targets"`
209209
Delegations *Delegations `json:"delegations,omitempty"`
@@ -302,15 +302,15 @@ func NewTargets() *Targets {
302302

303303
type TimestampFileMeta struct {
304304
FileMeta
305-
Version int `json:"version"`
305+
Version int64 `json:"version"`
306306
}
307307

308308
type TimestampFiles map[string]TimestampFileMeta
309309

310310
type Timestamp struct {
311311
Type string `json:"_type"`
312312
SpecVersion string `json:"spec_version"`
313-
Version int `json:"version"`
313+
Version int64 `json:"version"`
314314
Expires time.Time `json:"expires"`
315315
Meta TimestampFiles `json:"meta"`
316316
Custom *json.RawMessage `json:"custom,omitempty"`

local_store.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type LocalStore interface {
4040
// This will also reset the staged meta to signal incrementing version numbers.
4141
// TUF 1.0 requires that the root metadata version numbers in the repository does not
4242
// gaps. To avoid this, we will only increment the number once until we commit.
43-
Commit(bool, map[string]int, map[string]data.Hashes) error
43+
Commit(bool, map[string]int64, map[string]data.Hashes) error
4444

4545
// GetSigners return a list of signers for a role.
4646
GetSigners(role string) ([]keys.Signer, error)
@@ -125,7 +125,7 @@ func (m *memoryStore) WalkStagedTargets(paths []string, targetsFn TargetsWalkFun
125125
return nil
126126
}
127127

128-
func (m *memoryStore) Commit(consistentSnapshot bool, versions map[string]int, hashes map[string]data.Hashes) error {
128+
func (m *memoryStore) Commit(consistentSnapshot bool, versions map[string]int64, hashes map[string]data.Hashes) error {
129129
for name, meta := range m.stagedMeta {
130130
paths := computeMetadataPaths(consistentSnapshot, name, versions)
131131
for _, path := range paths {
@@ -369,7 +369,7 @@ func (f *fileSystemStore) createRepoFile(path string) (*os.File, error) {
369369
return os.Create(dst)
370370
}
371371

372-
func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]int, hashes map[string]data.Hashes) error {
372+
func (f *fileSystemStore) Commit(consistentSnapshot bool, versions map[string]int64, hashes map[string]data.Hashes) error {
373373
isTarget := func(path string) bool {
374374
return strings.HasPrefix(path, "targets/")
375375
}
@@ -700,7 +700,7 @@ func computeTargetPaths(consistentSnapshot bool, name string, hashes map[string]
700700
}
701701
}
702702

703-
func computeMetadataPaths(consistentSnapshot bool, name string, versions map[string]int) []string {
703+
func computeMetadataPaths(consistentSnapshot bool, name string, versions map[string]int64) []string {
704704
copyVersion := false
705705

706706
switch name {

repo.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (r *Repo) snapshot() (*data.Snapshot, error) {
143143
return snapshot, nil
144144
}
145145

146-
func (r *Repo) RootVersion() (int, error) {
146+
func (r *Repo) RootVersion() (int64, error) {
147147
root, err := r.root()
148148
if err != nil {
149149
return -1, err
@@ -202,7 +202,7 @@ func (r *Repo) Targets() (data.TargetFiles, error) {
202202
return targets.Targets, nil
203203
}
204204

205-
func (r *Repo) SetTargetsVersion(v int) error {
205+
func (r *Repo) SetTargetsVersion(v int64) error {
206206
t, err := r.topLevelTargets()
207207
if err != nil {
208208
return err
@@ -211,15 +211,15 @@ func (r *Repo) SetTargetsVersion(v int) error {
211211
return r.setTopLevelMeta("targets.json", t)
212212
}
213213

214-
func (r *Repo) TargetsVersion() (int, error) {
214+
func (r *Repo) TargetsVersion() (int64, error) {
215215
t, err := r.topLevelTargets()
216216
if err != nil {
217217
return -1, err
218218
}
219219
return t.Version, nil
220220
}
221221

222-
func (r *Repo) SetTimestampVersion(v int) error {
222+
func (r *Repo) SetTimestampVersion(v int64) error {
223223
ts, err := r.timestamp()
224224
if err != nil {
225225
return err
@@ -228,15 +228,15 @@ func (r *Repo) SetTimestampVersion(v int) error {
228228
return r.setTopLevelMeta("timestamp.json", ts)
229229
}
230230

231-
func (r *Repo) TimestampVersion() (int, error) {
231+
func (r *Repo) TimestampVersion() (int64, error) {
232232
ts, err := r.timestamp()
233233
if err != nil {
234234
return -1, err
235235
}
236236
return ts.Version, nil
237237
}
238238

239-
func (r *Repo) SetSnapshotVersion(v int) error {
239+
func (r *Repo) SetSnapshotVersion(v int64) error {
240240
s, err := r.snapshot()
241241
if err != nil {
242242
return err
@@ -246,7 +246,7 @@ func (r *Repo) SetSnapshotVersion(v int) error {
246246
return r.setTopLevelMeta("snapshot.json", s)
247247
}
248248

249-
func (r *Repo) SnapshotVersion() (int, error) {
249+
func (r *Repo) SnapshotVersion() (int64, error) {
250250
s, err := r.snapshot()
251251
if err != nil {
252252
return -1, err
@@ -903,7 +903,7 @@ func (r *Repo) TimestampWithExpires(expires time.Time) error {
903903
return err
904904
}
905905

906-
func (r *Repo) fileVersions() (map[string]int, error) {
906+
func (r *Repo) fileVersions() (map[string]int64, error) {
907907
root, err := r.root()
908908
if err != nil {
909909
return nil, err
@@ -916,7 +916,7 @@ func (r *Repo) fileVersions() (map[string]int, error) {
916916
if err != nil {
917917
return nil, err
918918
}
919-
versions := make(map[string]int)
919+
versions := make(map[string]int64)
920920
versions["root.json"] = root.Version
921921
versions["targets.json"] = targets.Version
922922
versions["snapshot.json"] = snapshot.Version

0 commit comments

Comments
 (0)