2023-01-17 21:34:37 +00:00
|
|
|
package eventbus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
|
|
"github.com/hashicorp/eventlogger"
|
|
|
|
"github.com/hashicorp/eventlogger/formatter_filters/cloudevents"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/hashicorp/go-uuid"
|
2023-02-03 21:24:16 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
2023-01-17 21:34:37 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNotStarted = errors.New("event broker has not been started")
|
|
|
|
|
|
|
|
var cloudEventsFormatterFilter *cloudevents.FormatterFilter
|
|
|
|
|
|
|
|
// EventBus contains the main logic of running an event broker for Vault.
|
|
|
|
// Start() must be called before the EventBus will accept events for sending.
|
|
|
|
type EventBus struct {
|
|
|
|
logger hclog.Logger
|
|
|
|
broker *eventlogger.Broker
|
|
|
|
started atomic.Bool
|
|
|
|
formatterNodeID eventlogger.NodeID
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
type pluginEventBus struct {
|
|
|
|
bus *EventBus
|
|
|
|
namespace *namespace.Namespace
|
|
|
|
pluginInfo *logical.EventPluginInfo
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:34:37 +00:00
|
|
|
type asyncChanNode struct {
|
2023-02-03 21:24:16 +00:00
|
|
|
// TODO: add bounded deque buffer of *EventReceived
|
|
|
|
ch chan *logical.EventReceived
|
|
|
|
namespace *namespace.Namespace
|
2023-01-17 21:34:37 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 18:18:23 +00:00
|
|
|
var (
|
|
|
|
_ eventlogger.Node = (*asyncChanNode)(nil)
|
2023-02-03 21:24:16 +00:00
|
|
|
_ logical.EventSender = (*pluginEventBus)(nil)
|
2023-01-20 18:18:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Start starts the event bus, allowing events to be written.
|
|
|
|
// It is not possible to stop or restart the event bus.
|
|
|
|
// It is safe to call Start() multiple times.
|
|
|
|
func (bus *EventBus) Start() {
|
|
|
|
wasStarted := bus.started.Swap(true)
|
|
|
|
if !wasStarted {
|
|
|
|
bus.logger.Info("Starting event system")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
// SendInternal sends an event to the event bus and routes it to all relevant subscribers.
|
2023-01-20 18:18:23 +00:00
|
|
|
// This function does *not* wait for all subscribers to acknowledge before returning.
|
2023-02-03 21:24:16 +00:00
|
|
|
// This function is meant to be used by trusted internal code, so it can specify details like the namespace
|
|
|
|
// and plugin info. Events from plugins should be routed through WithPlugin(), which will populate
|
|
|
|
// the namespace and plugin info automatically.
|
|
|
|
func (bus *EventBus) SendInternal(ctx context.Context, ns *namespace.Namespace, pluginInfo *logical.EventPluginInfo, eventType logical.EventType, data *logical.EventData) error {
|
|
|
|
if ns == nil {
|
|
|
|
return namespace.ErrNoNamespace
|
|
|
|
}
|
2023-01-20 18:18:23 +00:00
|
|
|
if !bus.started.Load() {
|
|
|
|
return ErrNotStarted
|
|
|
|
}
|
2023-02-03 21:24:16 +00:00
|
|
|
eventReceived := &logical.EventReceived{
|
|
|
|
Event: data,
|
|
|
|
Namespace: ns.Path,
|
|
|
|
EventType: string(eventType),
|
|
|
|
PluginInfo: pluginInfo,
|
|
|
|
}
|
|
|
|
bus.logger.Info("Sending event", "event", eventReceived)
|
|
|
|
_, err := bus.broker.Send(ctx, eventlogger.EventType(eventType), eventReceived)
|
2023-01-20 18:18:23 +00:00
|
|
|
if err != nil {
|
|
|
|
// if no listeners for this event type are registered, that's okay, the event
|
|
|
|
// will just not be sent anywhere
|
|
|
|
if strings.Contains(strings.ToLower(err.Error()), "no graph for eventtype") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-01-17 21:34:37 +00:00
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
func (bus *EventBus) WithPlugin(ns *namespace.Namespace, eventPluginInfo *logical.EventPluginInfo) (*pluginEventBus, error) {
|
|
|
|
if ns == nil {
|
|
|
|
return nil, namespace.ErrNoNamespace
|
|
|
|
}
|
|
|
|
return &pluginEventBus{
|
|
|
|
bus: bus,
|
|
|
|
namespace: ns,
|
|
|
|
pluginInfo: eventPluginInfo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send sends an event to the event bus and routes it to all relevant subscribers.
|
|
|
|
// This function does *not* wait for all subscribers to acknowledge before returning.
|
|
|
|
func (bus *pluginEventBus) Send(ctx context.Context, eventType logical.EventType, data *logical.EventData) error {
|
|
|
|
return bus.bus.SendInternal(ctx, bus.namespace, bus.pluginInfo, eventType, data)
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:34:37 +00:00
|
|
|
func init() {
|
|
|
|
// TODO: maybe this should relate to the Vault core somehow?
|
|
|
|
sourceUrl, err := url.Parse("https://vaultproject.io/")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
cloudEventsFormatterFilter = &cloudevents.FormatterFilter{
|
|
|
|
Source: sourceUrl,
|
|
|
|
Predicate: func(_ context.Context, e interface{}) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:46:01 +00:00
|
|
|
func NewEventBus(logger hclog.Logger) (*EventBus, error) {
|
2023-01-17 21:34:37 +00:00
|
|
|
broker := eventlogger.NewBroker()
|
|
|
|
|
|
|
|
formatterID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
formatterNodeID := eventlogger.NodeID(formatterID)
|
|
|
|
err = broker.RegisterNode(formatterNodeID, cloudEventsFormatterFilter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:46:01 +00:00
|
|
|
if logger == nil {
|
|
|
|
logger = hclog.Default().Named("events")
|
|
|
|
}
|
|
|
|
|
2023-01-17 21:34:37 +00:00
|
|
|
return &EventBus{
|
2023-01-18 18:46:01 +00:00
|
|
|
logger: logger,
|
2023-01-17 21:34:37 +00:00
|
|
|
broker: broker,
|
|
|
|
formatterNodeID: formatterNodeID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
func (bus *EventBus) Subscribe(_ context.Context, ns *namespace.Namespace, eventType logical.EventType) (chan *logical.EventReceived, error) {
|
2023-01-17 21:34:37 +00:00
|
|
|
// subscriptions are still stored even if the bus has not been started
|
|
|
|
pipelineID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeID, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
// TODO: should we have just one node per namespace, and handle all the routing ourselves?
|
|
|
|
asyncNode := newAsyncNode(ns)
|
2023-01-17 21:34:37 +00:00
|
|
|
err = bus.broker.RegisterNode(eventlogger.NodeID(nodeID), asyncNode)
|
|
|
|
if err != nil {
|
|
|
|
defer asyncNode.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := []eventlogger.NodeID{bus.formatterNodeID, eventlogger.NodeID(nodeID)}
|
|
|
|
|
|
|
|
pipeline := eventlogger.Pipeline{
|
|
|
|
PipelineID: eventlogger.PipelineID(pipelineID),
|
|
|
|
EventType: eventlogger.EventType(eventType),
|
|
|
|
NodeIDs: nodes,
|
|
|
|
}
|
|
|
|
err = bus.broker.RegisterPipeline(pipeline)
|
|
|
|
if err != nil {
|
|
|
|
defer asyncNode.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return asyncNode.ch, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
func newAsyncNode(namespace *namespace.Namespace) *asyncChanNode {
|
2023-01-17 21:34:37 +00:00
|
|
|
return &asyncChanNode{
|
2023-02-03 21:24:16 +00:00
|
|
|
ch: make(chan *logical.EventReceived),
|
|
|
|
namespace: namespace,
|
2023-01-17 21:34:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *asyncChanNode) Close() error {
|
|
|
|
close(node.ch)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *asyncChanNode) Process(ctx context.Context, e *eventlogger.Event) (*eventlogger.Event, error) {
|
|
|
|
// TODO: add timeout on sending to node.ch
|
|
|
|
// sends to the channel async in another goroutine
|
|
|
|
go func() {
|
2023-02-03 21:24:16 +00:00
|
|
|
eventRecv := e.Payload.(*logical.EventReceived)
|
|
|
|
// drop if event is not in our namespace
|
|
|
|
// TODO: add wildcard processing here in some cases?
|
|
|
|
if eventRecv.Namespace != node.namespace.Path {
|
|
|
|
return
|
|
|
|
}
|
2023-01-17 21:34:37 +00:00
|
|
|
select {
|
2023-02-03 21:24:16 +00:00
|
|
|
case node.ch <- eventRecv:
|
2023-01-17 21:34:37 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *asyncChanNode) Reopen() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *asyncChanNode) Type() eventlogger.NodeType {
|
|
|
|
return eventlogger.NodeTypeSink
|
|
|
|
}
|