[JAVA-2414] Can not catch com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' Created: 21/Dec/16  Updated: 27/Oct/23  Resolved: 28/Dec/16

Status: Closed
Project: Java Driver
Component/s: Authentication, Cluster Management
Affects Version/s: 3.4.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Martine [X] Assignee: Unassigned
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB server version is 3.2. mongo java driver is mongo-java-driver-3.4.0.jar. OS is windows 10.



 Description   

I have a test about authenticate by mongo-java-driver-3.4.0.jar as below

try{
        ServerAddress sa = new ServerAddress("192.168.126.63", 27017);
        String db = "admin";
        MongoCredential mc = MongoCredential.createCredential("sunny", db, "s1234".toCharArray());
        List<MongoCredential> mcs = new ArrayList<MongoCredential>();
        mcs.add(mc);
        MongoClientOptions.Builder builder = MongoClientOptions.builder();
        builder.addServerListener(new ServerListenerImpl());
        builder.addCommandListener(new CommandListenerImpl());
        builder.addServerMonitorListener(new ServerMonitorListenerImpl());
        builder.addClusterListener(new ClusterListenerImpl());
        builder.serverSelectionTimeout(1000);
       
        MongoClientOptions options = builder.build();
        MongoClient client = new MongoClient(sa, mcs, options);
        System.out.println("Connect to database successfully="+client.getConnectPoint());
        }catch(Throwable e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() ); 
        }

If give a error username , passoword or databasename, it will throw the following exception:

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='sunny', source='admin', password=<hidden>, mechanismProperties={}}
    at com.mongodb.connection.SaslAuthenticator.wrapInMongoSecurityException(SaslAuthenticator.java:157)
    at com.mongodb.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:37)
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:66)
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:44)
    at com.mongodb.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:162)
    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:44)
    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:109)
    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:46)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
    at java.lang.Thread.run(Thread.java:745)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 192.168.126.63:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }
    at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:117)
    at com.mongodb.connection.SaslAuthenticator.access$000(SaslAuthenticator.java:37)
    at com.mongodb.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:50)
    ... 9 more

How to catch the com.mongodb.MongoSecurityException and do not let it throw to console? I need catch and deal with it in my program.

Thanks in advance
Martine



 Comments   
Comment by Martine [X] [ 29/Dec/16 ]

Hi Jeff,
Thanks your feedback. I have not any question about this issue.

Thanks,
Martime

Comment by Jeffrey Yemin [ 28/Dec/16 ]

As I've not heard back from you I'm closing this issue. Let us know if you have further questions.

Comment by Jeffrey Yemin [ 21/Dec/16 ]

Hi Martine,

There is no way to catch MongoSecurityException, as it's not thrown to the application; it's actually being logged by a background thread. Instead, your application can catch MongoTimeoutException, which is thrown due to any failure to successfully connect to the necessary server in the MongoDB cluster. In this example, Mongo.getConnectPoint() will throw a MongoTimeoutException after, by default, 30 seconds, which as you seem to have discovered you can shorten by setting serverSelectionTimeout on MongoClientOptions.

Comment by Martine [X] [ 21/Dec/16 ]

I want to upgrade mongo java driver from version 2.13.1 to 3.4, but this issue block me. I spend many time to research and can not get result to resolve it.

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