-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5581: Test MONGODB-X509 on cloud-dev #1757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
eeac139
b5b29d1
b5698cb
887c072
86ac517
7663f41
992058f
41a83ee
6636f65
951c098
cd26e7d
449d828
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Security.Cryptography.X509Certificates; | ||
using FluentAssertions; | ||
using MongoDB.Driver.Core.TestHelpers.XunitExtensions; | ||
using MongoDB.TestHelpers.XunitExtensions; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests; | ||
|
||
[Trait("Category", "Integration")] | ||
[Trait("Category", "X509")] | ||
public class X509Tests | ||
{ | ||
const string MONGODB_X509_CLIENT_CERTIFICATE_PATH = "MONGO_X509_CLIENT_CERTIFICATE_PATH"; | ||
const string MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD = "MONGO_X509_CLIENT_CERTIFICATE_PASSWORD"; | ||
|
||
const string MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH = "MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH"; | ||
const string MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD = "MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD"; | ||
|
||
[Fact] | ||
public void Authentication_succeeds_with_MONGODB_X509_mechanism() | ||
{ | ||
var clientCertificate = GetClientCertificate(CertificateType.MONGO_X509); | ||
|
||
var settings = DriverTestConfiguration.GetClientSettings(); | ||
settings.SslSettings.ClientCertificates = [clientCertificate]; | ||
|
||
AssertAuthenticationSucceeds(settings); | ||
} | ||
|
||
[Fact] | ||
public void Authentication_fails_with_MONGODB_X509_mechanism_when_username_is_wrong() | ||
{ | ||
var clientCertificate = GetClientCertificate(CertificateType.MONGO_X509); | ||
|
||
var settings = DriverTestConfiguration.GetClientSettings(); | ||
settings.Credential = MongoCredential.CreateMongoX509Credential("wrong_username"); | ||
settings.SslSettings.ClientCertificates = [clientCertificate]; | ||
|
||
AssertAuthenticationFails(settings); | ||
} | ||
|
||
[Fact] | ||
public void Authentication_fails_with_MONGODB_X509_mechanism_when_user_is_not_in_database() | ||
{ | ||
var noUserClientCertificate = GetClientCertificate(CertificateType.MONGO_X509_CLIENT_NO_USER); | ||
|
||
var settings = DriverTestConfiguration.GetClientSettings(); | ||
settings.SslSettings.ClientCertificates = [noUserClientCertificate]; | ||
|
||
AssertAuthenticationFails(settings, "Could not find user"); | ||
} | ||
|
||
private void AssertAuthenticationSucceeds(MongoClientSettings settings) | ||
{ | ||
using var client = DriverTestConfiguration.CreateMongoClient(settings); | ||
_ = client.ListDatabaseNames().ToList(); | ||
} | ||
|
||
private void AssertAuthenticationFails(MongoClientSettings settings, string innerExceptionMessage = null) | ||
{ | ||
using var client = DriverTestConfiguration.CreateMongoClient(settings); | ||
var exception = Record.Exception(() => client.ListDatabaseNames().ToList()); | ||
exception.Should().BeOfType<MongoAuthenticationException>(); | ||
|
||
if (innerExceptionMessage != null) | ||
{ | ||
var innerException = exception.InnerException; | ||
innerException.Should().BeOfType<MongoCommandException>(); | ||
innerException.Message.Should().Contain(innerExceptionMessage); | ||
} | ||
} | ||
|
||
private enum CertificateType | ||
{ | ||
MONGO_X509, | ||
MONGO_X509_CLIENT_NO_USER | ||
} | ||
|
||
private X509Certificate2 GetClientCertificate(CertificateType certificateType) | ||
{ | ||
RequireServer.Check().Tls(required: true); | ||
|
||
string path, password; | ||
|
||
switch (certificateType) | ||
{ | ||
case CertificateType.MONGO_X509: | ||
RequireEnvironment.Check() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to reorganize the code a little: set the names of env variables inside the switch, but read the values out side. Something like this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! |
||
.EnvironmentVariable(MONGODB_X509_CLIENT_CERTIFICATE_PATH, isDefined: true) | ||
.EnvironmentVariable(MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD, isDefined: true); | ||
|
||
path = Environment.GetEnvironmentVariable(MONGODB_X509_CLIENT_CERTIFICATE_PATH); | ||
password = Environment.GetEnvironmentVariable(MONGODB_X509_CLIENT_CERTIFICATE_PASSWORD); | ||
break; | ||
case CertificateType.MONGO_X509_CLIENT_NO_USER: | ||
RequireEnvironment.Check() | ||
.EnvironmentVariable(MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH, isDefined: true) | ||
.EnvironmentVariable(MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD, isDefined: true); | ||
|
||
path = Environment.GetEnvironmentVariable(MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH); | ||
password = Environment.GetEnvironmentVariable(MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD); | ||
break; | ||
default: | ||
throw new ArgumentException("Wrong certificate type specified.", nameof(certificateType)); | ||
} | ||
|
||
return new X509Certificate2(path, password); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: It's unusual to have multiple variables declaration in a single line.