frostfs-node/pkg/services/netmap/audit.go
Dmitrii Stepanov ecd1ed7a5e [#1184] node: Add audit middleware for grpc services
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2024-06-19 16:05:53 +03:00

60 lines
1.7 KiB
Go

package netmap
import (
"context"
"sync/atomic"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
netmapGRPC "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap/grpc"
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/audit"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
)
var _ Server = (*auditService)(nil)
type auditService struct {
next Server
log *logger.Logger
enabled *atomic.Bool
}
func NewAuditService(next Server, log *logger.Logger, enabled *atomic.Bool) Server {
return &auditService{
next: next,
log: log,
enabled: enabled,
}
}
// LocalNodeInfo implements Server.
func (a *auditService) LocalNodeInfo(ctx context.Context, req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) {
res, err := a.next.LocalNodeInfo(ctx, req)
if !a.enabled.Load() {
return res, err
}
audit.LogRequest(a.log, netmapGRPC.NetmapService_LocalNodeInfo_FullMethodName, req,
nil, err == nil)
return res, err
}
// NetworkInfo implements Server.
func (a *auditService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
res, err := a.next.NetworkInfo(ctx, req)
if !a.enabled.Load() {
return res, err
}
audit.LogRequest(a.log, netmapGRPC.NetmapService_NetworkInfo_FullMethodName, req,
nil, err == nil)
return res, err
}
// Snapshot implements Server.
func (a *auditService) Snapshot(ctx context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
res, err := a.next.Snapshot(ctx, req)
if !a.enabled.Load() {
return res, err
}
audit.LogRequest(a.log, netmapGRPC.NetmapService_NetmapSnapshot_FullMethodName, req,
nil, err == nil)
return res, err
}