-
Notifications
You must be signed in to change notification settings - Fork 260
fix(mdb): multi-user - MTA-6329 #5377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
pages/managed-mongodb-databases/api-cli/managing-users-api.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| --- | ||
| title: Managing MongoDB® users with the Scaleway API | ||
| description: This page explains how to manage MongoDB® users via the API | ||
| tags: managed-database database postgresql mongodb database-instance mongodb | ||
| dates: | ||
| validation: 2025-05-08 | ||
| posted: 2025-04-08 | ||
| --- | ||
|
|
||
| When you create your MongoDB® Database Instance, the first user is created by default and has admin rights. | ||
|
|
||
| You can create more users and grant them pre-set roles via the [Scaleway Managed MongoDB® API](https://www.scaleway.com/en/developers/api/managed-database-mongodb/). | ||
|
|
||
| <Message type="important"> | ||
| All users you create have at first administrator roles, which can be changed after creation with the [Apply user roles](https://www.scaleway.com/en/developers/api/managed-database-mongodb/#path-users-apply-user-roles) call. The default user's role cannot be changed. | ||
ldecarvalho-doc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </Message> | ||
|
|
||
| ### How to create a user | ||
|
|
||
| 1. Edit the POST request payload you will use to create your user. Replace the values of each parameter with your values of choice following the parameter descriptions below. | ||
| ``` | ||
| { | ||
| "name": "<username>", | ||
| "password": "<password>", | ||
| } | ||
| ``` | ||
|
|
||
| | Parameter | Description | | ||
| | :--------------- | :----------------------------------------------------------------- | | ||
| | `name` | Set a name for the database user. | | ||
| | `password` | Set a password for the database user.| | ||
|
|
||
| 2. Run the following command to create a user. Make sure you include the payload you edited in the previous step. `{instance_id}` corresponds to the UUID of the Managed MongoDB®. | ||
| ``` | ||
| curl -X POST \ | ||
| -H "X-Auth-Token: $SCW_SECRET_KEY" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "name": "<username>", | ||
| "password": "<password>", | ||
| }' \ | ||
| "https://api.scaleway.com/mongodb/v1alpha1/regions/$SCW_REGION/par/instances/{instance_id}/users" | ||
| ``` | ||
|
|
||
| You should get a response like the following: | ||
| ``` | ||
| { | ||
| "name": "<username>", | ||
| "password": "<password>", | ||
| "roles": [ | ||
| { | ||
| "role": "read_write", | ||
| "any_database": true | ||
| }, | ||
| { | ||
| "role": "db_admin", | ||
| "any_database": true | ||
| }, | ||
| { | ||
| "role": "sync", | ||
| "any_database": true | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| All users you create will have the `read_write`, `db_admin` and `sync` roles on all databases by default. | ||
|
|
||
| You can follow the steps below to update a user's role(s) to the one(s) of your choice. | ||
|
|
||
| ### How to apply a role to a user | ||
|
|
||
| 1. Edit the POST request payload you will use to update the user role. Replace the values of each parameter with your values of choice following the parameter descriptions below. | ||
|
|
||
| In this example, we define a single `read_write` role for the user. This role applies only in the `example-db` database. | ||
|
|
||
| <Message type="tip"> | ||
| To grant the user this role in all databases, you can set `any_database` to true. | ||
| </Message> | ||
|
|
||
| ``` | ||
| { | ||
| "name": "<username>", | ||
| "roles": [ | ||
| { | ||
| "role": "read_write", | ||
| "database": "example-db", | ||
| "any_database": false | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| | Role | Description | | ||
| | :--------------- | :----------------------------------------------------------------- | | ||
| | `read` | Read privileges on all non-system collections and the `system.js` collection. Refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/manual/reference/built-in-roles/#mongodb-authrole-read) for an extensive list of the privileges granted to this role. | | ||
| | `read_write` | Read and write privileges on all non-system collections and the `system.js` collection. Refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/manual/reference/built-in-roles/#mongodb-authrole-readWrite) for an extensive list of the privileges granted to this role. | | ||
| | `db_admin` | Privileges to perform administrative tasks on the database, such as schema-related tasks, indexing, and gathering statistics. This role does not grant privileges for user and role management. Refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/manual/reference/built-in-roles/#mongodb-authrole-dbAdmin) for an extensive list of the privileges granted to this role. | | ||
| | `sync` | Role that aggregates three MongoDB roles: | | ||
| | | `clusterMonitor` - Read-only access to monitoring tools. Refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/upcoming/reference/built-in-roles/#mongodb-authrole-clusterMonitor) for an extensive list of the privileges granted to this role. | | ||
| | | `backup` - Grants the minimal privileges needed to back up data. Refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/upcoming/reference/built-in-roles/#mongodb-authrole-backup) for an extensive list of the privileges granted to this role. | | ||
| | | `restore` - Grants the privileges needed to restore data from backups. Refer to the [official MongoDB® documentation](https://www.mongodb.com/docs/upcoming/reference/built-in-roles/#mongodb-authrole-restore) for an extensive list of the privileges granted to this role. | | ||
|
|
||
| 2. Run the following command to apply a new role to the user. Make sure you include the payload you edited in the previous step and that you replace the parameters in the call with your information. `{instance_id}` corresponds to the UUID of the Managed MongoDB®. | ||
|
|
||
| ``` | ||
| curl -X PUT \ | ||
| -H "X-Auth-Token: $SCW_SECRET_KEY" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{ | ||
| "name": "<username>", | ||
| "roles": [ | ||
| { | ||
| "role": "read_write", | ||
| "database": "<name_of_database>" | ||
| "any_database": false | ||
| } | ||
| ] | ||
| }' \ | ||
| "https://api.scaleway.com/mongodb/v1alpha1/regions/$SCW_REGION/instances/{instance_id}/roles" | ||
| ``` | ||
|
|
||
| If the call was successful, you will get the payload with the new role(s) as a response. | ||
|
|
||
| <Message type="note"> | ||
| Assigning roles upon user creation will be possible by the second half of 2025. Refer to the [Scaleway Changelog](/changelog/?product=mongodb) to keep up with the latest Managed MongoDB® updates. | ||
| </Message> | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.