Listing Project Database Users via Python

I have a new requirement from auditing for get a list of all Database users on all of our Projects. We’ve been manually going in and taking screenshots which is extremely tedious. I was looking into a way to automate this with Python. I couldnt really find any good guides or anything to lead me in the right direction but was able to piece together something.

Im able to connect to my project and run a very basic query with:

import requests
from requests.auth import HTTPDigestAuth
import pprint

base_url = “https://cloud.mongodb.com/api/atlas/v1.0/

Replace these with your MongoDB Atlas username and API key

atlas_user = “<your_username>”
atlas_user_key = “<your_api_key>”

auth = HTTPDigestAuth(atlas_user, atlas_user_key)

response = requests.get(base_url, auth=auth)
pprint.pprint(response.json())

But looking for suggestions on how to construct the url to retrieve the user list. I came up with https://cloud.mongodb.com/api/atlas/v2/groups/self/databaseUsers but keep getting and error that thats an invalid url. Not sure how to construct the url so I can query the list of databases users. Any suggestions would be greatly appreciated

Hi @Chase_Russell

The syntax for this call is https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/databaseUsers the groupId is required not self.

 curl -s -n --digest \
--header "Accept: application/vnd.atlas.2023-11-15+json"  \
--request GET "https://cloud.mongodb.com/api/atlas/v2/groups/588fc162d383ad09d65f7ae2/databaseUsers?pretty=true" \
| jq '.results[]|select(.username=="cdellaway")'
Output, click to expand
{
  "awsIAMType": "NONE",
  "databaseName": "admin",
  "groupId": "588fc162d383ad09d65f7ae2",
  "labels": [],
  "ldapAuthType": "NONE",
  "links": [
    {
      "href": "https://cloud.mongodb.com/api/atlas/v2/groups/588fc162d383ad09d65f7ae2/databaseUsers/admin/cdellaway",
      "rel": "self"
    }
  ],
  "oidcAuthType": "NONE",
  "roles": [
    {
      "databaseName": "admin",
      "roleName": "atlasAdmin"
    },
    {
      "databaseName": "admin",
      "roleName": "clusterMonitor"
    }
  ],
  "scopes": [],
  "username": "cdellaway",
  "x509Type": "NONE"
}