api/internal/rpc/audit_server.go

55 lines
1.5 KiB
Go
Raw Normal View History

package rpc
import (
"context"
"io"
"os"
"gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/audit/sink"
2021-01-16 17:26:15 +00:00
"gitlab.com/inetmock/inetmock/pkg/logging"
"go.uber.org/zap"
)
type auditServer struct {
UnimplementedAuditServer
2021-01-16 17:26:15 +00:00
logger logging.Logger
eventStream audit.EventStream
}
func (a *auditServer) WatchEvents(req *WatchEventsRequest, srv Audit_WatchEventsServer) (err error) {
2021-01-16 17:26:15 +00:00
a.logger.Info("watcher attached", zap.String("name", req.WatcherName))
err = a.eventStream.RegisterSink(sink.NewGRPCSink(srv.Context(), req.WatcherName, func(ev audit.Event) {
if err = srv.Send(ev.ProtoMessage()); err != nil {
return
}
}))
if err != nil {
return
}
<-srv.Context().Done()
2021-01-16 17:26:15 +00:00
a.logger.Info("Watcher detached", zap.String("name", req.WatcherName))
return
}
func (a *auditServer) RegisterFileSink(_ context.Context, req *RegisterFileSinkRequest) (resp *RegisterFileSinkResponse, err error) {
var writer io.WriteCloser
if writer, err = os.OpenFile(req.TargetPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644); err != nil {
return
}
2021-01-16 17:26:15 +00:00
if err = a.eventStream.RegisterSink(sink.NewWriterSink(req.TargetPath, audit.NewEventWriter(writer))); err != nil {
return
}
resp = &RegisterFileSinkResponse{}
return
}
func (a *auditServer) RemoveFileSink(_ context.Context, req *RemoveFileSinkRequest) (*RemoveFileSinkResponse, error) {
2021-01-16 17:26:15 +00:00
gotRemoved := a.eventStream.RemoveSink(req.TargetPath)
return &RemoveFileSinkResponse{
SinkGotRemoved: gotRemoved,
}, nil
}