Skip to content

Instantly share code, notes, and snippets.

@reddare
Last active December 6, 2024 05:43
Show Gist options
  • Save reddare/2fa6b15ccfae6df427bc25af5bc79ff1 to your computer and use it in GitHub Desktop.
Save reddare/2fa6b15ccfae6df427bc25af5bc79ff1 to your computer and use it in GitHub Desktop.
gitlab ce extend tokens

gitlab ce extend tokens

GitLab starting from version 16.0 introduced mandatory lifetime limits for tokens.

Previously, if their lifetime was unlimited, after upgrading to version 16.0, the maximum token lifetime can be 365 days.

GitLab explains this in the following publication:
Why GitLab access tokens now have lifetime limits.

To extend the lifetime of tokens, you can use the command:

gitlab-rake gitlab:tokens:edit

However, it does not provide the ability to flexibly and precisely choose which tokens need to be extended.

In response, a script was created that allows for a more specific selection of tokens for extension.

The script should be executed with the command:

gitlab-rails runner "require '/opt/gitlab/extend_tokens.rb'"

new_expiry_date = Date.new(2026, 11, 1).end_of_day
# Open the file for writing
File.open('/tmp/updated_tokens_list', 'w') do |file|
# Get all tokens and update their expires_at
PersonalAccessToken.find_each do |token|
# Skip tokens with the name "something-to-skip"
next if token.name == "something-to-skip"
# Update the expiry date
token.update(expires_at: new_expiry_date)
# Write the information to the file
file.puts "Updated Token ID: #{token.id}, User: #{token.user.username}, Token name: #{token.name}, New Expiry Date: #{new_expiry_date}"
end
end
# Success message about the update
puts "All tokens have been updated. Output has been saved to /tmp/updated_tokens_list"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment