Commit 2bf964c6 by Arve Knudsen Committed by GitHub

Chore: Fix linting issues caught by ruleguard (#28799)

* Chore: Fix linting issues caught by ruleguard

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Improve error check

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent 967e9b39
...@@ -3,6 +3,7 @@ package bus ...@@ -3,6 +3,7 @@ package bus
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"reflect" "reflect"
) )
...@@ -140,9 +141,13 @@ func (b *InProcBus) Publish(msg Msg) error { ...@@ -140,9 +141,13 @@ func (b *InProcBus) Publish(msg Msg) error {
for _, listenerHandler := range listeners { for _, listenerHandler := range listeners {
ret := reflect.ValueOf(listenerHandler).Call(params) ret := reflect.ValueOf(listenerHandler).Call(params)
err := ret[0].Interface() e := ret[0].Interface()
if err != nil { if e != nil {
return err.(error) err, ok := e.(error)
if ok {
return err
}
return fmt.Errorf("expected listener to return an error, got '%T'", e)
} }
} }
......
...@@ -15,11 +15,11 @@ var header = setting.AuthProxyHeaderName ...@@ -15,11 +15,11 @@ var header = setting.AuthProxyHeaderName
func logUserIn(auth *authproxy.AuthProxy, username string, logger log.Logger, ignoreCache bool) (int64, *authproxy.Error) { func logUserIn(auth *authproxy.AuthProxy, username string, logger log.Logger, ignoreCache bool) (int64, *authproxy.Error) {
logger.Debug("Trying to log user in", "username", username, "ignoreCache", ignoreCache) logger.Debug("Trying to log user in", "username", username, "ignoreCache", ignoreCache)
// Try to log in user via various providers // Try to log in user via various providers
id, err := auth.Login(logger, ignoreCache) id, e := auth.Login(logger, ignoreCache)
if err != nil { if e != nil {
logger.Error("Failed to login", "username", username, "message", err.Error(), "error", err.DetailsError, logger.Error("Failed to login", "username", username, "message", e.Error(), "error", e.DetailsError,
"ignoreCache", ignoreCache) "ignoreCache", ignoreCache)
return 0, err return 0, e
} }
return id, nil return id, nil
} }
...@@ -55,16 +55,16 @@ func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqCon ...@@ -55,16 +55,16 @@ func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqCon
return true return true
} }
id, err := logUserIn(auth, username, logger, false) id, e := logUserIn(auth, username, logger, false)
if err != nil { if e != nil {
ctx.Handle(407, err.Error(), err.DetailsError) ctx.Handle(407, e.Error(), e.DetailsError)
return true return true
} }
logger.Debug("Got user ID, getting full user info", "userID", id) logger.Debug("Got user ID, getting full user info", "userID", id)
user, err := auth.GetSignedUser(id) user, e := auth.GetSignedUser(id)
if err != nil { if e != nil {
// The reason we couldn't find the user corresponding to the ID might be that the ID was found from a stale // The reason we couldn't find the user corresponding to the ID might be that the ID was found from a stale
// cache entry. For example, if a user is deleted via the API, corresponding cache entries aren't invalidated // cache entry. For example, if a user is deleted via the API, corresponding cache entries aren't invalidated
// because cache keys are computed from request header values and not just the user ID. Meaning that // because cache keys are computed from request header values and not just the user ID. Meaning that
...@@ -76,15 +76,15 @@ func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqCon ...@@ -76,15 +76,15 @@ func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqCon
logger.Error("Got unexpected error when removing user from auth cache", "error", err) logger.Error("Got unexpected error when removing user from auth cache", "error", err)
} }
} }
id, err = logUserIn(auth, username, logger, true) id, e = logUserIn(auth, username, logger, true)
if err != nil { if e != nil {
ctx.Handle(407, err.Error(), err.DetailsError) ctx.Handle(407, e.Error(), e.DetailsError)
return true return true
} }
user, err = auth.GetSignedUser(id) user, e = auth.GetSignedUser(id)
if err != nil { if e != nil {
ctx.Handle(407, err.Error(), err.DetailsError) ctx.Handle(407, e.Error(), e.DetailsError)
return true return true
} }
} }
...@@ -96,14 +96,14 @@ func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqCon ...@@ -96,14 +96,14 @@ func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqCon
ctx.IsSignedIn = true ctx.IsSignedIn = true
// Remember user data in cache // Remember user data in cache
if err := auth.Remember(id); err != nil { if e := auth.Remember(id); e != nil {
logger.Error( logger.Error(
"Failed to store user in cache", "Failed to store user in cache",
"username", username, "username", username,
"message", err.Error(), "message", e.Error(),
"error", err.DetailsError, "error", e.DetailsError,
) )
ctx.Handle(500, err.Error(), err.DetailsError) ctx.Handle(500, e.Error(), e.DetailsError)
return true return true
} }
......
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"hash/fnv" "hash/fnv"
"net" "net"
"net/mail" "net/mail"
"path"
"reflect" "reflect"
"strings" "strings"
"time" "time"
...@@ -179,12 +180,12 @@ func (auth *AuthProxy) Login(logger log.Logger, ignoreCache bool) (int64, *Error ...@@ -179,12 +180,12 @@ func (auth *AuthProxy) Login(logger log.Logger, ignoreCache bool) (int64, *Error
} }
if isLDAPEnabled() { if isLDAPEnabled() {
id, err := auth.LoginViaLDAP() id, e := auth.LoginViaLDAP()
if err != nil { if e != nil {
if err == ldap.ErrInvalidCredentials { if e == ldap.ErrInvalidCredentials {
return 0, newError("proxy authentication required", ldap.ErrInvalidCredentials) return 0, newError("proxy authentication required", ldap.ErrInvalidCredentials)
} }
return 0, newError("failed to get the user", err) return 0, newError("failed to get the user", e)
} }
return id, nil return id, nil
...@@ -347,7 +348,7 @@ func (auth *AuthProxy) Remember(id int64) *Error { ...@@ -347,7 +348,7 @@ func (auth *AuthProxy) Remember(id int64) *Error {
func coerceProxyAddress(proxyAddr string) (*net.IPNet, error) { func coerceProxyAddress(proxyAddr string) (*net.IPNet, error) {
proxyAddr = strings.TrimSpace(proxyAddr) proxyAddr = strings.TrimSpace(proxyAddr)
if !strings.Contains(proxyAddr, "/") { if !strings.Contains(proxyAddr, "/") {
proxyAddr = strings.Join([]string{proxyAddr, "32"}, "/") proxyAddr = path.Join(proxyAddr, "32")
} }
_, network, err := net.ParseCIDR(proxyAddr) _, network, err := net.ParseCIDR(proxyAddr)
......
...@@ -90,7 +90,7 @@ func (m *msSqlMacroEngine) evaluateMacro(name string, args []string) (string, er ...@@ -90,7 +90,7 @@ func (m *msSqlMacroEngine) evaluateMacro(name string, args []string) (string, er
case "__timeGroupAlias": case "__timeGroupAlias":
tg, err := m.evaluateMacro("__timeGroup", args) tg, err := m.evaluateMacro("__timeGroup", args)
if err == nil { if err == nil {
return tg + " AS [time]", err return tg + " AS [time]", nil
} }
return "", err return "", err
case "__unixEpochFilter": case "__unixEpochFilter":
...@@ -125,7 +125,7 @@ func (m *msSqlMacroEngine) evaluateMacro(name string, args []string) (string, er ...@@ -125,7 +125,7 @@ func (m *msSqlMacroEngine) evaluateMacro(name string, args []string) (string, er
case "__unixEpochGroupAlias": case "__unixEpochGroupAlias":
tg, err := m.evaluateMacro("__unixEpochGroup", args) tg, err := m.evaluateMacro("__unixEpochGroup", args)
if err == nil { if err == nil {
return tg + " AS [time]", err return tg + " AS [time]", nil
} }
return "", err return "", err
default: default:
......
...@@ -96,7 +96,7 @@ func (m *mySqlMacroEngine) evaluateMacro(name string, args []string) (string, er ...@@ -96,7 +96,7 @@ func (m *mySqlMacroEngine) evaluateMacro(name string, args []string) (string, er
case "__timeGroupAlias": case "__timeGroupAlias":
tg, err := m.evaluateMacro("__timeGroup", args) tg, err := m.evaluateMacro("__timeGroup", args)
if err == nil { if err == nil {
return tg + " AS \"time\"", err return tg + " AS \"time\"", nil
} }
return "", err return "", err
case "__unixEpochFilter": case "__unixEpochFilter":
...@@ -131,7 +131,7 @@ func (m *mySqlMacroEngine) evaluateMacro(name string, args []string) (string, er ...@@ -131,7 +131,7 @@ func (m *mySqlMacroEngine) evaluateMacro(name string, args []string) (string, er
case "__unixEpochGroupAlias": case "__unixEpochGroupAlias":
tg, err := m.evaluateMacro("__unixEpochGroup", args) tg, err := m.evaluateMacro("__unixEpochGroup", args)
if err == nil { if err == nil {
return tg + " AS \"time\"", err return tg + " AS \"time\"", nil
} }
return "", err return "", err
default: default:
......
...@@ -120,7 +120,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string, ...@@ -120,7 +120,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
case "__timeGroupAlias": case "__timeGroupAlias":
tg, err := m.evaluateMacro("__timeGroup", args) tg, err := m.evaluateMacro("__timeGroup", args)
if err == nil { if err == nil {
return tg + " AS \"time\"", err return tg + " AS \"time\"", nil
} }
return "", err return "", err
case "__unixEpochFilter": case "__unixEpochFilter":
...@@ -155,7 +155,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string, ...@@ -155,7 +155,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string,
case "__unixEpochGroupAlias": case "__unixEpochGroupAlias":
tg, err := m.evaluateMacro("__unixEpochGroup", args) tg, err := m.evaluateMacro("__unixEpochGroup", args)
if err == nil { if err == nil {
return tg + " AS \"time\"", err return tg + " AS \"time\"", nil
} }
return "", err return "", err
default: default:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment