[SERVER-12643] wrong exit code from the shell when incorrectly connecting to an authenticated database Created: 06/Feb/14  Updated: 06/Dec/22  Resolved: 19/Nov/21

Status: Closed
Project: Core Server
Component/s: Security, Shell
Affects Version/s: 2.4.9
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Daniel Coupal Assignee: Backlog - Server Tooling and Methods (STM) (Inactive)
Resolution: Won't Fix Votes: 0
Labels: move-stm
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MacOSX, but got the same under Linux


Assigned Teams:
Server Tooling & Methods
Operating System: ALL
Participants:

 Description   

If you run an 'eval' query through 'mongo' on an authenticated database, you get the following:

1) providing wrong credentials

MacBook-Pro:mongotools me$ mongo --quiet --host 172.16.65.202 --port 27017 --username user --password pass --eval "printjson(db.adminCommand('listDatabases').databases)" test

Thu Jan 30 13:23:39.744 Error: 18

{ code: 18, ok: 0.0, errmsg: "auth fails" }

at src/mongo/shell/db.js:228
exception: login failed

MacBook-Pro:mongotools me$ echo $?
1

2) not trying to authenticate (missing user/password)

MacBook-Pro:mongotools me$ mongo --quiet --host 172.16.65.202 --port 27017 --eval "printjson(db.adminCommand('listDatabases').databases)" test
undefined

MacBook-Pro:mongotools me$ echo $?
0 <=== Incoherent

In authenticating with wrong credentials (1), you get the expected error code (non-zero) from the shell.
In not passing credentials (2), you get 'undefined' as the output, which is not really descriptive.
But the main issue is getting 0 as the exit code.

From a calling script, the result in providing no credentials or bad credentials should be the same.
Also, I think it should be 1, as the expected result is not sent.

Apart from being coherent with Unix commands, one scenario for which it is important to report a failure is the following:

  • write script, test it, deploy it.
  • authentication is added to the DB.
  • the initial script, not using auth, should start failing by seeing a different return code.


 Comments   
Comment by Brooke Miller [ 19/Nov/21 ]

We've deprecated the mongo shell in favor of the new mongosh . Unfortunately, we aren't able to pursue improvements to the deprecated shell except in extreme cases, such as critical security fixes. Can you please start making use of mongosh and let us know if it works for you in this case?

Generated at Thu Feb 08 03:29:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.