Authn: error logs (#76264)

* Reduce to debug for session need rotation error

* try to extract log level from error and fallback to warning
This commit is contained in:
Karl Persson
2023-10-10 16:30:20 +02:00
committed by GitHub
parent 9dd38de5c1
commit ceb6f8b409
2 changed files with 15 additions and 9 deletions

View File

@@ -226,17 +226,12 @@ func (s *Service) authenticate(ctx context.Context, c authn.Client, r *authn.Req
r.OrgID = orgIDFromRequest(r) r.OrgID = orgIDFromRequest(r)
identity, err := c.Authenticate(ctx, r) identity, err := c.Authenticate(ctx, r)
if err != nil { if err != nil {
log := s.log.FromContext(ctx).Warn s.errorLogFunc(ctx, err)("Failed to authenticate request", "client", c.Name(), "error", err)
if errors.Is(err, authn.ErrTokenNeedsRotation) {
log = s.log.FromContext(ctx).Debug
}
log("Failed to authenticate request", "client", c.Name(), "error", err)
return nil, err return nil, err
} }
if err := s.runPostAuthHooks(ctx, identity, r); err != nil { if err := s.runPostAuthHooks(ctx, identity, r); err != nil {
s.log.FromContext(ctx).Warn("Failed to run post auth hook", "client", c.Name(), "id", identity.ID, "error", err) s.errorLogFunc(ctx, err)("Failed to run post auth hook", "client", c.Name(), "id", identity.ID, "error", err)
return nil, err return nil, err
} }
@@ -246,7 +241,7 @@ func (s *Service) authenticate(ctx context.Context, c authn.Client, r *authn.Req
if hc, ok := c.(authn.HookClient); ok { if hc, ok := c.(authn.HookClient); ok {
if err := hc.Hook(ctx, identity, r); err != nil { if err := hc.Hook(ctx, identity, r); err != nil {
s.log.FromContext(ctx).Warn("Failed to run post client auth hook", "client", c.Name(), "id", identity.ID, "error", err) s.errorLogFunc(ctx, err)("Failed to run post client auth hook", "client", c.Name(), "id", identity.ID, "error", err)
return nil, err return nil, err
} }
} }
@@ -355,6 +350,17 @@ func (s *Service) SyncIdentity(ctx context.Context, identity *authn.Identity) er
return s.runPostAuthHooks(ctx, identity, r) return s.runPostAuthHooks(ctx, identity, r)
} }
func (s *Service) errorLogFunc(ctx context.Context, err error) func(msg string, ctx ...any) {
l := s.log.FromContext(ctx)
var grfErr errutil.Error
if errors.As(err, &grfErr) {
return grfErr.LogLevel.LogFunc(l)
}
return l.Warn
}
func orgIDFromRequest(r *authn.Request) int64 { func orgIDFromRequest(r *authn.Request) int64 {
if r.HTTPRequest == nil { if r.HTTPRequest == nil {
return 0 return 0

View File

@@ -3,7 +3,7 @@ package authn
import "github.com/grafana/grafana/pkg/util/errutil" import "github.com/grafana/grafana/pkg/util/errutil"
var ( var (
ErrTokenNeedsRotation = errutil.Unauthorized("session.token.rotate") ErrTokenNeedsRotation = errutil.Unauthorized("session.token.rotate", errutil.WithLogLevel(errutil.LevelDebug))
ErrUnsupportedClient = errutil.BadRequest("auth.client.unsupported") ErrUnsupportedClient = errutil.BadRequest("auth.client.unsupported")
ErrClientNotConfigured = errutil.BadRequest("auth.client.notConfigured") ErrClientNotConfigured = errutil.BadRequest("auth.client.notConfigured")
ErrUnsupportedIdentity = errutil.NotImplemented("auth.identity.unsupported") ErrUnsupportedIdentity = errutil.NotImplemented("auth.identity.unsupported")