From 07bbc0716c8a5e105bbf96cf6965c03cd6f199aa Mon Sep 17 00:00:00 2001 From: Eric Leijonmarck Date: Mon, 16 Jan 2023 15:16:06 +0100 Subject: [PATCH] Auth: Fix correct error for updateapikey in context handler (#61544) * fix: correct error for updateapikey * refactor: send the correct err forward * update: based on review --- pkg/services/contexthandler/contexthandler.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/services/contexthandler/contexthandler.go b/pkg/services/contexthandler/contexthandler.go index b31a0e8b72a..aad500e1451 100644 --- a/pkg/services/contexthandler/contexthandler.go +++ b/pkg/services/contexthandler/contexthandler.go @@ -350,11 +350,17 @@ func (h *ContextHandler) initContextWithAPIKey(reqContext *models.ReqContext) bo return true } - // update api_key last used date - if err := h.apiKeyService.UpdateAPIKeyLastUsedDate(reqContext.Req.Context(), apikey.Id); err != nil { - reqContext.JsonApiErr(http.StatusInternalServerError, InvalidAPIKey, errKey) - return true - } + // non-blocking update api_key last used date + go func(id int64) { + defer func() { + if err := recover(); err != nil { + reqContext.Logger.Error("api key authentication panic", "err", err) + } + }() + if err := h.apiKeyService.UpdateAPIKeyLastUsedDate(context.Background(), id); err != nil { + reqContext.Logger.Warn("failed to update last use date for api key", "id", id) + } + }(apikey.Id) if apikey.ServiceAccountId == nil || *apikey.ServiceAccountId < 1 { //There is no service account attached to the apikey // Use the old APIkey method. This provides backwards compatibility.