2015-05-02 05:06:58 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2020-06-17 11:43:16 -05:00
|
|
|
"errors"
|
|
|
|
|
2019-08-20 12:13:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-04-08 06:31:46 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/remotecache"
|
2019-04-16 07:09:18 -05:00
|
|
|
authproxy "github.com/grafana/grafana/pkg/middleware/auth_proxy"
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-08-20 12:13:27 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-05-02 05:06:58 -05:00
|
|
|
)
|
|
|
|
|
2019-08-20 12:13:27 -05:00
|
|
|
var header = setting.AuthProxyHeaderName
|
|
|
|
|
2020-06-17 11:43:16 -05:00
|
|
|
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)
|
|
|
|
// Try to log in user via various providers
|
2020-11-17 04:27:45 -06:00
|
|
|
id, e := auth.Login(logger, ignoreCache)
|
|
|
|
if e != nil {
|
|
|
|
logger.Error("Failed to login", "username", username, "message", e.Error(), "error", e.DetailsError,
|
2020-06-17 11:43:16 -05:00
|
|
|
"ignoreCache", ignoreCache)
|
2020-11-17 04:27:45 -06:00
|
|
|
return 0, e
|
2020-06-17 11:43:16 -05:00
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
func initContextWithAuthProxy(store *remotecache.RemoteCache, ctx *models.ReqContext, orgID int64) bool {
|
2019-08-20 12:13:27 -05:00
|
|
|
username := ctx.Req.Header.Get(header)
|
2019-04-16 07:09:18 -05:00
|
|
|
auth := authproxy.New(&authproxy.Options{
|
|
|
|
Store: store,
|
|
|
|
Ctx: ctx,
|
|
|
|
OrgID: orgID,
|
|
|
|
})
|
|
|
|
|
2019-08-20 12:13:27 -05:00
|
|
|
logger := log.New("auth.proxy")
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Bail if auth proxy is not enabled
|
2019-05-14 02:18:28 -05:00
|
|
|
if !auth.IsEnabled() {
|
2015-05-02 05:06:58 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:16 -05:00
|
|
|
// If there is no header - we can't move forward
|
2019-05-14 02:18:28 -05:00
|
|
|
if !auth.HasHeader() {
|
2015-05-02 05:06:58 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Check if allowed to continue with this IP
|
2019-05-14 02:18:28 -05:00
|
|
|
if result, err := auth.IsAllowedIP(); !result {
|
2019-08-20 12:13:27 -05:00
|
|
|
logger.Error(
|
|
|
|
"Failed to check whitelisted IP addresses",
|
|
|
|
"message", err.Error(),
|
|
|
|
"error", err.DetailsError,
|
|
|
|
)
|
2019-04-16 07:09:18 -05:00
|
|
|
ctx.Handle(407, err.Error(), err.DetailsError)
|
2016-02-23 07:22:28 -06:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:27:45 -06:00
|
|
|
id, e := logUserIn(auth, username, logger, false)
|
|
|
|
if e != nil {
|
|
|
|
ctx.Handle(407, e.Error(), e.DetailsError)
|
2019-04-16 07:09:18 -05:00
|
|
|
return true
|
2018-04-16 15:17:01 -05:00
|
|
|
}
|
2018-03-22 16:02:34 -05:00
|
|
|
|
2020-06-17 11:43:16 -05:00
|
|
|
logger.Debug("Got user ID, getting full user info", "userID", id)
|
|
|
|
|
2020-11-17 04:27:45 -06:00
|
|
|
user, e := auth.GetSignedUser(id)
|
|
|
|
if e != nil {
|
2020-06-17 11:43:16 -05:00
|
|
|
// 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
|
|
|
|
// because cache keys are computed from request header values and not just the user ID. Meaning that
|
|
|
|
// we can't easily derive cache keys to invalidate when deleting a user. To work around this, we try to
|
|
|
|
// log the user in again without the cache.
|
|
|
|
logger.Debug("Failed to get user info given ID, retrying without cache", "userID", id)
|
|
|
|
if err := auth.RemoveUserFromCache(logger); err != nil {
|
|
|
|
if !errors.Is(err, remotecache.ErrCacheItemNotFound) {
|
|
|
|
logger.Error("Got unexpected error when removing user from auth cache", "error", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 04:27:45 -06:00
|
|
|
id, e = logUserIn(auth, username, logger, true)
|
|
|
|
if e != nil {
|
|
|
|
ctx.Handle(407, e.Error(), e.DetailsError)
|
2020-06-17 11:43:16 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:27:45 -06:00
|
|
|
user, e = auth.GetSignedUser(id)
|
|
|
|
if e != nil {
|
|
|
|
ctx.Handle(407, e.Error(), e.DetailsError)
|
2020-06-17 11:43:16 -05:00
|
|
|
return true
|
|
|
|
}
|
2016-02-23 07:22:28 -06:00
|
|
|
}
|
|
|
|
|
2020-06-17 11:43:16 -05:00
|
|
|
logger.Debug("Successfully got user info", "userID", user.UserId, "username", user.Login)
|
|
|
|
|
2019-04-16 07:09:18 -05:00
|
|
|
// Add user info to context
|
|
|
|
ctx.SignedInUser = user
|
|
|
|
ctx.IsSignedIn = true
|
2016-02-23 07:22:28 -06:00
|
|
|
|
2020-06-17 11:43:16 -05:00
|
|
|
// Remember user data in cache
|
2020-11-17 04:27:45 -06:00
|
|
|
if e := auth.Remember(id); e != nil {
|
2019-08-20 12:13:27 -05:00
|
|
|
logger.Error(
|
|
|
|
"Failed to store user in cache",
|
|
|
|
"username", username,
|
2020-11-17 04:27:45 -06:00
|
|
|
"message", e.Error(),
|
|
|
|
"error", e.DetailsError,
|
2019-08-20 12:13:27 -05:00
|
|
|
)
|
2020-11-17 04:27:45 -06:00
|
|
|
ctx.Handle(500, e.Error(), e.DetailsError)
|
2019-04-16 07:09:18 -05:00
|
|
|
return true
|
2016-02-23 07:22:28 -06:00
|
|
|
}
|
|
|
|
|
2015-05-02 05:06:58 -05:00
|
|
|
return true
|
|
|
|
}
|