1Panel-dev/1Panel · error · Error

failed to load mongodb user privileges

Error message

failed to load mongodb user privileges

What it means

loadMongodbPrivilege's script (database_mongodb.go:745) queries usersInfo for a user on a remote/local connection and requires ok === 1. Identical failure class to error 2, but reached through the privilege-view path: the connection's user cannot run usersInfo on that db, or the remote connection info itself is unusable.

Source

Thrown at agent/app/service/database_mongodb.go:745

func loadLocalMongodbPrivilege(connectionName, dbName, username string) (string, error) {
	databaseJSON, err := json.Marshal(dbName)
	if err != nil {
		return "", err
	}
	usernameJSON, err := json.Marshal(username)
	if err != nil {
		return "", err
	}
	script := strings.TrimSpace(fmt.Sprintf(`
const dbName = %s;
const userName = %s;
const result = db.getSiblingDB(dbName).runCommand({
  usersInfo: userName,
  showCredentials: false,
  showCustomData: false
});
if (!result || result.ok !== 1) {
  throw new Error("failed to load mongodb user privileges");
}
const roles = Array.isArray(result.users) && result.users.length > 0 ? result.users[0].roles || [] : [];
const permissions = roles.filter(role => role.db === dbName).map(role => role.role);
print("__1panel_json_begin__");
print(JSON.stringify(permissions));
print("__1panel_json_end__");
`, databaseJSON, usernameJSON))
	stdout, err := runMongodbAdminScriptWithStdout(connectionName, script)
	if err != nil {
		return "", err
	}
	var permissions []string
	jsonResult, err := extractMongodbJSONOutput(stdout)
	if err != nil {
		return "", err
	}
	if err := json.Unmarshal(jsonResult, &permissions); err != nil {
		return "", err

View on GitHub (pinned to 5ac7c80881)

Solutions

  1. Run usersInfo manually with the same credential and read codeName/errmsg
  2. Upgrade the connection user's roles (userAdmin/viewUser on the db) or re-point the connection to root
  3. Verify the user's home db via usersInfo on admin and align the record's dbName

Example fix

null
Defensive patterns

Strategy: validation

Validate before calling

// before loading privileges, confirm the connection can run usersInfo
// one-line probe through the same connection, expect ok:1

Try / catch

perms, err := loadMongodbPrivilege(...)
if err != nil && strings.Contains(err.Error(), "failed to load mongodb user privileges") {
    // fall back to showing the user's record without live permissions; prompt credential fix
}

Prevention

When it happens

Trigger: Loading a user's permission list in the 1Panel UI when the connection credential lacks viewUser; remote connection (loadRemoteMongodbConnection) configured with a limited user; dbName on the record differs from the db that owns the user so the command is rejected.

Common situations: Remote MongoDB with a least-privilege monitoring user; credential drift after rotation; user created on `admin` while the record points at a workload db.

Related errors


AI-assisted analysis of 1Panel-dev/1Panel@5ac7c80881 (2026-08-15). Data as JSON: /api/errors/9d214654acbfb9c7. Report an issue: GitHub.