2023-07-09 14:30:30 -06:00
|
|
|
package graphvent
|
|
|
|
|
|
|
|
import (
|
2023-07-27 15:27:14 -06:00
|
|
|
"time"
|
2023-07-27 16:48:39 -06:00
|
|
|
"errors"
|
2023-07-25 21:43:15 -06:00
|
|
|
"reflect"
|
2023-07-09 14:30:30 -06:00
|
|
|
"github.com/google/uuid"
|
|
|
|
badger "github.com/dgraph-io/badger/v3"
|
|
|
|
"fmt"
|
|
|
|
"encoding/binary"
|
2023-07-20 23:19:10 -06:00
|
|
|
"encoding/json"
|
2023-07-27 16:06:56 -06:00
|
|
|
"sync/atomic"
|
2023-07-28 15:07:38 -06:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/sha512"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
2023-07-09 14:30:30 -06:00
|
|
|
)
|
|
|
|
|
2023-07-28 13:12:17 -06:00
|
|
|
const (
|
|
|
|
// Magic first four bytes of serialized DB content, stored big endian
|
|
|
|
NODE_DB_MAGIC = 0x2491df14
|
|
|
|
// Total length of the node database header, has magic to verify and type_hash to map to load function
|
2023-07-28 15:07:38 -06:00
|
|
|
NODE_DB_HEADER_LEN = 28
|
2023-07-28 13:12:17 -06:00
|
|
|
EXTENSION_DB_HEADER_LEN = 16
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Base NodeID, used as a special value
|
|
|
|
ZeroUUID = uuid.UUID{}
|
|
|
|
ZeroID = NodeID(ZeroUUID)
|
|
|
|
)
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// A NodeID uniquely identifies a Node
|
2023-07-19 20:03:13 -06:00
|
|
|
type NodeID uuid.UUID
|
2023-07-30 10:09:04 -06:00
|
|
|
func (id *NodeID) MarshalJSON() ([]byte, error) {
|
|
|
|
str := ""
|
|
|
|
if id != nil {
|
|
|
|
str = id.String()
|
|
|
|
}
|
2023-07-24 17:07:27 -06:00
|
|
|
return json.Marshal(&str)
|
|
|
|
}
|
2023-07-28 15:07:38 -06:00
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
func (id *NodeID) UnmarshalJSON(bytes []byte) error {
|
|
|
|
var id_str string
|
|
|
|
err := json.Unmarshal(bytes, &id_str)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*id, err = ParseID(id_str)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
func (id NodeID) Serialize() []byte {
|
2023-07-19 20:03:13 -06:00
|
|
|
ser, _ := (uuid.UUID)(id).MarshalBinary()
|
|
|
|
return ser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id NodeID) String() string {
|
|
|
|
return (uuid.UUID)(id).String()
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Create an ID from a fixed length byte array
|
2023-07-25 21:43:15 -06:00
|
|
|
// Ignore the error since we're enforcing 16 byte length at compile time
|
|
|
|
func IDFromBytes(bytes [16]byte) NodeID {
|
|
|
|
id, _ := uuid.FromBytes(bytes[:])
|
|
|
|
return NodeID(id)
|
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Parse an ID from a string
|
2023-07-19 20:03:13 -06:00
|
|
|
func ParseID(str string) (NodeID, error) {
|
|
|
|
id_uuid, err := uuid.Parse(str)
|
|
|
|
if err != nil {
|
|
|
|
return NodeID{}, err
|
|
|
|
}
|
|
|
|
return NodeID(id_uuid), nil
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:31:43 -06:00
|
|
|
// Generate a random NodeID
|
2023-07-09 14:30:30 -06:00
|
|
|
func RandID() NodeID {
|
2023-07-19 20:03:13 -06:00
|
|
|
return NodeID(uuid.New())
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// A Serializable has a type that can be used to map to it, and a function to serialize the current state
|
2023-07-25 21:43:15 -06:00
|
|
|
type Serializable[I comparable] interface {
|
|
|
|
Type() I
|
2023-07-24 16:04:56 -06:00
|
|
|
Serialize() ([]byte, error)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Extensions are data attached to nodes that process signals
|
2023-07-25 21:43:15 -06:00
|
|
|
type Extension interface {
|
|
|
|
Serializable[ExtType]
|
2023-07-28 11:21:18 -06:00
|
|
|
Field(string)interface{}
|
2023-07-27 15:27:14 -06:00
|
|
|
Process(context *Context, source NodeID, node *Node, signal Signal)
|
2023-07-20 23:19:10 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// A QueuedSignal is a Signal that has been Queued to trigger at a set time
|
2023-07-27 15:27:14 -06:00
|
|
|
type QueuedSignal struct {
|
2023-07-30 01:29:15 -06:00
|
|
|
uuid.UUID
|
|
|
|
Signal
|
|
|
|
time.Time
|
2023-07-27 15:27:14 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Default message channel size for nodes
|
|
|
|
// Nodes represent a group of extensions that can be collectively addressed
|
2023-07-25 21:43:15 -06:00
|
|
|
type Node struct {
|
2023-07-28 15:07:38 -06:00
|
|
|
Key *ecdsa.PrivateKey
|
2023-07-25 21:43:15 -06:00
|
|
|
ID NodeID
|
2023-07-26 00:18:11 -06:00
|
|
|
Type NodeType
|
2023-07-26 00:42:12 -06:00
|
|
|
Extensions map[ExtType]Extension
|
2023-07-27 15:27:14 -06:00
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
// Channel for this node to receive messages from the Context
|
2023-07-27 15:27:14 -06:00
|
|
|
MsgChan chan Msg
|
2023-07-28 13:45:14 -06:00
|
|
|
// Size of MsgChan
|
|
|
|
BufferSize uint32
|
2023-07-27 16:06:56 -06:00
|
|
|
// Channel for this node to process delayed signals
|
2023-07-27 15:27:14 -06:00
|
|
|
TimeoutChan <-chan time.Time
|
|
|
|
|
2023-07-27 16:06:56 -06:00
|
|
|
Active atomic.Bool
|
2023-07-27 15:27:14 -06:00
|
|
|
|
|
|
|
SignalQueue []QueuedSignal
|
|
|
|
NextSignal *QueuedSignal
|
|
|
|
}
|
|
|
|
|
2023-07-30 01:29:15 -06:00
|
|
|
func (node *Node) QueueSignal(time time.Time, signal Signal) uuid.UUID {
|
|
|
|
id := uuid.New()
|
|
|
|
node.SignalQueue = append(node.SignalQueue, QueuedSignal{id, signal, time})
|
2023-07-27 15:27:14 -06:00
|
|
|
node.NextSignal, node.TimeoutChan = SoonestSignal(node.SignalQueue)
|
2023-07-30 01:29:15 -06:00
|
|
|
return id
|
2023-07-27 15:27:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) ClearSignalQueue() {
|
|
|
|
node.SignalQueue = []QueuedSignal{}
|
|
|
|
node.NextSignal = nil
|
|
|
|
node.TimeoutChan = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SoonestSignal(signals []QueuedSignal) (*QueuedSignal, <-chan time.Time) {
|
|
|
|
var soonest_signal *QueuedSignal
|
|
|
|
var soonest_time time.Time
|
|
|
|
for _, signal := range(signals) {
|
|
|
|
if signal.Time.Compare(soonest_time) == -1 || soonest_signal == nil {
|
|
|
|
soonest_signal = &signal
|
|
|
|
soonest_time = signal.Time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if soonest_signal != nil {
|
2023-07-30 11:12:47 -06:00
|
|
|
return soonest_signal, time.After(time.Until(soonest_signal.Time))
|
2023-07-27 15:27:14 -06:00
|
|
|
} else {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 23:15:58 -06:00
|
|
|
func runNode(ctx *Context, node *Node) {
|
2023-07-27 15:27:14 -06:00
|
|
|
ctx.Log.Logf("node", "RUN_START: %s", node.ID)
|
2023-07-27 23:15:58 -06:00
|
|
|
err := nodeLoop(ctx, node)
|
2023-07-27 15:27:14 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ctx.Log.Logf("node", "RUN_STOP: %s", node.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Msg struct {
|
|
|
|
Source NodeID
|
|
|
|
Signal Signal
|
|
|
|
}
|
|
|
|
|
2023-07-28 11:21:18 -06:00
|
|
|
func ReadNodeFields(ctx *Context, self *Node, princ NodeID, reqs map[ExtType][]string)map[ExtType]map[string]interface{} {
|
|
|
|
exts := map[ExtType]map[string]interface{}{}
|
|
|
|
for ext_type, field_reqs := range(reqs) {
|
|
|
|
fields := map[string]interface{}{}
|
|
|
|
for _, req := range(field_reqs) {
|
|
|
|
err := Allowed(ctx, princ, MakeAction(ReadSignalType, ext_type, req), self)
|
|
|
|
if err != nil {
|
|
|
|
fields[req] = err
|
|
|
|
} else {
|
|
|
|
ext, exists := self.Extensions[ext_type]
|
|
|
|
if exists == false {
|
|
|
|
fields[req] = fmt.Errorf("%s does not have %s extension", self.ID, ext_type)
|
|
|
|
} else {
|
|
|
|
fields[req] = ext.Field(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-28 11:59:01 -06:00
|
|
|
exts[ext_type] = fields
|
2023-07-28 11:21:18 -06:00
|
|
|
}
|
|
|
|
return exts
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
// Main Loop for Threads, starts a write context, so cannot be called from a write or read context
|
2023-07-27 23:15:58 -06:00
|
|
|
func nodeLoop(ctx *Context, node *Node) error {
|
2023-07-27 16:06:56 -06:00
|
|
|
started := node.Active.CompareAndSwap(false, true)
|
|
|
|
if started == false {
|
|
|
|
return fmt.Errorf("%s is already started, will not start again", node.ID)
|
|
|
|
}
|
2023-07-27 15:27:14 -06:00
|
|
|
for true {
|
|
|
|
var signal Signal
|
|
|
|
var source NodeID
|
|
|
|
select {
|
|
|
|
case msg := <- node.MsgChan:
|
|
|
|
signal = msg.Signal
|
|
|
|
source = msg.Source
|
2023-07-28 00:39:27 -06:00
|
|
|
err := Allowed(ctx, msg.Source, signal.Permission(), node)
|
2023-07-27 15:27:14 -06:00
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("signal", "SIGNAL_POLICY_ERR: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-node.TimeoutChan:
|
|
|
|
signal = node.NextSignal.Signal
|
2023-07-30 11:07:41 -06:00
|
|
|
t := node.NextSignal.Time
|
2023-07-27 15:27:14 -06:00
|
|
|
source = node.ID
|
2023-07-30 01:29:15 -06:00
|
|
|
i := -1
|
|
|
|
for j, queued := range(node.SignalQueue) {
|
|
|
|
if queued.UUID == node.NextSignal.UUID {
|
|
|
|
i = j
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if i == -1 {
|
|
|
|
panic("node.NextSignal not in node.SignalQueue")
|
|
|
|
}
|
|
|
|
l := len(node.SignalQueue)
|
|
|
|
node.SignalQueue[i] = node.SignalQueue[l-1]
|
|
|
|
node.SignalQueue = node.SignalQueue[:(l-1)]
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
node.NextSignal, node.TimeoutChan = SoonestSignal(node.SignalQueue)
|
2023-07-30 11:02:22 -06:00
|
|
|
if node.NextSignal == nil {
|
2023-07-30 11:07:41 -06:00
|
|
|
ctx.Log.Logf("node", "NODE_TIMEOUT(%s) - PROCESSING %+v@%s - NEXT_SIGNAL nil", node.ID, t, signal)
|
2023-07-30 11:02:22 -06:00
|
|
|
} else {
|
2023-07-30 11:07:41 -06:00
|
|
|
ctx.Log.Logf("node", "NODE_TIMEOUT(%s) - PROCESSING %+v@%s - NEXT_SIGNAL: %s@%s", node.ID, t, signal, node.NextSignal, node.NextSignal.Time)
|
2023-07-30 11:02:22 -06:00
|
|
|
}
|
2023-07-27 15:27:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle special signal types
|
|
|
|
if signal.Type() == StopSignalType {
|
|
|
|
node.Process(ctx, node.ID, NewStatusSignal("stopped", node.ID))
|
|
|
|
break
|
2023-07-28 11:21:18 -06:00
|
|
|
} else if signal.Type() == ReadSignalType {
|
|
|
|
read_signal, ok := signal.(ReadSignal)
|
|
|
|
if ok == false {
|
|
|
|
ctx.Log.Logf("signal", "READ_SIGNAL: bad cast %+v", signal)
|
|
|
|
} else {
|
2023-07-28 11:59:01 -06:00
|
|
|
result := ReadNodeFields(ctx, node, source, read_signal.Extensions)
|
2023-07-29 16:00:01 -06:00
|
|
|
ctx.Send(node.ID, source, NewReadResultSignal(read_signal.UUID, node.Type, result))
|
2023-07-28 11:21:18 -06:00
|
|
|
}
|
2023-07-27 15:27:14 -06:00
|
|
|
}
|
2023-07-28 11:21:18 -06:00
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
node.Process(ctx, source, signal)
|
|
|
|
}
|
2023-07-27 16:06:56 -06:00
|
|
|
|
|
|
|
stopped := node.Active.CompareAndSwap(true, false)
|
|
|
|
if stopped == false {
|
|
|
|
panic("BAD_STATE: stopping already stopped node")
|
|
|
|
}
|
2023-07-27 15:27:14 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) Process(ctx *Context, source NodeID, signal Signal) {
|
|
|
|
for ext_type, ext := range(node.Extensions) {
|
|
|
|
ctx.Log.Logf("signal", "PROCESSING_EXTENSION: %s/%s", node.ID, ext_type)
|
|
|
|
ext.Process(ctx, source, node, signal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
func GetCtx[T Extension, C any](ctx *Context) (C, error) {
|
|
|
|
var zero T
|
|
|
|
var zero_ctx C
|
|
|
|
ext_type := zero.Type()
|
2023-07-27 23:26:58 -06:00
|
|
|
type_hash := Hash(ext_type)
|
2023-07-27 16:06:56 -06:00
|
|
|
ext_info, ok := ctx.Extensions[type_hash]
|
|
|
|
if ok == false {
|
2023-07-26 11:56:10 -06:00
|
|
|
return zero_ctx, fmt.Errorf("%s is not an extension in ctx", ext_type)
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_ctx, ok := ext_info.Data.(C)
|
|
|
|
if ok == false {
|
|
|
|
return zero_ctx, fmt.Errorf("context for %s is %+v, not %+v", ext_type, reflect.TypeOf(ext_info.Data), reflect.TypeOf(zero))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ext_ctx, nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func GetExt[T Extension](node *Node) (T, error) {
|
|
|
|
var zero T
|
|
|
|
ext_type := zero.Type()
|
2023-07-26 00:42:12 -06:00
|
|
|
ext, exists := node.Extensions[ext_type]
|
2023-07-25 21:43:15 -06:00
|
|
|
if exists == false {
|
2023-07-26 11:56:10 -06:00
|
|
|
return zero, fmt.Errorf("%s does not have %s extension - %+v", node.ID, ext_type, node.Extensions)
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
2023-07-24 01:41:47 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
ret, ok := ext.(T)
|
|
|
|
if ok == false {
|
|
|
|
return zero, fmt.Errorf("%s in %s is wrong type(%+v), expecting %+v", ext_type, node.ID, reflect.TypeOf(ext), reflect.TypeOf(zero))
|
|
|
|
}
|
2023-07-20 23:19:10 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return ret, nil
|
2023-07-20 23:19:10 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (node *Node) Serialize() ([]byte, error) {
|
2023-07-26 00:42:12 -06:00
|
|
|
extensions := make([]ExtensionDB, len(node.Extensions))
|
2023-07-28 15:07:38 -06:00
|
|
|
|
|
|
|
key_bytes, err := x509.MarshalECPrivateKey(node.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
node_db := NodeDB{
|
|
|
|
Header: NodeDBHeader{
|
|
|
|
Magic: NODE_DB_MAGIC,
|
2023-07-27 23:26:58 -06:00
|
|
|
TypeHash: Hash(node.Type),
|
2023-07-28 15:07:38 -06:00
|
|
|
KeyLength: uint32(len(key_bytes)),
|
2023-07-28 13:45:14 -06:00
|
|
|
BufferSize: node.BufferSize,
|
2023-07-25 21:43:15 -06:00
|
|
|
NumExtensions: uint32(len(extensions)),
|
2023-07-27 15:27:14 -06:00
|
|
|
NumQueuedSignals: uint32(len(node.SignalQueue)),
|
2023-07-25 21:43:15 -06:00
|
|
|
},
|
|
|
|
Extensions: extensions,
|
2023-07-27 15:27:14 -06:00
|
|
|
QueuedSignals: node.SignalQueue,
|
2023-07-28 15:07:38 -06:00
|
|
|
KeyBytes: key_bytes,
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
|
2023-07-24 16:04:56 -06:00
|
|
|
i := 0
|
2023-07-26 00:42:12 -06:00
|
|
|
for ext_type, info := range(node.Extensions) {
|
2023-07-25 21:43:15 -06:00
|
|
|
ser, err := info.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node_db.Extensions[i] = ExtensionDB{
|
|
|
|
Header: ExtensionDBHeader{
|
2023-07-27 23:26:58 -06:00
|
|
|
TypeHash: Hash(ext_type),
|
2023-07-25 21:43:15 -06:00
|
|
|
Length: uint64(len(ser)),
|
|
|
|
},
|
|
|
|
Data: ser,
|
|
|
|
}
|
2023-07-24 16:04:56 -06:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return node_db.Serialize(), nil
|
|
|
|
}
|
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
func KeyID(pub *ecdsa.PublicKey) NodeID {
|
|
|
|
ser := elliptic.Marshal(pub.Curve, pub.X, pub.Y)
|
|
|
|
str := uuid.NewHash(sha512.New(), ZeroUUID, ser, 3)
|
|
|
|
return NodeID(str)
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
// Create a new node in memory and start it's event loop
|
2023-07-28 15:07:38 -06:00
|
|
|
// TODO: Change panics to errors
|
|
|
|
func NewNode(ctx *Context, key *ecdsa.PrivateKey, node_type NodeType, buffer_size uint32, queued_signals []QueuedSignal, extensions ...Extension) *Node {
|
|
|
|
var err error
|
|
|
|
if key == nil {
|
|
|
|
key, err = ecdsa.GenerateKey(ctx.ECDSA, rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id := KeyID(&key.PublicKey)
|
2023-07-28 12:46:06 -06:00
|
|
|
_, exists := ctx.Node(id)
|
2023-07-26 15:08:14 -06:00
|
|
|
if exists == true {
|
|
|
|
panic("Attempted to create an existing node")
|
|
|
|
}
|
|
|
|
|
2023-07-27 23:26:58 -06:00
|
|
|
def, exists := ctx.Types[Hash(node_type)]
|
2023-07-27 11:33:11 -06:00
|
|
|
if exists == false {
|
|
|
|
panic("Node type %s not registered in Context")
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_map := map[ExtType]Extension{}
|
|
|
|
for _, ext := range(extensions) {
|
|
|
|
_, exists := ext_map[ext.Type()]
|
|
|
|
if exists == true {
|
|
|
|
panic("Cannot add the same extension to a node twice")
|
|
|
|
}
|
|
|
|
ext_map[ext.Type()] = ext
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, required_ext := range(def.Extensions) {
|
|
|
|
_, exists := ext_map[required_ext]
|
|
|
|
if exists == false {
|
|
|
|
panic(fmt.Sprintf("%s requires %s", node_type, required_ext))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
if queued_signals == nil {
|
|
|
|
queued_signals = []QueuedSignal{}
|
|
|
|
}
|
|
|
|
|
|
|
|
next_signal, timeout_chan := SoonestSignal(queued_signals)
|
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
node := &Node{
|
2023-07-28 15:07:38 -06:00
|
|
|
Key: key,
|
2023-07-25 21:43:15 -06:00
|
|
|
ID: id,
|
2023-07-26 00:18:11 -06:00
|
|
|
Type: node_type,
|
2023-07-27 11:33:11 -06:00
|
|
|
Extensions: ext_map,
|
2023-07-28 13:45:14 -06:00
|
|
|
MsgChan: make(chan Msg, buffer_size),
|
|
|
|
BufferSize: buffer_size,
|
2023-07-27 15:27:14 -06:00
|
|
|
TimeoutChan: timeout_chan,
|
|
|
|
SignalQueue: queued_signals,
|
|
|
|
NextSignal: next_signal,
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
2023-07-28 12:46:06 -06:00
|
|
|
ctx.AddNode(id, node)
|
2023-07-28 15:07:38 -06:00
|
|
|
err = WriteNode(ctx, node)
|
2023-07-28 00:04:18 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-07-27 15:27:14 -06:00
|
|
|
|
2023-07-27 23:15:58 -06:00
|
|
|
go runNode(ctx, node)
|
2023-07-27 11:33:11 -06:00
|
|
|
|
2023-07-26 15:08:14 -06:00
|
|
|
return node
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 00:39:27 -06:00
|
|
|
func Allowed(ctx *Context, principal_id NodeID, action Action, node *Node) error {
|
2023-07-28 11:59:01 -06:00
|
|
|
ctx.Log.Logf("policy", "POLICY_CHECK: %s -> %s.%s", principal_id, node.ID, action)
|
2023-07-26 00:42:12 -06:00
|
|
|
// Nodes are allowed to perform all actions on themselves regardless of whether or not they have an ACL extension
|
2023-07-27 01:30:32 -06:00
|
|
|
if principal_id == node.ID {
|
2023-07-28 11:59:01 -06:00
|
|
|
ctx.Log.Logf("policy", "POLICY_CHECK_SAME_NODE: %s.%s", principal_id, action)
|
2023-07-26 00:42:12 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:28:03 -06:00
|
|
|
// Check if the node has a policy extension itself, and check against the policies in it
|
2023-07-27 09:32:33 -06:00
|
|
|
policy_ext, err := GetExt[*ACLExt](node)
|
2023-07-26 00:42:12 -06:00
|
|
|
if err != nil {
|
2023-07-28 11:59:01 -06:00
|
|
|
ctx.Log.Logf("policy", "POLICY_CHECK_NO_ACL_EXT: %s", node.ID)
|
2023-07-27 01:30:32 -06:00
|
|
|
return err
|
2023-07-24 16:04:56 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
|
2023-07-28 11:59:01 -06:00
|
|
|
err = policy_ext.Allows(ctx, principal_id, action, node)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Log.Logf("policy", "POLICY_CHECK_FAIL: %s -> %s.%s : %s", principal_id, node.ID, action, err)
|
|
|
|
} else {
|
|
|
|
ctx.Log.Logf("policy", "POLICY_CHECK_PASS: %s -> %s.%s", principal_id, node.ID, action)
|
|
|
|
}
|
|
|
|
return err
|
2023-07-23 19:04:04 -06:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:31:43 -06:00
|
|
|
// A DBHeader is parsed from the first NODE_DB_HEADER_LEN bytes of a serialized DB node
|
2023-07-25 21:43:15 -06:00
|
|
|
type NodeDBHeader struct {
|
2023-07-09 14:30:30 -06:00
|
|
|
Magic uint32
|
2023-07-25 21:43:15 -06:00
|
|
|
NumExtensions uint32
|
2023-07-27 15:27:14 -06:00
|
|
|
NumQueuedSignals uint32
|
2023-07-28 13:45:14 -06:00
|
|
|
BufferSize uint32
|
2023-07-28 15:07:38 -06:00
|
|
|
KeyLength uint32
|
2023-07-26 00:18:11 -06:00
|
|
|
TypeHash uint64
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type NodeDB struct {
|
|
|
|
Header NodeDBHeader
|
2023-07-27 15:27:14 -06:00
|
|
|
QueuedSignals []QueuedSignal
|
2023-07-25 21:43:15 -06:00
|
|
|
Extensions []ExtensionDB
|
2023-07-28 15:07:38 -06:00
|
|
|
KeyBytes []byte
|
2023-07-25 21:43:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: add size safety checks
|
|
|
|
func NewNodeDB(data []byte) (NodeDB, error) {
|
|
|
|
var zero NodeDB
|
|
|
|
|
|
|
|
ptr := 0
|
|
|
|
|
|
|
|
magic := binary.BigEndian.Uint32(data[0:4])
|
|
|
|
num_extensions := binary.BigEndian.Uint32(data[4:8])
|
2023-07-27 15:27:14 -06:00
|
|
|
num_queued_signals := binary.BigEndian.Uint32(data[8:12])
|
2023-07-28 13:45:14 -06:00
|
|
|
buffer_size := binary.BigEndian.Uint32(data[12:16])
|
2023-07-28 15:07:38 -06:00
|
|
|
key_length := binary.BigEndian.Uint32(data[16:20])
|
|
|
|
node_type_hash := binary.BigEndian.Uint64(data[20:28])
|
2023-07-25 21:43:15 -06:00
|
|
|
|
|
|
|
ptr += NODE_DB_HEADER_LEN
|
|
|
|
|
|
|
|
if magic != NODE_DB_MAGIC {
|
|
|
|
return zero, fmt.Errorf("header has incorrect magic 0x%x", magic)
|
|
|
|
}
|
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
key_bytes := make([]byte, key_length)
|
|
|
|
n := copy(key_bytes, data[ptr:(ptr+int(key_length))])
|
|
|
|
if n != int(key_length) {
|
|
|
|
return zero, fmt.Errorf("not enough key bytes: %d", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += int(key_length)
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
extensions := make([]ExtensionDB, num_extensions)
|
|
|
|
for i, _ := range(extensions) {
|
|
|
|
cur := data[ptr:]
|
|
|
|
|
|
|
|
type_hash := binary.BigEndian.Uint64(cur[0:8])
|
|
|
|
length := binary.BigEndian.Uint64(cur[8:16])
|
|
|
|
|
|
|
|
data_start := uint64(EXTENSION_DB_HEADER_LEN)
|
|
|
|
data_end := data_start + length
|
|
|
|
ext_data := cur[data_start:data_end]
|
|
|
|
|
|
|
|
extensions[i] = ExtensionDB{
|
|
|
|
Header: ExtensionDBHeader{
|
|
|
|
TypeHash: type_hash,
|
|
|
|
Length: length,
|
|
|
|
},
|
|
|
|
Data: ext_data,
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += int(EXTENSION_DB_HEADER_LEN + length)
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
queued_signals := make([]QueuedSignal, num_queued_signals)
|
|
|
|
for i, _ := range(queued_signals) {
|
|
|
|
queued_signals[i] = QueuedSignal{}
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
return NodeDB{
|
|
|
|
Header: NodeDBHeader{
|
|
|
|
Magic: magic,
|
2023-07-26 00:18:11 -06:00
|
|
|
TypeHash: node_type_hash,
|
2023-07-28 13:45:14 -06:00
|
|
|
BufferSize: buffer_size,
|
2023-07-28 15:07:38 -06:00
|
|
|
KeyLength: key_length,
|
2023-07-25 21:43:15 -06:00
|
|
|
NumExtensions: num_extensions,
|
2023-07-27 15:27:14 -06:00
|
|
|
NumQueuedSignals: num_queued_signals,
|
2023-07-25 21:43:15 -06:00
|
|
|
},
|
2023-07-28 15:07:38 -06:00
|
|
|
KeyBytes: key_bytes,
|
2023-07-25 21:43:15 -06:00
|
|
|
Extensions: extensions,
|
2023-07-27 15:27:14 -06:00
|
|
|
QueuedSignals: queued_signals,
|
2023-07-25 21:43:15 -06:00
|
|
|
}, nil
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (header NodeDBHeader) Serialize() []byte {
|
2023-07-09 14:30:30 -06:00
|
|
|
if header.Magic != NODE_DB_MAGIC {
|
|
|
|
panic(fmt.Sprintf("Serializing header with invalid magic %0x", header.Magic))
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]byte, NODE_DB_HEADER_LEN)
|
|
|
|
binary.BigEndian.PutUint32(ret[0:4], header.Magic)
|
2023-07-25 21:43:15 -06:00
|
|
|
binary.BigEndian.PutUint32(ret[4:8], header.NumExtensions)
|
2023-07-27 15:27:14 -06:00
|
|
|
binary.BigEndian.PutUint32(ret[8:12], header.NumQueuedSignals)
|
2023-07-28 13:45:14 -06:00
|
|
|
binary.BigEndian.PutUint32(ret[12:16], header.BufferSize)
|
2023-07-28 15:07:38 -06:00
|
|
|
binary.BigEndian.PutUint32(ret[16:20], header.KeyLength)
|
|
|
|
binary.BigEndian.PutUint64(ret[20:28], header.TypeHash)
|
2023-07-09 14:30:30 -06:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func (node NodeDB) Serialize() []byte {
|
|
|
|
ser := node.Header.Serialize()
|
2023-07-28 15:07:38 -06:00
|
|
|
ser = append(ser, node.KeyBytes...)
|
2023-07-25 21:43:15 -06:00
|
|
|
for _, extension := range(node.Extensions) {
|
|
|
|
ser = append(ser, extension.Serialize()...)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
|
|
|
|
return ser
|
|
|
|
}
|
|
|
|
|
|
|
|
func (header ExtensionDBHeader) Serialize() []byte {
|
|
|
|
ret := make([]byte, EXTENSION_DB_HEADER_LEN)
|
|
|
|
binary.BigEndian.PutUint64(ret[0:8], header.TypeHash)
|
|
|
|
binary.BigEndian.PutUint64(ret[8:16], header.Length)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (extension ExtensionDB) Serialize() []byte {
|
|
|
|
header_bytes := extension.Header.Serialize()
|
|
|
|
return append(header_bytes, extension.Data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExtensionDBHeader struct {
|
|
|
|
TypeHash uint64
|
|
|
|
Length uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExtensionDB struct {
|
|
|
|
Header ExtensionDBHeader
|
|
|
|
Data []byte
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-27 15:49:21 -06:00
|
|
|
// Write a node to the database
|
2023-07-27 15:27:14 -06:00
|
|
|
func WriteNode(ctx *Context, node *Node) error {
|
|
|
|
ctx.Log.Logf("db", "DB_WRITE: %s", node.ID)
|
|
|
|
|
|
|
|
bytes, err := node.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id_bytes := node.ID.Serialize()
|
2023-07-28 00:04:18 -06:00
|
|
|
ctx.Log.Logf("db", "DB_WRITE_ID: %+v", id_bytes)
|
2023-07-27 15:27:14 -06:00
|
|
|
|
|
|
|
return ctx.DB.Update(func(txn *badger.Txn) error {
|
|
|
|
return txn.Set(id_bytes, bytes)
|
|
|
|
})
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func LoadNode(ctx * Context, id NodeID) (*Node, error) {
|
2023-07-27 15:27:14 -06:00
|
|
|
ctx.Log.Logf("db", "LOADING_NODE: %s", id)
|
2023-07-09 14:30:30 -06:00
|
|
|
var bytes []byte
|
|
|
|
err := ctx.DB.View(func(txn *badger.Txn) error {
|
2023-07-28 00:04:18 -06:00
|
|
|
id_bytes := id.Serialize()
|
|
|
|
ctx.Log.Logf("db", "DB_READ_ID: %+v", id_bytes)
|
|
|
|
item, err := txn.Get(id_bytes)
|
2023-07-09 14:30:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return item.Value(func(val []byte) error {
|
|
|
|
bytes = append([]byte{}, val...)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
})
|
2023-07-27 16:48:39 -06:00
|
|
|
if errors.Is(err, badger.ErrKeyNotFound) {
|
|
|
|
return nil, NodeNotFoundError
|
|
|
|
}else if err != nil {
|
2023-07-25 21:43:15 -06:00
|
|
|
return nil, err
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
// Parse the bytes from the DB
|
|
|
|
node_db, err := NewNodeDB(bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-28 15:07:38 -06:00
|
|
|
key, err := x509.ParseECPrivateKey(node_db.KeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if key.PublicKey.Curve != ctx.ECDSA {
|
|
|
|
return nil, fmt.Errorf("%s - wrong ec curve for private key: %+v, expected %+v", id, key.PublicKey.Curve, ctx.ECDSA)
|
|
|
|
}
|
|
|
|
|
|
|
|
key_id := KeyID(&key.PublicKey)
|
|
|
|
if key_id != id {
|
|
|
|
return nil, fmt.Errorf("KeyID(%s) != %s", key_id, id)
|
|
|
|
}
|
|
|
|
|
2023-07-26 00:18:11 -06:00
|
|
|
node_type, known := ctx.Types[node_db.Header.TypeHash]
|
|
|
|
if known == false {
|
|
|
|
return nil, fmt.Errorf("Tried to load node %s of type 0x%x, which is not a known node type", id, node_db.Header.TypeHash)
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:27:14 -06:00
|
|
|
next_signal, timeout_chan := SoonestSignal(node_db.QueuedSignals)
|
2023-07-27 16:48:39 -06:00
|
|
|
node := &Node{
|
2023-07-28 15:07:38 -06:00
|
|
|
Key: key,
|
|
|
|
ID: key_id,
|
2023-07-27 11:33:11 -06:00
|
|
|
Type: node_type.Type,
|
|
|
|
Extensions: map[ExtType]Extension{},
|
2023-07-28 13:45:14 -06:00
|
|
|
MsgChan: make(chan Msg, node_db.Header.BufferSize),
|
|
|
|
BufferSize: node_db.Header.BufferSize,
|
2023-07-27 15:27:14 -06:00
|
|
|
TimeoutChan: timeout_chan,
|
|
|
|
SignalQueue: node_db.QueuedSignals,
|
|
|
|
NextSignal: next_signal,
|
2023-07-27 11:33:11 -06:00
|
|
|
}
|
2023-07-28 12:46:06 -06:00
|
|
|
ctx.AddNode(id, node)
|
2023-07-09 14:30:30 -06:00
|
|
|
|
2023-07-26 11:56:10 -06:00
|
|
|
found_extensions := []ExtType{}
|
2023-07-25 21:43:15 -06:00
|
|
|
// Parse each of the extensions from the db
|
|
|
|
for _, ext_db := range(node_db.Extensions) {
|
|
|
|
type_hash := ext_db.Header.TypeHash
|
|
|
|
def, known := ctx.Extensions[type_hash]
|
|
|
|
if known == false {
|
|
|
|
return nil, fmt.Errorf("%s tried to load extension 0x%x, which is not a known extension type", id, type_hash)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-26 15:40:33 -06:00
|
|
|
ctx.Log.Logf("db", "DB_EXTENSION_LOADING: %s/%s", id, def.Type)
|
2023-07-25 21:43:15 -06:00
|
|
|
extension, err := def.Load(ctx, ext_db.Data)
|
2023-07-09 14:30:30 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-07-26 00:42:12 -06:00
|
|
|
node.Extensions[def.Type] = extension
|
2023-07-26 11:56:10 -06:00
|
|
|
found_extensions = append(found_extensions, def.Type)
|
2023-07-26 15:40:33 -06:00
|
|
|
ctx.Log.Logf("db", "DB_EXTENSION_LOADED: %s/%s - %+v", id, def.Type, extension)
|
2023-07-26 11:56:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
missing_extensions := []ExtType{}
|
|
|
|
for _, ext := range(node_type.Extensions) {
|
|
|
|
found := false
|
|
|
|
for _, found_ext := range(found_extensions) {
|
|
|
|
if found_ext == ext {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found == false {
|
|
|
|
missing_extensions = append(missing_extensions, ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(missing_extensions) > 0 {
|
|
|
|
return nil, fmt.Errorf("DB_LOAD_MISSING_EXTENSIONS: %s - %+v - %+v", id, node_type, missing_extensions)
|
|
|
|
}
|
|
|
|
|
|
|
|
extra_extensions := []ExtType{}
|
|
|
|
for _, found_ext := range(found_extensions) {
|
|
|
|
found := false
|
|
|
|
for _, ext := range(node_type.Extensions) {
|
|
|
|
if ext == found_ext {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found == false {
|
|
|
|
extra_extensions = append(extra_extensions, found_ext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(extra_extensions) > 0 {
|
2023-07-27 00:57:18 -06:00
|
|
|
ctx.Log.Logf("db", "DB_LOAD_EXTRA_EXTENSIONS: %s - %+v - %+v", id, node_type, extra_extensions)
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-25 21:43:15 -06:00
|
|
|
|
|
|
|
ctx.Log.Logf("db", "DB_NODE_LOADED: %s", id)
|
2023-07-27 15:27:14 -06:00
|
|
|
|
2023-07-27 23:15:58 -06:00
|
|
|
go runNode(ctx, node)
|
2023-07-27 15:27:14 -06:00
|
|
|
|
2023-07-09 14:30:30 -06:00
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func NewACLInfo(node *Node, resources []string) ACLMap {
|
|
|
|
return ACLMap{
|
|
|
|
node.ID: ACLInfo{
|
2023-07-22 21:24:54 -06:00
|
|
|
Node: node,
|
|
|
|
Resources: resources,
|
|
|
|
},
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func NewACLMap(requests ...ACLMap) ACLMap {
|
|
|
|
reqs := ACLMap{}
|
2023-07-22 20:21:17 -06:00
|
|
|
for _, req := range(requests) {
|
2023-07-22 21:24:54 -06:00
|
|
|
for id, info := range(req) {
|
|
|
|
reqs[id] = info
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
return reqs
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func ACLListM(m map[NodeID]*Node, resources[]string) ACLMap {
|
|
|
|
reqs := ACLMap{}
|
2023-07-24 16:04:56 -06:00
|
|
|
for _, node := range(m) {
|
2023-07-25 21:43:15 -06:00
|
|
|
reqs[node.ID] = ACLInfo{
|
2023-07-24 16:04:56 -06:00
|
|
|
Node: node,
|
|
|
|
Resources: resources,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return reqs
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
func ACLList(list []*Node, resources []string) ACLMap {
|
|
|
|
reqs := ACLMap{}
|
2023-07-22 21:24:54 -06:00
|
|
|
for _, node := range(list) {
|
2023-07-25 21:43:15 -06:00
|
|
|
reqs[node.ID] = ACLInfo{
|
2023-07-22 20:21:17 -06:00
|
|
|
Node: node,
|
|
|
|
Resources: resources,
|
|
|
|
}
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
2023-07-22 21:24:54 -06:00
|
|
|
return reqs
|
2023-07-09 14:30:30 -06:00
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
type NodeMap map[NodeID]*Node
|
2023-07-22 20:21:17 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
type ACLInfo struct {
|
|
|
|
Node *Node
|
2023-07-22 20:21:17 -06:00
|
|
|
Resources []string
|
|
|
|
}
|
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
type ACLMap map[NodeID]ACLInfo
|
|
|
|
type ExtMap map[uint64]Extension
|
2023-07-10 22:31:43 -06:00
|
|
|
|
2023-07-25 21:43:15 -06:00
|
|
|
// Context of running state usage(read/write)
|
2023-07-23 17:57:47 -06:00
|
|
|
type StateContext struct {
|
2023-07-25 21:43:15 -06:00
|
|
|
// Type of the state context
|
2023-07-23 17:57:47 -06:00
|
|
|
Type string
|
2023-07-25 21:43:15 -06:00
|
|
|
// The wrapped graph context
|
2023-07-22 20:21:17 -06:00
|
|
|
Graph *Context
|
2023-07-25 21:43:15 -06:00
|
|
|
// Granted permissions in the context
|
|
|
|
Permissions map[NodeID]ACLMap
|
|
|
|
// Locked extensions in the context
|
|
|
|
Locked map[NodeID]*Node
|
|
|
|
|
|
|
|
// Context state for validation
|
2023-07-23 17:57:47 -06:00
|
|
|
Started bool
|
|
|
|
Finished bool
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
|
|
|
|
2023-07-23 17:57:47 -06:00
|
|
|
func ValidateStateContext(context *StateContext, Type string, Finished bool) error {
|
|
|
|
if context == nil {
|
|
|
|
return fmt.Errorf("context is nil")
|
|
|
|
}
|
|
|
|
if context.Finished != Finished {
|
|
|
|
return fmt.Errorf("context in wrong Finished state")
|
|
|
|
}
|
|
|
|
if context.Type != Type {
|
|
|
|
return fmt.Errorf("%s is not a %s context", context.Type, Type)
|
|
|
|
}
|
|
|
|
if context.Locked == nil || context.Graph == nil || context.Permissions == nil {
|
|
|
|
return fmt.Errorf("context is not initialized correctly")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReadContext(ctx *Context) *StateContext {
|
|
|
|
return &StateContext{
|
|
|
|
Type: "read",
|
|
|
|
Graph: ctx,
|
2023-07-25 21:43:15 -06:00
|
|
|
Permissions: map[NodeID]ACLMap{},
|
|
|
|
Locked: map[NodeID]*Node{},
|
2023-07-23 17:57:47 -06:00
|
|
|
Started: false,
|
|
|
|
Finished: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewWriteContext(ctx *Context) *StateContext {
|
|
|
|
return &StateContext{
|
|
|
|
Type: "write",
|
|
|
|
Graph: ctx,
|
2023-07-25 21:43:15 -06:00
|
|
|
Permissions: map[NodeID]ACLMap{},
|
|
|
|
Locked: map[NodeID]*Node{},
|
2023-07-23 17:57:47 -06:00
|
|
|
Started: false,
|
|
|
|
Finished: false,
|
|
|
|
}
|
2023-07-22 20:21:17 -06:00
|
|
|
}
|
2023-07-23 17:57:47 -06:00
|
|
|
|
|
|
|
type StateFn func(*StateContext)(error)
|
2023-07-09 14:30:30 -06:00
|
|
|
|
2023-07-22 20:21:17 -06:00
|
|
|
func del[K comparable](list []K, val K) []K {
|
|
|
|
idx := -1
|
|
|
|
for i, v := range(list) {
|
|
|
|
if v == val {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
list[idx] = list[len(list)-1]
|
|
|
|
return list[:len(list)-1]
|
|
|
|
}
|
2023-07-28 00:04:18 -06:00
|
|
|
|
|
|
|
func IDMap[S any, T map[NodeID]S](m T)map[string]S {
|
|
|
|
ret := map[string]S{}
|
|
|
|
for id, val := range(m) {
|
|
|
|
ret[id.String()] = val
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadIDMap[S any, T map[string]S](m T)(map[NodeID]S, error) {
|
|
|
|
ret := map[NodeID]S{}
|
|
|
|
for str, val := range(m) {
|
|
|
|
id, err := ParseID(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret[id] = val
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|