Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

Commit

Permalink
fix: aggregate permissions (#142)
Browse files Browse the repository at this point in the history
* fix: aggregate permissions

* examples

* Update chaincode/tuple_aggregate.go

Co-authored-by: Matthieu Blottière <[email protected]>

* implement as union

* remove print

* make linter happy

Co-authored-by: Matthieu Blottière <[email protected]>
  • Loading branch information
AurelienGasser and mblottiere authored Jun 10, 2021
1 parent beacebd commit f93cff6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
33 changes: 33 additions & 0 deletions chaincode/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ func mergePermissions(x, y Permission) Permission {
return priv
}

// UnionPermissions returns the union between two sets of permissions
func UnionPermissions(x, y Permissions) Permissions {
perm := Permissions{}
perm.Process = unionPermissions(x.Process, y.Process)
perm.Download = unionPermissions(x.Download, y.Download)
return perm
}

func unionPermissions(x, y Permission) Permission {
res := Permission{
Public: x.Public || y.Public,
}
if !res.Public {
res.AuthorizedIDs = x.getNodesUnion(y)
}
return res
}

func (priv Permission) getNodesUnion(p Permission) []string {
authorizedIds := map[string]bool{}
for _, i := range priv.AuthorizedIDs {
authorizedIds[i] = true
}
for _, i := range p.AuthorizedIDs {
authorizedIds[i] = true
}
res := make([]string, 0, len(authorizedIds))
for k := range authorizedIds {
res = append(res, k)
}
return res
}

func (priv Permission) getNodesIntersection(p Permission) []string {
nodes := []string{}
for _, i := range priv.AuthorizedIDs {
Expand Down
5 changes: 2 additions & 3 deletions chaincode/tuple_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (tuple *Aggregatetuple) SetFromInput(db *LedgerDB, inp inputAggregatetuple)
func (tuple *Aggregatetuple) SetFromParents(db *LedgerDB, inModels []string) error {
var parentStatuses []string
inModelKeys := tuple.InModelKeys
permissions, err := NewPermissions(db, OpenPermissions)
permissions, err := NewPermissions(db, inputPermissions{})
if err != nil {
return errors.BadRequest(err, "could not generate open permissions")
}
Expand All @@ -82,7 +82,6 @@ func (tuple *Aggregatetuple) SetFromParents(db *LedgerDB, inModels []string) err
case CompositeTraintupleType:
tuple, err := db.GetCompositeTraintuple(parentTraintupleKey)
if err == nil {
// if the parent is composite, always take the "trunk" out-model
parentPermissions = tuple.OutTrunkModel.Permissions
parentStatuses = append(parentStatuses, tuple.Status)
}
Expand All @@ -107,7 +106,7 @@ func (tuple *Aggregatetuple) SetFromParents(db *LedgerDB, inModels []string) err
}

inModelKeys = append(inModelKeys, parentTraintupleKey)
permissions = MergePermissions(permissions, parentPermissions)
permissions = UnionPermissions(permissions, parentPermissions)
}
tuple.Status = determineStatusFromInModels(parentStatuses)
tuple.InModelKeys = inModelKeys
Expand Down
33 changes: 29 additions & 4 deletions chaincode/tuple_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ func TestTraintupleAggregate(t *testing.T) {
Status: StatusTodo,
Permissions: outputPermissions{
Process: Permission{
Public: true,
AuthorizedIDs: []string{},
Public: false,
AuthorizedIDs: []string{workerA},
},
},
Metadata: map[string]string{},
Expand Down Expand Up @@ -507,8 +507,33 @@ func TestAggregatetuplePermissions(t *testing.T) {
// verify permissions
assert.EqualValues(t, false, aggr.Permissions.Process.Public,
"the aggregate tuple should not be public")
assert.EqualValues(t, []string{workerA, "nodeC"}, aggr.Permissions.Process.AuthorizedIDs,
"the aggregate tuple permissions should be the intersect of the in-model permissions")
assert.True(t, sameStringSlice([]string{workerA, "nodeA", "nodeB", "nodeC", "nodeD"}, aggr.Permissions.Process.AuthorizedIDs),
"the aggregate tuple permissions should be union of the in-model permissions")
}

// return true if slices contain the same elements, regardless of order
// https://stackoverflow.com/a/36000696/1370722
func sameStringSlice(x, y []string) bool {
if len(x) != len(y) {
return false
}
// create a map of string -> int
diff := make(map[string]int, len(x))
for _, _x := range x {
// 0 value for int is 0, so just increment a counter for the string
diff[_x]++
}
for _, _y := range y {
// If the string _y is not in diff bail out early
if _, ok := diff[_y]; !ok {
return false
}
diff[_y]--
if diff[_y] == 0 {
delete(diff, _y)
}
}
return len(diff) == 0
}

func TestAggregatetupleLogSuccessFail(t *testing.T) {
Expand Down

0 comments on commit f93cff6

Please sign in to comment.