# HG changeset patch # User Florent Aide <florent.aide@gmail.com> # Date 1741106098 -3600 # Tue Mar 04 17:34:58 2025 +0100 # Node ID b6ff4327d8a3d00a5ca91e933fabe503cc52b395 # Parent f6ef74a5e274bd9321aba96fddbbed628dd65a28 rendering engines receive a context containing the username diff --git a/lib/render.go b/lib/render.go --- a/lib/render.go +++ b/lib/render.go @@ -20,8 +20,11 @@ const LogRenderBatchSize = 1000 +type contextKey int + var ( - ErrInvalidInput = errors.New("invalid input") + userNameContextKey contextKey = 1 + ErrInvalidInput = errors.New("invalid input") renderPipelineDuration = prometheus.NewSummaryVec( prometheus.SummaryOpts{ @@ -228,7 +231,8 @@ reader := rendering.NewDocumentReader(document) writer := rendering.NewDocumentWriter(&output) - if err := pipeline.Render(ctx, writer, reader); err != nil { + userContext := SetUsername(ctx, username) + if err := pipeline.Render(userContext, writer, reader); err != nil { return nil, err } @@ -262,6 +266,28 @@ return result, nil } +// GetUsername allows to extract the username from a context +// this is intended to be used by engines to extract the username +// info. +func GetUsername(ctx context.Context) string { + v := ctx.Value(userNameContextKey) + if v == nil { + return "" + } + res, ok := v.(string) + if !ok { + panic("could not type assert on the userName from context") + } + + return res +} + +// SetUsername set the given username in the given context and returns +// a derived context. +func SetUsername(ctx context.Context, username string) context.Context { + return context.WithValue(ctx, userNameContextKey, username) +} + func init() { prometheus.MustRegister(renderPipelineDuration) }