2023-07-20 22:08:28 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-10-11 18:11:24 -06:00
|
|
|
"slices"
|
2023-07-20 22:08:28 -06:00
|
|
|
)
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
type AddSubGroupSignal struct {
|
|
|
|
SignalHeader
|
|
|
|
Name string `gv:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAddSubGroupSignal(name string) *AddSubGroupSignal {
|
|
|
|
return &AddSubGroupSignal{
|
|
|
|
NewSignalHeader(Direct),
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal AddSubGroupSignal) Permission() Tree {
|
|
|
|
return Tree{
|
|
|
|
SerializedType(AddSubGroupSignalType): {
|
|
|
|
Hash("name", signal.Name): nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoveSubGroupSignal struct {
|
|
|
|
SignalHeader
|
|
|
|
Name string `gv:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveSubGroupSignal(name string) *RemoveSubGroupSignal {
|
|
|
|
return &RemoveSubGroupSignal{
|
|
|
|
NewSignalHeader(Direct),
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal RemoveSubGroupSignal) Permission() Tree {
|
|
|
|
return Tree{
|
|
|
|
SerializedType(RemoveSubGroupSignalType): {
|
|
|
|
Hash("command", signal.Name): nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:24 -06:00
|
|
|
type AddMemberSignal struct {
|
|
|
|
SignalHeader
|
2023-10-15 15:14:33 -06:00
|
|
|
SubGroup string `gv:"sub_group"`
|
2023-10-11 18:11:24 -06:00
|
|
|
MemberID NodeID `gv:"member_id"`
|
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
type SubGroupGQL struct {
|
|
|
|
Name string
|
|
|
|
Members []NodeID
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:11:24 -06:00
|
|
|
func (signal AddMemberSignal) Permission() Tree {
|
|
|
|
return Tree{
|
2023-10-15 15:14:33 -06:00
|
|
|
SerializedType(AddMemberSignalType): {
|
|
|
|
Hash("sub_group", signal.SubGroup): nil,
|
|
|
|
},
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
func NewAddMemberSignal(sub_group string, member_id NodeID) *AddMemberSignal {
|
2023-10-11 18:11:24 -06:00
|
|
|
return &AddMemberSignal{
|
|
|
|
NewSignalHeader(Direct),
|
2023-10-15 15:14:33 -06:00
|
|
|
sub_group,
|
2023-10-11 18:11:24 -06:00
|
|
|
member_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RemoveMemberSignal struct {
|
|
|
|
SignalHeader
|
2023-10-15 15:14:33 -06:00
|
|
|
SubGroup string `gv:"sub_group"`
|
2023-10-11 18:11:24 -06:00
|
|
|
MemberID NodeID `gv:"member_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (signal RemoveMemberSignal) Permission() Tree {
|
|
|
|
return Tree{
|
2023-10-15 15:14:33 -06:00
|
|
|
SerializedType(RemoveMemberSignalType): {
|
|
|
|
Hash("sub_group", signal.SubGroup): nil,
|
|
|
|
},
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
func NewRemoveMemberSignal(sub_group string, member_id NodeID) *RemoveMemberSignal {
|
2023-10-11 18:11:24 -06:00
|
|
|
return &RemoveMemberSignal{
|
|
|
|
NewSignalHeader(Direct),
|
2023-10-15 15:14:33 -06:00
|
|
|
sub_group,
|
2023-10-11 18:11:24 -06:00
|
|
|
member_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-13 00:32:24 -06:00
|
|
|
var DefaultGroupPolicy = NewAllNodesPolicy(Tree{
|
2023-10-03 21:18:06 -06:00
|
|
|
SerializedType(ReadSignalType): {
|
|
|
|
SerializedType(GroupExtType): {
|
2023-10-15 15:14:33 -06:00
|
|
|
Hash(FieldNameBase, "sub_groups"): nil,
|
2023-10-03 21:18:06 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2023-07-25 09:51:55 -06:00
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
type SubGroup struct {
|
|
|
|
Members []NodeID
|
|
|
|
Permissions Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemberOfPolicy struct {
|
|
|
|
PolicyHeader
|
|
|
|
Groups map[NodeID]map[string]Tree
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemberOfPolicy(groups map[NodeID]map[string]Tree) MemberOfPolicy {
|
|
|
|
return MemberOfPolicy{
|
|
|
|
PolicyHeader: NewPolicyHeader(),
|
|
|
|
Groups: groups,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy MemberOfPolicy) ContinueAllows(ctx *Context, current PendingACL, signal Signal) RuleResult {
|
|
|
|
sig, ok := signal.(*ReadResultSignal)
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
ctx.Log.Logf("group", "member_of_read_result: %+v", sig.Extensions)
|
|
|
|
|
|
|
|
group_ext_data, ok := sig.Extensions[GroupExtType]
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
sub_groups_ser, ok := group_ext_data["sub_groups"]
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
_, sub_groups_if, _, err := DeserializeValue(ctx, sub_groups_ser)
|
|
|
|
if err != nil {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_sub_groups, ok := sub_groups_if.Interface().(map[string][]NodeID)
|
|
|
|
if ok == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
group, exists := policy.Groups[sig.NodeID]
|
|
|
|
if exists == false {
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
for sub_group_name, permissions := range(group) {
|
|
|
|
ext_sub_group, exists := ext_sub_groups[sub_group_name]
|
|
|
|
if exists == true {
|
|
|
|
for _, member_id := range(ext_sub_group) {
|
|
|
|
if member_id == current.Principal {
|
|
|
|
if permissions.Allows(current.Action) == Allow {
|
|
|
|
return Allow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Deny
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a read signal to Group to check if principal_id is a member of it
|
|
|
|
func (policy MemberOfPolicy) Allows(ctx *Context, principal_id NodeID, action Tree, node *Node) (Messages, RuleResult) {
|
|
|
|
var messages Messages = nil
|
|
|
|
for group_id, sub_groups := range(policy.Groups) {
|
|
|
|
if group_id == node.ID {
|
|
|
|
ext, err := GetExt[*GroupExt](node, GroupExtType)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("group", "MemberOfPolicy with self ID error: %s", err)
|
|
|
|
} else {
|
2023-10-15 16:29:16 -06:00
|
|
|
for sub_group_name, permission := range(sub_groups) {
|
2023-10-15 15:14:33 -06:00
|
|
|
ext_sub_group, exists := ext.SubGroups[sub_group_name]
|
|
|
|
if exists == true {
|
|
|
|
for _, member := range(ext_sub_group) {
|
|
|
|
if member == principal_id {
|
2023-10-15 16:29:16 -06:00
|
|
|
if permission.Allows(action) == Allow {
|
2023-10-15 15:14:33 -06:00
|
|
|
return nil, Allow
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Send the read request to the group so that ContinueAllows can parse the response to check membership
|
|
|
|
messages = messages.Add(ctx, group_id, node, nil, NewReadSignal(map[ExtType][]string{
|
|
|
|
GroupExtType: {"sub_groups"},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(messages) > 0 {
|
|
|
|
return messages, Pending
|
|
|
|
} else {
|
|
|
|
return nil, Deny
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-03 21:18:06 -06:00
|
|
|
type GroupExt struct {
|
2023-10-15 15:14:33 -06:00
|
|
|
SubGroups map[string][]NodeID `gv:"sub_groups"`
|
2023-07-25 09:51:55 -06:00
|
|
|
}
|
|
|
|
|
2023-10-15 15:14:33 -06:00
|
|
|
func NewGroupExt(sub_groups map[string][]NodeID) *GroupExt {
|
|
|
|
if sub_groups == nil {
|
|
|
|
sub_groups = map[string][]NodeID{}
|
|
|
|
}
|
2023-07-26 11:56:10 -06:00
|
|
|
return &GroupExt{
|
2023-10-15 15:14:33 -06:00
|
|
|
SubGroups: sub_groups,
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-07 23:00:07 -06:00
|
|
|
func (ext *GroupExt) Process(ctx *Context, node *Node, source NodeID, signal Signal) (Messages, Changes) {
|
2023-10-11 18:11:24 -06:00
|
|
|
var messages Messages = nil
|
|
|
|
var changes Changes = nil
|
|
|
|
|
|
|
|
switch sig := signal.(type) {
|
|
|
|
case *AddMemberSignal:
|
2023-10-15 15:14:33 -06:00
|
|
|
sub_group, exists := ext.SubGroups[sig.SubGroup]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_subgroup"))
|
2023-10-11 18:11:24 -06:00
|
|
|
} else {
|
2023-10-15 15:14:33 -06:00
|
|
|
if slices.Contains(sub_group, sig.MemberID) == true {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "already_member"))
|
|
|
|
} else {
|
|
|
|
sub_group = append(sub_group, sig.MemberID)
|
|
|
|
ext.SubGroups[sig.SubGroup] = sub_group
|
|
|
|
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
|
|
|
|
changes = changes.Add("member_added")
|
|
|
|
}
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
2023-10-15 15:14:33 -06:00
|
|
|
|
2023-10-11 18:11:24 -06:00
|
|
|
case *RemoveMemberSignal:
|
2023-10-15 15:14:33 -06:00
|
|
|
sub_group, exists := ext.SubGroups[sig.SubGroup]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_subgroup"))
|
2023-10-11 18:11:24 -06:00
|
|
|
} else {
|
2023-10-15 15:14:33 -06:00
|
|
|
idx := slices.Index(sub_group, sig.MemberID)
|
|
|
|
if idx == -1 {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_member"))
|
|
|
|
} else {
|
|
|
|
sub_group = slices.Delete(sub_group, idx, idx+1)
|
|
|
|
ext.SubGroups[sig.SubGroup] = sub_group
|
|
|
|
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
|
|
|
|
changes = changes.Add("member_removed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *AddSubGroupSignal:
|
|
|
|
_, exists := ext.SubGroups[sig.Name]
|
|
|
|
if exists == true {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "already_subgroup"))
|
|
|
|
} else {
|
|
|
|
ext.SubGroups[sig.Name] = []NodeID{}
|
|
|
|
|
|
|
|
changes = changes.Add("subgroup_added")
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
|
|
|
|
}
|
|
|
|
case *RemoveSubGroupSignal:
|
|
|
|
_, exists := ext.SubGroups[sig.Name]
|
|
|
|
if exists == false {
|
|
|
|
messages = messages.Add(ctx, source, node, nil, NewErrorSignal(sig.Id, "not_subgroup"))
|
|
|
|
} else {
|
|
|
|
delete(ext.SubGroups, sig.Name)
|
|
|
|
|
|
|
|
changes = changes.Add("subgroup_removed")
|
2023-10-14 15:05:23 -06:00
|
|
|
messages = messages.Add(ctx, source, node, nil, NewSuccessSignal(sig.Id))
|
2023-10-11 18:11:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messages, changes
|
2023-07-26 00:18:11 -06:00
|
|
|
}
|
2023-07-28 00:04:18 -06:00
|
|
|
|