2023-07-20 23:19:10 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-07-25 00:50:26 -06:00
|
|
|
"fmt"
|
2023-07-20 23:19:10 -06:00
|
|
|
)
|
|
|
|
|
2023-07-27 23:15:58 -06:00
|
|
|
const (
|
2023-08-07 20:26:02 -06:00
|
|
|
UserOfPolicyType = PolicyType("USER_OF")
|
2023-07-27 23:15:58 -06:00
|
|
|
RequirementOfPolicyType = PolicyType("REQUIREMENT_OF")
|
|
|
|
PerNodePolicyType = PolicyType("PER_NODE")
|
|
|
|
AllNodesPolicyType = PolicyType("ALL_NODES")
|
|
|
|
)
|
|
|
|
|
2023-07-20 23:19:10 -06:00
|
|
|
type Policy interface {
|
2023-07-26 15:08:14 -06:00
|
|
|
Serializable[PolicyType]
|
2023-07-28 00:39:27 -06:00
|
|
|
Allows(principal_id NodeID, action Action, node *Node) error
|
2023-07-28 00:32:43 -06:00
|
|
|
// Merge with another policy of the same underlying type
|
|
|
|
Merge(Policy) Policy
|
2023-08-01 20:55:15 -06:00
|
|
|
// Make a copy of this policy
|
|
|
|
Copy() Policy
|
2023-07-27 01:30:32 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
func (policy AllNodesPolicy) Allows(principal_id NodeID, action Action, node *Node) error {
|
2023-07-27 01:30:32 -06:00
|
|
|
return policy.Actions.Allows(action)
|
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
func (policy PerNodePolicy) Allows(principal_id NodeID, action Action, node *Node) error {
|
2023-07-27 01:30:32 -06:00
|
|
|
for id, actions := range(policy.NodeActions) {
|
|
|
|
if id != principal_id {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-28 10:04:31 -06:00
|
|
|
return actions.Allows(action)
|
2023-07-27 01:30:32 -06:00
|
|
|
}
|
2023-07-27 09:32:33 -06:00
|
|
|
return fmt.Errorf("%s is not in per node policy of %s", principal_id, node.ID)
|
2023-07-27 01:30:32 -06:00
|
|
|
}
|
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
func (policy *RequirementOfPolicy) Allows(principal_id NodeID, action Action, node *Node) error {
|
2023-07-27 09:32:33 -06:00
|
|
|
lockable_ext, err := GetExt[*LockableExt](node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-27 18:08:43 -06:00
|
|
|
for id, _ := range(lockable_ext.Requirements) {
|
2023-07-27 09:32:33 -06:00
|
|
|
if id == principal_id {
|
|
|
|
return policy.Actions.Allows(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("%s is not a requirement of %s", principal_id, node.ID)
|
2023-07-27 01:30:32 -06:00
|
|
|
}
|
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
type UserOfPolicy struct {
|
|
|
|
PerNodePolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy *UserOfPolicy) Type() PolicyType {
|
|
|
|
return UserOfPolicyType
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserOfPolicy(group_actions NodeActions) UserOfPolicy {
|
|
|
|
return UserOfPolicy{
|
|
|
|
PerNodePolicy: NewPerNodePolicy(group_actions),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a read signal to Group to check if principal_id is a member of it
|
|
|
|
func (policy *UserOfPolicy) Allows(principal_id NodeID, action Action, node *Node) error {
|
|
|
|
// Send a read signal to each of the groups in the map
|
|
|
|
// Check for principal_id in any of the returned member lists(skipping errors)
|
|
|
|
// Return an error in the default case
|
|
|
|
return fmt.Errorf("NOT_IMPLEMENTED")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy *UserOfPolicy) Merge(p Policy) Policy {
|
|
|
|
other := p.(*UserOfPolicy)
|
|
|
|
policy.NodeActions = MergeNodeActions(policy.NodeActions, other.NodeActions)
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy *UserOfPolicy) Copy() Policy {
|
|
|
|
new_actions := CopyNodeActions(policy.NodeActions)
|
|
|
|
return &UserOfPolicy{
|
|
|
|
PerNodePolicy: NewPerNodePolicy(new_actions),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
type RequirementOfPolicy struct {
|
2023-07-27 09:32:33 -06:00
|
|
|
AllNodesPolicy
|
2023-07-26 15:08:14 -06:00
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
func (policy *RequirementOfPolicy) Type() PolicyType {
|
2023-07-26 15:08:14 -06:00
|
|
|
return RequirementOfPolicyType
|
|
|
|
}
|
|
|
|
|
2023-07-27 09:32:33 -06:00
|
|
|
func NewRequirementOfPolicy(actions Actions) RequirementOfPolicy {
|
2023-07-26 15:08:14 -06:00
|
|
|
return RequirementOfPolicy{
|
2023-07-27 09:32:33 -06:00
|
|
|
AllNodesPolicy: NewAllNodesPolicy(actions),
|
2023-07-26 15:08:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:32:43 -06:00
|
|
|
func MergeActions(first Actions, second Actions) Actions {
|
|
|
|
ret := second
|
|
|
|
for _, action := range(first) {
|
2023-07-28 10:04:31 -06:00
|
|
|
ret = append(ret, action)
|
2023-07-28 00:32:43 -06:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func CopyNodeActions(actions NodeActions) NodeActions {
|
|
|
|
ret := NodeActions{}
|
|
|
|
for id, a := range(actions) {
|
|
|
|
ret[id] = a
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-08-07 20:26:02 -06:00
|
|
|
func MergeNodeActions(first NodeActions, second NodeActions) NodeActions {
|
|
|
|
merged := NodeActions{}
|
|
|
|
for id, actions := range(first) {
|
|
|
|
merged[id] = actions
|
|
|
|
}
|
|
|
|
for id, actions := range(second) {
|
|
|
|
existing, exists := merged[id]
|
2023-07-28 00:32:43 -06:00
|
|
|
if exists {
|
2023-08-07 20:26:02 -06:00
|
|
|
merged[id] = MergeActions(existing, actions)
|
2023-07-28 00:32:43 -06:00
|
|
|
} else {
|
2023-08-07 20:26:02 -06:00
|
|
|
merged[id] = actions
|
2023-07-28 00:32:43 -06:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 20:26:02 -06:00
|
|
|
return merged
|
2023-07-28 00:32:43 -06:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *PerNodePolicy) Merge(p Policy) Policy {
|
|
|
|
other := p.(*PerNodePolicy)
|
2023-08-07 20:26:02 -06:00
|
|
|
policy.NodeActions = MergeNodeActions(policy.NodeActions, other.NodeActions)
|
2023-07-28 00:32:43 -06:00
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *PerNodePolicy) Copy() Policy {
|
|
|
|
new_actions := CopyNodeActions(policy.NodeActions)
|
|
|
|
return &PerNodePolicy{
|
|
|
|
NodeActions: new_actions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy *AllNodesPolicy) Merge(p Policy) Policy {
|
|
|
|
other := p.(*AllNodesPolicy)
|
2023-07-28 00:32:43 -06:00
|
|
|
policy.Actions = MergeActions(policy.Actions, other.Actions)
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *AllNodesPolicy) Copy() Policy {
|
|
|
|
new_actions := policy.Actions
|
|
|
|
return &AllNodesPolicy {
|
|
|
|
Actions: new_actions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (policy *RequirementOfPolicy) Merge(p Policy) Policy {
|
|
|
|
other := p.(*RequirementOfPolicy)
|
2023-07-28 00:32:43 -06:00
|
|
|
policy.Actions = MergeActions(policy.Actions, other.Actions)
|
|
|
|
return policy
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *RequirementOfPolicy) Copy() Policy {
|
|
|
|
new_actions := policy.Actions
|
|
|
|
return &RequirementOfPolicy{
|
|
|
|
AllNodesPolicy {
|
|
|
|
Actions: new_actions,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 10:04:31 -06:00
|
|
|
type Action []string
|
|
|
|
|
|
|
|
func MakeAction(parts ...interface{}) Action {
|
|
|
|
action := make(Action, len(parts))
|
|
|
|
for i, part := range(parts) {
|
|
|
|
stringer, ok := part.(fmt.Stringer)
|
|
|
|
if ok == false {
|
|
|
|
switch p := part.(type) {
|
|
|
|
case string:
|
|
|
|
action[i] = p
|
|
|
|
default:
|
|
|
|
panic("%s can not be part of an action")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
action[i] = stringer.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
func (action Action) Allows(test Action) bool {
|
2023-07-28 11:59:01 -06:00
|
|
|
action_len := len(action)
|
2023-07-28 10:04:31 -06:00
|
|
|
for i, part := range(test) {
|
2023-07-28 11:59:01 -06:00
|
|
|
if i >= action_len {
|
|
|
|
return false
|
|
|
|
} else if action[i] == part || action[i] == "*" {
|
2023-07-28 10:04:31 -06:00
|
|
|
continue
|
|
|
|
} else if action[i] == "+" {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:39:27 -06:00
|
|
|
type Actions []Action
|
2023-07-26 15:40:33 -06:00
|
|
|
|
2023-07-28 00:39:27 -06:00
|
|
|
func (actions Actions) Allows(action Action) error {
|
2023-07-26 15:40:33 -06:00
|
|
|
for _, a := range(actions) {
|
2023-07-28 10:04:31 -06:00
|
|
|
if a.Allows(action) == true {
|
2023-07-27 09:32:33 -06:00
|
|
|
return nil
|
2023-07-26 15:40:33 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 09:32:33 -06:00
|
|
|
return fmt.Errorf("%s not in allows list", action)
|
2023-07-26 15:40:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type NodeActions map[NodeID]Actions
|
2023-07-26 13:28:03 -06:00
|
|
|
|
2023-07-28 11:59:01 -06:00
|
|
|
func (actions NodeActions) MarshalJSON() ([]byte, error) {
|
|
|
|
tmp := map[string]Actions{}
|
|
|
|
for id, a := range(actions) {
|
|
|
|
tmp[id.String()] = a
|
|
|
|
}
|
|
|
|
return json.Marshal(tmp)
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:44:52 -06:00
|
|
|
func (actions *NodeActions) UnmarshalJSON(data []byte) error {
|
2023-07-28 11:59:01 -06:00
|
|
|
tmp := map[string]Actions{}
|
|
|
|
err := json.Unmarshal(data, &tmp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for id_str, a := range(tmp) {
|
|
|
|
id, err := ParseID(id_str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-31 19:44:52 -06:00
|
|
|
ac := *actions
|
|
|
|
ac[id] = a
|
2023-07-28 11:59:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
func NewPerNodePolicy(node_actions NodeActions) PerNodePolicy {
|
2023-07-26 13:28:03 -06:00
|
|
|
if node_actions == nil {
|
2023-07-26 15:40:33 -06:00
|
|
|
node_actions = NodeActions{}
|
2023-07-26 13:28:03 -06:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
return PerNodePolicy{
|
2023-07-26 13:28:03 -06:00
|
|
|
NodeActions: node_actions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PerNodePolicy struct {
|
2023-07-27 18:08:43 -06:00
|
|
|
NodeActions NodeActions `json:"node_actions"`
|
2023-07-26 13:28:03 -06:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *PerNodePolicy) Type() PolicyType {
|
2023-07-26 13:28:03 -06:00
|
|
|
return PerNodePolicyType
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *PerNodePolicy) Serialize() ([]byte, error) {
|
2023-07-27 18:08:43 -06:00
|
|
|
return json.MarshalIndent(policy, "", " ")
|
2023-07-26 13:28:03 -06:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *PerNodePolicy) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
return json.Unmarshal(data, policy)
|
|
|
|
}
|
|
|
|
|
2023-07-27 00:30:24 -06:00
|
|
|
func NewAllNodesPolicy(actions Actions) AllNodesPolicy {
|
|
|
|
if actions == nil {
|
|
|
|
actions = Actions{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AllNodesPolicy{
|
|
|
|
Actions: actions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AllNodesPolicy struct {
|
2023-07-27 09:32:33 -06:00
|
|
|
Actions Actions
|
2023-07-27 00:30:24 -06:00
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *AllNodesPolicy) Type() PolicyType {
|
2023-07-27 00:30:24 -06:00
|
|
|
return AllNodesPolicyType
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *AllNodesPolicy) Serialize() ([]byte, error) {
|
2023-07-27 00:30:24 -06:00
|
|
|
return json.MarshalIndent(policy, "", " ")
|
|
|
|
}
|
|
|
|
|
2023-08-01 20:55:15 -06:00
|
|
|
func (policy *AllNodesPolicy) Deserialize(ctx *Context, data []byte) error {
|
|
|
|
return json.Unmarshal(data, policy)
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:16:23 -06:00
|
|
|
var ErrorSignalAction = Action{"ERROR_RESP"}
|
2023-07-31 19:22:33 -06:00
|
|
|
var ReadResultSignalAction = Action{"READ_RESULT"}
|
2023-08-01 14:09:29 -06:00
|
|
|
var AuthorizedSignalAction = Action{"AUTHORIZED_READ"}
|
2023-08-01 20:55:15 -06:00
|
|
|
var defaultPolicy = NewAllNodesPolicy(Actions{ErrorSignalAction, ReadResultSignalAction, AuthorizedSignalAction})
|
2023-07-31 19:16:23 -06:00
|
|
|
var DefaultACLPolicies = []Policy{
|
2023-08-01 20:55:15 -06:00
|
|
|
&defaultPolicy,
|
2023-07-31 19:16:23 -06:00
|
|
|
}
|