Skip to content

Commit 31a1049

Browse files
committed
Support for authorizedDatabases option
JAVA-3694
1 parent a1a668f commit 31a1049

File tree

16 files changed

+146
-10
lines changed

16 files changed

+146
-10
lines changed

driver-core/src/main/com/mongodb/internal/async/client/AsyncListDatabasesIterable.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,15 @@ public interface AsyncListDatabasesIterable<T> extends AsyncMongoIterable<T> {
6767
* @mongodb.server.release 3.4.3
6868
*/
6969
AsyncListDatabasesIterable<T> nameOnly(@Nullable Boolean nameOnly);
70+
71+
/**
72+
* Sets the authorizedDatabasesOnly flag that indicates whether the command should return just the databases which the user
73+
* is authorized to see.
74+
*
75+
* @param authorizedDatabasesOnly the authorizedDatabasesOnly flag, which may be null
76+
* @return this
77+
* @since 4.1
78+
* @mongodb.server.release 4.0
79+
*/
80+
AsyncListDatabasesIterable<T> authorizedDatabasesOnly(@Nullable Boolean authorizedDatabasesOnly);
7081
}

driver-core/src/main/com/mongodb/internal/async/client/AsyncListDatabasesIterableImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ final class AsyncListDatabasesIterableImpl<TResult> extends AsyncMongoIterableIm
3838
private long maxTimeMS;
3939
private Bson filter;
4040
private Boolean nameOnly;
41+
private Boolean authorizedDatabasesOnly;
4142

4243
AsyncListDatabasesIterableImpl(@Nullable final AsyncClientSession clientSession, final Class<TResult> resultClass,
4344
final CodecRegistry codecRegistry, final ReadPreference readPreference,
@@ -73,8 +74,14 @@ public AsyncListDatabasesIterable<TResult> nameOnly(@Nullable final Boolean name
7374
return this;
7475
}
7576

77+
@Override
78+
public AsyncListDatabasesIterable<TResult> authorizedDatabasesOnly(@Nullable final Boolean authorizedDatabasesOnly) {
79+
this.authorizedDatabasesOnly = authorizedDatabasesOnly;
80+
return this;
81+
}
82+
7683
@Override
7784
AsyncReadOperation<AsyncBatchCursor<TResult>> asAsyncReadOperation() {
78-
return operations.listDatabases(resultClass, filter, nameOnly, maxTimeMS);
85+
return operations.listDatabases(resultClass, filter, nameOnly, maxTimeMS, authorizedDatabasesOnly);
7986
}
8087
}

driver-core/src/main/com/mongodb/internal/operation/AsyncOperations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,9 @@ public <TResult> AsyncReadOperation<AsyncBatchCursor<TResult>> listCollections(f
251251
}
252252

253253
public <TResult> AsyncReadOperation<AsyncBatchCursor<TResult>> listDatabases(final Class<TResult> resultClass, final Bson filter,
254-
final Boolean nameOnly, final long maxTimeMS) {
255-
return operations.listDatabases(resultClass, filter, nameOnly, maxTimeMS);
254+
final Boolean nameOnly, final long maxTimeMS,
255+
final Boolean authorizedDatabasesOnly) {
256+
return operations.listDatabases(resultClass, filter, nameOnly, maxTimeMS, authorizedDatabasesOnly);
256257
}
257258

