Skip to content

Commit 2c15f36

Browse files
CSHARP-3053: Support for 'authorizedDatabases' option
1 parent e6a5bfa commit 2c15f36

File tree

10 files changed

+359
-11
lines changed

10 files changed

+359
-11
lines changed

src/MongoDB.Driver.Core/Core/Misc/Feature.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class Feature
7272
private static readonly Feature __keepConnectionPoolWhenReplSetStepDown = new Feature("KeepConnectionPoolWhenReplSetStepDown", new SemanticVersion(4, 1, 10));
7373
private static readonly Feature __killCursorsCommand = new Feature("KillCursorsCommand", new SemanticVersion(3, 2, 0));
7474
private static readonly Feature __listCollectionsCommand = new Feature("ListCollectionsCommand", new SemanticVersion(3, 0, 0));
75+
private static readonly Feature __listDatabasesAuthorizedDatabases = new Feature("ListDatabasesAuthorizedDatabases", new SemanticVersion(4, 0, 5));
7576
private static readonly Feature __listDatabasesFilter = new Feature("ListDatabasesFilter", new SemanticVersion(3, 4, 2));
7677
private static readonly Feature __listDatabasesNameOnlyOption = new Feature("ListDatabasesNameOnlyOption", new SemanticVersion(3, 4, 3));
7778
private static readonly Feature __listIndexesCommand = new Feature("ListIndexesCommand", new SemanticVersion(3, 0, 0));
@@ -342,6 +343,11 @@ public class Feature
342343
/// </summary>
343344
public static Feature IndexOptionsDefaults => __indexOptionsDefaults;
344345

346+
/// <summary>
347+
/// Get the list databases authorizedDatabases feature.
348+
/// </summary>
349+
public static Feature ListDatabasesAuthorizedDatabases => __listDatabasesAuthorizedDatabases;
350+
345351
/// <summary>
346352
/// Gets the list databases filter feature.
347353
/// </summary>

src/MongoDB.Driver.Core/Core/Operations/ListDatabasesOperation.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace MongoDB.Driver.Core.Operations
3030
public class ListDatabasesOperation : IReadOperation<IAsyncCursor<BsonDocument>>
3131
{
3232
// fields
33+
private bool? _authorizedDatabases;
3334
private BsonDocument _filter;
3435
private MessageEncoderSettings _messageEncoderSettings;
3536
private bool? _nameOnly;
@@ -46,6 +47,18 @@ public ListDatabasesOperation(MessageEncoderSettings messageEncoderSettings)
4647
}
4748

4849
// properties
50+
/// <summary>
51+
/// Gets or sets the AuthorizedDatabases flag.
52+
/// </summary>
53+
/// <value>
54+
/// The AuthorizedDatabases flag.
55+
/// </value>
56+
public bool? AuthorizedDatabases
57+
{
58+
get { return _authorizedDatabases; }
59+
set { _authorizedDatabases = value; }
60+
}
61+
4962
/// <summary>
5063
/// Gets or sets the filter.
5164
/// </summary>
@@ -119,7 +132,8 @@ internal BsonDocument CreateCommand()
119132
{
120133
{ "listDatabases", 1 },
121134
{ "filter", _filter, _filter != null },
122-
{ "nameOnly", _nameOnly, _nameOnly != null }
135+
{ "nameOnly", _nameOnly, _nameOnly != null },
136+
{ "authorizedDatabases", _authorizedDatabases, _authorizedDatabases != null }
123137
};
124138
}
125139

src/MongoDB.Driver/IMongoClient.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,36 @@ public interface IMongoClient
9393
IAsyncCursor<string> ListDatabaseNames(
9494
CancellationToken cancellationToken = default(CancellationToken));
9595

96+
/// <summary>
97+
/// Returns the names of the databases on the server.
98+
/// </summary>
99+
/// <param name="cancellationToken">The cancellation token.</param>
100+
/// <param name="options">The options.</param>
101+
/// <returns>The database names.</returns>
102+
IAsyncCursor<string> ListDatabaseNames(
103+
ListDatabaseNamesOptions options,
104+
CancellationToken cancellationToken = default(CancellationToken));
105+
106+
/// <summary>
107+
/// Returns the names of the databases on the server.
108+
/// </summary>
109+
/// <param name="session">The session.</param>
110+
/// <param name="cancellationToken">The cancellation token.</param>
111+
/// <returns>The database names.</returns>
112+
IAsyncCursor<string> ListDatabaseNames(
113+
IClientSessionHandle session,
114+
CancellationToken cancellationToken = default(CancellationToken));
115+
96116
/// <summary>
97117
/// Returns the names of the databases on the server.
98118
/// </summary>
99119
/// <param name="session">The session.</param>
120+
/// <param name="options">The options.</param>
100121
/// <param name="cancellationToken">The cancellation token.</param>
101122
/// <returns>The database names.</returns>
102123
IAsyncCursor<string> ListDatabaseNames(
103124
IClientSessionHandle session,
125+
ListDatabaseNamesOptions options,
104126
CancellationToken cancellationToken = default(CancellationToken));
105127

