How to import users into Bitbucket Server from an external user directory
Outages sometimes make it necessary to move users from an external user directory like JIRA or LDAP into Stash to prevent interruption.
Unfortunately, we don't have a great option to import users into the internal directory right now. We currently have a feature request you can watch, follow and vote on here: - BSERV-7887Getting issue details... STATUS
If you only have a few users that you need to import you can do so manually from the UI or via REST API.
Importing with the UI
To make these changes manually, please use the following steps:
- Identify the users that you need to copy and note their attributes: user name and full name.
- Disable the external user directory in Administration >> User Directories.
- Go to Administration >> Users and Add user at the top right.
- Manually add all the noted users in this way.
- Enable the external directory again.
When you reenable the external directory Bitbucket Server should now label these users in the user list as part of the "Bitbucket Server Internal" directory.
Importing with REST API
If you have more than a few users to add, the quickest way to add them may be to write a script utilizing the REST API. The creation of such scripts can by accomplished in many ways and you can find an example of this in the our article How to add users to Bitbucket Server using Rest API.
Script and Tabulated Text File
You can create a tabulated text file from querying the Bitbucket Server Database with something similar to the below.
SELECT user_name, user_name, display_name, email_address FROM BSERV.cwd_user Where directory_id like '<directory_id>'
This will generate the following text file:
user_name user_name display_name email_address bstuart bstuart bstuart firstname.lastname@example.org rgoody rgoody "ryan goody" email@example.com Pirates Pirates "Fletcher Christian" firstname.lastname@example.org
Here is an example script to import your users and assign their user name as the password by default.
#!/bin/bash ABORT="N" case "$1" in "") echo "Usage: AddUsers.sh Admin_UserName Admin_Password" ABORT="Y";; * ) USERNAME=$1;; esac if [ "$ABORT" == 'N' ]; then case "$2" in "") echo "Usage: AddUsers.sh Admin_UserName Admin_Password" ABORT="Y";; * ) PASSWORD=$2;; esac fi if [ "$ABORT" == 'N' ]; then clear while read -r a b c d; do echo "User: $a" curl -u $USERNAME:$PASSWORD -X POST -H "Content-Type: application/json" "http://localhost:7990/bitbucket/rest/api/1.0/admin/users?name=$a&password=$b&displayName=$c&emailAddress=$d&addToDefualtGroup=true¬ify=false" done < users.txt fi
This is only meant as an example and is not supported. There is no error checking or handling if users already exist in Bitbucket Server.
There are ways of editing the database directly in order to add users as well, but this is riskier and is not recommended.