Changed acl from using SignalType to Action, and added Permission() method to Signal interface that returns the Action the signal is trying to take

gql_cataclysm v0.2.5
noah metz 2023-07-28 00:39:27 -06:00
parent a16cf6bb38
commit 27687add1b
5 changed files with 18 additions and 12 deletions

@ -84,7 +84,7 @@ func (t * GraphTester) CheckForNone(listener *ListenerExt, str string) {
const SimpleListenerNodeType = NodeType("SIMPLE_LISTENER") const SimpleListenerNodeType = NodeType("SIMPLE_LISTENER")
func NewSimpleListener(ctx *Context, buffer int) (*Node, *ListenerExt) { func NewSimpleListener(ctx *Context, buffer int) (*Node, *ListenerExt) {
policy := NewAllNodesPolicy([]SignalType{SignalType("status")}) policy := NewAllNodesPolicy([]Action{Action("status")})
listener_extension := NewListenerExt(buffer) listener_extension := NewListenerExt(buffer)
listener := NewNode(ctx, listener := NewNode(ctx,
RandID(), RandID(),

@ -16,8 +16,8 @@ func lockableTestContext(t *testing.T, logs []string) *Context {
} }
var link_policy = NewAllNodesPolicy([]SignalType{LinkSignalType, StatusSignalType}) var link_policy = NewAllNodesPolicy([]Action{Action(LinkSignalType), Action(StatusSignalType)})
var lock_policy = NewAllNodesPolicy([]SignalType{LockSignalType}) var lock_policy = NewAllNodesPolicy([]Action{Action(LockSignalType)})
func TestLink(t *testing.T) { func TestLink(t *testing.T) {
ctx := lockableTestContext(t, []string{"lockable"}) ctx := lockableTestContext(t, []string{"lockable"})

@ -155,7 +155,7 @@ func nodeLoop(ctx *Context, node *Node) error {
case msg := <- node.MsgChan: case msg := <- node.MsgChan:
signal = msg.Signal signal = msg.Signal
source = msg.Source source = msg.Source
err := Allowed(ctx, msg.Source, signal.Type(), node) err := Allowed(ctx, msg.Source, signal.Permission(), node)
if err != nil { if err != nil {
ctx.Log.Logf("signal", "SIGNAL_POLICY_ERR: %s", err) ctx.Log.Logf("signal", "SIGNAL_POLICY_ERR: %s", err)
continue continue
@ -309,7 +309,7 @@ func NewNode(ctx *Context, id NodeID, node_type NodeType, queued_signals []Queue
return node return node
} }
func Allowed(ctx *Context, principal_id NodeID, action SignalType, node *Node) error { func Allowed(ctx *Context, principal_id NodeID, action Action, node *Node) error {
ctx.Log.Logf("policy", "POLICY_CHECK: %s %s.%s", principal_id, node.ID, action) ctx.Log.Logf("policy", "POLICY_CHECK: %s %s.%s", principal_id, node.ID, action)
// Nodes are allowed to perform all actions on themselves regardless of whether or not they have an ACL extension // Nodes are allowed to perform all actions on themselves regardless of whether or not they have an ACL extension
if principal_id == node.ID { if principal_id == node.ID {

@ -17,17 +17,17 @@ const (
type Policy interface { type Policy interface {
Serializable[PolicyType] Serializable[PolicyType]
Allows(principal_id NodeID, action SignalType, node *Node) error Allows(principal_id NodeID, action Action, node *Node) error
// Merge with another policy of the same underlying type // Merge with another policy of the same underlying type
Merge(Policy) Policy Merge(Policy) Policy
} }
//TODO: Update with change from principal *Node to principal_id so sane policies can still be made //TODO: Update with change from principal *Node to principal_id so sane policies can still be made
func (policy *AllNodesPolicy) Allows(principal_id NodeID, action SignalType, node *Node) error { func (policy *AllNodesPolicy) Allows(principal_id NodeID, action Action, node *Node) error {
return policy.Actions.Allows(action) return policy.Actions.Allows(action)
} }
func (policy *PerNodePolicy) Allows(principal_id NodeID, action SignalType, node *Node) error { func (policy *PerNodePolicy) Allows(principal_id NodeID, action Action, node *Node) error {
for id, actions := range(policy.NodeActions) { for id, actions := range(policy.NodeActions) {
if id != principal_id { if id != principal_id {
continue continue
@ -41,7 +41,7 @@ func (policy *PerNodePolicy) Allows(principal_id NodeID, action SignalType, node
return fmt.Errorf("%s is not in per node policy of %s", principal_id, node.ID) return fmt.Errorf("%s is not in per node policy of %s", principal_id, node.ID)
} }
func (policy *RequirementOfPolicy) Allows(principal_id NodeID, action SignalType, node *Node) error { func (policy *RequirementOfPolicy) Allows(principal_id NodeID, action Action, node *Node) error {
lockable_ext, err := GetExt[*LockableExt](node) lockable_ext, err := GetExt[*LockableExt](node)
if err != nil { if err != nil {
return err return err
@ -114,9 +114,10 @@ func (policy *RequirementOfPolicy) Merge(p Policy) Policy {
return policy return policy
} }
type Actions []SignalType type Action string
type Actions []Action
func (actions Actions) Allows(action SignalType) error { func (actions Actions) Allows(action Action) error {
for _, a := range(actions) { for _, a := range(actions) {
if a == action { if a == action {
return nil return nil
@ -320,7 +321,7 @@ func (ext *ACLExt) Type() ExtType {
} }
// Check if the extension allows the principal to perform action on node // Check if the extension allows the principal to perform action on node
func (ext *ACLExt) Allows(ctx *Context, principal_id NodeID, action SignalType, node *Node) error { func (ext *ACLExt) Allows(ctx *Context, principal_id NodeID, action Action, node *Node) error {
ctx.Log.Logf("policy", "POLICY_EXT_ALLOWED: %+v", ext) ctx.Log.Logf("policy", "POLICY_EXT_ALLOWED: %+v", ext)
errs := []error{} errs := []error{}
for _, policy := range(ext.Policies) { for _, policy := range(ext.Policies) {

@ -16,6 +16,7 @@ type SignalType string
type Signal interface { type Signal interface {
Serializable[SignalType] Serializable[SignalType]
Direction() SignalDirection Direction() SignalDirection
Permission() Action
} }
type BaseSignal struct { type BaseSignal struct {
@ -27,6 +28,10 @@ func (signal BaseSignal) Type() SignalType {
return signal.SignalType return signal.SignalType
} }
func (signal BaseSignal) Permission() Action {
return Action(signal.Type())
}
func (signal BaseSignal) Direction() SignalDirection { func (signal BaseSignal) Direction() SignalDirection {
return signal.SignalDirection return signal.SignalDirection
} }