db.dropUser() not able to drop a user containing a trailing space

Hi how do one go about deleting a user that was created with a trailing space?
I am connected as the admin user to a replica set, with the required permissions to drop users.

db.getUser(“admin”)

{
  _id: 'admin.admin',
  userId: UUID("15920e8b-3019-43cf-8938-9357699d3bf8"),
  user: 'admin',
  db: 'admin',
  roles: [
    { role: 'userAdminAnyDatabase', db: 'admin' },
    { role: 'readWriteAnyDatabase', db: 'admin' },
    { role: 'dbAdminAnyDatabase', db: 'admin' },
    { role: 'dbOwner', db: 'admin' },
    { role: 'clusterAdmin', db: 'admin' }
  ],
  mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}

use admin
‘already on db admin’
db.getUsers()

  users: [
{
      _id: 'admin.someuser  ',
      userId: UUID("4ede2b9e-cb81-44cd-b2bb-6c92a078b350"),
      user: 'someuser  ',
      db: 'admin',
      roles: [Array],
      mechanisms: [Array]
    }
	],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1677838887, i: 923 }),
    signature: {
      hash: Binary(Buffer.from("0e632a7fd10df8bb9f211b0509ffdaa1eb07377b", "hex"), 0),
      keyId: Long("7142600741474009090")
    }
  },
  operationTime: Timestamp({ t: 1677838887, i: 923 })
}

when trying to drop the user containing the trailing space I get a server error.

db.dropUser('someuser ')
MongoServerError: User ‘someuser @admin’ not found

Hello @Leon_De_Leeuw ,

Welcome to The MongoDB Community Forums! :wave:

I tried the db.dropUser() as below and it is working as expected, can you please check if username you are trying to drop is same(number of trailing spaces can be different if you are writing the username yourself, please copy the username from getUser and try to run again)

test> db.createUser( { user: "accountAdmin01 ",
...                  pwd: passwordPrompt(),  // Or  "<cleartext password>"
...                  customData: { employeeId: 12345 },
...                  roles: [ { role: "clusterAdmin", db: "admin" },
...                           { role: "readAnyDatabase", db: "admin" },
...                           "readWrite"] },
...                { w: "majority" , wtimeout: 5000 } )
Enter password
***{ ok: 1 }
test> db.getusers
test.getusers
test> db.getUsers()
{
  users: [
    {
      _id: 'test.accountAdmin01 ',
      userId: new UUID("462ddb92-424d-430d-9d64-79e606eb2f42"),
      user: 'accountAdmin01 ',
      db: 'test',
      customData: { employeeId: 12345 },
      roles: [
        { role: 'readWrite', db: 'test' },
        { role: 'clusterAdmin', db: 'admin' },
        { role: 'readAnyDatabase', db: 'admin' }
      ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}
test> db.dropUser('accountAdmin01 ')
{ ok: 1 }
test> db.getUsers()
{ users: [], ok: 1 }

Regards,
Tarun

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.