Skip to content

Commit 59fe148

Browse files
CSHARP-3281: Versioned MongoDB API for Drivers
1 parent e72a8b2 commit 59fe148

File tree

93 files changed

+6985
-336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+6985
-336
lines changed

src/MongoDB.Driver.Core/Core/Authentication/DefaultAuthenticator.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,39 @@ public class DefaultAuthenticator : IAuthenticator
3535
// fields
3636
private readonly UsernamePasswordCredential _credential;
3737
private readonly IRandomStringGenerator _randomStringGenerator;
38+
private readonly ServerApi _serverApi;
3839
private IAuthenticator _speculativeAuthenticator;
3940

4041
// constructors
4142
/// <summary>
4243
/// Initializes a new instance of the <see cref="DefaultAuthenticator"/> class.
4344
/// </summary>
4445
/// <param name="credential">The credential.</param>
46+
[Obsolete("Use the newest overload instead.")]
4547
public DefaultAuthenticator(UsernamePasswordCredential credential)
46-
: this(credential, new DefaultRandomStringGenerator())
48+
: this(credential, new DefaultRandomStringGenerator(), serverApi: null)
4749
{
4850
}
4951

50-
internal DefaultAuthenticator(UsernamePasswordCredential credential, IRandomStringGenerator randomStringGenerator)
52+
// constructors
53+
/// <summary>
54+
/// Initializes a new instance of the <see cref="DefaultAuthenticator"/> class.
55+
/// </summary>
56+
/// <param name="credential">The credential.</param>
57+
/// <param name="serverApi">The server API.</param>
58+
public DefaultAuthenticator(UsernamePasswordCredential credential, ServerApi serverApi)
59+
: this(credential, new DefaultRandomStringGenerator(), serverApi)
60+
{
61+
}
62+
63+
internal DefaultAuthenticator(
64+
UsernamePasswordCredential credential,
65+
IRandomStringGenerator randomStringGenerator,
66+
ServerApi serverApi)
5167
{
5268
_credential = Ensure.IsNotNull(credential, nameof(credential));
5369
_randomStringGenerator = Ensure.IsNotNull(randomStringGenerator, nameof(randomStringGenerator));
70+
_serverApi = serverApi;
5471
}
5572

