-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
156 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
using HomeAssistantDiscoveryNet; | ||
using MQTTnet.Client; | ||
using System.ComponentModel.DataAnnotations; | ||
using HomeAssistantDiscoveryNet; | ||
|
||
namespace ToMqttNet; | ||
|
||
public class MqttConnectionOptions | ||
{ | ||
[Required] | ||
public string NodeId { get; set; } = null!; | ||
public const string Section = "MqttConnection"; | ||
|
||
public MqttClientOptions ClientOptions { get; set; } = new MqttClientOptions { }; | ||
public int? Port { get; set; } | ||
public bool UseTls { get; set; } | ||
[Required] | ||
public string NodeId { get; set; } = null!; | ||
public string? Server { get; set; } | ||
public string? CaCrt { get; set; } | ||
public string? ClientCrt { get; set; } | ||
public string? ClientKey { get; set; } | ||
|
||
public MqttDiscoveryConfigOrigin? OriginConfig { get; set; } | ||
public MqttDiscoveryConfigOrigin? OriginConfig { get; set; } | ||
} |
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,76 @@ | ||
using System.Security.Cryptography.X509Certificates; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using MQTTnet.Client; | ||
|
||
namespace ToMqttNet; | ||
|
||
public class WatchingMqttCertificateProvider : IMqttClientCertificatesProvider | ||
{ | ||
private readonly List<FileSystemWatcher> _watchers = []; | ||
private readonly MqttConnectionOptions _options; | ||
private readonly ILogger<WatchingMqttCertificateProvider> _logger; | ||
|
||
public X509Certificate2? CaCertificate { get; private set; } | ||
public X509Certificate2Collection ClientCertificates { get; private set; } = new(); | ||
|
||
public WatchingMqttCertificateProvider(ILogger<WatchingMqttCertificateProvider> logger, IOptions<MqttConnectionOptions> options) | ||
{ | ||
_options = options.Value; | ||
_logger = logger; | ||
var certDirectories = new string?[] { _options.CaCrt, _options.ClientCrt, _options.ClientKey } | ||
.Where(x => x != null) | ||
.Select(x => Path.GetDirectoryName(x)!) | ||
.Distinct() | ||
.ToList(); | ||
|
||
foreach (var directory in certDirectories) | ||
{ | ||
var watcher = new FileSystemWatcher(directory); | ||
_watchers.Add(watcher); | ||
|
||
watcher.Changed += OnCertificateChanged; | ||
watcher.EnableRaisingEvents = true; | ||
} | ||
|
||
LoadCertificates(); | ||
} | ||
|
||
private void LoadCertificates() | ||
{ | ||
_logger.LogInformation("Loading certificates"); | ||
try | ||
{ | ||
ClientCertificates.Clear(); | ||
if (_options.ClientCrt != null && _options.ClientKey != null) | ||
{ | ||
var clientCert = X509Certificate2.CreateFromPemFile(_options.ClientCrt, _options.ClientKey); | ||
ClientCertificates.Add(clientCert); | ||
_logger.LogInformation("Loaded Client Certificate {name} from {certPath}, {keyPath}", clientCert.Thumbprint, _options.ClientCrt, _options.ClientKey); | ||
} | ||
|
||
if (_options.CaCrt != null) | ||
{ | ||
CaCertificate = new X509Certificate2(_options.CaCrt); | ||
Check warning on line 54 in src/ToMqttNet/WatchingMqttCertificateProvider.cs GitHub Actions / build
|
||
ClientCertificates.Add(CaCertificate); | ||
_logger.LogInformation("Loaded CA Certificate {name} from {path}", CaCertificate.Thumbprint, _options.CaCrt); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to load certificates"); | ||
throw; | ||
} | ||
_logger.LogInformation("Certificates loaded"); | ||
} | ||
|
||
private void OnCertificateChanged(object sender, FileSystemEventArgs e) | ||
{ | ||
LoadCertificates(); | ||
} | ||
|
||
public X509CertificateCollection GetCertificates() | ||
{ | ||
return ClientCertificates; | ||
} | ||
} |
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