-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple options for configuring RavenDB client certificate (#4767)
* Centralize certificate finding * Add settings to Primary/Audit and plumb through to certificate finder * Setting for certificate path, in cases where mounted from a secrets mount * Add certificate password * Changes from review
- Loading branch information
1 parent
793468c
commit 4633f7b
Showing
8 changed files
with
89 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#nullable enable | ||
|
||
namespace ServiceControl.RavenDB; | ||
|
||
using System.Reflection; | ||
using System.Security.Cryptography; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
public static class RavenClientCertificate | ||
{ | ||
public static X509Certificate2? FindClientCertificate(IRavenClientCertificateInfo certInfo) | ||
{ | ||
if (certInfo.ClientCertificateBase64 is not null) | ||
{ | ||
try | ||
{ | ||
var bytes = Convert.FromBase64String(certInfo.ClientCertificateBase64); | ||
return new X509Certificate2(bytes, certInfo.ClientCertificatePassword); | ||
} | ||
catch (Exception x) when (x is FormatException or CryptographicException) | ||
{ | ||
throw new Exception("Could not read the RavenDB client certificate from the configured Base64 value.", x); | ||
} | ||
} | ||
|
||
if (certInfo.ClientCertificatePath is not null) | ||
{ | ||
if (!File.Exists(certInfo.ClientCertificatePath)) | ||
{ | ||
throw new Exception("Could not read the RavenDB client certificate from the supplied path because no file was found."); | ||
} | ||
return new X509Certificate2(certInfo.ClientCertificatePath, certInfo.ClientCertificatePassword); | ||
} | ||
|
||
var applicationDirectory = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location) ?? string.Empty; | ||
var certificatePath = Path.Combine(applicationDirectory, "raven-client-certificate.pfx"); | ||
|
||
if (File.Exists(certificatePath)) | ||
{ | ||
return new X509Certificate2(certificatePath, certInfo.ClientCertificatePassword); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public interface IRavenClientCertificateInfo | ||
{ | ||
string? ClientCertificatePath { get; } | ||
string? ClientCertificateBase64 { get; } | ||
string? ClientCertificatePassword { get; } | ||
} |