106128
/// <summary>
@@ -111,14 +133,36 @@ IAsyncCursor<string> ListDatabaseNames(
111133
Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
112134
CancellationToken cancellationToken = default(CancellationToken));
113135

136+
/// <summary>
137+
/// Returns the names of the databases on the server.
138+
/// </summary>
139+
/// <param name="options">The options.</param>
140+
/// <param name="cancellationToken">The cancellation token.</param>
141+
/// <returns>The database names.</returns>
142+
Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
143+
ListDatabaseNamesOptions options,
144+
CancellationToken cancellationToken = default(CancellationToken));
145+
146+
/// <summary>
147+
/// Returns the names of the databases on the server.
148+
/// </summary>
149+
/// <param name="session">The session.</param>
150+
/// <param name="cancellationToken">The cancellation token.</param>
151+
/// <returns>The database names.</returns>
152+
Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
153+
IClientSessionHandle session,
154+
CancellationToken cancellationToken = default(CancellationToken));
155+
114156
/// <summary>
115157
/// Returns the names of the databases on the server.
116158
/// </summary>
117159
/// <param name="session">The session.</param>
160+
/// <param name="options">The options.</param>
118161
/// <param name="cancellationToken">The cancellation token.</param>
119162
/// <returns>The database names.</returns>
120163
Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
121164
IClientSessionHandle session,
165+
ListDatabaseNamesOptions options,
122166
CancellationToken cancellationToken = default(CancellationToken));
123167

124168
/// <summary>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright 2020-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using MongoDB.Bson;
17+
18+
namespace MongoDB.Driver
19+
{
20+
/// <summary>
21+
/// Options for a list database names operation.
22+
/// </summary>
23+
public sealed class ListDatabaseNamesOptions
24+
{
25+
// fields
26+
private bool? _authorizedDatabases;
27+
private FilterDefinition<BsonDocument> _filter;
28+
29+
// properties
30+
/// <summary>
31+
/// Gets or sets the AuthorizedDatabases flag.
32+
/// </summary>
33+
public bool? AuthorizedDatabases
34+
{
35+
get { return _authorizedDatabases; }
36+
set { _authorizedDatabases = value; }
37+
}
38+
39+
/// <summary>
40+
/// Gets or sets the filter.
41+
/// </summary>
42+
public FilterDefinition<BsonDocument> Filter
43+
{
44+
get { return _filter; }
45+
set { _filter = value; }
46+
}
47+
}
48+
}

src/MongoDB.Driver/ListDatabasesOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@ namespace MongoDB.Driver
2323
public sealed class ListDatabasesOptions
2424
{
2525
// fields
26+
private bool? _authorizedDatabases;
2627
private FilterDefinition<BsonDocument> _filter;
2728
private bool? _nameOnly;
2829

2930
// properties
31+
/// <summary>
32+
/// Gets or sets the AuthorizedDatabases flag.
33+
/// </summary>
34+
public bool? AuthorizedDatabases
35+
{
36+
get { return _authorizedDatabases; }
37+
set { _authorizedDatabases = value; }
38+
}
39+
3040
/// <summary>
3141
/// Gets or sets the filter.
3242
/// </summary>

src/MongoDB.Driver/MongoClient.cs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,33 +210,69 @@ public sealed override IMongoDatabase GetDatabase(string name, MongoDatabaseSett
210210
public sealed override IAsyncCursor<string> ListDatabaseNames(
211211
CancellationToken cancellationToken = default(CancellationToken))
212212
{
213-
return UsingImplicitSession(session => ListDatabaseNames(session, cancellationToken), cancellationToken);
213+
return ListDatabaseNames(options: null, cancellationToken);
214+
}
215+
216+
/// <inheritdoc />
217+
public sealed override IAsyncCursor<string> ListDatabaseNames(
218+
ListDatabaseNamesOptions options,
219+
CancellationToken cancellationToken = default(CancellationToken))
220+
{
221+
return UsingImplicitSession(session => ListDatabaseNames(session, options, cancellationToken), cancellationToken);
214222
}
215223

216224
/// <inheritdoc />
217225
public sealed override IAsyncCursor<string> ListDatabaseNames(
218226
IClientSessionHandle session,
219227
CancellationToken cancellationToken = default(CancellationToken))
220228
{
221-
var options = new ListDatabasesOptions { NameOnly = true };
222-
var databases = ListDatabases(session, options, cancellationToken);
229+
return ListDatabaseNames(session, options: null, cancellationToken);
230+
}
231+
232+
/// <inheritdoc />
233+
public sealed override IAsyncCursor<string> ListDatabaseNames(
234+
IClientSessionHandle session,
235+
ListDatabaseNamesOptions options,
236+
CancellationToken cancellationToken = default(CancellationToken))
237+
{
238+
var listDatabasesOptions = CreateListDatabasesOptionsFromListDatabaseNamesOptions(options);
239+
var databases = ListDatabases(session, listDatabasesOptions, cancellationToken);
240+
223241
return CreateDatabaseNamesCursor(databases);
224242
}
225243

226244
/// <inheritdoc />
227245
public sealed override Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
228246
CancellationToken cancellationToken = default(CancellationToken))
229247
{
230-
return UsingImplicitSessionAsync(session => ListDatabaseNamesAsync(session, cancellationToken), cancellationToken);
248+
return ListDatabaseNamesAsync(options: null, cancellationToken);
249+
}
250+
251+
/// <inheritdoc />
252+
public sealed override Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
253+
ListDatabaseNamesOptions options,
254+
CancellationToken cancellationToken = default(CancellationToken))
255+
{
256+
return UsingImplicitSessionAsync(session => ListDatabaseNamesAsync(session, options, cancellationToken), cancellationToken);
257+
}
258+
259+
/// <inheritdoc />
260+
public sealed override Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
261+
IClientSessionHandle session,
262+
CancellationToken cancellationToken = default(CancellationToken))
263+
{
264+
return ListDatabaseNamesAsync(session, options: null, cancellationToken);
231265
}
232266

