2023-10-25 12:09:15 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
2023-11-28 15:56:29 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2023-10-25 12:09:15 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/activity"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/gerrors"
|
2023-11-22 10:12:23 +00:00
|
|
|
ainfo "github.com/zitadel/zitadel/internal/api/info"
|
2023-10-25 12:09:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ActivityInterceptor() grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
2023-11-28 15:56:29 +00:00
|
|
|
ctx = activityInfoFromGateway(ctx).SetMethod(info.FullMethod).IntoContext(ctx)
|
2023-10-25 12:09:15 +00:00
|
|
|
resp, err := handler(ctx, req)
|
|
|
|
if isResourceAPI(info.FullMethod) {
|
2023-12-08 14:30:55 +00:00
|
|
|
code, _, _, _ := gerrors.ExtractZITADELError(err)
|
2023-11-22 10:12:23 +00:00
|
|
|
ctx = ainfo.ActivityInfoFromContext(ctx).SetGRPCStatus(code).IntoContext(ctx)
|
|
|
|
activity.TriggerGRPCWithContext(ctx, activity.ResourceAPI)
|
|
|
|
}
|
2023-10-25 12:09:15 +00:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resourcePrefixes = []string{
|
|
|
|
"/zitadel.management.v1.ManagementService/",
|
|
|
|
"/zitadel.admin.v1.AdminService/",
|
|
|
|
"/zitadel.user.v2beta.UserService/",
|
|
|
|
"/zitadel.settings.v2beta.SettingsService/",
|
|
|
|
"/zitadel.auth.v1.AuthService/",
|
|
|
|
}
|
|
|
|
|
|
|
|
func isResourceAPI(method string) bool {
|
|
|
|
return slices.ContainsFunc(resourcePrefixes, func(prefix string) bool {
|
|
|
|
return strings.HasPrefix(method, prefix)
|
|
|
|
})
|
|
|
|
}
|
2023-11-28 15:56:29 +00:00
|
|
|
|
|
|
|
func activityInfoFromGateway(ctx context.Context) *ainfo.ActivityInfo {
|
|
|
|
info := ainfo.ActivityInfoFromContext(ctx)
|
|
|
|
md, ok := metadata.FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
path := md.Get(activity.PathKey)
|
|
|
|
if len(path) != 1 {
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
requestMethod := md.Get(activity.RequestMethodKey)
|
|
|
|
if len(requestMethod) != 1 {
|
|
|
|
return info
|
|
|
|
}
|
|
|
|
return info.SetPath(path[0]).SetRequestMethod(requestMethod[0])
|
|
|
|
}
|