16
16
using System ;
17
17
using System . Collections . Generic ;
18
18
using System . Linq ;
19
- using System . Runtime . InteropServices ;
20
19
using System . Security . Cryptography . X509Certificates ;
21
20
using System . Threading ;
22
- using System . Threading . Tasks ;
23
21
using FluentAssertions ;
24
22
using MongoDB . Bson ;
25
- using MongoDB . Bson . Serialization . Serializers ;
26
23
using MongoDB . Bson . TestHelpers . XunitExtensions ;
24
+ using MongoDB . Driver . Core . Clusters . ServerSelectors ;
27
25
using MongoDB . Driver . Core . Misc ;
28
- using MongoDB . Driver . Core . Operations ;
29
26
using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
30
27
using MongoDB . Driver . TestHelpers ;
31
- using Moq ;
32
28
using Xunit ;
33
29
34
30
namespace MongoDB . Driver . Tests
@@ -144,7 +140,7 @@ public void Authentication_succeeds_when_user_has_Scram_Sha_1_Mechanism_and_mech
144
140
settings . Credential = MongoCredential
145
141
. FromComponents ( mechanism : null , source : null , username : userName , password : password ) ;
146
142
147
- AssertAuthenticationSucceeds ( settings , async ) ;
143
+ AssertAuthenticationSucceeds ( settings , async , speculativeAuthenticatationShouldSucceedIfPossible : false ) ;
148
144
}
149
145
150
146
[ SkippableTheory ]
@@ -352,10 +348,13 @@ public void Authentication_succeeds_with_MONGODB_X509_mechanism(
352
348
settings . SslSettings = settings . SslSettings . Clone ( ) ;
353
349
settings . SslSettings . ClientCertificates = new [ ] { clientCertificate } ;
354
350
355
- AssertAuthenticationSucceeds ( settings , async ) ;
351
+ AssertAuthenticationSucceeds ( settings , async , speculativeAuthenticatationShouldSucceedIfPossible : true ) ;
356
352
}
357
353
358
- private void AssertAuthenticationSucceeds ( MongoClientSettings settings , bool async )
354
+ private void AssertAuthenticationSucceeds (
355
+ MongoClientSettings settings ,
356
+ bool async ,
357
+ bool speculativeAuthenticatationShouldSucceedIfPossible = true )
359
358
{
360
359
// If we don't use a DisposableClient, the second run of AuthenticationSucceedsWithMongoDB_X509_mechanism
361
360
// will fail because the backing Cluster's connections will be associated with a dropped user
@@ -371,6 +370,17 @@ private void AssertAuthenticationSucceeds(MongoClientSettings settings, bool asy
371
370
{
372
371
_ = client . ListDatabaseNames ( ) . ToList ( ) ;
373
372
}
373
+ if ( Feature . SpeculativeAuthentication . IsSupported ( CoreTestConfiguration . ServerVersion ) &&
374
+ speculativeAuthenticatationShouldSucceedIfPossible &&
375
+ Driver . CoreTestConfiguration . Cluster . Description . Type != Core . Clusters . ClusterType . Sharded ) // Until https://jira.mongodb.org/browse/SERVER-47908 is resolved
376
+ {
377
+ var cancellationToken = CancellationToken. None;
378
+ var serverSelector = new ReadPreferenceServerSelector ( settings . ReadPreference ) ;
379
+ var server = client . Cluster . SelectServer ( serverSelector , cancellationToken ) ;
380
+ var channel = server. GetChannel ( cancellationToken ) ;
381
+ var isMasterResult = channel. ConnectionDescription . IsMasterResult ;
382
+ isMasterResult. SpeculativeAuthenticate . Should ( ) . NotBeNull ( ) ;
383
+ }
374
384
}
375
385
}
376
386
@@ -453,9 +463,10 @@ private void DropDatabaseUser(MongoClient client, string database, string userNa
453
463
private string GetRfc2253FormattedUsernameFromX509ClientCertificate ( X509Certificate2 certificate )
454
464
{
455
465
var distinguishedName = certificate . SubjectName . Name ;
456
- // Authentication will fail if we don't remove the spaces, even if we add the username WITH the spaces.
457
- var nameWithNoSpaces = string . Join ( "," , distinguishedName . Split ( ',' ) . Select ( s => s . Trim ( ) ) ) ;
458
- return nameWithNoSpaces. Replace ( "S=" , "ST=" ) ;
466
+ // Authentication will fail if we don't remove the delimiting spaces, even if we add the username WITH the
467
+ // delimiting spaces.
468
+ var nameWithoutDelimitingSpaces = string . Join ( "," , distinguishedName . Split ( ',' ) . Select ( s => s . Trim ( ) ) ) ;
469
+ return nameWithoutDelimitingSpaces . Replace ( "S=" , "ST=" ) ;
459
470
}
460
471
}
461
472
}
0 commit comments