Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include JWT sub field in logs #1103

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cmds/core-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ func RunHTTPServer(ctx context.Context, ctxCanceler func(), address, locality st
multiRouter.Routers = append(multiRouter.Routers, &scdV1Router)
}

handler := logging.HTTPMiddleware(logger, *dumpRequests,
healthyEndpointMiddleware(logger,
&multiRouter,
))
handler := logging.HTTPMiddleware(logger, *dumpRequests, healthyEndpointMiddleware(logger, &multiRouter))

handler = auth.DecoderMiddleware(authorizer, handler)

httpServer := &http.Server{
Addr: address,
Expand Down
107 changes: 88 additions & 19 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,42 @@ func (a *Authorizer) setKeys(keys []interface{}) {
// Authorize extracts and verifies bearer tokens from a http.Request.
func (a *Authorizer) Authorize(_ http.ResponseWriter, r *http.Request, authOptions []api.AuthorizationOption) api.AuthorizationResult {

tknStr, ok := getToken(r)
if !ok {
return api.AuthorizationResult{Error: stacktrace.NewErrorWithCode(dsserr.Unauthenticated, "Missing access token")}
}

a.keyGuard.RLock()
keys := a.keys
a.keyGuard.RUnlock()
missing := false
validated := false
var err error
var keyClaims claims
keyClaims := claims{}
err := error(nil)
ok := false

// if the decoding middleware wasn't triggered, attempt decoding
if _, ok = r.Context().Value("authDecoded").(bool); !ok {
missing, validated, keyClaims, err = a.extractClaims(r)
} else {
// Use previously decoded values
missing, ok = r.Context().Value("authTokenMissing").(bool)
if !ok {
missing = true
}

for _, key := range keys {
keyClaims = claims{}
key := key
_, err = jwt.ParseWithClaims(tknStr, &keyClaims, func(token *jwt.Token) (interface{}, error) {
return key, nil
})
if err == nil {
validated = true
break
validated, ok = r.Context().Value("authValidated").(bool)
if !ok {
validated = false
}

err, ok = r.Context().Value("authError").(error)
if !ok {
err = nil
}

keyClaims, ok = r.Context().Value("authClaims").(claims)
if !ok {
keyClaims = claims{}
}
}

if missing {
return api.AuthorizationResult{Error: stacktrace.NewErrorWithCode(dsserr.Unauthenticated, "Missing access token")}
}

if !validated {
return api.AuthorizationResult{Error: stacktrace.PropagateWithCode(err, dsserr.Unauthenticated, "Access token validation failed")}
}
Expand Down Expand Up @@ -300,3 +313,59 @@ func getToken(r *http.Request) (string, bool) {
}
return authHeader[7:], true
}

// Extract valid claims from a JWT token.
func (a *Authorizer) extractClaims(r *http.Request) (missing bool, validated bool, keyClaims claims, err error) {
tknStr, ok := getToken(r)

if !ok {
return true, false, claims{}, nil
}

a.keyGuard.RLock()
keys := a.keys
a.keyGuard.RUnlock()
validated = false
missing = false

for _, key := range keys {
keyClaims = claims{}
key := key
_, err = jwt.ParseWithClaims(tknStr, &keyClaims, func(token *jwt.Token) (interface{}, error) {
return key, nil
})
if err == nil {
validated = true
break
}
}

return missing, validated, keyClaims, err

}

// Decoder Middleware
func DecoderMiddleware(a *Authorizer, handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

missing, validated, keyClaims, err := a.extractClaims(r)

ctx := context.WithValue(r.Context(), "authValidated", validated)
ctx = context.WithValue(ctx, "authTokenMissing", missing)
ctx = context.WithValue(ctx, "authClaims", keyClaims)
ctx = context.WithValue(ctx, "authError", err)

if validated && err == nil {
// If the token is valid, we can extract the subject from the token and add them to the context.
ctx = context.WithValue(ctx, "authSubject", keyClaims.Subject)
}

// Add a flag to the context to indicate that the token was decoded
ctx = context.WithValue(ctx, "authDecoded", true)

// Create a new request with the updated context
r = r.WithContext(ctx)

handler.ServeHTTP(w, r)
})
}
6 changes: 6 additions & 0 deletions pkg/logging/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ func HTTPMiddleware(logger *zap.Logger, dump bool, handler http.Handler) http.Ha
// replace req.Body with a copy
r.Body = io.NopCloser(bytes.NewReader(reqData))
}

subject, ok := r.Context().Value("authSubject").(string)
if !ok {
subject = ""
}
logger = logger.With(zap.String("req_sub", subject))
}

handler.ServeHTTP(trw, r)
Expand Down
Loading