Skip to content

Commit

Permalink
[Delegations prereq] Use a verify.DB for delegation in client
Browse files Browse the repository at this point in the history
Splitting up #175
  • Loading branch information
ethan-lowman-dd committed Mar 7, 2022
1 parent eaefa2e commit e5e6333
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
6 changes: 1 addition & 5 deletions client/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ func (c *Client) getTargetFileMeta(target string) (data.TargetFileMeta, error) {
// - filter delegations with paths or path_hash_prefixes matching searched target
// - 5.6.7.1 cycles protection
// - 5.6.7.2 terminations
delegations, err := targets.NewDelegationsIterator(target, c.db)
if err != nil {
return data.TargetFileMeta{}, err
}

delegations := targets.NewDelegationsIterator(target, c.db)
for i := 0; i < c.MaxDelegations; i++ {
d, ok := delegations.Next()
if !ok {
Expand Down
14 changes: 7 additions & 7 deletions pkg/targets/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ var ErrTopLevelTargetsRoleMissing = errors.New("tuf: top level targets role miss

// NewDelegationsIterator initialises an iterator with a first step
// on top level targets.
func NewDelegationsIterator(target string, topLevelKeysDB *verify.DB) (*delegationsIterator, error) {
targetsRole := topLevelKeysDB.GetRole("targets")
if targetsRole == nil {
return nil, ErrTopLevelTargetsRoleMissing
func NewDelegationsIterator(target string, topLevelKeysDB *verify.DB) *delegationsIterator {
role := topLevelKeysDB.GetRole("targets")
keyIDs := []string{}
if role != nil {
keyIDs = sets.StringSetToSlice(role.KeyIDs)
}

i := &delegationsIterator{
target: target,
stack: []Delegation{
{
Delegatee: data.DelegatedRole{
Name: "targets",
KeyIDs: sets.StringSetToSlice(targetsRole.KeyIDs),
Threshold: targetsRole.Threshold,
Name: "targets",
KeyIDs: keyIDs,
},
DB: topLevelKeysDB,
},
Expand Down
28 changes: 8 additions & 20 deletions pkg/targets/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@ var (
)

func TestDelegationsIterator(t *testing.T) {
topTargetsPubKey := &data.PublicKey{
Type: data.KeyTypeEd25519,
Scheme: data.KeySchemeEd25519,
Algorithms: data.HashAlgorithms,
Value: []byte(`{"public":"aaaaec567e5901ba3976c34f7cd5169704292439bf71e6aa19c64b96706f95ef"}`),
}
delTargetsPubKey := &data.PublicKey{
Type: data.KeyTypeEd25519,
Scheme: data.KeySchemeEd25519,
Algorithms: data.HashAlgorithms,
Value: []byte(`{"public":"bbbbec567e5901ba3976c34f7cd5169704292439bf71e6aa19c64b96706f95ef"}`),
}

defaultKeyIDs := delTargetsPubKey.IDs()
defaultKeyIDs := []string{"26b878ad73362774b8b69dd4fdeb2cc6a2688e4133ed5ace9e18a06e9d998a6d"}
var iteratorTests = []struct {
testName string
roles map[string][]data.DelegatedRole
Expand Down Expand Up @@ -201,15 +188,16 @@ func TestDelegationsIterator(t *testing.T) {

for _, tt := range iteratorTests {
t.Run(tt.testName, func(t *testing.T) {
topLevelDB := verify.NewDB()
topLevelDB.AddKey(topTargetsPubKey.IDs()[0], topTargetsPubKey)
topLevelDB.AddRole("targets", &data.Role{
KeyIDs: topTargetsPubKey.IDs(),
Threshold: 1,
flattened := []data.DelegatedRole{}
for _, roles := range tt.roles {
flattened = append(flattened, roles...)
}
db, err := verify.NewDBFromDelegations(&data.Delegations{
Roles: flattened,
})

d, err := NewDelegationsIterator(tt.file, topLevelDB)
assert.NoError(t, err)
d := NewDelegationsIterator(tt.file, db)

var iterationOrder []string
for {
Expand Down

0 comments on commit e5e6333

Please sign in to comment.