Skip to content

Commit e363f90

Browse files
committed
Changed class names provisionally by removing 'Client' from MongoClientAuthority and MongoClientCredentials
1 parent 7f252f5 commit e363f90

File tree

8 files changed

+76
-63
lines changed

8 files changed

+76
-63
lines changed

examples/CramMd5CredentialsExample.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import com.mongodb.BasicDBObject;
1818
import com.mongodb.DB;
19+
import com.mongodb.MongoAuthority;
1920
import com.mongodb.MongoClient;
20-
import com.mongodb.MongoClientAuthority;
21-
import com.mongodb.MongoClientCredentials;
21+
import com.mongodb.MongoCredentials;
2222
import com.mongodb.MongoClientOptions;
2323
import com.mongodb.ServerAddress;
2424
import com.mongodb.WriteResult;
@@ -41,19 +41,18 @@ public static void main(String[] args) throws UnknownHostException, InterruptedE
4141
String pwd = args[2];
4242
String db = args[3];
4343
MongoClient mongo = new MongoClient(
44-
new MongoClientAuthority(new ServerAddress(server),
45-
new MongoClientCredentials(user, pwd.toCharArray(),
46-
MongoClientCredentials.CRAM_MD5_MECHANISM, db)),
47-
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
44+
new MongoAuthority(new ServerAddress(server),
45+
new MongoCredentials(user, pwd.toCharArray(),
46+
MongoCredentials.CRAM_MD5_MECHANISM, db)),
47+
new MongoClientOptions.Builder().build());
4848
DB testDB = mongo.getDB(db);
49-
System.out.println("Find one: " + testDB.getCollection("test").findOne());
49+
System.out.println("Find one: " + testDB.getCollection("test").findOne());
5050
System.out.println("Count: " + testDB.getCollection("test").count());
5151
WriteResult writeResult = testDB.getCollection("test").insert(new BasicDBObject());
5252
System.out.println("Write result: " + writeResult);
5353

5454
System.out.println();
5555

5656
System.out.println("Count: " + testDB.getCollection("test").count());
57-
5857
}
5958
}

examples/DefaultSecurityCallbackHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
2626
for (Callback callback : callbacks) {
2727
if (callback instanceof NameCallback) {
2828
NameCallback nameCallback = (NameCallback) callback;
29-
nameCallback.setName("[email protected]");
29+
nameCallback.setName("<your name goes here>"); // or just return
3030
}
3131
if (callback instanceof PasswordCallback) {
3232
PasswordCallback passwordCallback = (PasswordCallback) callback;
33-
passwordCallback.setPassword("a".toCharArray());
33+
passwordCallback.setPassword("<your password goes here>".toCharArray()); // or just return
3434
}
3535
}
3636
}

