Skip to content

Commit

Permalink
Rename options
Browse files Browse the repository at this point in the history
  • Loading branch information
w1am committed Apr 18, 2024
1 parent 26ae29a commit 1401066
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ private static class ConnectionStringParser {
private const string ThrowOnAppendFailure = nameof(ThrowOnAppendFailure);
private const string KeepAliveInterval = nameof(KeepAliveInterval);
private const string KeepAliveTimeout = nameof(KeepAliveTimeout);
private const string CertPath = nameof(CertPath);
private const string CertKeyPath = nameof(CertKeyPath);
private const string UserCertFile = nameof(UserCertFile);
private const string UserKeyFile = nameof(UserKeyFile);

private const string UriSchemeDiscover = "esdb+discover";

Expand All @@ -64,8 +64,8 @@ private static class ConnectionStringParser {
{ ThrowOnAppendFailure, typeof(bool) },
{ KeepAliveInterval, typeof(int) },
{ KeepAliveTimeout, typeof(int) },
{ CertPath, typeof(string)},
{ CertKeyPath, typeof(string)},
{ UserCertFile, typeof(string)},
{ UserKeyFile, typeof(string)},
};

public static EventStoreClientSettings Parse(string connectionString) {
Expand Down Expand Up @@ -288,23 +288,23 @@ HttpMessageHandler CreateDefaultHandler() {
}

static void ConfigureClientCertificate(EventStoreClientSettings settings, IReadOnlyDictionary<string, object> options) {
var certPemFilePath = GetOptionValueAsString(CertPath);
var keyPemFilePath = GetOptionValueAsString(CertKeyPath);
var certPemFilePath = GetOptionValueAsString(UserCertFile);
var keyPemFilePath = GetOptionValueAsString(UserKeyFile);

if (string.IsNullOrEmpty(certPemFilePath) && string.IsNullOrEmpty(keyPemFilePath))
return;

if (string.IsNullOrEmpty(certPemFilePath) || string.IsNullOrEmpty(keyPemFilePath))
throw new InvalidClientCertificateException("Invalid client certificate settings. Both CertPath and CertKeyPath must be set.");
throw new InvalidClientCertificateException("Invalid client certificate settings. Both UserCertFile and UserKeyFile must be set.");

if (!File.Exists(certPemFilePath))
throw new InvalidClientCertificateException(
$"Invalid client certificate settings. The specified CertPath does not exist: {certPemFilePath}"
$"Invalid client certificate settings. The specified UserCertFile does not exist: {certPemFilePath}"
);

if (!File.Exists(keyPemFilePath))
throw new InvalidClientCertificateException(
$"Invalid client certificate settings. The specified CertKeyPath does not exist: {keyPemFilePath}"
$"Invalid client certificate settings. The specified UserKeyFile does not exist: {keyPemFilePath}"
);

try {
Expand Down
13 changes: 6 additions & 7 deletions test/EventStore.Client.Plugins.Tests/ClientCertificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,23 @@ async Task append_with_different_tls_cert_path(string certificateFilePath) {

[Theory]
[MemberData(nameof(AdminClientCertPaths))]
async Task append_with_admin_client_certificate(string certPath, string certKeyPath) {
Fixture.Log.Information("CertPath: {certPath}, CertKeyPath: {certKeyPath}", certPath, certKeyPath);
await AppendWithCertificate($"esdb://localhost:2113/?tls=true&tlsVerifyCert=true&certPath={certPath}&certKeyPath={certKeyPath}");
async Task append_with_admin_client_certificate(string userCertFile, string userKeyFile) {
await AppendWithCertificate($"esdb://localhost:2113/?tls=true&tlsVerifyCert=true&userCertFile={userCertFile}&userKeyFile={userKeyFile}");
}

[Theory]
[MemberData(nameof(BadClientCertPaths))]
async Task append_with_bad_client_certificate(string certPath, string certKeyPath) {
async Task append_with_bad_client_certificate(string userCertFile, string userKeyFile) {
await AssertAppendFailsWithCertificate(
$"esdb://localhost:2113/?tls=true&tlsVerifyCert=true&certPath={certPath}&certKeyPath={certKeyPath}",
$"esdb://localhost:2113/?tls=true&tlsVerifyCert=true&userCertFile={userCertFile}&userKeyFile={userKeyFile}",
typeof(NotAuthenticatedException)
);
}

[Theory]
[MemberData(nameof(BadClientCertPaths))]
async Task user_credentials_takes_precedence_over_client_certificates(string certPath, string certKeyPath) {
await AppendWithCertificate($"esdb://admin:changeit@localhost:2113/?tls=true&tlsVerifyCert=true&certPath={certPath}&certKeyPath={certKeyPath}");
async Task user_credentials_takes_precedence_over_client_certificates(string userCertFile, string userKeyFile) {
await AppendWithCertificate($"esdb://admin:changeit@localhost:2113/?tls=true&tlsVerifyCert=true&userCertFile={userCertFile}&userKeyFile={userKeyFile}");
}

async Task AppendWithCertificate(string connectionString) {
Expand Down
4 changes: 2 additions & 2 deletions test/EventStore.Client.Tests/ConnectionStringTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ public void connection_string_with_invalid_tls_certificate_should_throw(string c

[Theory]
[MemberData(nameof(InvalidClientCertificates))]
public void connection_string_with_invalid_client_certificate_should_throw(string certPath, string certKeyPath) {
public void connection_string_with_invalid_client_certificate_should_throw(string userCertFile, string userKeyFile) {
Assert.Throws<InvalidClientCertificateException>(
() => EventStoreClientSettings.Create(
$"esdb://admin:changeit@localhost:2113/?tls=true&tlsVerifyCert=true&certPath={certPath}&certKeyPath={certKeyPath}"
$"esdb://admin:changeit@localhost:2113/?tls=true&tlsVerifyCert=true&userCertFile={userCertFile}&userKeyFile={userKeyFile}"
)
);
}
Expand Down

0 comments on commit 1401066

Please sign in to comment.