From 25a1e9aed76bf0e42063846f7d75913e56082a3d Mon Sep 17 00:00:00 2001 From: Riyaz Faizullabhoy Date: Wed, 20 Jan 2016 09:27:32 -0800 Subject: [PATCH] change to ListRoles, and GetAllLoadedRoles Signed-off-by: Riyaz Faizullabhoy --- client/client.go | 11 ++++------- client/client_test.go | 16 ++++++++-------- tuf/data/roles.go | 7 ------- tuf/tuf.go | 10 +++------- tuf/tuf_test.go | 9 ++++----- 5 files changed, 19 insertions(+), 34 deletions(-) diff --git a/client/client.go b/client/client.go index c8cd8a1982..df7b6608d5 100644 --- a/client/client.go +++ b/client/client.go @@ -577,20 +577,17 @@ type RoleWithSignatures struct { data.Role } -// GetRepoRoleMetaInfo returns a list of RoleWithSignatures objects for this repo +// ListRoles returns a list of RoleWithSignatures objects for this repo // This represents the latest metadata for each role in this repo -func (r *NotaryRepository) GetRepoRoleMetaInfo() ([]RoleWithSignatures, error) { +func (r *NotaryRepository) ListRoles() ([]RoleWithSignatures, error) { // Update to latest repo state _, err := r.Update(false) if err != nil { return nil, err } - // Get all role info from our updated keysDB - roles, err := r.tufRepo.GetAllRoles() - if err != nil { - return nil, err - } + // Get all role info from our updated keysDB, can be empty + roles := r.tufRepo.GetAllLoadedRoles() var roleWithSigs []RoleWithSignatures diff --git a/client/client_test.go b/client/client_test.go index 04a838e235..3e966a71da 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -2950,8 +2950,8 @@ func TestDeleteRepoNoCerts(t *testing.T) { assertRepoHasExpectedKeys(t, repo, rootKeyID, true) } -// Test that we get a correct map of key IDs -func TestGetRepoRoleMetaInfo(t *testing.T) { +// Test that we get a correct list of roles with keys and signatures +func TestListRoles(t *testing.T) { ts := fullTestServer(t) defer ts.Close() @@ -2960,7 +2960,7 @@ func TestGetRepoRoleMetaInfo(t *testing.T) { assert.NoError(t, repo.Publish()) - rolesWithSigs, err := repo.GetRepoRoleMetaInfo() + rolesWithSigs, err := repo.ListRoles() assert.NoError(t, err) // Should only have base roles at this point @@ -2980,7 +2980,7 @@ func TestGetRepoRoleMetaInfo(t *testing.T) { assert.NoError(t, repo.Publish()) - rolesWithSigs, err = repo.GetRepoRoleMetaInfo() + rolesWithSigs, err = repo.ListRoles() assert.NoError(t, err) assert.Len(t, rolesWithSigs, len(data.BaseRoles)+1) @@ -2998,7 +2998,7 @@ func TestGetRepoRoleMetaInfo(t *testing.T) { addTarget(t, repo, "current", "../fixtures/intermediate-ca.crt", "targets/a") assert.NoError(t, repo.Publish()) - rolesWithSigs, err = repo.GetRepoRoleMetaInfo() + rolesWithSigs, err = repo.ListRoles() assert.NoError(t, err) assert.Len(t, rolesWithSigs, len(data.BaseRoles)+1) @@ -3017,7 +3017,7 @@ func TestGetRepoRoleMetaInfo(t *testing.T) { assert.NoError(t, repo.Publish()) - rolesWithSigs, err = repo.GetRepoRoleMetaInfo() + rolesWithSigs, err = repo.ListRoles() assert.NoError(t, err) assert.Len(t, rolesWithSigs, len(data.BaseRoles)+2) @@ -3039,12 +3039,12 @@ func TestGetRepoRoleMetaInfo(t *testing.T) { assert.NoError(t, repo2.Publish()) // repo2 only has the base roles - rolesWithSigs2, err := repo2.GetRepoRoleMetaInfo() + rolesWithSigs2, err := repo2.ListRoles() assert.NoError(t, err) assert.Len(t, rolesWithSigs2, len(data.BaseRoles)) // original repo stays in same state (base roles + 2 delegations) - rolesWithSigs, err = repo.GetRepoRoleMetaInfo() + rolesWithSigs, err = repo.ListRoles() assert.NoError(t, err) assert.Len(t, rolesWithSigs, len(data.BaseRoles)+2) } diff --git a/tuf/data/roles.go b/tuf/data/roles.go index c323c25c61..a505c92304 100644 --- a/tuf/data/roles.go +++ b/tuf/data/roles.go @@ -37,13 +37,6 @@ func (e ErrNoSuchRole) Error() string { return fmt.Sprintf("role does not exist: %s", e.Role) } -// ErrNoRoles indicates no roles exist for this repo -type ErrNoRoles struct{} - -func (e ErrNoRoles) Error() string { - return fmt.Sprintf("no roles exist") -} - // ErrInvalidRole represents an error regarding a role. Typically // something like a role for which sone of the public keys were // not found in the TUF repo. diff --git a/tuf/tuf.go b/tuf/tuf.go index 4b5b686e02..96ab7da1d6 100644 --- a/tuf/tuf.go +++ b/tuf/tuf.go @@ -173,13 +173,9 @@ func (tr *Repo) RemoveBaseKeys(role string, keyIDs ...string) error { return nil } -// GetAllRoles returns a list of all role entries for this TUF repo -func (tr *Repo) GetAllRoles() ([]*data.Role, error) { - roles := tr.keysDB.GetAllRoles() - if len(roles) == 0 { - return nil, data.ErrNoRoles{} - } - return roles, nil +// GetAllLoadedRoles returns a list of all role entries loaded in this TUF repo, could be empty +func (tr *Repo) GetAllLoadedRoles() []*data.Role { + return tr.keysDB.GetAllRoles() } // GetDelegation finds the role entry representing the provided diff --git a/tuf/tuf_test.go b/tuf/tuf_test.go index d75a583bbd..9178d257da 100644 --- a/tuf/tuf_test.go +++ b/tuf/tuf_test.go @@ -943,12 +943,11 @@ func TestGetAllRoles(t *testing.T) { repo := initRepo(t, ed25519, keyDB) // After we init, we get the base roles - roles, err := repo.GetAllRoles() + roles := repo.GetAllLoadedRoles() assert.Len(t, roles, len(data.BaseRoles)) - // Clear the keysDB, check that we error + // Clear the keysDB, check that we get an empty list repo.keysDB = keys.NewDB() - roles, err = repo.GetAllRoles() - assert.Error(t, err) - assert.IsType(t, data.ErrNoRoles{}, err) + roles = repo.GetAllLoadedRoles() + assert.Len(t, roles, 0) }