Skip to content

Commit 7c4172e

Browse files
zeripathlunny
andauthored
Pass down SignedUserName down to AccessLogger context (#16605)
* Pass down SignedUserName down to AccessLogger context Unfortunately when the AccessLogger was moved back before the contexters the SignedUserName reporting was lost. This is due to Request.WithContext leading to a shallow copy of the Request and the modules/context/Context being within that request. This PR adds a new context variable of a string pointer which is set and handled in the contexters. Fix #16600 Signed-off-by: Andrew Thornton <[email protected]> * handle nil ptr issue Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent f03abe8 commit 7c4172e

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

modules/context/access_log.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package context
66

77
import (
88
"bytes"
9+
"context"
910
"html/template"
1011
"net/http"
1112
"time"
@@ -22,18 +23,19 @@ type routerLoggerOptions struct {
2223
Ctx map[string]interface{}
2324
}
2425

26+
var signedUserNameStringPointerKey interface{} = "signedUserNameStringPointerKey"
27+
2528
// AccessLogger returns a middleware to log access logger
2629
func AccessLogger() func(http.Handler) http.Handler {
2730
logger := log.GetLogger("access")
2831
logTemplate, _ := template.New("log").Parse(setting.AccessLogTemplate)
2932
return func(next http.Handler) http.Handler {
3033
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
3134
start := time.Now()
32-
next.ServeHTTP(w, req)
3335
identity := "-"
34-
if val := SignedUserName(req); val != "" {
35-
identity = val
36-
}
36+
r := req.WithContext(context.WithValue(req.Context(), signedUserNameStringPointerKey, &identity))
37+
38+
next.ServeHTTP(w, r)
3739
rw := w.(ResponseWriter)
3840

3941
buf := bytes.NewBuffer([]byte{})

modules/context/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ func APIContexter() func(http.Handler) http.Handler {
275275
ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())
276276

277277
next.ServeHTTP(ctx.Resp, ctx.Req)
278+
279+
// Handle adding signedUserName to the context for the AccessLogger
280+
usernameInterface := ctx.Data["SignedUserName"]
281+
identityPtrInterface := ctx.Req.Context().Value(signedUserNameStringPointerKey)
282+
if usernameInterface != nil && identityPtrInterface != nil {
283+
username := usernameInterface.(string)
284+
identityPtr := identityPtrInterface.(*string)
285+
if identityPtr != nil && username != "" {
286+
*identityPtr = username
287+
}
288+
}
278289
})
279290
}
280291
}

modules/context/context.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,17 @@ func Contexter() func(next http.Handler) http.Handler {
770770
}
771771

772772
next.ServeHTTP(ctx.Resp, ctx.Req)
773+
774+
// Handle adding signedUserName to the context for the AccessLogger
775+
usernameInterface := ctx.Data["SignedUserName"]
776+
identityPtrInterface := ctx.Req.Context().Value(signedUserNameStringPointerKey)
777+
if usernameInterface != nil && identityPtrInterface != nil {
778+
username := usernameInterface.(string)
779+
identityPtr := identityPtrInterface.(*string)
780+
if identityPtr != nil && username != "" {
781+
*identityPtr = username
782+
}
783+
}
773784
})
774785
}
775786
}

0 commit comments

Comments
 (0)