7d3d404ee2
Also updates the event receieved to include a timestamp. Websockets support both JSON and protobuf binary formats. This can be used by either `wscat` or the new `vault events subscribe`: e.g., ```sh $ wscat -H "X-Vault-Token: $(vault print token)" --connect ws://127.0.0.1:8200/v1/sys/events/subscribe/abc?json=true {"event":{"id":"5c5c8c83-bf43-7da5-fe88-fc3cac814b2e", "note":"testing"}, "eventType":"abc", "timestamp":"2023-02-07T18:40:50.598408Z"} ... ``` and ```sh $ vault events subscribe abc {"event":{"id":"5c5c8c83-bf43-7da5-fe88-fc3cac814b2e", "note":"testing"}, "eventType":"abc", "timestamp":"2023-02-07T18:40:50.598408Z"} ... ``` Co-authored-by: Tom Proctor <tomhjp@users.noreply.github.com>
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
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)
|
|
}
|
|
ch, cancel, err := c.events.Subscribe(ctx, namespace.RootNamespace, logical.EventType(eventType))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer cancel()
|
|
|
|
// 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 {
|
|
case received := <-ch:
|
|
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")
|
|
}
|
|
}
|