diff --git a/lib/render.go b/lib/render.go index f6ef74a5e274bd9321aba96fddbbed628dd65a28_bGliL3JlbmRlci5nbw==..b6ff4327d8a3d00a5ca91e933fabe503cc52b395_bGliL3JlbmRlci5nbw== 100644 --- a/lib/render.go +++ b/lib/render.go @@ -20,4 +20,6 @@ const LogRenderBatchSize = 1000 +type contextKey int + var ( @@ -23,5 +25,6 @@ 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) }