Files
upstream-mattermost/server/channels/jobs/cleanup_expired_access_tokens/worker.go
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

135 lines
4.6 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package cleanup_expired_access_tokens
import (
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/jobs"
)
const (
workerName = "CleanupExpiredAccessTokens"
// batchLimit bounds both the number of rows fetched by GetExpiredBefore
// and the corresponding DeleteByIds call, keeping the transaction
// footprint bounded even when a large number of tokens expire at once.
batchLimit = 1000
// maxBatches caps the number of iterations per job execution so that very
// large expiry backlogs are drained across multiple scheduled runs rather
// than a single unbounded loop.
maxBatches = 1000
)
// expiredTokenStore is the subset of UserAccessTokenStore used by the worker.
// Defined here rather than depending on the full store interface so the
// orchestration logic can be unit-tested with a small fake.
type expiredTokenStore interface {
GetExpiredBefore(cutoff int64, limit int) ([]*model.UserAccessToken, error)
DeleteByIds(tokenIDs []string) (int64, error)
}
// MakeWorker creates a worker that periodically deletes personal access tokens
// whose ExpiresAt has passed, along with any sessions created from them.
// The work is done in batches to keep the transaction footprint bounded.
//
// clearSessionCache is called for each affected user after their tokens are
// deleted so that in-memory session caches don't serve stale sessions.
//
// notifyExpired is called with each batch of expired tokens immediately before
// they are deleted, so the owner can be DMed that their token has been removed.
// It is best-effort: any failure is handled internally and must not block the
// cleanup. Note that after this change PAT expiry notifications come from two
// jobs — the pre-expiry warning cascade (pat_expiry_notify) and this
// at-deletion notice — so a future reader shouldn't be surprised to find an
// expiry DM originating from the cleanup job.
func MakeWorker(jobServer *jobs.JobServer, clearSessionCache func(userID string), notifyExpired func(rctx request.CTX, tokens []*model.UserAccessToken)) *jobs.SimpleWorker {
isEnabled := func(cfg *model.Config) bool {
return *cfg.ServiceSettings.EnableUserAccessTokens
}
execute := func(logger mlog.LoggerIFace, job *model.Job) error {
defer jobServer.HandleJobPanic(logger, job)
return cleanupExpired(
request.EmptyContext(logger),
jobServer.Store.UserAccessToken(),
clearSessionCache,
notifyExpired,
model.GetMillis(),
batchLimit,
maxBatches,
)
}
return jobs.NewSimpleWorker(workerName, jobServer, execute, isEnabled)
}
// cleanupExpired drains expired personal access tokens in batches up to
// maxBatches iterations. It is extracted from MakeWorker so that the batching
// and error-propagation logic can be exercised by unit tests with a fake store.
//
// clearSessionCache is called for each unique user whose tokens were deleted so
// that in-memory session caches don't continue serving the removed sessions.
//
// notifyExpired is called with each batch immediately before deletion so token
// owners can be notified. It runs best-effort: it is invoked before the delete
// so the token→owner mapping is still available, and the delete proceeds
// regardless of what it does. The worst case (a crash between notify and
// delete) is a single duplicate DM on the next run, which is acceptable. The
// request context is passed through so the notifier logs under the job's logger.
func cleanupExpired(
rctx request.CTX,
store expiredTokenStore,
clearSessionCache func(userID string),
notifyExpired func(rctx request.CTX, tokens []*model.UserAccessToken),
cutoff int64,
limit int,
maxIter int,
) error {
var totalDeleted int64
for range maxIter {
expired, err := store.GetExpiredBefore(cutoff, limit)
if err != nil {
return err
}
if len(expired) == 0 {
break
}
ids := make([]string, len(expired))
userIDs := make(map[string]struct{}, len(expired))
for i, token := range expired {
ids[i] = token.Id
userIDs[token.UserId] = struct{}{}
}
if notifyExpired != nil {
notifyExpired(rctx, expired)
}
deleted, err := store.DeleteByIds(ids)
if err != nil {
return err
}
totalDeleted += deleted
for userID := range userIDs {
clearSessionCache(userID)
}
if len(expired) < limit {
break
}
}
rctx.Logger().Info(
"Cleaned up expired personal access tokens",
mlog.Int("deleted", int(totalDeleted)),
mlog.Int("cutoff", int(cutoff)),
)
return nil
}