Skip to content

Commit

Permalink
user int64 for version (#240)
Browse files Browse the repository at this point in the history
feat!: use int64 instead of int for metadata version
  • Loading branch information
arbll authored Apr 13, 2022
1 parent a747dcc commit 507e038
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 109 deletions.
10 changes: 5 additions & 5 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ type Client struct {

// The following four fields represent the versions of metatdata either
// from local storage or from recently downloaded metadata
rootVer int
targetsVer int
snapshotVer int
timestampVer int
rootVer int64
targetsVer int64
snapshotVer int64
timestampVer int64

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

// downloadVersionedMeta downloads top-level metadata from remote storage and
// verifies it using the given file metadata.
func (c *Client) downloadMeta(name string, version int, m data.FileMeta) ([]byte, error) {
func (c *Client) downloadMeta(name string, version int64, m data.FileMeta) ([]byte, error) {
r, size, err := func() (io.ReadCloser, int64, error) {
if c.consistentSnapshot {
path := util.VersionedPath(name, version)
Expand Down
36 changes: 18 additions & 18 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,39 +456,39 @@ func (s *ClientSuite) TestUpdateRoots(c *C) {
var tests = []struct {
fixturePath string
expectedError error
expectedVersions map[string]int
expectedVersions map[string]int64
}{
// Succeeds when there is no root update.
{"testdata/Published1Time", nil, map[string]int{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published1Time", nil, map[string]int64{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds when client only has root.json
{"testdata/Published1Time_client_root_only", nil, map[string]int{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published1Time_client_root_only", nil, map[string]int64{"root": 1, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 1 to version 2.
{"testdata/Published2Times_keyrotated", nil, map[string]int{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published2Times_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 1 to version 2 when the client's initial root version is expired.
{"testdata/Published2Times_keyrotated_initialrootexpired", nil, map[string]int{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published2Times_keyrotated_initialrootexpired", nil, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 1 to version 3 when versions 1 and 2 are expired.
{"testdata/Published3Times_keyrotated_initialrootsexpired", nil, map[string]int{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published3Times_keyrotated_initialrootsexpired", nil, map[string]int64{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Succeeds updating root from version 2 to version 3.
{"testdata/Published3Times_keyrotated_initialrootsexpired_clientversionis2", nil, map[string]int{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published3Times_keyrotated_initialrootsexpired_clientversionis2", nil, map[string]int64{"root": 3, "timestamp": 1, "snapshot": 1, "targets": 1}},
// Fails updating root from version 1 to version 3 when versions 1 and 3 are expired but version 2 is not expired.
{"testdata/Published3Times_keyrotated_latestrootexpired", ErrDecodeFailed{File: "root.json", Err: verify.ErrExpired{}}, map[string]int{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
{"testdata/Published3Times_keyrotated_latestrootexpired", ErrDecodeFailed{File: "root.json", Err: verify.ErrExpired{}}, map[string]int64{"root": 2, "timestamp": 1, "snapshot": 1, "targets": 1}},
// 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).
{"testdata/Published2Times_keyrotated_invalidOldRootSignature", errors.New("tuf: signature verification failed"), map[string]int{}},
{"testdata/Published2Times_keyrotated_invalidOldRootSignature", errors.New("tuf: signature verification failed"), map[string]int64{}},
// 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)
{"testdata/Published2Times_keyrotated_invalidNewRootSignature", errors.New("tuf: signature verification failed"), map[string]int{}},
{"testdata/Published2Times_keyrotated_invalidNewRootSignature", errors.New("tuf: signature verification failed"), map[string]int64{}},
// Fails updating root to 2.root.json when the value of the version field inside it is 1 (rollback attack prevention).
{"testdata/Published1Time_backwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 1, Expected: 2}), map[string]int{}},
{"testdata/Published1Time_backwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 1, Expected: 2}), map[string]int64{}},
// Fails updating root to 2.root.json when the value of the version field inside it is 3 (rollforward attack prevention).
{"testdata/Published3Times_keyrotated_forwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 3, Expected: 2}), map[string]int{}},
{"testdata/Published3Times_keyrotated_forwardRootVersion", verify.ErrWrongVersion(verify.ErrWrongVersion{Given: 3, Expected: 2}), map[string]int64{}},
// Fails updating when there is no local trusted root.
{"testdata/Published1Time_client_no_root", errors.New("tuf: no root keys found in local meta store"), map[string]int{}},
{"testdata/Published1Time_client_no_root", errors.New("tuf: no root keys found in local meta store"), map[string]int64{}},

// snapshot role key rotation increase the snapshot and timestamp.
{"testdata/Published2Times_snapshot_keyrotated", nil, map[string]int{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 1}},
{"testdata/Published2Times_snapshot_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 1}},
// targets role key rotation increase the snapshot, timestamp, and targets.
{"testdata/Published2Times_targets_keyrotated", nil, map[string]int{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 2}},
{"testdata/Published2Times_targets_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 2, "targets": 2}},
// timestamp role key rotation increase the timestamp.
{"testdata/Published2Times_timestamp_keyrotated", nil, map[string]int{"root": 2, "timestamp": 2, "snapshot": 1, "targets": 1}},
{"testdata/Published2Times_timestamp_keyrotated", nil, map[string]int64{"root": 2, "timestamp": 2, "snapshot": 1, "targets": 1}},
}

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

const expectedRootVersion = 3
const expectedRootVersion = int64(3)

// check the update downloads the non expired remote root.json and
// restarts itself, thus successfully updating
Expand Down
4 changes: 2 additions & 2 deletions client/delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestPersistedMeta(t *testing.T) {

type expectedTargets struct {
name string
version int
version int64
}
var persistedTests = []struct {
file string
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestPersistedMeta(t *testing.T) {
}
}

func versionOfStoredTargets(name string, store map[string]json.RawMessage) (int, error) {
func versionOfStoredTargets(name string, store map[string]json.RawMessage) (int64, error) {
rawTargets, ok := store[name]
if !ok {
return 0, nil
Expand Down
8 changes: 4 additions & 4 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (e ErrDecodeFailed) Error() string {
type ErrMaxDelegations struct {
Target string
MaxDelegations int
SnapshotVersion int
SnapshotVersion int64
}

func (e ErrMaxDelegations) Error() string {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (e ErrWrongSize) Error() string {
}

type ErrLatestSnapshot struct {
Version int
Version int64
}

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

type ErrUnknownTarget struct {
Name string
SnapshotVersion int
SnapshotVersion int64
}

func (e ErrUnknownTarget) Error() string {
Expand All @@ -128,7 +128,7 @@ func (e ErrInvalidURL) Error() string {

type ErrRoleNotInSnapshot struct {
Role string
SnapshotVersion int
SnapshotVersion int64
}

func (e ErrRoleNotInSnapshot) Error() string {
Expand Down
12 changes: 6 additions & 6 deletions data/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func DefaultExpires(role string) time.Time {
type Root struct {
Type string `json:"_type"`
SpecVersion string `json:"spec_version"`
Version int `json:"version"`
Version int64 `json:"version"`
Expires time.Time `json:"expires"`
Keys map[string]*PublicKey `json:"keys"`
Roles map[string]*Role `json:"roles"`
Expand Down Expand Up @@ -167,15 +167,15 @@ func (f FileMeta) HashAlgorithms() []string {

type SnapshotFileMeta struct {
FileMeta
Version int `json:"version"`
Version int64 `json:"version"`
}

type SnapshotFiles map[string]SnapshotFileMeta

type Snapshot struct {
Type string `json:"_type"`
SpecVersion string `json:"spec_version"`
Version int `json:"version"`
Version int64 `json:"version"`
Expires time.Time `json:"expires"`
Meta SnapshotFiles `json:"meta"`
Custom *json.RawMessage `json:"custom,omitempty"`
Expand Down Expand Up @@ -203,7 +203,7 @@ func (f TargetFileMeta) HashAlgorithms() []string {
type Targets struct {
Type string `json:"_type"`
SpecVersion string `json:"spec_version"`
Version int `json:"version"`
Version int64 `json:"version"`
Expires time.Time `json:"expires"`
Targets TargetFiles `json:"targets"`
Delegations *Delegations `json:"delegations,omitempty"`
Expand Down Expand Up @@ -302,15 +302,15 @@ func NewTargets() *Targets {

type TimestampFileMeta struct {
FileMeta
Version int `json:"version"`
Version int64 `json:"version"`
}

type TimestampFiles map[string]TimestampFileMeta

type Timestamp struct {
Type string `json:"_type"`
SpecVersion string `json:"spec_version"`
Version int `json:"version"`
Version int64 `json:"version"`
Expires time.Time `json:"expires"`
Meta TimestampFiles `json:"meta"`
Custom *json.RawMessage `json:"custom,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions local_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type LocalStore interface {
// This will also reset the staged meta to signal incrementing version numbers.
// TUF 1.0 requires that the root metadata version numbers in the repository does not
// gaps. To avoid this, we will only increment the number once until we commit.
Commit(bool, map[string]int, map[string]data.Hashes) error
Commit(bool, map[string]int64, map[string]data.Hashes) error

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

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

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

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

switch name {
Expand Down
18 changes: 9 additions & 9 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (r *Repo) snapshot() (*data.Snapshot, error) {
return snapshot, nil
}

func (r *Repo) RootVersion() (int, error) {
func (r *Repo) RootVersion() (int64, error) {
root, err := r.root()
if err != nil {
return -1, err
Expand Down Expand Up @@ -202,7 +202,7 @@ func (r *Repo) Targets() (data.TargetFiles, error) {
return targets.Targets, nil
}

func (r *Repo) SetTargetsVersion(v int) error {
func (r *Repo) SetTargetsVersion(v int64) error {
t, err := r.topLevelTargets()
if err != nil {
return err
Expand All @@ -211,15 +211,15 @@ func (r *Repo) SetTargetsVersion(v int) error {
return r.setTopLevelMeta("targets.json", t)
}

func (r *Repo) TargetsVersion() (int, error) {
func (r *Repo) TargetsVersion() (int64, error) {
t, err := r.topLevelTargets()
if err != nil {
return -1, err
}
return t.Version, nil
}

func (r *Repo) SetTimestampVersion(v int) error {
func (r *Repo) SetTimestampVersion(v int64) error {
ts, err := r.timestamp()
if err != nil {
return err
Expand All @@ -228,15 +228,15 @@ func (r *Repo) SetTimestampVersion(v int) error {
return r.setTopLevelMeta("timestamp.json", ts)
}

func (r *Repo) TimestampVersion() (int, error) {
func (r *Repo) TimestampVersion() (int64, error) {
ts, err := r.timestamp()
if err != nil {
return -1, err
}
return ts.Version, nil
}

func (r *Repo) SetSnapshotVersion(v int) error {
func (r *Repo) SetSnapshotVersion(v int64) error {
s, err := r.snapshot()
if err != nil {
return err
Expand All @@ -246,7 +246,7 @@ func (r *Repo) SetSnapshotVersion(v int) error {
return r.setTopLevelMeta("snapshot.json", s)
}

func (r *Repo) SnapshotVersion() (int, error) {
func (r *Repo) SnapshotVersion() (int64, error) {
s, err := r.snapshot()
if err != nil {
return -1, err
Expand Down Expand Up @@ -903,7 +903,7 @@ func (r *Repo) TimestampWithExpires(expires time.Time) error {
return err
}

func (r *Repo) fileVersions() (map[string]int, error) {
func (r *Repo) fileVersions() (map[string]int64, error) {
root, err := r.root()
if err != nil {
return nil, err
Expand All @@ -916,7 +916,7 @@ func (r *Repo) fileVersions() (map[string]int, error) {
if err != nil {
return nil, err
}
versions := make(map[string]int)
versions := make(map[string]int64)
versions["root.json"] = root.Version
versions["targets.json"] = targets.Version
versions["snapshot.json"] = snapshot.Version
Expand Down
Loading

0 comments on commit 507e038

Please sign in to comment.