using System.IO;
using Serilog;
namespace ZB.MOM.WW.LmxProxy.Host.Configuration
{
///
/// Configuration for TLS/SSL settings for secure gRPC communication
///
public class TlsConfiguration
{
///
/// Gets or sets whether TLS is enabled for gRPC communication
///
public bool Enabled { get; set; } = false;
///
/// Gets or sets the path to the server certificate file (.pem or .crt)
///
public string ServerCertificatePath { get; set; } = string.Empty;
///
/// Gets or sets the path to the server private key file (.key)
///
public string ServerKeyPath { get; set; } = string.Empty;
///
/// Gets or sets the path to the certificate authority file for client certificate validation (optional)
///
public string? ClientCaCertificatePath { get; set; }
///
/// Gets or sets whether to require client certificates for mutual TLS
///
public bool RequireClientCertificate { get; set; } = false;
///
/// Gets or sets whether to check certificate revocation
///
public bool CheckCertificateRevocation { get; set; } = true;
///
/// Validates the TLS configuration
///
/// True if configuration is valid, false otherwise
public bool Validate()
{
if (!Enabled)
{
return true; // No validation needed if TLS is disabled
}
if (string.IsNullOrWhiteSpace(ServerCertificatePath))
{
Log.Error("TLS is enabled but ServerCertificatePath is not configured");
return false;
}
if (string.IsNullOrWhiteSpace(ServerKeyPath))
{
Log.Error("TLS is enabled but ServerKeyPath is not configured");
return false;
}
if (!File.Exists(ServerCertificatePath))
{
Log.Warning("Server certificate file not found: {Path} - will be auto-generated on startup",
ServerCertificatePath);
}
if (!File.Exists(ServerKeyPath))
{
Log.Warning("Server key file not found: {Path} - will be auto-generated on startup", ServerKeyPath);
}
if (RequireClientCertificate && string.IsNullOrWhiteSpace(ClientCaCertificatePath))
{
Log.Error("Client certificate is required but ClientCaCertificatePath is not configured");
return false;
}
if (!string.IsNullOrWhiteSpace(ClientCaCertificatePath) && !File.Exists(ClientCaCertificatePath))
{
Log.Warning("Client CA certificate file not found: {Path} - will be auto-generated on startup",
ClientCaCertificatePath);
}
return true;
}
}
}