Skip to content

Commit 8517edf

Browse files
committed
Deprecated User operations
1 parent a5140b1 commit 8517edf

File tree

7 files changed

+136
-152
lines changed

7 files changed

+136
-152
lines changed

driver-core/src/main/com/mongodb/operation/CreateUserOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
* An operation to create a user.
4747
*
4848
* @since 3.0
49+
* @deprecated use {@link CommandWriteOperation} directly or the mongod shell helpers.
4950
*/
51+
@Deprecated
5052
public class CreateUserOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
5153
private final MongoCredential credential;
5254
private final boolean readOnly;
@@ -57,9 +59,7 @@ public class CreateUserOperation implements AsyncWriteOperation<Void>, WriteOper
5759
*
5860
* @param credential the users credentials.
5961
* @param readOnly true if the user is a readOnly user.
60-
* @deprecated Prefer {@link #CreateUserOperation(MongoCredential, boolean, WriteConcern)}
6162
*/
62-
@Deprecated
6363
public CreateUserOperation(final MongoCredential credential, final boolean readOnly) {
6464
this(credential, readOnly, null);
6565
}
@@ -131,7 +131,7 @@ public void call(final AsyncConnection connection, final Throwable t) {
131131
}
132132

133133
private BsonDocument getCommand(final ConnectionDescription description) {
134-
BsonDocument commandDocument = asCommandDocument(credential, readOnly, "createUser");
134+
BsonDocument commandDocument = asCommandDocument(credential, description, readOnly, "createUser");
135135
appendWriteConcernToCommand(writeConcern, commandDocument, description);
136136
return commandDocument;
137137
}

driver-core/src/main/com/mongodb/operation/DropUserOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
* An operation to remove a user.
4646
*
4747
* @since 3.0
48+
* @deprecated use {@link CommandWriteOperation} directly or the mongod shell helpers.
4849
*/
50+
@Deprecated
4951
public class DropUserOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
5052
private final String databaseName;
5153
private final String userName;
@@ -56,9 +58,7 @@ public class DropUserOperation implements AsyncWriteOperation<Void>, WriteOperat
5658
*
5759
* @param databaseName the name of the database for the operation.
5860
* @param userName the name of the user to be dropped.
59-
* @deprecated Prefer {@link #DropUserOperation(String, String, WriteConcern)}
6061
*/
61-
@Deprecated
6262
public DropUserOperation(final String databaseName, final String userName) {
6363
this(databaseName, userName, null);
6464
}

driver-core/src/main/com/mongodb/operation/UpdateUserOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@
4646
* An operation that updates a user.
4747
*
4848
* @since 3.0
49+
* @deprecated use {@link CommandWriteOperation} directly or the mongod shell helpers.
4950
*/
51+
@Deprecated
5052
public class UpdateUserOperation implements AsyncWriteOperation<Void>, WriteOperation<Void> {
5153
private final MongoCredential credential;
5254
private final boolean readOnly;
@@ -57,9 +59,7 @@ public class UpdateUserOperation implements AsyncWriteOperation<Void>, WriteOper
5759
*
5860
* @param credential the users credentials.
5961
* @param readOnly true if the user is a readOnly user.
60-
* @deprecated Prefer {@link #UpdateUserOperation(MongoCredential, boolean, WriteConcern)}
6162
*/
62-
@Deprecated
6363
public UpdateUserOperation(final MongoCredential credential, final boolean readOnly) {
6464
this(credential, readOnly, null);
6565
}
@@ -131,7 +131,7 @@ public void call(final AsyncConnection connection, final Throwable t) {
131131
}
132132

133133
private BsonDocument getCommand(final ConnectionDescription description) {
134-
BsonDocument commandDocument = asCommandDocument(credential, readOnly, "updateUser");
134+
BsonDocument commandDocument = asCommandDocument(credential, description, readOnly, "updateUser");
135135
appendWriteConcernToCommand(writeConcern, commandDocument, description);
136136
return commandDocument;
137137
}

driver-core/src/main/com/mongodb/operation/UserExistsOperation.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
* An operation that determines if a user exists.
4242
*
4343
* @since 3.0
44+
* @deprecated use {@link CommandWriteOperation} directly or the mongod shell helpers.
4445
*/
46+
@Deprecated
4547
public class UserExistsOperation implements AsyncReadOperation<Boolean>, ReadOperation<Boolean> {
4648
private final String databaseName;
4749
private final String userName;

driver-core/src/main/com/mongodb/operation/UserOperationHelper.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,35 @@
2121
import com.mongodb.MongoInternalException;
2222
import com.mongodb.async.SingleResultCallback;
2323
import com.mongodb.lang.NonNull;
24+
import com.mongodb.connection.ConnectionDescription;
25+
import com.mongodb.connection.ServerVersion;
2426
import org.bson.BsonArray;
2527
import org.bson.BsonBoolean;
2628
import org.bson.BsonDocument;
2729
import org.bson.BsonString;
2830
import org.bson.BsonValue;
2931

30-
import java.util.Arrays;
32+
import java.util.Collections;
3133

3234
import static com.mongodb.internal.authentication.NativeAuthenticationHelper.createAuthenticationHash;
3335
import static com.mongodb.operation.WriteConcernHelper.createWriteConcernError;
3436
import static com.mongodb.operation.WriteConcernHelper.hasWriteConcernError;
3537

3638
final class UserOperationHelper {
39+
private static final ServerVersion FOUR_ZERO = new ServerVersion(3, 7);
3740

38-
static BsonDocument asCommandDocument(final MongoCredential credential, final boolean readOnly, final String commandName) {
41+
static BsonDocument asCommandDocument(final MongoCredential credential, final ConnectionDescription connectionDescription,
42+
final boolean readOnly, final String commandName) {
43+
boolean serverDigestPassword = connectionDescription.getServerVersion().compareTo(FOUR_ZERO) >= 0;
3944
BsonDocument document = new BsonDocument();
40-
document.put(commandName, new BsonString(credential.getUserName()));
41-
document.put("pwd", new BsonString(createAuthenticationHash(getUserNameNonNull(credential), getPasswordNonNull(credential))));
42-
document.put("digestPassword", BsonBoolean.FALSE);
43-
document.put("roles", new BsonArray(Arrays.<BsonValue>asList(new BsonString(getRoleName(credential, readOnly)))));
45+
document.put(commandName, new BsonString(getUserNameNonNull(credential)));
46+
if (serverDigestPassword) {
47+
document.put("pwd", new BsonString(new String(getPasswordNonNull(credential))));
48+
} else {
49+
document.put("pwd", new BsonString(createAuthenticationHash(getUserNameNonNull(credential), getPasswordNonNull(credential))));
50+
}
51+
document.put("digestPassword", BsonBoolean.valueOf(serverDigestPassword));
52+
document.put("roles", new BsonArray(Collections.<BsonValue>singletonList(new BsonString(getRoleName(credential, readOnly)))));
4453
return document;
4554
}
4655

driver-core/src/test/functional/com/mongodb/operation/DropUserOperationSpecification.groovy

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,48 @@ import com.mongodb.OperationFunctionalSpecification
2222
import com.mongodb.WriteConcern
2323
import spock.lang.IgnoreIf
2424

25-
import static com.mongodb.ClusterFixture.executeAsync
2625
import static com.mongodb.ClusterFixture.getBinding
2726
import static com.mongodb.ClusterFixture.isDiscoverableReplicaSet
2827
import static com.mongodb.ClusterFixture.serverVersionAtLeast
2928
import static com.mongodb.MongoCredential.createMongoCRCredential
29+
import static com.mongodb.MongoCredential.createScramSha1Credential
30+
import static com.mongodb.MongoCredential.createScramSha256Credential
31+
3032

31-
@IgnoreIf({ serverVersionAtLeast(3, 7) })
3233
class DropUserOperationSpecification extends OperationFunctionalSpecification {
3334

35+
@IgnoreIf({ !serverVersionAtLeast(3, 0) })
3436
def 'should delete user without error'() {
3537
given:
36-
def credential = createMongoCRCredential('userToDrop', databaseName, '123'.toCharArray())
38+
def credential = createScramSha1Credential('userToDrop', databaseName, '123'.toCharArray())
3739
new CreateUserOperation(credential, true).execute(getBinding())
40+
DropUserOperation operation = new DropUserOperation(databaseName, credential.userName)
3841

3942
when:
43+
execute(operation, async)
44+
45+
then:
46+
notThrown(MongoException)
47+
48+
where:
49+
async << [true, false]
50+
}
51+
52+
@IgnoreIf({ !serverVersionAtLeast(3, 7) })
53+
def 'should delete user without error SHA_256'() {
54+
given:
55+
def credential = createScramSha256Credential('userToDrop', databaseName, '123'.toCharArray())
56+
new CreateUserOperation(credential, true).execute(getBinding())
4057
DropUserOperation operation = new DropUserOperation(databaseName, credential.userName)
41-
operation.execute(getBinding())
58+
59+
when:
60+
execute(operation, async)
4261

4362
then:
4463
notThrown(MongoException)
64+
65+
where:
66+
async << [true, false]
4567
}
4668

4769
@IgnoreIf({ !serverVersionAtLeast(3, 4) || !isDiscoverableReplicaSet() })
@@ -52,7 +74,7 @@ class DropUserOperationSpecification extends OperationFunctionalSpecification {
5274
def operation = new DropUserOperation(databaseName, credential.userName, new WriteConcern(5))
5375

5476
when:
55-
async ? executeAsync(operation) : operation.execute(getBinding())
77+
execute(operation, async)
5678

5779
then:
5880
def ex = thrown(MongoWriteConcernException)

0 commit comments

Comments
 (0)