@@ -35,15 +35,9 @@ public class X509Tests
3535 [ Fact ]
3636 public void Authentication_succeeds_with_MONGODB_X509_mechanism ( )
3737 {
38- RequireEnvironment . Check ( ) . EnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PATH , isDefined : true ) ;
39- RequireEnvironment . Check ( ) . EnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD , isDefined : true ) ;
40- RequireServer . Check ( ) . Tls ( required : true ) ;
41-
42- var pathToClientCertificate = Environment . GetEnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PATH ) ;
43- var password = Environment . GetEnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD ) ;
44- var clientCertificate = new X509Certificate2 ( pathToClientCertificate , password ) ;
38+ var clientCertificate = GetClientCertificate ( CertificateType . MONGO_X509 ) ;
4539
46- var settings = DriverTestConfiguration . GetClientSettings ( ) . Clone ( ) ;
40+ var settings = DriverTestConfiguration . GetClientSettings ( ) ;
4741 settings . SslSettings . ClientCertificates = [ clientCertificate ] ;
4842
4943 AssertAuthenticationSucceeds ( settings ) ;
@@ -52,15 +46,9 @@ public void Authentication_succeeds_with_MONGODB_X509_mechanism()
5246 [ Fact ]
5347 public void Authentication_fails_with_MONGODB_X509_mechanism_when_username_is_wrong ( )
5448 {
55- RequireEnvironment . Check ( ) . EnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PATH , isDefined : true ) ;
56- RequireEnvironment . Check ( ) . EnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD , isDefined : true ) ;
57- RequireServer . Check ( ) . Tls ( required : true ) ;
58-
59- var pathToClientCertificate = Environment . GetEnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PATH ) ;
60- var password = Environment . GetEnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD ) ;
61- var clientCertificate = new X509Certificate2 ( pathToClientCertificate , password ) ;
49+ var clientCertificate = GetClientCertificate ( CertificateType . MONGO_X509 ) ;
6250
63- var settings = DriverTestConfiguration . GetClientSettings ( ) . Clone ( ) ;
51+ var settings = DriverTestConfiguration . GetClientSettings ( ) ;
6452 settings . Credential = MongoCredential . CreateMongoX509Credential ( "wrong_username" ) ;
6553 settings . SslSettings . ClientCertificates = [ clientCertificate ] ;
6654
@@ -70,18 +58,12 @@ public void Authentication_fails_with_MONGODB_X509_mechanism_when_username_is_wr
7058 [ Fact ]
7159 public void Authentication_fails_with_MONGODB_X509_mechanism_when_user_is_not_in_database ( )
7260 {
73- RequireEnvironment . Check ( ) . EnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH , isDefined : true ) ;
74- RequireEnvironment . Check ( ) . EnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD , isDefined : true ) ;
75- RequireServer . Check ( ) . Tls ( required : true ) ;
76-
77- var pathToClientCertificate = Environment . GetEnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH ) ;
78- var password = Environment . GetEnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD ) ;
79- var clientCertificate = new X509Certificate2 ( pathToClientCertificate , password ) ;
61+ var noUserClientCertificate = GetClientCertificate ( CertificateType . MONGO_X509_CLIENT_NO_USER ) ;
8062
81- var settings = DriverTestConfiguration . GetClientSettings ( ) . Clone ( ) ;
82- settings . SslSettings . ClientCertificates = [ clientCertificate ] ;
63+ var settings = DriverTestConfiguration . GetClientSettings ( ) ;
64+ settings . SslSettings . ClientCertificates = [ noUserClientCertificate ] ;
8365
84- AssertAuthenticationFails ( settings ) ;
66+ AssertAuthenticationFails ( settings , "Could not find user" ) ;
8567 }
8668
8769 private void AssertAuthenticationSucceeds ( MongoClientSettings settings )
@@ -90,14 +72,54 @@ private void AssertAuthenticationSucceeds(MongoClientSettings settings)
9072 _ = client . ListDatabaseNames ( ) . ToList ( ) ;
9173 }
9274
93- private void AssertAuthenticationFails ( MongoClientSettings settings )
75+ private void AssertAuthenticationFails ( MongoClientSettings settings , string innerExceptionMessage = null )
9476 {
9577 using var client = DriverTestConfiguration . CreateMongoClient ( settings ) ;
9678 var exception = Record . Exception ( ( ) => client . ListDatabaseNames ( ) . ToList ( ) ) ;
9779 exception . Should ( ) . BeOfType < MongoAuthenticationException > ( ) ;
9880
99- // var innerException = exception.InnerException;
100- // innerException.Should().BeOfType<MongoCommandException>();
101- // innerException.Message.Should().Contain("Could not find user");
81+ if ( innerExceptionMessage != null )
82+ {
83+ var innerException = exception . InnerException ;
84+ innerException . Should ( ) . BeOfType < MongoCommandException > ( ) ;
85+ innerException . Message . Should ( ) . Contain ( innerExceptionMessage ) ;
86+ }
87+ }
88+
89+ private enum CertificateType
90+ {
91+ MONGO_X509 ,
92+ MONGO_X509_CLIENT_NO_USER
93+ }
94+
95+ private X509Certificate2 GetClientCertificate ( CertificateType certificateType )
96+ {
97+ RequireServer . Check ( ) . Tls ( required : true ) ;
98+
99+ string path , password ;
100+
101+ switch ( certificateType )
102+ {
103+ case CertificateType . MONGO_X509 :
104+ RequireEnvironment . Check ( )
105+ . EnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PATH , isDefined : true )
106+ . EnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD , isDefined : true ) ;
107+
108+ path = Environment . GetEnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PATH ) ;
109+ password = Environment . GetEnvironmentVariable ( MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD ) ;
110+ break ;
111+ case CertificateType . MONGO_X509_CLIENT_NO_USER :
112+ RequireEnvironment . Check ( )
113+ . EnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH , isDefined : true )
114+ . EnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD , isDefined : true ) ;
115+
116+ path = Environment . GetEnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH ) ;
117+ password = Environment . GetEnvironmentVariable ( MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD ) ;
118+ break ;
119+ default :
120+ throw new ArgumentException ( "Wrong certificate type specified." , nameof ( certificateType ) ) ;
121+ }
122+
123+ return new X509Certificate2 ( path , password ) ;
102124 }
103125}
0 commit comments