Skip to content

Commit

Permalink
Revert "Match spec for delegation traversal"
Browse files Browse the repository at this point in the history
This reverts commit 15fee6b.
  • Loading branch information
ethan-lowman-dd committed Jul 14, 2021
1 parent bf42347 commit cfbb024
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 15 additions & 11 deletions client/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,22 @@ type delegation struct {
child data.DelegatedRole
}

type delegationID struct {
parent string
child string
}

type delegationsIterator struct {
stack []delegation
file string
visitedRoles map[string]struct{}
stack []delegation
file string
visited map[delegationID]struct{}
}

func newDelegationsIterator(role data.DelegatedRole, parent string, file string) *delegationsIterator {
i := &delegationsIterator{
file: file,
stack: make([]delegation, 0, 1),
visitedRoles: make(map[string]struct{}),
file: file,
stack: make([]delegation, 0, 1),
visited: make(map[delegationID]struct{}),
}

i.add([]data.DelegatedRole{role}, parent)
Expand All @@ -174,13 +179,12 @@ func (d *delegationsIterator) next() (delegation, bool) {
delegation := d.stack[len(d.stack)-1]
d.stack = d.stack[:len(d.stack)-1]

// 5.6.7.1: If this role has been visited before, then skip this role (so
// that cycles in the delegation graph are avoided).
roleName := delegation.child.Name
if _, ok := d.visitedRoles[roleName]; ok {
// 5.6.7.1 cycles protection
id := delegationID{delegation.parent, delegation.child.Name}
if _, ok := d.visited[id]; ok {
return d.next()
}
d.visitedRoles[roleName] = struct{}{}
d.visited[id] = struct{}{}

// 5.6.7.2 trim delegations to visit, only the current role and its delegations
// will be considered
Expand Down
6 changes: 3 additions & 3 deletions client/delegations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestDelegationsIterator(t *testing.T) {
},
rootDelegation: data.DelegatedRole{Name: "a", Paths: defaultPathPatterns},
file: "",
resultOrder: []string{"a", "b", "d", "e"},
resultOrder: []string{"a", "b", "a", "e", "d"},
},
{
testName: "cycle avoided 2",
Expand All @@ -122,7 +122,7 @@ func TestDelegationsIterator(t *testing.T) {
},
rootDelegation: data.DelegatedRole{Name: "a", Paths: defaultPathPatterns},
file: "",
resultOrder: []string{"a", "b", "c"},
resultOrder: []string{"a", "a", "b", "a", "b", "c", "c"},
},
{
testName: "diamond delegation",
Expand All @@ -140,7 +140,7 @@ func TestDelegationsIterator(t *testing.T) {
},
rootDelegation: data.DelegatedRole{Name: "a", Paths: defaultPathPatterns},
file: "",
resultOrder: []string{"a", "b", "d", "c"},
resultOrder: []string{"a", "b", "d", "c", "d"},
},
}

Expand Down

0 comments on commit cfbb024

Please sign in to comment.