Qball's Weblog

GITLAB - Reset all passwords

Tags GitLab 

I had to reset all user paswords on a GitLab instance. As I could not find a good way to do this from the UI I wrote a small script.

The script goes through all users (that are not already blocked) and don’t have two factor authentication enabled. It generates a temporary password, sets this and sends them an e-mail. When they login, they are asked to reset their password.

#!/usr/bin/env python3

from mailer import Mailer
from mailer import Message

import gitlab
import secrets
import string

import time



# private token or personal token authentication
gl = gitlab.Gitlab('https://myGitLab.com', private_token='1234567890', api_version=4)
gl.auth()


a = gl.users.list(all=True)

for user in a:
    if user.state != "blocked" and user.two_factor_enabled == False:
        alphabet = string.ascii_letters + string.digits
        password = ''.join(secrets.choice(alphabet) for i in range(20)) # for a 20-character password
        print(user.email)
        user.password = password
        user.save()
        message = Message(From="noreply@myGitLab.com",
                To=user.email,
                charset="utf-8")
        message.Subject = "GitLab password reset"
        message.Body = (
                f"Dear {user.name}\r\n\r\n"
                "On request of XXX all passwords on the gitlab server need to be reset.\r\n"
                f"Your new, temporary password is:\r\n{password}\r\n"
                "This is a one-time password, after logging in you are required to provide a new password.\r\n\r\n"
                "Regards,\r\Admin"
                )
        sender = Mailer('smtp.myGitLab.com')
        sender.send(message)
        time.sleep(1)
comments powered by Disqus