Skip to content

Commit cea17ca

Browse files
feat: add audit in context in connect interceptor (#1309)
1 parent 28cbd62 commit cea17ca

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

core/audit/context.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package audit
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/raystack/frontier/pkg/server/consts"
78
)
@@ -11,6 +12,7 @@ import (
1112
func GetService(ctx context.Context) *Service {
1213
u, ok := ctx.Value(consts.AuditServiceContextKey).(*Service)
1314
if !ok {
15+
fmt.Println("err: no audit log service found inside context")
1416
return NewService("default", NewNoopRepository(), NewNoopWebhookService())
1517
}
1618
return u

internal/api/v1beta1connect/organization.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,13 @@ func (h *ConnectHandler) CreateOrganization(ctx context.Context, request *connec
158158
return nil, connect.NewError(connect.CodeInternal, ErrInternalServerError)
159159
}
160160

161-
audit.GetAuditor(ctx, newOrg.ID).LogWithAttrs(audit.OrgCreatedEvent, audit.OrgTarget(newOrg.ID), map[string]string{
161+
if err := audit.GetAuditor(ctx, newOrg.ID).LogWithAttrs(audit.OrgCreatedEvent, audit.OrgTarget(newOrg.ID), map[string]string{
162162
"title": newOrg.Title,
163163
"name": newOrg.Name,
164-
})
164+
}); err != nil {
165+
errorLogger.LogServiceError(ctx, request, "CreateOrganization.AuditLog", err,
166+
zap.String("org_id", newOrg.ID))
167+
}
165168
return connect.NewResponse(&frontierv1beta1.CreateOrganizationResponse{Organization: orgPB}), nil
166169
}
167170

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package connectinterceptors
2+
3+
import (
4+
"context"
5+
6+
"connectrpc.com/connect"
7+
"github.com/raystack/frontier/core/audit"
8+
)
9+
10+
type AuditInterceptor struct {
11+
service *audit.Service
12+
}
13+
14+
func NewAuditInterceptor(service *audit.Service) *AuditInterceptor {
15+
return &AuditInterceptor{
16+
service: service,
17+
}
18+
}
19+
20+
func (a *AuditInterceptor) WrapUnary(next connect.UnaryFunc) connect.UnaryFunc {
21+
return connect.UnaryFunc(func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
22+
ctx = audit.SetContextWithService(ctx, a.service)
23+
return next(ctx, req)
24+
})
25+
}
26+
27+
func (a *AuditInterceptor) WrapStreamingClient(next connect.StreamingClientFunc) connect.StreamingClientFunc {
28+
return connect.StreamingClientFunc(func(ctx context.Context, spec connect.Spec) connect.StreamingClientConn {
29+
conn := next(ctx, spec)
30+
return conn
31+
})
32+
}
33+
34+
func (a *AuditInterceptor) WrapStreamingHandler(next connect.StreamingHandlerFunc) connect.StreamingHandlerFunc {
35+
return connect.StreamingHandlerFunc(func(ctx context.Context, conn connect.StreamingHandlerConn) error {
36+
ctx = audit.SetContextWithService(ctx, a.service)
37+
return next(ctx, conn)
38+
})
39+
}

pkg/server/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ func ServeConnect(ctx context.Context, logger log.Logger, cfg Config, deps api.D
197197
authNInterceptor := connectinterceptors.NewAuthenticationInterceptor(frontierService, cfg.Authentication.Session.Headers)
198198
authZInterceptor := connectinterceptors.NewAuthorizationInterceptor(frontierService)
199199
sessionInterceptor := connectinterceptors.NewSessionInterceptor(sessionCookieCutter, cfg.Authentication.Session, frontierService)
200+
auditInterceptor := connectinterceptors.NewAuditInterceptor(deps.AuditService)
200201

201202
interceptors := connect.WithInterceptors(
202203
otelInterceptor,
@@ -205,6 +206,7 @@ func ServeConnect(ctx context.Context, logger log.Logger, cfg Config, deps api.D
205206
sessionInterceptor,
206207
authNInterceptor,
207208
authZInterceptor,
209+
auditInterceptor,
208210
sessionInterceptor.UnaryConnectResponseInterceptor())
209211

210212
frontierPath, frontierHandler := frontierv1beta1connect.NewFrontierServiceHandler(frontierService, interceptors, connect.WithCodec(connectCodec{}))

0 commit comments

Comments
 (0)