5673
// properties
@@ -71,7 +88,7 @@ public void Authenticate(IConnection connection, ConnectionDescription descripti
7188
&& Feature.ScramSha256Authentication.IsSupported(description.ServerVersion))
7289
{
7390
var command = CustomizeInitialIsMasterCommand(IsMasterHelper.CreateCommand());
74-
var isMasterProtocol = IsMasterHelper.CreateProtocol(command);
91+
var isMasterProtocol = IsMasterHelper.CreateProtocol(command, _serverApi);
7592
var isMasterResult = IsMasterHelper.GetResult(connection, isMasterProtocol, cancellationToken);
7693
var mergedIsMasterResult = new IsMasterResult(description.IsMasterResult.Wrapped.Merge(isMasterResult.Wrapped));
7794
description = new ConnectionDescription(
@@ -97,7 +114,7 @@ public async Task AuthenticateAsync(IConnection connection, ConnectionDescriptio
97114
&& Feature.ScramSha256Authentication.IsSupported(description.ServerVersion))
98115
{
99116
var command = CustomizeInitialIsMasterCommand(IsMasterHelper.CreateCommand());
100-
var isMasterProtocol = IsMasterHelper.CreateProtocol(command);
117+
var isMasterProtocol = IsMasterHelper.CreateProtocol(command, _serverApi);
101118
var isMasterResult = await IsMasterHelper.GetResultAsync(connection, isMasterProtocol, cancellationToken).ConfigureAwait(false);
102119
var mergedIsMasterResult = new IsMasterResult(description.IsMasterResult.Wrapped.Merge(isMasterResult.Wrapped));
103120
description = new ConnectionDescription(
@@ -110,13 +127,12 @@ public async Task AuthenticateAsync(IConnection connection, ConnectionDescriptio
110127
await authenticator.AuthenticateAsync(connection, description, cancellationToken).ConfigureAwait(false);
111128
}
112129

113-
114130
/// <inheritdoc/>
115131
public BsonDocument CustomizeInitialIsMasterCommand(BsonDocument isMasterCommand)
116132
{
117133
var saslSupportedMechs = CreateSaslSupportedMechsRequest(_credential.Source, _credential.Username);
118134
isMasterCommand = isMasterCommand.Merge(saslSupportedMechs);
119-
_speculativeAuthenticator = new ScramSha256Authenticator(_credential, _randomStringGenerator);
135+
_speculativeAuthenticator = new ScramSha256Authenticator(_credential, _randomStringGenerator, _serverApi);
120136
return _speculativeAuthenticator.CustomizeInitialIsMasterCommand(isMasterCommand);
121137
}
122138

@@ -135,15 +151,15 @@ private IAuthenticator CreateAuthenticator(IConnection connection, ConnectionDes
135151
// If SCRAM-SHA-256 is present in the list of mechanisms, then it MUST be used as the default;
136152
// otherwise, SCRAM-SHA-1 MUST be used as the default, regardless of whether SCRAM-SHA-1 is in the list.
137153
return description.IsMasterResult.SaslSupportedMechs.Contains("SCRAM-SHA-256")
138-
? (IAuthenticator)new ScramSha256Authenticator(_credential, _randomStringGenerator)
139-
: new ScramSha1Authenticator(_credential, _randomStringGenerator);
154+
? (IAuthenticator)new ScramSha256Authenticator(_credential, _randomStringGenerator, _serverApi)
155+
: new ScramSha1Authenticator(_credential, _randomStringGenerator, _serverApi);
140156
}
141157
// If saslSupportedMechs is not present in the isMaster results for mechanism negotiation, then SCRAM-SHA-1
142158
// MUST be used when talking to servers >= 3.0. Prior to server 3.0, MONGODB-CR MUST be used.
143159
#pragma warning disable 618
144160
return Feature.ScramSha1Authentication.IsSupported(description.ServerVersion)
145-
? (IAuthenticator)new ScramSha1Authenticator(_credential, _randomStringGenerator)
146-
: new MongoDBCRAuthenticator(_credential);
161+
? (IAuthenticator)new ScramSha1Authenticator(_credential, _randomStringGenerator, _serverApi)
162+
: new MongoDBCRAuthenticator(_credential, _serverApi);
147163
#pragma warning restore 618
148164
}
149165

src/MongoDB.Driver.Core/Core/Authentication/GssapiAuthenticator.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,23 @@ public static string ServiceRealmPropertyName
108108
/// </summary>
109109
/// <param name="credential">The credential.</param>
110110
/// <param name="properties">The properties.</param>
111+
[Obsolete("Use the newest overload instead.")]
111112
public GssapiAuthenticator(UsernamePasswordCredential credential, IEnumerable<KeyValuePair<string, string>> properties)
112-
: base(CreateMechanism(credential, properties))
113+
: this(credential, properties, serverApi: null)
114+
{
115+
}
116+
117+
/// <summary>
118+
/// Initializes a new instance of the <see cref="GssapiAuthenticator"/> class.
119+
/// </summary>
120+
/// <param name="credential">The credential.</param>
121+
/// <param name="properties">The properties.</param>
122+
/// <param name="serverApi">The server API.</param>
123+
public GssapiAuthenticator(
124+
UsernamePasswordCredential credential,
125+
IEnumerable<KeyValuePair<string, string>> properties,
126+
ServerApi serverApi)
127+
: base(CreateMechanism(credential, properties), serverApi)
113128
{
114129
}
115130

@@ -118,8 +133,23 @@ public GssapiAuthenticator(UsernamePasswordCredential credential, IEnumerable<Ke
118133
/// </summary>
119134
/// <param name="username">The username.</param>
120135
/// <param name="properties">The properties.</param>
136+
[Obsolete("Use the newest overload instead.")]
121137
public GssapiAuthenticator(string username, IEnumerable<KeyValuePair<string, string>> properties)
122-
: base(CreateMechanism(username, null, properties))
138+
: this(username, properties, serverApi: null)
139+
{
140+
}
141+
142+
/// <summary>
143+
/// Initializes a new instance of the <see cref="GssapiAuthenticator"/> class.
144+
/// </summary>
145+
/// <param name="username">The username.</param>
146+
/// <param name="properties">The properties.</param>
147+
/// <param name="serverApi">The server API.</param>
148+
public GssapiAuthenticator(
149+
string username,
150+
IEnumerable<KeyValuePair<string, string>> properties,
151+
ServerApi serverApi)
152+
: base(CreateMechanism(username, null, properties), serverApi)
123153
{
124154
}
125155

src/MongoDB.Driver.Core/Core/Authentication/MongoAWSAuthenticator.cs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,23 @@ private static void ValidateMechanismProperties(IEnumerable<KeyValuePair<string,
200200
/// </summary>
201201
/// <param name="credential">The credentials.</param>
202202
/// <param name="properties">The properties.</param>
203+
[Obsolete("Use the newest overload instead.")]
203204
public MongoAWSAuthenticator(UsernamePasswordCredential credential, IEnumerable<KeyValuePair<string, string>> properties)
204-
: this(credential, properties, new DefaultRandomByteGenerator(), SystemClock.Instance)
205+
: this(credential, properties, serverApi: null)
206+
{
207+
}
208+
209+
/// <summary>
210+
/// Initializes a new instance of the <see cref="MongoAWSAuthenticator"/> class.
211+
/// </summary>
212+
/// <param name="credential">The credentials.</param>
213+
/// <param name="properties">The properties.</param>
214+
/// <param name="serverApi">The server API.</param>
215+
public MongoAWSAuthenticator(
216+
UsernamePasswordCredential credential,
217+
IEnumerable<KeyValuePair<string, string>> properties,
218+
ServerApi serverApi)
219+
: this(credential, properties, new DefaultRandomByteGenerator(), SystemClock.Instance, serverApi)
205220
{
206221
}
207222

@@ -210,26 +225,43 @@ public MongoAWSAuthenticator(UsernamePasswordCredential credential, IEnumerable<
210225
/// </summary>
211226
/// <param name="username">The username.</param>
212227
/// <param name="properties">The properties.</param>
228+
[Obsolete("Use the newest overload instead.")]
213229
public MongoAWSAuthenticator(string username, IEnumerable<KeyValuePair<string, string>> properties)
214-
: this(username, properties, new DefaultRandomByteGenerator(), SystemClock.Instance)
230+
: this(username, properties, serverApi: null)
231+
{
232+
}
233+
234+
/// <summary>
235+
/// Initializes a new instance of the <see cref="MongoAWSAuthenticator"/> class.
236+
/// </summary>
237+
/// <param name="username">The username.</param>
238+
/// <param name="properties">The properties.</param>
239+
/// <param name="serverApi">The server API.</param>
240+
public MongoAWSAuthenticator(
241+
string username,
242+
IEnumerable<KeyValuePair<string, string>> properties,
243+
ServerApi serverApi)
244+
: this(username, properties, new DefaultRandomByteGenerator(), SystemClock.Instance, serverApi)
215245
{
216246
}
217247

218248
internal MongoAWSAuthenticator(
219249
UsernamePasswordCredential credential,
220250
IEnumerable<KeyValuePair<string, string>> properties,
221251
IRandomByteGenerator randomByteGenerator,
222-
IClock clock)
223-
: base(CreateMechanism(credential, properties, randomByteGenerator, clock))
252+
IClock clock,
253+
ServerApi serverApi)
254+
: base(CreateMechanism(credential, properties, randomByteGenerator, clock), serverApi)
224255
{
225256
}
226257

227258
internal MongoAWSAuthenticator(
228259
string username,
229260
IEnumerable<KeyValuePair<string, string>> properties,
230261
IRandomByteGenerator randomByteGenerator,
231-
IClock clock)
232-
: base(CreateMechanism(username, null, properties, randomByteGenerator, clock))
262+
IClock clock,
263+
ServerApi serverApi)
264+
: base(CreateMechanism(username, null, properties, randomByteGenerator, clock), serverApi)
233265
{
234266
}
235267

src/MongoDB.Driver.Core/Core/Authentication/MongoDBCRAuthenticator.cs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System;
1717
using System.Security;
1818
using System.Security.Cryptography;
19-
using System.Text;
2019
using System.Threading;
2120
using System.Threading.Tasks;
2221
using MongoDB.Bson;
@@ -49,15 +48,27 @@ public static string MechanismName
4948

5049
// fields
5150
private readonly UsernamePasswordCredential _credential;
51+
private readonly ServerApi _serverApi;
5252

5353
// constructors
5454
/// <summary>
5555
/// Initializes a new instance of the <see cref="MongoDBCRAuthenticator"/> class.
5656
/// </summary>
5757
/// <param name="credential">The credential.</param>
5858
public MongoDBCRAuthenticator(UsernamePasswordCredential credential)
59+
: this(credential, serverApi: null)
60+
{
61+
}
62+
63+
/// <summary>
64+
/// Initializes a new instance of the <see cref="MongoDBCRAuthenticator"/> class.
65+
/// </summary>
66+
/// <param name="credential">The credential.</param>
67+
/// <param name="serverApi">The server API.</param>
68+
public MongoDBCRAuthenticator(UsernamePasswordCredential credential, ServerApi serverApi)
5969
{
6070
_credential = Ensure.IsNotNull(credential, nameof(credential));
71+
_serverApi = serverApi; // can be null
6172
}
6273

6374
// properties
@@ -124,11 +135,12 @@ private CommandWireProtocol<BsonDocument> CreateAuthenticateProtocol(BsonDocumen
124135
{ "key", CreateKey(_credential.Username, _credential.Password, nonce) }
125136
};
126137
var protocol = new CommandWireProtocol<BsonDocument>(
127-
new DatabaseNamespace(_credential.Source),
128-
command,
129-
true,
130-
BsonDocumentSerializer.Instance,
131-
null);
138+
databaseNamespace: new DatabaseNamespace(_credential.Source),
139+
command: command,
140+
slaveOk: true,
141+
resultSerializer: BsonDocumentSerializer.Instance,
142+
messageEncoderSettings: null,
143+
serverApi: _serverApi);
132144
return protocol;
133145
}
134146

@@ -142,11 +154,12 @@ private CommandWireProtocol<BsonDocument> CreateGetNonceProtocol()
142154
{
143155
var command = new BsonDocument("getnonce", 1);
144156
var protocol = new CommandWireProtocol<BsonDocument>(
145-
new DatabaseNamespace(_credential.Source),
146-
command,
147-
true,
148-
BsonDocumentSerializer.Instance,
149-
null);
157+
databaseNamespace: new DatabaseNamespace(_credential.Source),
158+
command: command,
159+
slaveOk: true,
160+
resultSerializer: BsonDocumentSerializer.Instance,
161+
messageEncoderSettings: null,
162+
serverApi: _serverApi);
150163
return protocol;
151164
}
152165

src/MongoDB.Driver.Core/Core/Authentication/MongoDBX509Authenticator.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,28 @@ public static string MechanismName
4343

4444
// fields
4545
private readonly string _username;
46+
private readonly ServerApi _serverApi;
4647

4748
// constructors
4849
/// <summary>
4950
/// Initializes a new instance of the <see cref="MongoDBX509Authenticator"/> class.
5051
/// </summary>
5152
/// <param name="username">The username.</param>
53+
[Obsolete("Use the newest overload instead.")]
5254
public MongoDBX509Authenticator(string username)
55+
: this(username, serverApi: null)
56+
{
57+
}
58+
59+
/// <summary>
60+
/// Initializes a new instance of the <see cref="MongoDBX509Authenticator"/> class.
61+
/// </summary>
62+
/// <param name="username">The username.</param>
63+
/// <param name="serverApi">The server API.</param>
64+
public MongoDBX509Authenticator(string username, ServerApi serverApi)
5365
{
5466
_username = Ensure.IsNullOrNotEmpty(username, nameof(username));
67+
_serverApi = serverApi; // can be null
5568
}
5669

5770
// properties
@@ -132,11 +145,12 @@ private CommandWireProtocol<BsonDocument> CreateAuthenticateProtocol()
132145
var command = CreateAuthenticateCommand();
133146

134147
var protocol = new CommandWireProtocol<BsonDocument>(
135-
new DatabaseNamespace("$external"),
136-
command,
137-
true,
138-
BsonDocumentSerializer.Instance,
139-
null);
148+
databaseNamespace: new DatabaseNamespace("$external"),
149+
command: command,
150+
slaveOk: true,
151+
resultSerializer: BsonDocumentSerializer.Instance,
152+
messageEncoderSettings: null,
153+
serverApi: _serverApi);
140154

