2023-02-15 01:52:11 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/logstore"
|
2023-09-15 14:58:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/logstore/record"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
func AccessStorageInterceptor(svc *logstore.Service[*record.AccessLog]) grpc.UnaryServerInterceptor {
|
2023-02-15 01:52:11 +00:00
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (_ interface{}, err error) {
|
|
|
|
if !svc.Enabled() {
|
|
|
|
return handler(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
reqMd, _ := metadata.FromIncomingContext(ctx)
|
|
|
|
|
|
|
|
resp, handlerErr := handler(ctx, req)
|
|
|
|
|
|
|
|
interceptorCtx, span := tracing.NewServerInterceptorSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
var respStatus uint32
|
|
|
|
grpcStatus, ok := status.FromError(handlerErr)
|
|
|
|
if ok {
|
|
|
|
respStatus = uint32(grpcStatus.Code())
|
|
|
|
}
|
|
|
|
|
|
|
|
resMd, _ := metadata.FromOutgoingContext(ctx)
|
|
|
|
instance := authz.GetInstance(ctx)
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
r := &record.AccessLog{
|
2023-02-15 01:52:11 +00:00
|
|
|
LogDate: time.Now(),
|
2023-09-15 14:58:45 +00:00
|
|
|
Protocol: record.GRPC,
|
2023-02-15 01:52:11 +00:00
|
|
|
RequestURL: info.FullMethod,
|
|
|
|
ResponseStatus: respStatus,
|
|
|
|
RequestHeaders: reqMd,
|
|
|
|
ResponseHeaders: resMd,
|
|
|
|
InstanceID: instance.InstanceID(),
|
|
|
|
ProjectID: instance.ProjectID(),
|
|
|
|
RequestedDomain: instance.RequestedDomain(),
|
|
|
|
RequestedHost: instance.RequestedHost(),
|
|
|
|
}
|
|
|
|
|
2023-09-15 14:58:45 +00:00
|
|
|
svc.Handle(interceptorCtx, r)
|
2023-02-15 01:52:11 +00:00
|
|
|
return resp, handlerErr
|
|
|
|
}
|
|
|
|
}
|