18896: add locking. Also handle v1 tokens shorter than 5 characters.
authorWard Vandewege <ward@curii.com>
Fri, 25 Mar 2022 20:37:28 +0000 (16:37 -0400)
committerWard Vandewege <ward@curii.com>
Fri, 25 Mar 2022 20:37:40 +0000 (16:37 -0400)
Arvados-DCO-1.1-Signed-off-by: Ward Vandewege <ward@curii.com>

lib/controller/router/router.go
sdk/go/httpserver/logger.go

index e42d9a555b5993e00e2a7ce3eff29ab174ab0b44..05bdb4754f0a860ac552867d42bf6e30af9eb4d6 100644 (file)
@@ -597,7 +597,11 @@ func (rtr *router) addRoute(endpoint arvados.APIEndpoint, defaultOpts func() int
                                        tokenUUIDs = append(tokenUUIDs, tokenParts[1])
                                }
                        } else {
-                               tokenUUIDs = append(tokenUUIDs, "v1 token ending in "+t[len(t)-5:])
+                               end := t
+                               if len(t) > 5 {
+                                       end = t[len(t)-5:]
+                               }
+                               tokenUUIDs = append(tokenUUIDs, "v1 token ending in "+end)
                        }
                }
                httpserver.SetResponseLogFields(req.Context(), logrus.Fields{"tokenUUIDs": tokenUUIDs})
index 437429611cb1ee1d0a5db75be353865863985bb6..ef2ec417047bfae26c9ea0363c883abf2dacabed 100644 (file)
@@ -9,6 +9,7 @@ import (
        "context"
        "net"
        "net/http"
+       "sync"
        "time"
 
        "git.arvados.org/arvados.git/sdk/go/ctxlog"
@@ -23,6 +24,7 @@ type contextKey struct {
 var (
        requestTimeContextKey       = contextKey{"requestTime"}
        responseLogFieldsContextKey = contextKey{"responseLogFields"}
+       mutexContextKey             = contextKey{"mutex"}
 )
 
 type hijacker interface {
@@ -66,11 +68,18 @@ func HandlerWithDeadline(timeout time.Duration, next http.Handler) http.Handler
 }
 
 func SetResponseLogFields(ctx context.Context, fields logrus.Fields) {
-       ctxfields := ctx.Value(&responseLogFieldsContextKey)
-       if c, ok := ctxfields.(logrus.Fields); ok {
-               for k, v := range fields {
-                       c[k] = v
+       m := ctx.Value(&mutexContextKey)
+       if mutex, ok := m.(sync.Mutex); ok {
+               mutex.Lock()
+               defer mutex.Unlock()
+               ctxfields := ctx.Value(&responseLogFieldsContextKey)
+               if c, ok := ctxfields.(logrus.Fields); ok {
+                       for k, v := range fields {
+                               c[k] = v
+                       }
                }
+       } else {
+               // We can't lock, don't set the fields
        }
 }
 
@@ -92,6 +101,7 @@ func LogRequests(h http.Handler) http.Handler {
                ctx := req.Context()
                ctx = context.WithValue(ctx, &requestTimeContextKey, time.Now())
                ctx = context.WithValue(ctx, &responseLogFieldsContextKey, logrus.Fields{})
+               ctx = context.WithValue(ctx, &mutexContextKey, sync.Mutex{})
                ctx = ctxlog.Context(ctx, lgr)
                req = req.WithContext(ctx)