open-vault/sdk/plugin/grpc_events.go
Christopher Swenson 5864075c30
Add events sending routed from plugins (#18834)
This isn't perfect for sure, but it's solidifying and becoming a useful
base to work off.

This routes events sent from auth and secrets plugins to the main
`EventBus` in the Vault Core. Events sent from plugins are automatically
tagged with the namespace and plugin information associated with them.
2023-02-03 13:24:16 -08:00

43 lines
975 B
Go

package plugin
import (
"context"
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/vault/sdk/plugin/pb"
"google.golang.org/grpc"
)
func newGRPCEventsClient(conn *grpc.ClientConn) *GRPCEventsClient {
return &GRPCEventsClient{
client: pb.NewEventsClient(conn),
}
}
type GRPCEventsClient struct {
client pb.EventsClient
}
var _ logical.EventSender = (*GRPCEventsClient)(nil)
func (s *GRPCEventsClient) Send(ctx context.Context, eventType logical.EventType, event *logical.EventData) error {
_, err := s.client.SendEvent(ctx, &pb.SendEventRequest{
EventType: string(eventType),
Event: event,
})
return err
}
type GRPCEventsServer struct {
pb.UnimplementedEventsServer
impl logical.EventSender
}
func (s *GRPCEventsServer) SendEvent(ctx context.Context, req *pb.SendEventRequest) (*pb.Empty, error) {
err := s.impl.Send(ctx, logical.EventType(req.EventType), req.Event)
if err != nil {
return nil, err
}
return &pb.Empty{}, nil
}