Rotating access tokens in web UI with validity duration longer than lifetime limit fails with an error

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Summary

Users are unable to rotate (using the web UI) Group/Project/Personal access tokens that have a validity duration longer than the current allowed lifetime limit for access tokens. Attempting to use the existing rotate button/functionality fails with an error.

API to rotate the access token is unaffected, as clients can specify the expiry date as a parameter. This issue only affects the web UI.

Steps to reproduce

This should equally apply to Group Access Token, Project Access Token, and Personal Access Token, but for illustration purposes, a Group Access Token is used.

  1. As an Admin, ensure that the maximum lifetime of access tokens is set to 365 days, or similarly high value
  2. Create a Group Access Token with a long expiry date that is allowed by the lifetime, such as 365 days
  3. As an Admin, reduce the maximum lifetime of access tokens to lower than the validity period of the access token, such as 180 days
  4. Go to the Group Settings, Access Tokens, and click the rotate icon for the group access token.

Example Project

I can't reproduce it on GitLab.com because it's not possible to reduce the lifetime limit. That's only supported on Self-Managed and Dedicated according to https://docs.gitlab.com/administration/settings/account_and_limit_settings/#limit-the-lifetime-of-access-tokens

What is the current bug behavior?

The PUT request to /groups/:path/-/settings/access_tokens/:token_id/rotate returns a HTTP 422 with the response body {"message":"Expiration date must be before yyyy-mm-dd"}, where the date is the maximum expiry date permitted by the current lifetime limit.

The UI shows the error message Expiration date must be before yyyy-mm-dd.

The token rotation fails.

What is the expected correct behavior?

EITHER allow to specify the expiry date during rotation, OR automatically use the lesser of the currently maximum allowed lifetime, or the access token's current validity duration.

The token rotation succeeds.

Relevant logs and/or screenshots

Output of checks

Results of GitLab environment info

Encountered on GitLab Dedicated on v17.10.6-ee

Expand for output related to GitLab environment info

 (For installations with omnibus-gitlab package run and paste the output of: \`sudo gitlab-rake gitlab:env:info\`)  (For installations from source run and paste the output of: \`sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production\`)  

Results of GitLab application Check

Expand for output related to the GitLab application check

(For installations with omnibus-gitlab package run and paste the output of: `sudo gitlab-rake gitlab:check SANITIZE=true`)

(For installations from source run and paste the output of: `sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true`)

(we will only investigate if the tests are passing)

Possible fixes

https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/personal_access_tokens/rotate_service.rb#L83-88

The current implementation always uses exactly the validity period that the access token has now, in order to calculate the new expiry date. When the lifetime limit reduces, this may fail.

Patch release information for backports

If the bug fix needs to be backported in a patch release to a version under the maintenance policy, please follow the steps on the patch release runbook for GitLab engineers.

Refer to the internal "Release Information" dashboard for information about the next patch release, including the targeted versions, expected release date, and current status.

High-severity bug remediation

To remediate high-severity issues requiring an internal release for single-tenant SaaS instances, refer to the internal release process for engineers.

Edited by 🤖 GitLab Bot 🤖