258259
public <TResult> AsyncReadOperation<AsyncBatchCursor<TResult>> listIndexes(final Class<TResult> resultClass, final Integer batchSize,

driver-core/src/main/com/mongodb/internal/operation/ListDatabasesOperation.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public class ListDatabasesOperation<T> implements AsyncReadOperation<AsyncBatchC
5858
private long maxTimeMS;
5959
private BsonDocument filter;
6060
private Boolean nameOnly;
61+
private Boolean authorizedDatabasesOnly;
6162

6263
/**
6364
* Construct a new instance.
@@ -132,6 +133,18 @@ public ListDatabasesOperation<T> nameOnly(final Boolean nameOnly) {
132133
return this;
133134
}
134135

136+
/**
137+
* Sets the authorizedDatabasesOnly flag that indicates whether the command should return just the databases which the user
138+
* is authorized to see.
139+
*
140+
* @param authorizedDatabasesOnly the authorizedDatabasesOnly flag, which may be null
141+
* @return this
142+
*/
143+
public ListDatabasesOperation<T> authorizedDatabasesOnly(final Boolean authorizedDatabasesOnly) {
144+
this.authorizedDatabasesOnly = authorizedDatabasesOnly;
145+
return this;
146+
}
147+
135148
/**
136149
* Enables retryable reads if a read fails due to a network error.
137150
*
@@ -166,6 +179,17 @@ public Boolean getNameOnly() {
166179
return nameOnly;
167180
}
168181

182+
/**
183+
* Gets the authorizedDatabasesOnly flag that indicates whether the command should return just the databases which the user
184+
* is authorized to see.
185+
*
186+
* @return the authorized databases value
187+
* @since 4.1
188+
*/
189+
public Boolean getAuthorizedDatabasesOnly() {
190+
return authorizedDatabasesOnly;
191+
}
192+
169193
/**
170194
* Executing this will return a list of all the databases names in the MongoDB instance.
171195
*
@@ -231,6 +255,9 @@ private BsonDocument getCommand() {
231255
if (nameOnly != null) {
232256
command.put("nameOnly", new BsonBoolean(nameOnly));
233257
}
258+
if (authorizedDatabasesOnly != null) {
259+
command.put("authorizedDatabases", new BsonBoolean(authorizedDatabasesOnly));
260+
}
234261
return command;
235262
}
236263
}

driver-core/src/main/com/mongodb/internal/operation/Operations.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,13 @@ <TResult> ListCollectionsOperation<TResult> listCollections(final String databas
508508
}
509509

510510
<TResult> ListDatabasesOperation<TResult> listDatabases(final Class<TResult> resultClass, final Bson filter,
511-
final Boolean nameOnly, final long maxTimeMS) {
511+
final Boolean nameOnly, final long maxTimeMS,
512+
final Boolean authorizedDatabasesOnly) {
512513
return new ListDatabasesOperation<TResult>(codecRegistry.get(resultClass)).maxTime(maxTimeMS, MILLISECONDS)
513514
.retryReads(retryReads)
514515
.filter(toBsonDocumentOrNull(filter))
515-
.nameOnly(nameOnly);
516+
.nameOnly(nameOnly)
517+
.authorizedDatabasesOnly(authorizedDatabasesOnly);
516518
}
517519

518520
<TResult> ListIndexesOperation<TResult> listIndexes(final Class<TResult> resultClass, final Integer batchSize,

driver-core/src/main/com/mongodb/internal/operation/SyncOperations.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ public <TResult> ReadOperation<BatchCursor<TResult>> listCollections(final Strin
235235
}
236236

237237
public <TResult> ReadOperation<BatchCursor<TResult>> listDatabases(final Class<TResult> resultClass, final Bson filter,
238-
final Boolean nameOnly, final long maxTimeMS) {
239-
return operations.listDatabases(resultClass, filter, nameOnly, maxTimeMS);
238+
final Boolean nameOnly, final long maxTimeMS,
239+
final Boolean authorizedDatabases) {
240+
return operations.listDatabases(resultClass, filter, nameOnly, maxTimeMS, authorizedDatabases);
240241
}
241242

242243
public <TResult> ReadOperation<BatchCursor<TResult>> listIndexes(final Class<TResult> resultClass, final Integer batchSize,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ class ListDatabasesOperationSpecification extends OperationFunctionalSpecificati
6464
then:
6565
names.contains(getDatabaseName())
6666

67+
when:
68+
operation = operation.authorizedDatabasesOnly(true).nameOnly(true)
69+
.filter(new BsonDocument('name', new BsonRegularExpression("^${getDatabaseName()}")))
70+
names = executeAndCollectBatchCursorResults(operation, async)*.get('name')
71+
72+
then:
73+
names.contains(getDatabaseName())
74+
6775
where:
6876
async << [true, false]
6977
}

driver-core/src/test/unit/com/mongodb/internal/async/client/AsyncListDatabasesIterableSpecification.groovy

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class AsyncListDatabasesIterableSpecification extends Specification {
4848
it[0].onResult(null, null)
4949
}
5050
}
51-
def executor = new TestOperationExecutor([cursor, cursor]);
51+
def executor = new TestOperationExecutor([cursor, cursor, cursor]);
5252
def listDatabasesIterable = new AsyncListDatabasesIterableImpl<Document>(null, Document, codecRegistry, readPreference,
5353
executor, true)
5454
.maxTime(1000, MILLISECONDS)
@@ -73,6 +73,16 @@ class AsyncListDatabasesIterableSpecification extends Specification {
7373
then: 'should use the overrides'
7474
expect operation, isTheSameAs(new ListDatabasesOperation<Document>(new DocumentCodec()).maxTime(999, MILLISECONDS)
7575
.filter(BsonDocument.parse('{a: 1}')).nameOnly(true).retryReads(true))
76+
77+
when: 'overriding initial options'
78+
listDatabasesIterable.maxTime(101, MILLISECONDS).filter(Document.parse('{a: 1}'))
79+
.authorizedDatabasesOnly(true).into([]) { result, t -> }
80+
81+
operation = executor.getReadOperation() as ListDatabasesOperation<Document>
82+
83+
then: 'should use the overrides'
84+
expect operation, isTheSameAs(new ListDatabasesOperation<Document>(new DocumentCodec()).maxTime(101, MILLISECONDS)
85+
.filter(BsonDocument.parse('{a: 1}')).nameOnly(true).authorizedDatabasesOnly(true).retryReads(true))
7686
}
7787

7888
def 'should follow the MongoIterable interface as expected'() {

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/ListDatabasesPublisher.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ public interface ListDatabasesPublisher<TResult> extends Publisher<TResult> {
6161
*/
6262
ListDatabasesPublisher<TResult> nameOnly(Boolean nameOnly);
6363

64+
/**
65+
* Sets the authorizedDatabasesOnly flag that indicates whether the command should return just the databases which the user
66+
* is authorized to see.
67+
*
68+
* @param authorizedDatabasesOnly the authorizedDatabasesOnly flag, which may be null
69+
* @return this
70+
* @since 4.1
71+
* @mongodb.server.release 4.0
72+
*/
73+
ListDatabasesPublisher<TResult> authorizedDatabasesOnly(Boolean authorizedDatabasesOnly);
74+
6475
/**
6576
* Sets the number of documents to return per batch.
6677
*

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/ListDatabasesPublisherImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public ListDatabasesPublisher<TResult> nameOnly(final Boolean nameOnly) {
5454
return this;
5555
}
5656

57+
@Override
58+
public ListDatabasesPublisher<TResult> authorizedDatabasesOnly(final Boolean authorizedDatabasesOnly) {
59+
wrapped.authorizedDatabasesOnly(authorizedDatabasesOnly);
60+
return this;
61+
}
62+
5763
@Override
5864
public ListDatabasesPublisher<TResult> batchSize(final int batchSize) {
5965
wrapped.batchSize(batchSize);

0 commit comments

Comments
 (0)