2019-11-29 12:59:40 +01:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2017-01-13 13:53:37 -05:00
package app
import (
MM-30882: Fix read-after-write issue for demoting user (#16911)
* MM-30882: Fix read-after-write issue for demoting user
In (*App).DemoteUserToGuest, we would demote a user, and then immediately
read it back to do future operations from the user. This reading back
of the user had the effect of sticking the old value into the cache
after which it would never be updated.
There was another issue along with this, which was when the invalidation
message would broadcast across the cluster, it would hit the cache invalidation
problem where an unrelated store call would miss the cache because
it was invalidated, and then again read from replica and stick the old value.
To fix all these, we return the new value directly from the store method
to avoid having the app to read it again.
And we add a map in the localcache layer which tracks invalidations made,
and then switch to use master if it's true.
The core change is fairly limited, but due to changing the store method signatures,
a lot of code needed to be updated to pass "context.Background". Therefore the PR
just "appears" to be big, but the main changes are limited to app/user.go,
sqlstore/user_store.go and user_layer.go
https://mattermost.atlassian.net/browse/MM-30882
```release-note
Fix an issue where demoting a user to guest would not take effect in
an environment with read replicas.
```
* Fix concurrent map access
* Fixing mistakes
* fix tests
2021-02-12 19:04:05 +05:30
"context"
2020-07-15 09:26:28 -04:00
"errors"
2020-05-06 15:41:10 -04:00
"math"
2017-04-24 12:40:17 -04:00
"net/http"
2020-11-23 14:34:10 -05:00
"os"
2017-04-24 12:40:17 -04:00
2021-08-24 10:10:40 +03:00
"github.com/mattermost/mattermost-server/v6/app/users"
2021-07-22 12:21:47 +05:30
"github.com/mattermost/mattermost-server/v6/audit"
"github.com/mattermost/mattermost-server/v6/model"
"github.com/mattermost/mattermost-server/v6/shared/mlog"
"github.com/mattermost/mattermost-server/v6/store"
2017-01-13 13:53:37 -05:00
)
2017-09-06 17:12:54 -05:00
func ( a * App ) CreateSession ( session * model . Session ) ( * model . Session , * model . AppError ) {
2021-06-14 18:08:00 +03:00
session , err := a . srv . userService . CreateSession ( session )
2019-05-30 12:10:24 -04:00
if err != nil {
2020-07-15 09:26:28 -04:00
var invErr * store . ErrInvalidInput
switch {
case errors . As ( err , & invErr ) :
return nil , model . NewAppError ( "CreateSession" , "app.session.save.existing.app_error" , nil , invErr . Error ( ) , http . StatusBadRequest )
default :
return nil , model . NewAppError ( "CreateSession" , "app.session.save.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
}
2018-10-16 00:33:22 +05:30
}
2017-01-19 09:00:13 -05:00
2018-10-16 00:33:22 +05:30
return session , nil
2017-01-19 09:00:13 -05:00
}
2020-11-23 14:34:10 -05:00
func ( a * App ) GetCloudSession ( token string ) ( * model . Session , * model . AppError ) {
apiKey := os . Getenv ( "MM_CLOUD_API_KEY" )
if apiKey != "" && apiKey == token {
// Need a bare-bones session object for later checks
session := & model . Session {
Token : token ,
IsOAuth : false ,
}
2021-07-12 20:05:36 +02:00
session . AddProp ( model . SessionPropType , model . SessionTypeCloudKey )
2020-11-23 14:34:10 -05:00
return session , nil
}
return nil , model . NewAppError ( "GetCloudSession" , "api.context.invalid_token.error" , map [ string ] interface { } { "Token" : token , "Error" : "" } , "The provided token is invalid" , http . StatusUnauthorized )
}
2021-04-01 13:44:56 -04:00
func ( a * App ) GetRemoteClusterSession ( token string , remoteId string ) ( * model . Session , * model . AppError ) {
rc , appErr := a . GetRemoteCluster ( remoteId )
if appErr == nil && rc . Token == token {
// Need a bare-bones session object for later checks
session := & model . Session {
Token : token ,
IsOAuth : false ,
}
2021-07-12 20:05:36 +02:00
session . AddProp ( model . SessionPropType , model . SessionTypeRemoteclusterToken )
2021-04-01 13:44:56 -04:00
return session , nil
}
return nil , model . NewAppError ( "GetRemoteClusterSession" , "api.context.invalid_token.error" , map [ string ] interface { } { "Token" : token , "Error" : "" } , "The provided token is invalid" , http . StatusUnauthorized )
}
2017-09-06 17:12:54 -05:00
func ( a * App ) GetSession ( token string ) ( * model . Session , * model . AppError ) {
2021-06-18 19:25:03 +03:00
var session * model . Session
// We intentionally skip the error check here, we only want to check if the token is valid.
// If we don't have the session we are going to create one with the token eventually.
if session , _ = a . srv . userService . GetSession ( token ) ; session != nil {
if session . Token != token {
return nil , model . NewAppError ( "GetSession" , "api.context.invalid_token.error" , map [ string ] interface { } { "Token" : token , "Error" : "" } , "session token is different from the one in DB" , http . StatusUnauthorized )
}
2017-07-31 12:59:32 -04:00
2021-06-18 19:25:03 +03:00
if ! session . IsExpired ( ) {
a . srv . userService . AddSessionToCache ( session )
2017-01-13 13:53:37 -05:00
}
}
2021-06-14 18:08:00 +03:00
var appErr * model . AppError
2021-01-28 05:58:24 +11:00
if session == nil || session . Id == "" {
2021-06-14 18:08:00 +03:00
session , appErr = a . createSessionForUserAccessToken ( token )
if appErr != nil {
2018-04-20 08:44:01 -04:00
detailedError := ""
statusCode := http . StatusUnauthorized
2021-06-14 18:08:00 +03:00
if appErr . Id != "app.user_access_token.invalid_or_missing" {
detailedError = appErr . Error ( )
statusCode = appErr . StatusCode
2020-08-21 21:07:07 +05:30
} else {
2021-06-14 18:08:00 +03:00
mlog . Warn ( "Error while creating session for user access token" , mlog . Err ( appErr ) )
2018-04-20 08:44:01 -04:00
}
2020-08-21 21:07:07 +05:30
return nil , model . NewAppError ( "GetSession" , "api.context.invalid_token.error" , map [ string ] interface { } { "Token" : token , "Error" : detailedError } , "" , statusCode )
2017-07-31 12:59:32 -04:00
}
}
2021-01-28 05:58:24 +11:00
if session . Id == "" || session . IsExpired ( ) {
2020-08-21 21:07:07 +05:30
return nil , model . NewAppError ( "GetSession" , "api.context.invalid_token.error" , map [ string ] interface { } { "Token" : token , "Error" : "" } , "session is either nil or expired" , http . StatusUnauthorized )
2017-01-13 13:53:37 -05:00
}
2019-11-04 13:47:59 +01:00
if * a . Config ( ) . ServiceSettings . SessionIdleTimeoutInMinutes > 0 &&
2020-05-19 12:26:54 -04:00
! session . IsOAuth && ! session . IsMobileApp ( ) &&
2021-07-12 20:05:36 +02:00
session . Props [ model . SessionPropType ] != model . SessionTypeUserAccessToken &&
2020-05-06 15:41:10 -04:00
! * a . Config ( ) . ServiceSettings . ExtendSessionLengthWithActivity {
2017-09-28 09:04:52 -04:00
2017-10-18 15:36:43 -07:00
timeout := int64 ( * a . Config ( ) . ServiceSettings . SessionIdleTimeoutInMinutes ) * 1000 * 60
2019-03-15 10:44:27 -07:00
if ( model . GetMillis ( ) - session . LastActivityAt ) > timeout {
2020-08-19 23:27:48 +05:30
// Revoking the session is an asynchronous task anyways since we are not checking
// for the return value of the call before returning the error.
// So moving this to a goroutine has 2 advantages:
// 1. We are treating this as a proper asynchronous task.
// 2. This also fixes a race condition in the web hub, where GetSession
// gets called from (*WebConn).isMemberOfTeam and revoking a session involves
// clearing the webconn cache, which needs the hub again.
a . Srv ( ) . Go ( func ( ) {
err := a . RevokeSessionById ( session . Id )
if err != nil {
mlog . Warn ( "Error while revoking session" , mlog . Err ( err ) )
}
} )
2020-08-21 21:07:07 +05:30
return nil , model . NewAppError ( "GetSession" , "api.context.invalid_token.error" , map [ string ] interface { } { "Token" : token , "Error" : "" } , "idle timeout" , http . StatusUnauthorized )
2017-09-28 09:04:52 -04:00
}
}
2017-01-13 13:53:37 -05:00
return session , nil
}
2021-02-05 11:22:27 +01:00
func ( a * App ) GetSessions ( userID string ) ( [ ] * model . Session , * model . AppError ) {
2021-06-18 19:25:03 +03:00
sessions , err := a . srv . userService . GetSessions ( userID )
2020-07-15 09:26:28 -04:00
if err != nil {
return nil , model . NewAppError ( "GetSessions" , "app.session.get_sessions.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
}
return sessions , nil
2017-01-19 09:00:13 -05:00
}
2021-02-05 11:22:27 +01:00
func ( a * App ) RevokeAllSessions ( userID string ) * model . AppError {
2021-06-18 19:25:03 +03:00
if err := a . srv . userService . RevokeAllSessions ( userID ) ; err != nil {
switch {
case errors . Is ( err , users . GetSessionError ) :
return model . NewAppError ( "RevokeAllSessions" , "app.session.get_sessions.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
case errors . Is ( err , users . DeleteSessionError ) :
return model . NewAppError ( "RevokeAllSessions" , "app.session.remove.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
default :
return model . NewAppError ( "RevokeAllSessions" , "app.session.remove.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-01-19 09:00:13 -05:00
}
}
return nil
}
2021-06-14 18:08:00 +03:00
func ( a * App ) AddSessionToCache ( session * model . Session ) {
a . srv . userService . AddSessionToCache ( session )
}
2019-07-01 23:28:46 +02:00
// RevokeSessionsFromAllUsers will go through all the sessions active
// in the server and revoke them
func ( a * App ) RevokeSessionsFromAllUsers ( ) * model . AppError {
2021-06-18 19:25:03 +03:00
if err := a . srv . userService . RevokeSessionsFromAllUsers ( ) ; err != nil {
switch {
case errors . Is ( err , users . DeleteAllAccessDataError ) :
return model . NewAppError ( "RevokeSessionsFromAllUsers" , "app.oauth.remove_access_data.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
default :
return model . NewAppError ( "RevokeSessionsFromAllUsers" , "app.session.remove_all_sessions_for_team.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
}
2019-07-01 23:28:46 +02:00
}
return nil
}
2021-06-14 18:08:00 +03:00
func ( a * App ) ReturnSessionToPool ( session * model . Session ) {
a . srv . userService . ReturnSessionToPool ( session )
}
2017-01-13 13:53:37 -05:00
2021-06-14 18:08:00 +03:00
func ( a * App ) ClearSessionCacheForUser ( userID string ) {
a . srv . userService . ClearUserSessionCache ( userID )
2017-01-13 13:53:37 -05:00
}
2019-07-01 23:28:46 +02:00
func ( a * App ) ClearSessionCacheForAllUsers ( ) {
2021-06-14 18:08:00 +03:00
a . srv . userService . ClearAllUsersSessionCache ( )
2019-07-01 23:28:46 +02:00
}
2021-02-05 11:22:27 +01:00
func ( a * App ) ClearSessionCacheForUserSkipClusterSend ( userID string ) {
2021-04-01 11:29:56 +03:00
a . Srv ( ) . clearSessionCacheForUserSkipClusterSend ( userID )
2017-01-13 13:53:37 -05:00
}
2019-07-01 23:28:46 +02:00
func ( a * App ) ClearSessionCacheForAllUsersSkipClusterSend ( ) {
2021-04-01 11:29:56 +03:00
a . Srv ( ) . clearSessionCacheForAllUsersSkipClusterSend ( )
2019-07-01 23:28:46 +02:00
}
2021-02-25 20:22:27 +01:00
func ( a * App ) RevokeSessionsForDeviceId ( userID string , deviceID string , currentSessionId string ) * model . AppError {
2021-06-18 19:25:03 +03:00
if err := a . srv . userService . RevokeSessionsForDeviceId ( userID , deviceID , currentSessionId ) ; err != nil {
2020-07-15 09:26:28 -04:00
return model . NewAppError ( "RevokeSessionsForDeviceId" , "app.session.get_sessions.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2018-10-16 00:33:22 +05:30
}
2017-01-13 15:17:50 -05:00
return nil
}
2021-02-25 20:22:27 +01:00
func ( a * App ) GetSessionById ( sessionID string ) ( * model . Session , * model . AppError ) {
2021-06-18 19:25:03 +03:00
session , err := a . srv . userService . GetSessionByID ( sessionID )
2019-05-30 18:22:19 -03:00
if err != nil {
2020-07-15 09:26:28 -04:00
return nil , model . NewAppError ( "GetSessionById" , "app.session.get.app_error" , nil , err . Error ( ) , http . StatusBadRequest )
2017-10-04 11:04:17 -04:00
}
2020-07-15 09:26:28 -04:00
2019-05-30 18:22:19 -03:00
return session , nil
2017-10-04 11:04:17 -04:00
}
2021-02-25 20:22:27 +01:00
func ( a * App ) RevokeSessionById ( sessionID string ) * model . AppError {
2021-06-18 19:25:03 +03:00
session , err := a . GetSessionById ( sessionID )
2019-05-30 18:22:19 -03:00
if err != nil {
2020-07-15 09:26:28 -04:00
return model . NewAppError ( "RevokeSessionById" , "app.session.get.app_error" , nil , err . Error ( ) , http . StatusBadRequest )
2017-01-13 15:17:50 -05:00
}
2019-05-30 18:22:19 -03:00
return a . RevokeSession ( session )
2018-10-16 00:33:22 +05:30
2017-01-13 15:17:50 -05:00
}
2017-09-06 17:12:54 -05:00
func ( a * App ) RevokeSession ( session * model . Session ) * model . AppError {
2021-06-18 19:25:03 +03:00
if err := a . srv . userService . RevokeSession ( session ) ; err != nil {
switch {
case errors . Is ( err , users . DeleteSessionError ) :
return model . NewAppError ( "RevokeSession" , "app.session.remove.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
default :
2020-07-15 09:26:28 -04:00
return model . NewAppError ( "RevokeSession" , "app.session.remove.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-01-13 15:17:50 -05:00
}
}
return nil
}
2021-02-25 20:22:27 +01:00
func ( a * App ) AttachDeviceId ( sessionID string , deviceID string , expiresAt int64 ) * model . AppError {
_ , err := a . Srv ( ) . Store . Session ( ) . UpdateDeviceId ( sessionID , deviceID , expiresAt )
2019-06-03 10:16:50 -04:00
if err != nil {
2020-07-15 09:26:28 -04:00
return model . NewAppError ( "AttachDeviceId" , "app.session.update_device_id.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-01-13 15:17:50 -05:00
}
return nil
}
2017-05-10 09:48:50 -04:00
2017-09-06 17:12:54 -05:00
func ( a * App ) UpdateLastActivityAtIfNeeded ( session model . Session ) {
2017-05-10 09:48:50 -04:00
now := model . GetMillis ( )
2018-06-27 17:19:06 -04:00
a . UpdateWebConnUserActivity ( session , now )
2021-07-12 20:05:36 +02:00
if now - session . LastActivityAt < model . SessionActivityTimeout {
2017-05-10 09:48:50 -04:00
return
}
2020-02-13 13:26:58 +01:00
if err := a . Srv ( ) . Store . Session ( ) . UpdateLastActivityAt ( session . Id , now ) ; err != nil {
2021-01-15 09:58:34 +03:00
mlog . Warn ( "Failed to update LastActivityAt" , mlog . String ( "user_id" , session . UserId ) , mlog . String ( "session_id" , session . Id ) , mlog . Err ( err ) )
2017-05-10 09:48:50 -04:00
}
session . LastActivityAt = now
2021-06-14 18:08:00 +03:00
a . srv . userService . AddSessionToCache ( & session )
2017-05-10 09:48:50 -04:00
}
2017-07-31 12:59:32 -04:00
2020-05-06 15:41:10 -04:00
// ExtendSessionExpiryIfNeeded extends Session.ExpiresAt based on session lengths in config.
// A new ExpiresAt is only written if enough time has elapsed since last update.
// Returns true only if the session was extended.
func ( a * App ) ExtendSessionExpiryIfNeeded ( session * model . Session ) bool {
2021-05-07 14:43:41 -04:00
if ! * a . Srv ( ) . Config ( ) . ServiceSettings . ExtendSessionLengthWithActivity {
return false
}
2020-05-06 15:41:10 -04:00
if session == nil || session . IsExpired ( ) {
return false
}
sessionLength := a . GetSessionLengthInMillis ( session )
// Only extend the expiry if the lessor of 1% or 1 day has elapsed within the
// current session duration.
threshold := int64 ( math . Min ( float64 ( sessionLength ) * 0.01 , float64 ( 24 * 60 * 60 * 1000 ) ) )
// Minimum session length is 1 day as of this writing, therefore a minimum ~14 minutes threshold.
// However we'll add a sanity check here in case that changes. Minimum 5 minute threshold,
// meaning we won't write a new expiry more than every 5 minutes.
if threshold < 5 * 60 * 1000 {
threshold = 5 * 60 * 1000
}
now := model . GetMillis ( )
elapsed := now - ( session . ExpiresAt - sessionLength )
if elapsed < threshold {
return false
}
auditRec := a . MakeAuditRecord ( "extendSessionExpiry" , audit . Fail )
defer a . LogAuditRec ( auditRec , nil )
auditRec . AddMeta ( "session" , session )
newExpiry := now + sessionLength
if err := a . Srv ( ) . Store . Session ( ) . UpdateExpiresAt ( session . Id , newExpiry ) ; err != nil {
mlog . Error ( "Failed to update ExpiresAt" , mlog . String ( "user_id" , session . UserId ) , mlog . String ( "session_id" , session . Id ) , mlog . Err ( err ) )
auditRec . AddMeta ( "err" , err . Error ( ) )
return false
}
// Update local cache. No need to invalidate cache for cluster as the session cache timeout
// ensures each node will get an extended expiry within the next 10 minutes.
// Worst case is another node may generate a redundant expiry update.
session . ExpiresAt = newExpiry
2021-06-14 18:08:00 +03:00
a . srv . userService . AddSessionToCache ( session )
2020-05-06 15:41:10 -04:00
2020-08-04 16:10:37 -04:00
mlog . Debug ( "Session extended" , mlog . String ( "user_id" , session . UserId ) , mlog . String ( "session_id" , session . Id ) ,
mlog . Int64 ( "newExpiry" , newExpiry ) , mlog . Int64 ( "session_length" , sessionLength ) )
2020-05-06 15:41:10 -04:00
auditRec . Success ( )
auditRec . AddMeta ( "extended_session" , session )
return true
}
// GetSessionLengthInMillis returns the session length, in milliseconds,
// based on the type of session (Mobile, SSO, Web/LDAP).
func ( a * App ) GetSessionLengthInMillis ( session * model . Session ) int64 {
if session == nil {
return 0
}
var days int
if session . IsMobileApp ( ) {
days = * a . Config ( ) . ServiceSettings . SessionLengthMobileInDays
2020-06-30 10:34:05 -04:00
} else if session . IsSSOLogin ( ) {
2020-05-06 15:41:10 -04:00
days = * a . Config ( ) . ServiceSettings . SessionLengthSSOInDays
} else {
days = * a . Config ( ) . ServiceSettings . SessionLengthWebInDays
}
return int64 ( days * 24 * 60 * 60 * 1000 )
}
2020-08-04 16:10:37 -04:00
// SetSessionExpireInDays sets the session's expiry the specified number of days
// relative to either the session creation date or the current time, depending
// on the `ExtendSessionOnActivity` config setting.
func ( a * App ) SetSessionExpireInDays ( session * model . Session , days int ) {
2021-06-18 19:25:03 +03:00
a . srv . userService . SetSessionExpireInDays ( session , days )
2020-08-04 16:10:37 -04:00
}
2017-09-06 17:12:54 -05:00
func ( a * App ) CreateUserAccessToken ( token * model . UserAccessToken ) ( * model . UserAccessToken , * model . AppError ) {
2021-06-18 19:25:03 +03:00
user , nErr := a . srv . userService . GetUser ( token . UserId )
2020-10-26 06:41:27 -03:00
if nErr != nil {
var nfErr * store . ErrNotFound
switch {
case errors . As ( nErr , & nfErr ) :
2021-01-04 11:32:29 +05:30
return nil , model . NewAppError ( "CreateUserAccessToken" , MissingAccountError , nil , nfErr . Error ( ) , http . StatusNotFound )
2020-10-26 06:41:27 -03:00
default :
return nil , model . NewAppError ( "CreateUserAccessToken" , "app.user.get.app_error" , nil , nErr . Error ( ) , http . StatusInternalServerError )
}
2019-05-21 08:44:29 -05:00
}
if ! * a . Config ( ) . ServiceSettings . EnableUserAccessTokens && ! user . IsBot {
2017-07-31 12:59:32 -04:00
return nil , model . NewAppError ( "CreateUserAccessToken" , "app.user_access_token.disabled" , nil , "" , http . StatusNotImplemented )
}
token . Token = model . NewId ( )
2020-10-26 06:41:27 -03:00
token , nErr = a . Srv ( ) . Store . UserAccessToken ( ) . Save ( token )
2020-07-23 04:58:49 -04:00
if nErr != nil {
var appErr * model . AppError
switch {
case errors . As ( nErr , & appErr ) :
return nil , appErr
default :
2020-10-26 06:41:27 -03:00
return nil , model . NewAppError ( "CreateUserAccessToken" , "app.user_access_token.save.app_error" , nil , nErr . Error ( ) , http . StatusInternalServerError )
2020-07-23 04:58:49 -04:00
}
2017-08-03 08:51:52 -04:00
}
2019-05-31 23:12:03 -07:00
// Don't send emails to bot users.
if ! user . IsBot {
2021-07-19 18:26:06 +03:00
if err := a . Srv ( ) . EmailService . SendUserAccessTokenAddedEmail ( user . Email , user . Locale , a . GetSiteURL ( ) ) ; err != nil {
2020-02-13 13:26:58 +01:00
a . Log ( ) . Error ( "Unable to send user access token added email" , mlog . Err ( err ) , mlog . String ( "user_id" , user . Id ) )
2019-05-31 23:12:03 -07:00
}
2017-07-31 12:59:32 -04:00
}
2017-08-03 08:51:52 -04:00
return token , nil
2017-07-31 12:59:32 -04:00
}
2017-09-06 17:12:54 -05:00
func ( a * App ) createSessionForUserAccessToken ( tokenString string ) ( * model . Session , * model . AppError ) {
2020-07-23 04:58:49 -04:00
token , nErr := a . Srv ( ) . Store . UserAccessToken ( ) . GetByToken ( tokenString )
if nErr != nil {
return nil , model . NewAppError ( "createSessionForUserAccessToken" , "app.user_access_token.invalid_or_missing" , nil , nErr . Error ( ) , http . StatusUnauthorized )
2018-10-16 00:33:22 +05:30
}
2017-10-19 08:10:29 -04:00
2018-10-16 00:33:22 +05:30
if ! token . IsActive {
return nil , model . NewAppError ( "createSessionForUserAccessToken" , "app.user_access_token.invalid_or_missing" , nil , "inactive_token" , http . StatusUnauthorized )
2017-07-31 12:59:32 -04:00
}
MM-30882: Fix read-after-write issue for demoting user (#16911)
* MM-30882: Fix read-after-write issue for demoting user
In (*App).DemoteUserToGuest, we would demote a user, and then immediately
read it back to do future operations from the user. This reading back
of the user had the effect of sticking the old value into the cache
after which it would never be updated.
There was another issue along with this, which was when the invalidation
message would broadcast across the cluster, it would hit the cache invalidation
problem where an unrelated store call would miss the cache because
it was invalidated, and then again read from replica and stick the old value.
To fix all these, we return the new value directly from the store method
to avoid having the app to read it again.
And we add a map in the localcache layer which tracks invalidations made,
and then switch to use master if it's true.
The core change is fairly limited, but due to changing the store method signatures,
a lot of code needed to be updated to pass "context.Background". Therefore the PR
just "appears" to be big, but the main changes are limited to app/user.go,
sqlstore/user_store.go and user_layer.go
https://mattermost.atlassian.net/browse/MM-30882
```release-note
Fix an issue where demoting a user to guest would not take effect in
an environment with read replicas.
```
* Fix concurrent map access
* Fixing mistakes
* fix tests
2021-02-12 19:04:05 +05:30
user , nErr := a . Srv ( ) . Store . User ( ) . Get ( context . Background ( ) , token . UserId )
2020-10-26 06:41:27 -03:00
if nErr != nil {
var nfErr * store . ErrNotFound
switch {
case errors . As ( nErr , & nfErr ) :
2021-01-04 11:32:29 +05:30
return nil , model . NewAppError ( "createSessionForUserAccessToken" , MissingAccountError , nil , nfErr . Error ( ) , http . StatusNotFound )
2020-10-26 06:41:27 -03:00
default :
return nil , model . NewAppError ( "createSessionForUserAccessToken" , "app.user.get.app_error" , nil , nErr . Error ( ) , http . StatusInternalServerError )
}
2017-07-31 12:59:32 -04:00
}
2019-06-13 11:54:09 -07:00
if ! * a . Config ( ) . ServiceSettings . EnableUserAccessTokens && ! user . IsBot {
return nil , model . NewAppError ( "createSessionForUserAccessToken" , "app.user_access_token.invalid_or_missing" , nil , "EnableUserAccessTokens=false" , http . StatusUnauthorized )
}
2017-07-31 12:59:32 -04:00
if user . DeleteAt != 0 {
return nil , model . NewAppError ( "createSessionForUserAccessToken" , "app.user_access_token.invalid_or_missing" , nil , "inactive_user_id=" + user . Id , http . StatusUnauthorized )
}
session := & model . Session {
Token : token . Token ,
UserId : user . Id ,
Roles : user . GetRawRoles ( ) ,
IsOAuth : false ,
}
2021-07-12 20:05:36 +02:00
session . AddProp ( model . SessionPropUserAccessTokenId , token . Id )
session . AddProp ( model . SessionPropType , model . SessionTypeUserAccessToken )
2019-06-13 11:54:09 -07:00
if user . IsBot {
2021-07-12 20:05:36 +02:00
session . AddProp ( model . SessionPropIsBot , model . SessionPropIsBotValue )
2019-06-13 11:54:09 -07:00
}
2019-07-22 22:13:39 +02:00
if user . IsGuest ( ) {
2021-07-12 20:05:36 +02:00
session . AddProp ( model . SessionPropIsGuest , "true" )
2019-07-22 22:13:39 +02:00
} else {
2021-07-12 20:05:36 +02:00
session . AddProp ( model . SessionPropIsGuest , "false" )
2019-07-22 22:13:39 +02:00
}
2021-07-12 20:05:36 +02:00
a . srv . userService . SetSessionExpireInDays ( session , model . SessionUserAccessTokenExpiry )
2017-07-31 12:59:32 -04:00
2020-07-23 04:58:49 -04:00
session , nErr = a . Srv ( ) . Store . Session ( ) . Save ( session )
2020-07-15 09:26:28 -04:00
if nErr != nil {
var invErr * store . ErrInvalidInput
switch {
case errors . As ( nErr , & invErr ) :
return nil , model . NewAppError ( "CreateSession" , "app.session.save.existing.app_error" , nil , invErr . Error ( ) , http . StatusBadRequest )
default :
return nil , model . NewAppError ( "CreateSession" , "app.session.save.app_error" , nil , nErr . Error ( ) , http . StatusInternalServerError )
}
2018-10-16 00:33:22 +05:30
}
2017-07-31 12:59:32 -04:00
2021-06-14 18:08:00 +03:00
a . srv . userService . AddSessionToCache ( session )
2018-10-16 00:33:22 +05:30
return session , nil
2017-07-31 12:59:32 -04:00
}
2017-09-06 17:12:54 -05:00
func ( a * App ) RevokeUserAccessToken ( token * model . UserAccessToken ) * model . AppError {
2017-07-31 12:59:32 -04:00
var session * model . Session
2021-06-18 19:25:03 +03:00
session , _ = a . srv . userService . GetSessionContext ( context . Background ( ) , token . Token )
2017-07-31 12:59:32 -04:00
2020-02-13 13:26:58 +01:00
if err := a . Srv ( ) . Store . UserAccessToken ( ) . Delete ( token . Id ) ; err != nil {
2020-07-23 04:58:49 -04:00
return model . NewAppError ( "RevokeUserAccessToken" , "app.user_access_token.delete.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-07-31 12:59:32 -04:00
}
if session == nil {
return nil
}
2017-09-06 17:12:54 -05:00
return a . RevokeSession ( session )
2017-07-31 12:59:32 -04:00
}
2017-10-19 08:10:29 -04:00
func ( a * App ) DisableUserAccessToken ( token * model . UserAccessToken ) * model . AppError {
var session * model . Session
2021-06-18 19:25:03 +03:00
session , _ = a . srv . userService . GetSessionContext ( context . Background ( ) , token . Token )
2017-10-19 08:10:29 -04:00
2020-02-13 13:26:58 +01:00
if err := a . Srv ( ) . Store . UserAccessToken ( ) . UpdateTokenDisable ( token . Id ) ; err != nil {
2020-07-23 04:58:49 -04:00
return model . NewAppError ( "DisableUserAccessToken" , "app.user_access_token.update_token_disable.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-10-19 08:10:29 -04:00
}
if session == nil {
return nil
}
return a . RevokeSession ( session )
}
func ( a * App ) EnableUserAccessToken ( token * model . UserAccessToken ) * model . AppError {
var session * model . Session
2021-06-18 19:25:03 +03:00
session , _ = a . srv . userService . GetSessionContext ( context . Background ( ) , token . Token )
2017-10-19 08:10:29 -04:00
2020-02-13 13:26:58 +01:00
err := a . Srv ( ) . Store . UserAccessToken ( ) . UpdateTokenEnable ( token . Id )
2019-06-28 11:02:51 +01:00
if err != nil {
2020-07-23 04:58:49 -04:00
return model . NewAppError ( "EnableUserAccessToken" , "app.user_access_token.update_token_enable.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-10-19 08:10:29 -04:00
}
if session == nil {
return nil
}
return nil
}
2018-01-05 14:46:48 -05:00
func ( a * App ) GetUserAccessTokens ( page , perPage int ) ( [ ] * model . UserAccessToken , * model . AppError ) {
2020-02-13 13:26:58 +01:00
tokens , err := a . Srv ( ) . Store . UserAccessToken ( ) . GetAll ( page * perPage , perPage )
2019-06-20 22:34:55 +05:30
if err != nil {
2020-07-23 04:58:49 -04:00
return nil , model . NewAppError ( "GetUserAccessTokens" , "app.user_access_token.get_all.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2018-01-05 14:46:48 -05:00
}
2019-06-20 22:34:55 +05:30
2018-10-16 00:33:22 +05:30
for _ , token := range tokens {
token . Token = ""
}
return tokens , nil
2018-01-05 14:46:48 -05:00
}
2021-02-05 11:22:27 +01:00
func ( a * App ) GetUserAccessTokensForUser ( userID string , page , perPage int ) ( [ ] * model . UserAccessToken , * model . AppError ) {
tokens , err := a . Srv ( ) . Store . UserAccessToken ( ) . GetByUser ( userID , page * perPage , perPage )
2019-06-21 18:49:56 +04:00
if err != nil {
2020-07-23 04:58:49 -04:00
return nil , model . NewAppError ( "GetUserAccessTokensForUser" , "app.user_access_token.get_by_user.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2017-07-31 12:59:32 -04:00
}
2018-10-16 00:33:22 +05:30
for _ , token := range tokens {
token . Token = ""
}
return tokens , nil
2017-07-31 12:59:32 -04:00
}
2021-02-05 11:22:27 +01:00
func ( a * App ) GetUserAccessToken ( tokenID string , sanitize bool ) ( * model . UserAccessToken , * model . AppError ) {
token , err := a . Srv ( ) . Store . UserAccessToken ( ) . Get ( tokenID )
2019-06-20 23:52:10 +05:30
if err != nil {
2020-07-23 04:58:49 -04:00
var nfErr * store . ErrNotFound
switch {
case errors . As ( err , & nfErr ) :
return nil , model . NewAppError ( "GetUserAccessToken" , "app.user_access_token.get_by_user.app_error" , nil , nfErr . Error ( ) , http . StatusNotFound )
default :
return nil , model . NewAppError ( "GetUserAccessToken" , "app.user_access_token.get_by_user.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
}
2017-07-31 12:59:32 -04:00
}
2019-06-20 23:52:10 +05:30
2018-10-16 00:33:22 +05:30
if sanitize {
token . Token = ""
}
return token , nil
2017-07-31 12:59:32 -04:00
}
2018-01-11 16:30:55 -05:00
func ( a * App ) SearchUserAccessTokens ( term string ) ( [ ] * model . UserAccessToken , * model . AppError ) {
2020-02-13 13:26:58 +01:00
tokens , err := a . Srv ( ) . Store . UserAccessToken ( ) . Search ( term )
2019-06-24 07:12:23 -07:00
if err != nil {
2020-07-23 04:58:49 -04:00
return nil , model . NewAppError ( "SearchUserAccessTokens" , "app.user_access_token.search.app_error" , nil , err . Error ( ) , http . StatusInternalServerError )
2018-01-11 16:30:55 -05:00
}
2018-10-16 00:33:22 +05:30
for _ , token := range tokens {
token . Token = ""
}
return tokens , nil
2018-01-11 16:30:55 -05:00
}