1
+ /* Copyright 2010-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 System ;
17
+ using System . Security . Cryptography . X509Certificates ;
18
+ using FluentAssertions ;
19
+ using MongoDB . Driver . Core . TestHelpers . XunitExtensions ;
20
+ using MongoDB . TestHelpers . XunitExtensions ;
21
+ using Xunit ;
22
+
23
+ namespace MongoDB . Driver . Tests ;
24
+
25
+ [ Trait ( "Category" , "Integration" ) ]
26
+ [ Trait ( "Category" , "X509" ) ]
27
+ public class X509Tests
28
+ {
29
+ const string MONGODB_X509_CLIENT_CERTIFICATE_PATH = "MONGO_X509_CLIENT_CERTIFICATE_PATH" ;
30
+ const string MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD = "MONGO_X509_CLIENT_CERTIFICATE_PASSWORD" ;
31
+
32
+ const string MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH = "MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH" ;
33
+ const string MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD = "MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD" ;
34
+
35
+ [ Fact ]
36
+ public void Authentication_succeeds_with_MONGODB_X509_mechanism ( )
37
+ {
38
+ var clientCertificate = GetClientCertificate ( CertificateType . MONGO_X509 ) ;
39
+
40
+ var settings = DriverTestConfiguration . GetClientSettings ( ) ;
41
+ settings . SslSettings . ClientCertificates = [ clientCertificate ] ;
42
+
43
+ AssertAuthenticationSucceeds ( settings ) ;
44
+ }
45
+
46
+ [ Fact ]
47
+ public void Authentication_fails_with_MONGODB_X509_mechanism_when_username_is_wrong ( )
48
+ {
49
+ var clientCertificate = GetClientCertificate ( CertificateType . MONGO_X509 ) ;
50
+
51
+ var settings = DriverTestConfiguration . GetClientSettings ( ) ;
52
+ settings . Credential = MongoCredential . CreateMongoX509Credential ( "wrong_username" ) ;
53
+ settings . SslSettings . ClientCertificates = [ clientCertificate ] ;
54
+
55
+ AssertAuthenticationFails ( settings ) ;
56
+ }
57
+
58
+ [ Fact ]
59
+ public void Authentication_fails_with_MONGODB_X509_mechanism_when_user_is_not_in_database ( )
60
+ {
61
+ var noUserClientCertificate = GetClientCertificate ( CertificateType . MONGO_X509_CLIENT_NO_USER ) ;
62
+
63
+ var settings = DriverTestConfiguration . GetClientSettings ( ) ;
64
+ settings . SslSettings . ClientCertificates = [ noUserClientCertificate ] ;
65
+
66
+ AssertAuthenticationFails ( settings , "Could not find user" ) ;
67
+ }
68
+
69
+ private void AssertAuthenticationSucceeds ( MongoClientSettings settings )
70
+ {
71
+ using var client = DriverTestConfiguration . CreateMongoClient ( settings ) ;
72
+ _ = client . ListDatabaseNames ( ) . ToList ( ) ;
73
+ }
74
+
75
+ private void AssertAuthenticationFails ( MongoClientSettings settings , string innerExceptionMessage = null )
76
+ {
77
+ using var client = DriverTestConfiguration . CreateMongoClient ( settings ) ;
78
+ var exception = Record . Exception ( ( ) => client . ListDatabaseNames ( ) . ToList ( ) ) ;
79
+ exception . Should ( ) . BeOfType < MongoAuthenticationException > ( ) ;
80
+
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 pathVariable = null ;
100
+ string passwordVariable = null ;
101
+
102
+ switch ( certificateType )
103
+ {
104
+ case CertificateType . MONGO_X509 :
105
+ pathVariable = MONGODB_X509_CLIENT_CERTIFICATE_PATH ;
106
+ passwordVariable = MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD ;
107
+ break ;
108
+ case CertificateType . MONGO_X509_CLIENT_NO_USER :
109
+ pathVariable = MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH ;
110
+ passwordVariable = MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD ;
111
+ break ;
112
+ default :
113
+ throw new ArgumentException ( "Wrong certificate type specified." , nameof ( certificateType ) ) ;
114
+ }
115
+
116
+ RequireEnvironment . Check ( )
117
+ . EnvironmentVariable ( pathVariable , isDefined : true )
118
+ . EnvironmentVariable ( passwordVariable , isDefined : true ) ;
119
+
120
+ var path = Environment . GetEnvironmentVariable ( pathVariable ) ;
121
+ var password = Environment . GetEnvironmentVariable ( passwordVariable ) ;
122
+
123
+ return new X509Certificate2 ( path , password ) ;
124
+ }
125
+ }
0 commit comments