141155
return protocol;
142156
}

src/MongoDB.Driver.Core/Core/Authentication/PlainAuthenticator.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16-
using System.Text;
16+
using System;
1717
using MongoDB.Bson.IO;
1818
using MongoDB.Driver.Core.Connections;
1919
using MongoDB.Driver.Core.Misc;
@@ -45,8 +45,19 @@ public static string MechanismName
4545
/// Initializes a new instance of the <see cref="PlainAuthenticator"/> class.
4646
/// </summary>
4747
/// <param name="credential">The credential.</param>
48+
[Obsolete("Use the newest overload instead.")]
4849
public PlainAuthenticator(UsernamePasswordCredential credential)
49-
: base(new PlainMechanism(credential))
50+
: this(credential, serverApi: null)
51+
{
52+
}
53+
54+
/// <summary>
55+
/// Initializes a new instance of the <see cref="PlainAuthenticator"/> class.
56+
/// </summary>
57+
/// <param name="credential">The credential.</param>
58+
/// <param name="serverApi">The server API.</param>
59+
public PlainAuthenticator(UsernamePasswordCredential credential, ServerApi serverApi)
60+
: base(new PlainMechanism(credential), serverApi)
5061
{
5162
_databaseName = credential.Source;
5263
}

0 commit comments

Comments
 (0)