Atlas provides excellent tools, such as Cluster-to-Cluster Sync, Live Migrate and mongomirror for migrating your data to Atlas. However, neither of these tools import user and role data.
This utility is a Python script that was written to assist migrating your existing database users and roles to Atlas. The only thing it can't migrate is user passwords, so the script randomly generates passwords and saves them to a users.text
file.
The script uses the Python driver to fetch your existing users and roles. However, as the createUser command is not supported by Atlas, users need to be created using the Database Users REST api.
To use the tool:
-
pip install -r requirements.txt
-
Pouplate the params.py file with your credentials. As a prerequisite, you must have an Atlas Organization API Key.
# Input parameters
# Source DB
source_conn_string = <Application connection string>
source_database = 'admin'
# For Atlas REST API
target_project_id = '<Project ID found under Atlas Project Settings>'
target_api_public_key = '<API Public Key>'
target_api_private_key = '<API Private Key>'
Given this existing set of users:
When run the script will display the following:
brianleonard$ python3 migrate_users.py
Migrating MongoDB Users
4 potential users to be migrated
>>> Migrating user:
{'_id': 'admin.brian', 'user': 'brian', 'db': 'admin', 'roles': [{'role': 'readAnyDatabase', 'db': 'admin'}], 'mechanisms': ['SCRAM-SHA-1']}
Formatted roles:
[{'databaseName': 'admin', 'roleName': 'readAnyDatabase'}]
User data sent to Atlas API:
{'databaseName': 'admin', 'roles': [{'databaseName': 'admin', 'roleName': 'readAnyDatabase'}], 'username': 'brian', 'password': 'changeme123'}
User brian created.
>>>
>>> Migrating user:
{'_id': 'admin.david', 'user': 'david', 'db': 'admin', 'roles': [{'role': 'read', 'db': 'marketing'}, {'role': 'readWrite', 'db': 'sales'}], 'mechanisms': ['SCRAM-SHA-1']}
Formatted roles:
[{'databaseName': 'marketing', 'roleName': 'read'}, {'databaseName': 'sales', 'roleName': 'readWrite'}]
User data sent to Atlas API:
{'databaseName': 'admin', 'roles': [{'databaseName': 'marketing', 'roleName': 'read'}, {'databaseName': 'sales', 'roleName': 'readWrite'}], 'username': 'david', 'password': 'changeme123'}
User david created.
>>>
<<<Skipping user mms-automation>>>
>>> Migrating user:
{'_id': 'admin.scott', 'user': 'scott', 'db': 'admin', 'roles': [{'role': 'readWriteAnyDatabase', 'db': 'admin'}], 'mechanisms': ['SCRAM-SHA-1']}
Formatted roles:
[{'databaseName': 'admin', 'roleName': 'readWriteAnyDatabase'}]
User data sent to Atlas API:
{'databaseName': 'admin', 'roles': [{'databaseName': 'admin', 'roleName': 'readWriteAnyDatabase'}], 'username': 'scott', 'password': 'changeme123'}
User scott created.
>>>