examples/GSSAPICredentialsExample.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
import com.mongodb.BasicDBObject;
1818
import com.mongodb.DB;
19+
import com.mongodb.MongoAuthority;
1920
import com.mongodb.MongoClient;
20-
import com.mongodb.MongoClientAuthority;
21-
import com.mongodb.MongoClientCredentials;
21+
import com.mongodb.MongoCredentials;
2222
import com.mongodb.MongoClientOptions;
2323
import com.mongodb.ServerAddress;
2424
import com.mongodb.WriteResult;
@@ -32,7 +32,7 @@
3232
* Usage:
3333
* </p>
3434
* <pre>
35-
* java CramMd5CredentialsExample server userName databaseName
35+
* java GSSAPICredentialsExample server userName databaseName
3636
* </pre>
3737
*/
3838
public class GSSAPICredentialsExample {
@@ -44,22 +44,38 @@ public class GSSAPICredentialsExample {
4444
// 3. Set system properties, e.g.:
4545
// -Djava.security.krb5.realm=10GEN.ME -Djavax.security.auth.useSubjectCredsOnly=false -Djava.security.krb5.kdc=kdc.10gen.me
4646
// auth.login.defaultCallbackHandler=name of class that implements javax.security.auth.callback.CallbackHandler
47-
// You may also need to define realms and domain_realm entries in your krb5.conf file (in /etc by default)
4847
public static void main(String[] args) throws UnknownHostException, InterruptedException {
49-
// Set this property to avoid the default behavior where the program prompts on the command line
50-
// for username/password
48+
// Set this property to avoid the default behavior where the program prompts on the command line for username/password
5149
Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");
5250

5351
String server = args[0];
5452
String user = args[1];
55-
String dbName = args[2];
53+
String databaseName = args[2];
54+
55+
System.out.println("javax.security.auth.useSubjectCredsOnly: " + System.getProperty("javax.security.auth.useSubjectCredsOnly"));
56+
System.out.println("java.security.krb5.realm: " + System.getProperty("java.security.krb5.realm"));
57+
System.out.println("java.security.krb5.kdc: " + System.getProperty("java.security.krb5.kdc"));
58+
System.out.println("auth.login.defaultCallbackHandler: " + Security.getProperty("auth.login.defaultCallbackHandler"));
59+
System.out.println("login.configuration.provider: " + Security.getProperty("login.configuration.provider"));
60+
System.out.println("java.security.auth.login.config: " + Security.getProperty("java.security.auth.login.config"));
61+
System.out.println("login.config.url.1: " + Security.getProperty("login.config.url.1"));
62+
System.out.println("login.config.url.2: " + Security.getProperty("login.config.url.2"));
63+
System.out.println("login.config.url.3: " + Security.getProperty("login.config.url.3"));
64+
65+
System.out.println("server: " + server);
66+
System.out.println("user: " + user);
67+
System.out.println("database: " + databaseName);
68+
69+
System.out.println();
70+
71+
Thread.sleep(5000);
5672

5773
MongoClient mongo = new MongoClient(
58-
new MongoClientAuthority(new ServerAddress(server),
59-
new MongoClientCredentials(user, MongoClientCredentials.GSSAPI_MECHANISM)),
74+
new MongoAuthority(new ServerAddress(server),
75+
new MongoCredentials(user, MongoCredentials.GSSAPI_MECHANISM)),
6076
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
61-
DB testDB = mongo.getDB(dbName);
62-
System.out.println("Find one: " + testDB.getCollection("test").findOne());
77+
DB testDB = mongo.getDB(databaseName);
78+
System.out.println("Find one: " + testDB.getCollection("test").findOne());
6379
System.out.println("Count: " + testDB.getCollection("test").count());
6480
WriteResult writeResult = testDB.getCollection("test").insert(new BasicDBObject());
6581
System.out.println("Write result: " + writeResult);

src/main/com/mongodb/DBPort.java

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.io.OutputStream;
4242
import java.net.InetSocketAddress;
4343
import java.net.Socket;
44-
import java.util.Arrays;
4544
import java.util.HashMap;
4645
import java.util.Map;
4746
import java.util.concurrent.ConcurrentHashMap;
@@ -317,15 +316,15 @@ protected void close(){
317316
void checkAuth(DB db) throws IOException {
318317
if (db.getMongo().getCredentials() != null) {
319318
if (_saslAuthenticator == null) {
320-
if (db.getMongo().getCredentials().getMechanism().equals(MongoClientCredentials.GSSAPI_MECHANISM)) {
319+
if (db.getMongo().getCredentials().getMechanism().equals(MongoCredentials.GSSAPI_MECHANISM)) {
321320
_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)) {
323322
_saslAuthenticator = new CRAMMD5Authenticator(db.getMongo());
324323
} else {
325324
throw new MongoException("Unsupported authentication mechanism: " + db.getMongo().getCredentials().getMechanism());
326325
}
326+
_saslAuthenticator.authenticate();
327327
}
328-
_saslAuthenticator.acquirePrivilegeForDatabase(db);
329328
}
330329
else {
331330
DB.AuthenticationCredentials credentials = db.getAuthenticationCredentials();
@@ -392,9 +391,9 @@ class CRAMMD5Authenticator extends SaslAuthenticator {
392391
public static final String CRAM_MD5_MECHANISM = "CRAM-MD5";
393392

394393
CRAMMD5Authenticator(final Mongo mongo) {
395-
super(mongo);
394+
super(mongo);
396395

397-
if (!mongo.getCredentials().getMechanism().equals(MongoClientCredentials.CRAM_MD5_MECHANISM)) {
396+
if (!mongo.getCredentials().getMechanism().equals(MongoCredentials.CRAM_MD5_MECHANISM)) {
398397
throw new MongoException("Incorrect mechanism: " + mongo.getCredentials().getMechanism());
399398
}
400399
}
@@ -416,7 +415,12 @@ protected Object getMechanism() {
416415

417416
@Override
418417
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());
420424
}
421425

422426
class CredentialsHandlingCallbackHandler implements CallbackHandler {
@@ -436,7 +440,8 @@ public void handle(final Callback[] callbacks) throws IOException, UnsupportedCa
436440
}
437441
}
438442
}
439-
// TODO: copoied from DB.AuthenticationCredentials
443+
444+
// TODO: copied from DB.AuthenticationCredentials
440445
byte[] createHash( String userName , char[] password ){
441446
ByteArrayOutputStream bout = new ByteArrayOutputStream( userName.length() + 20 + password.length );
442447
try {
@@ -453,7 +458,6 @@ byte[] createHash( String userName , char[] password ){
453458
}
454459
return Util.hexMD5(bout.toByteArray()).getBytes();
455460
}
456-
457461
}
458462

459463
class GSSAPIAuthenticator extends SaslAuthenticator {
@@ -463,7 +467,7 @@ class GSSAPIAuthenticator extends SaslAuthenticator {
463467
GSSAPIAuthenticator(final Mongo mongo) {
464468
super(mongo);
465469

466-
if (!mongo.getCredentials().getMechanism().equals(MongoClientCredentials.GSSAPI_MECHANISM)) {
470+
if (!mongo.getCredentials().getMechanism().equals(MongoCredentials.GSSAPI_MECHANISM)) {
467471
throw new MongoException("Incorrect mechanism: " + mongo.getCredentials().getMechanism());
468472
}
469473
}
@@ -493,6 +497,11 @@ protected String getUserNameForMechanism() {
493497
return mongo.getCredentials().getUserName();
494498
}
495499

500+
@Override
501+
protected DB getDatabase() {
502+
return mongo.getDB("$external");
503+
}
504+
496505
private GSSCredential getGSSCredential(String userName) throws GSSException {
497506
Oid krb5Mechanism = new Oid(GSSAPI_OID);
498507
GSSManager manager = GSSManager.getInstance();
@@ -566,32 +575,21 @@ void authenticate() {
566575

567576
protected abstract String getUserNameForMechanism();
568577

578+
protected abstract DB getDatabase();
579+
569580
private CommandResult sendSaslStart(final byte[] outToken) throws IOException {
570581
DBObject cmd = new BasicDBObject("saslStart", 1).append("mechanism", getMechanism()).append("payload",
571582
outToken != null ? outToken : new byte[0]);
572-
return runCommand(mongo.getDB("admin"), cmd);
583+
return runCommand(getDatabase(), cmd);
573584
}
574585

575586
private CommandResult sendSaslContinue(final int conversationId, final byte[] outToken) throws IOException {
576-
DB adminDB = mongo.getDB("admin");
587+
DB adminDB = getDatabase();
577588
DBObject cmd = new BasicDBObject("saslContinue", 1).append("conversationId", conversationId).
578589
append("payload", outToken);
579590
return runCommand(adminDB, cmd);
580591
}
581592

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-
}
595593
}
596594

597595
private static Logger _rootLogger = Logger.getLogger( "com.mongodb.port" );

src/main/com/mongodb/Mongo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public Mongo( MongoURI uri )
361361
* @param authority the authority
362362
* @param options the options
363363
*/
364-
public Mongo(MongoClientAuthority authority, MongoOptions options) {
364+
public Mongo(MongoAuthority authority, MongoOptions options) {
365365
_options = options;
366366
_applyMongoOptions();
367367

@@ -377,7 +377,7 @@ public Mongo(MongoClientAuthority authority, MongoOptions options) {
377377
}
378378

379379
if (authority.getCredentials() != null) {
380-
if (authority.getCredentials().getMechanism().equals(MongoClientCredentials.MONGODB_MECHANISM)) {
380+
if (authority.getCredentials().getMechanism().equals(MongoCredentials.MONGODB_MECHANISM)) {
381381
String databaseName;
382382
if (authority.getCredentials().getDatabase() != null) {
383383
databaseName = authority.getCredentials().getDatabase();
@@ -637,7 +637,7 @@ public int getOptions(){
637637
return _netOptions.get();
638638
}
639639

640-
public MongoClientCredentials getCredentials() {
640+
public MongoCredentials getCredentials() {
641641
return _credentials;
642642
}
643643

@@ -692,7 +692,7 @@ boolean isMongosConnection() {
692692
private ReadPreference _readPref = ReadPreference.primary();
693693
final Bytes.OptionHolder _netOptions = new Bytes.OptionHolder( null );
694694
final CursorCleanerThread _cleaner;
695-
MongoClientCredentials _credentials;
695+
MongoCredentials _credentials;
696696

697697
org.bson.util.SimplePool<PoolOutputBuffer> _bufferPool =
698698
new org.bson.util.SimplePool<PoolOutputBuffer>( 1000 ){

src/main/com/mongodb/MongoClientAuthority.java renamed to src/main/com/mongodb/MongoAuthority.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@
2727
* <p>
2828
* Note: This constructor is provisional and is subject to change before the final release
2929
*/
30-
public class MongoClientAuthority {
30+
public class MongoAuthority {
3131
private final ServerAddress serverAddress;
3232
private final List<ServerAddress> serverAddresses;
33-
private final MongoClientCredentials credentials;
33+
private final MongoCredentials credentials;
3434

35-
public MongoClientAuthority(final ServerAddress serverAddress) {
35+
public MongoAuthority(final ServerAddress serverAddress) {
3636
this(serverAddress, null);
3737
}
3838

39-
public MongoClientAuthority(final ServerAddress serverAddress, MongoClientCredentials credentials) {
39+
public MongoAuthority(final ServerAddress serverAddress, MongoCredentials credentials) {
4040
this.serverAddress = serverAddress;
4141
this.credentials = credentials;
4242
this.serverAddresses = null;
4343
}
4444

45-
public MongoClientAuthority(final List<ServerAddress> serverAddresses) {
45+
public MongoAuthority(final List<ServerAddress> serverAddresses) {
4646
this(serverAddresses, null);
4747
}
4848

49-
public MongoClientAuthority(final List<ServerAddress> serverAddresses, MongoClientCredentials credentials) {
49+
public MongoAuthority(final List<ServerAddress> serverAddresses, MongoCredentials credentials) {
5050
this.serverAddresses = serverAddresses;
5151
this.credentials = credentials;
5252
this.serverAddress = null;
@@ -64,7 +64,7 @@ public List<ServerAddress> getServerAddresses() {
6464
return new ArrayList<ServerAddress>(serverAddresses);
6565
}
6666

67-
public MongoClientCredentials getCredentials() {
67+
public MongoCredentials getCredentials() {
6868
return credentials;
6969
}
7070
}

src/main/com/mongodb/MongoClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public MongoClient(MongoClientURI uri) throws UnknownHostException {
200200
* @param authority the authority
201201
* @param options the options
202202
*/
203-
public MongoClient(MongoClientAuthority authority, MongoClientOptions options) {
203+
public MongoClient(MongoAuthority authority, MongoClientOptions options) {
204204
super(authority, new MongoOptions(options));
205205
}
206206
}

src/main/com/mongodb/MongoClientCredentials.java renamed to src/main/com/mongodb/MongoCredentials.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
/**
2121
* Represents credentials to authenticate to a mongo server.
2222
* <p>
23-
* Note: This constructor is provisional and is subject to change before the final release
23+
* Note: This class is provisional and is subject to change before the final release
2424
*/
25-
public class MongoClientCredentials {
25+
public class MongoCredentials {
2626
public static final String MONGODB_MECHANISM = "mongodb";
2727
public static final String GSSAPI_MECHANISM = "GSSAPI";
2828
public static final String CRAM_MD5_MECHANISM = "CRAM-MD5";
@@ -32,15 +32,15 @@ public class MongoClientCredentials {
3232
private final char[] password;
3333
private final String database;
3434

35-
public MongoClientCredentials(final String userName, final char[] password, String mechanism) {
35+
public MongoCredentials(final String userName, final char[] password, String mechanism) {
3636
this(userName, password, mechanism, null);
3737
}
3838

39-
public MongoClientCredentials(final String userName, final String mechanism) {
39+
public MongoCredentials(final String userName, final String mechanism) {
4040
this(userName, null, mechanism);
4141
}
4242

43-
public MongoClientCredentials(final String userName, final char[] password, String mechanism, String database) {
43+
public MongoCredentials(final String userName, final char[] password, String mechanism, String database) {
4444
this.userName = userName;
4545
this.password = password;
4646
this.database = database;

0 commit comments

Comments
 (0)