233267
/// <inheritdoc />
234268
public sealed override async Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
235269
IClientSessionHandle session,
270+
ListDatabaseNamesOptions options,
236271
CancellationToken cancellationToken = default(CancellationToken))
237272
{
238-
var options = new ListDatabasesOptions { NameOnly = true };
239-
var databases = await ListDatabasesAsync(session, options, cancellationToken).ConfigureAwait(false);
273+
var listDatabasesOptions = CreateListDatabasesOptionsFromListDatabaseNamesOptions(options);
274+
var databases = await ListDatabasesAsync(session, listDatabasesOptions, cancellationToken).ConfigureAwait(false);
275+
240276
return CreateDatabaseNamesCursor(databases);
241277
}
242278

@@ -475,12 +511,25 @@ private ListDatabasesOperation CreateListDatabaseOperation(
475511
{
476512
return new ListDatabasesOperation(messageEncoderSettings)
477513
{
514+
AuthorizedDatabases = options.AuthorizedDatabases,
478515
Filter = options.Filter?.Render(BsonDocumentSerializer.Instance, BsonSerializer.SerializerRegistry),
479516
NameOnly = options.NameOnly,
480517
RetryRequested = _settings.RetryReads
481518
};
482519
}
483520

521+
private ListDatabasesOptions CreateListDatabasesOptionsFromListDatabaseNamesOptions(ListDatabaseNamesOptions options)
522+
{
523+
var listDatabasesOptions = new ListDatabasesOptions { NameOnly = true };
524+
if (options != null)
525+
{
526+
listDatabasesOptions.AuthorizedDatabases = options.AuthorizedDatabases;
527+
listDatabasesOptions.Filter = options.Filter;
528+
}
529+
530+
return listDatabasesOptions;
531+
}
532+
484533
private IReadBindingHandle CreateReadBinding(IClientSessionHandle session)
485534
{
486535
var readPreference = _settings.ReadPreference;

src/MongoDB.Driver/MongoClientBase.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,26 @@ public virtual IAsyncCursor<string> ListDatabaseNames(
6464
throw new NotImplementedException();
6565
}
6666

67+
/// <inheritdoc />
68+
public virtual IAsyncCursor<string> ListDatabaseNames(
69+
ListDatabaseNamesOptions options,
70+
CancellationToken cancellationToken = default(CancellationToken))
71+
{
72+
throw new NotImplementedException();
73+
}
74+
75+
/// <inheritdoc />
76+
public virtual IAsyncCursor<string> ListDatabaseNames(
77+
IClientSessionHandle session,
78+
CancellationToken cancellationToken = default(CancellationToken))
79+
{
80+
throw new NotImplementedException();
81+
}
82+
6783
/// <inheritdoc />
6884
public virtual IAsyncCursor<string> ListDatabaseNames(
6985
IClientSessionHandle session,
86+
ListDatabaseNamesOptions options,
7087
CancellationToken cancellationToken = default(CancellationToken))
7188
{
7289
throw new NotImplementedException();
@@ -79,9 +96,26 @@ public virtual Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
7996
throw new NotImplementedException();
8097
}
8198

99+
/// <inheritdoc />
100+
public virtual Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
101+
ListDatabaseNamesOptions options,
102+
CancellationToken cancellationToken = default(CancellationToken))
103+
{
104+
throw new NotImplementedException();
105+
}
106+
107+
/// <inheritdoc />
108+
public virtual Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
109+
IClientSessionHandle session,
110+
CancellationToken cancellationToken = default(CancellationToken))
111+
{
112+
throw new NotImplementedException();
113+
}
114+
82115
/// <inheritdoc />
83116
public virtual Task<IAsyncCursor<string>> ListDatabaseNamesAsync(
84117
IClientSessionHandle session,
118+
ListDatabaseNamesOptions options,
85119
CancellationToken cancellationToken = default(CancellationToken))
86120
{
87121
throw new NotImplementedException();

0 commit comments

Comments
 (0)