41
41
import java .io .OutputStream ;
42
42
import java .net .InetSocketAddress ;
43
43
import java .net .Socket ;
44
- import java .util .Arrays ;
45
44
import java .util .HashMap ;
46
45
import java .util .Map ;
47
46
import java .util .concurrent .ConcurrentHashMap ;
@@ -317,15 +316,15 @@ protected void close(){
317
316
void checkAuth (DB db ) throws IOException {
318
317
if (db .getMongo ().getCredentials () != null ) {
319
318
if (_saslAuthenticator == null ) {
320
- if (db .getMongo ().getCredentials ().getMechanism ().equals (MongoClientCredentials .GSSAPI_MECHANISM )) {
319
+ if (db .getMongo ().getCredentials ().getMechanism ().equals (MongoCredentials .GSSAPI_MECHANISM )) {
321
320
_saslAuthenticator = new GSSAPIAuthenticator (db .getMongo ());
322
- } else if (db .getMongo ().getCredentials ().getMechanism ().equals (MongoClientCredentials .CRAM_MD5_MECHANISM )) {
321
+ } else if (db .getMongo ().getCredentials ().getMechanism ().equals (MongoCredentials .CRAM_MD5_MECHANISM )) {
323
322
_saslAuthenticator = new CRAMMD5Authenticator (db .getMongo ());
324
323
} else {
325
324
throw new MongoException ("Unsupported authentication mechanism: " + db .getMongo ().getCredentials ().getMechanism ());
326
325
}
326
+ _saslAuthenticator .authenticate ();
327
327
}
328
- _saslAuthenticator .acquirePrivilegeForDatabase (db );
329
328
}
330
329
else {
331
330
DB .AuthenticationCredentials credentials = db .getAuthenticationCredentials ();
@@ -392,9 +391,9 @@ class CRAMMD5Authenticator extends SaslAuthenticator {
392
391
public static final String CRAM_MD5_MECHANISM = "CRAM-MD5" ;
393
392
394
393
CRAMMD5Authenticator (final Mongo mongo ) {
395
- super (mongo );
394
+ super (mongo );
396
395
397
- if (!mongo .getCredentials ().getMechanism ().equals (MongoClientCredentials .CRAM_MD5_MECHANISM )) {
396
+ if (!mongo .getCredentials ().getMechanism ().equals (MongoCredentials .CRAM_MD5_MECHANISM )) {
398
397
throw new MongoException ("Incorrect mechanism: " + mongo .getCredentials ().getMechanism ());
399
398
}
400
399
}
@@ -416,7 +415,12 @@ protected Object getMechanism() {
416
415
417
416
@ Override
418
417
protected String getUserNameForMechanism () {
419
- return mongo .getCredentials ().getDatabase () + "$" + mongo .getCredentials ().getUserName ();
418
+ return mongo .getCredentials ().getUserName ();
419
+ }
420
+
421
+ @ Override
422
+ protected DB getDatabase () {
423
+ return mongo .getDB (mongo .getCredentials ().getDatabase ());
420
424
}
421
425
422
426
class CredentialsHandlingCallbackHandler implements CallbackHandler {
@@ -436,7 +440,8 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
436
440
}
437
441
}
438
442
}
439
- // TODO: copoied from DB.AuthenticationCredentials
443
+
444
+ // TODO: copied from DB.AuthenticationCredentials
440
445
byte [] createHash ( String userName , char [] password ){
441
446
ByteArrayOutputStream bout = new ByteArrayOutputStream ( userName .length () + 20 + password .length );
442
447
try {
@@ -453,7 +458,6 @@ byte[] createHash( String userName , char[] password ){
453
458
}
454
459
return Util .hexMD5 (bout .toByteArray ()).getBytes ();
455
460
}
456
-
457
461
}
458
462
459
463
class GSSAPIAuthenticator extends SaslAuthenticator {
@@ -463,7 +467,7 @@ class GSSAPIAuthenticator extends SaslAuthenticator {
463
467
GSSAPIAuthenticator (final Mongo mongo ) {
464
468
super (mongo );
465
469
466
- if (!mongo .getCredentials ().getMechanism ().equals (MongoClientCredentials .GSSAPI_MECHANISM )) {
470
+ if (!mongo .getCredentials ().getMechanism ().equals (MongoCredentials .GSSAPI_MECHANISM )) {
467
471
throw new MongoException ("Incorrect mechanism: " + mongo .getCredentials ().getMechanism ());
468
472
}
469
473
}
@@ -493,6 +497,11 @@ protected String getUserNameForMechanism() {
493
497
return mongo .getCredentials ().getUserName ();
494
498
}
495
499
500
+ @ Override
501
+ protected DB getDatabase () {
502
+ return mongo .getDB ("$external" );
503
+ }
504
+
496
505
private GSSCredential getGSSCredential (String userName ) throws GSSException {
497
506
Oid krb5Mechanism = new Oid (GSSAPI_OID );
498
507
GSSManager manager = GSSManager .getInstance ();
@@ -566,32 +575,21 @@ void authenticate() {
566
575
567
576
protected abstract String getUserNameForMechanism ();
568
577
578
+ protected abstract DB getDatabase ();
579
+
569
580
private CommandResult sendSaslStart (final byte [] outToken ) throws IOException {
570
581
DBObject cmd = new BasicDBObject ("saslStart" , 1 ).append ("mechanism" , getMechanism ()).append ("payload" ,
571
582
outToken != null ? outToken : new byte [0 ]);
572
- return runCommand (mongo . getDB ( "admin" ), cmd );
583
+ return runCommand (getDatabase ( ), cmd );
573
584
}
574
585
575
586
private CommandResult sendSaslContinue (final int conversationId , final byte [] outToken ) throws IOException {
576
- DB adminDB = mongo . getDB ( "admin" );
587
+ DB adminDB = getDatabase ( );
577
588
DBObject cmd = new BasicDBObject ("saslContinue" , 1 ).append ("conversationId" , conversationId ).
578
589
append ("payload" , outToken );
579
590
return runCommand (adminDB , cmd );
580
591
}
581
592
582
- public void acquirePrivilegeForDatabase (final DB db ) throws IOException {
583
- authenticate ();
584
-
585
- if (authorizeDatabases .get (db ) == null ) {
586
- BasicDBObject acquirePrivilegeCmd = new BasicDBObject ("acquirePrivilege" , 1 ).
587
- append ("principal" , getUserNameForMechanism ()).
588
- append ("resource" , db .getName ()).
589
- append ("actions" , Arrays .asList ("oldWrite" ));
590
- CommandResult res = runCommand (db .getSisterDB ("admin" ), acquirePrivilegeCmd );
591
- res .throwOnError ();
592
- authorizeDatabases .put (db , true );
593
- }
594
- }
595
593
}
596
594
597
595
private static Logger _rootLogger = Logger .getLogger ( "com.mongodb.port" );
0 commit comments