2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2023-02-03 21:24:16 +00:00
|
|
|
package vault
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-uuid"
|
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCanSendEventsFromBuiltinPlugin(t *testing.T) {
|
|
|
|
c, _, _ := TestCoreUnsealed(t)
|
|
|
|
|
|
|
|
ctx := namespace.RootContext(nil)
|
|
|
|
|
|
|
|
// subscribe to an event type
|
|
|
|
eventType, err := uuid.GenerateUUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-02-16 17:22:56 +00:00
|
|
|
ch, cancel, err := c.events.Subscribe(ctx, namespace.RootNamespace, eventType)
|
2023-02-03 21:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2023-02-09 21:18:58 +00:00
|
|
|
defer cancel()
|
2023-02-03 21:24:16 +00:00
|
|
|
|
|
|
|
// generate the event in a plugin
|
|
|
|
event, err := logical.NewEvent()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = c.cubbyholeBackend.SendEvent(ctx, logical.EventType(eventType), event)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that the event is routed to the subscription
|
|
|
|
select {
|
2023-02-17 19:38:03 +00:00
|
|
|
case receivedEvent := <-ch:
|
|
|
|
received := receivedEvent.Payload.(*logical.EventReceived)
|
2023-02-03 21:24:16 +00:00
|
|
|
if event.Id != received.Event.Id {
|
|
|
|
t.Errorf("Got wrong event: %+v, expected %+v", received, event)
|
|
|
|
}
|
|
|
|
if received.PluginInfo == nil {
|
|
|
|
t.Error("Expected plugin info but got nil")
|
|
|
|
} else {
|
|
|
|
if received.PluginInfo.Plugin != "cubbyhole" {
|
|
|
|
t.Errorf("Expected cubbyhole plugin but got %s", received.PluginInfo.Plugin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("timeout waiting for event")
|
|
|
|
}
|
|
|
|
}
|