dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#96
Script #96
mongodb
Securityread-onlyMin Version: 4.0

MongoDB User and Role Privilege Audit

Lists users with their roles and highlights root and high-privilege roles. A baseline check for security audits and least privilege.

#users#roles#authentication#hardening
mongodb · javascript
db.getSiblingDB("admin").system.users.find(
  {}, { user: 1, db: 1, roles: 1, "credentials.SCRAM-SHA-256": { $exists: true } }
).toArray().map(u => ({
  user: u.user, db: u.db,
  roles: u.roles.map(r => r.role + "@" + r.db).join(", "),
  privileged: u.roles.some(r => ["root","dbOwner","userAdminAnyDatabase","clusterAdmin"].includes(r.role))
}));
Operational Notes & Remediation Guidance

Ensure roles adhere to the principle of least privilege and review accounts with root or dbOwner access.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 4.0).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.