summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Services/NotificationService.cs
diff options
context:
space:
mode:
authorWouterTinus <wouter.tinus@gmail.com>2019-09-06 22:43:32 +0200
committerWouterTinus <wouter.tinus@gmail.com>2019-09-06 22:43:32 +0200
commit42aa0faa4de6ea4184cfe1a5830508777418b11a (patch)
tree8e13b046964d271f58a02ff3ddbde2f690fd12b7 /src/main.lib/Services/NotificationService.cs
parent5a565e83a0aad40f935d4625c4d6770ad22fb603 (diff)
downloadletsencrypt-win-simple-42aa0faa4de6ea4184cfe1a5830508777418b11a.zip
letsencrypt-win-simple-42aa0faa4de6ea4184cfe1a5830508777418b11a.tar.gz
letsencrypt-win-simple-42aa0faa4de6ea4184cfe1a5830508777418b11a.tar.bz2
moves
Diffstat (limited to 'src/main.lib/Services/NotificationService.cs')
-rw-r--r--src/main.lib/Services/NotificationService.cs105
1 files changed, 105 insertions, 0 deletions
diff --git a/src/main.lib/Services/NotificationService.cs b/src/main.lib/Services/NotificationService.cs
new file mode 100644
index 0000000..3620eb8
--- /dev/null
+++ b/src/main.lib/Services/NotificationService.cs
@@ -0,0 +1,105 @@
+using PKISharp.WACS.Clients;
+using PKISharp.WACS.DomainObjects;
+using System;
+using System.Linq;
+using System.Net.Mail;
+
+namespace PKISharp.WACS.Services
+{
+ class NotificationService
+ {
+ private readonly ILogService _log;
+ private readonly ICertificateService _certificateService;
+ private readonly ISettingsService _settings;
+ private readonly EmailClient _email;
+
+ public NotificationService(
+ ILogService log,
+ ISettingsService setttings,
+ EmailClient email,
+ ICertificateService certificateService)
+ {
+ _log = log;
+ _certificateService = certificateService;
+ _email = email;
+ _settings = setttings;
+ }
+
+ /// <summary>
+ /// Handle success notification
+ /// </summary>
+ /// <param name="runLevel"></param>
+ /// <param name="renewal"></param>
+ internal void NotifySuccess(RunLevel runLevel, Renewal renewal)
+ {
+ // Do not send emails when running interactively
+ _log.Information(LogType.All, "Renewal for {friendlyName} succeeded", renewal.LastFriendlyName);
+ if (runLevel.HasFlag(RunLevel.Unattended) && _settings.EmailOnSuccess)
+ {
+ _email.Send(
+ "Certificate renewal completed",
+ $"<p>Certificate <b>{renewal.LastFriendlyName}</b> succesfully renewed.</p> {NotificationInformation(renewal)}",
+ MailPriority.Low);
+ }
+ }
+
+ /// <summary>
+ /// Handle failure notification
+ /// </summary>
+ /// <param name="runLevel"></param>
+ /// <param name="renewal"></param>
+ internal void NotifyFailure(RunLevel runLevel, Renewal renewal, string errorMessage)
+ {
+ // Do not send emails when running interactively
+ _log.Error("Renewal for {friendlyName} failed, will retry on next run", renewal.LastFriendlyName);
+ if (runLevel.HasFlag(RunLevel.Unattended))
+ {
+ _email.Send("Error processing certificate renewal",
+ $"<p>Renewal for <b>{renewal.LastFriendlyName}</b> failed with error <b>{errorMessage}</b>, will retry on next run.</p> {NotificationInformation(renewal)}",
+ MailPriority.High);
+ }
+ }
+
+ private string NotificationInformation(Renewal renewal)
+ {
+ try
+ {
+ var extraMessage = "";
+ extraMessage += $"<p>Hosts: {NotificationHosts(renewal)}</p>";
+ extraMessage += "<p><table><tr><td>Plugins</td><td></td></tr>";
+ extraMessage += $"<tr><td>Target: </td><td> {renewal.TargetPluginOptions.Name}</td></tr>";
+ extraMessage += $"<tr><td>Validation: </td><td> {renewal.ValidationPluginOptions.Name}</td></tr>";
+ extraMessage += $"<tr><td>CSR: </td><td> {renewal.CsrPluginOptions.Name}</td></tr>";
+ extraMessage += $"<tr><td>Store: </td><td> {string.Join(", ", renewal.StorePluginOptions.Select(x => x.Name))}</td></tr>";
+ extraMessage += $"<tr><td>Installation: </td><td> {string.Join(", ", renewal.InstallationPluginOptions.Select(x => x.Name))}</td></tr>";
+ extraMessage += "</table></p>";
+ return extraMessage;
+ }
+ catch (Exception ex)
+ {
+ _log.Error(ex, "Retrieval of metadata for email failed.");
+ return "";
+ }
+ }
+
+ private string NotificationHosts(Renewal renewal)
+ {
+ try
+ {
+ var cache = _certificateService.CachedInfo(renewal);
+ if (cache == null)
+ {
+ return "Unknown";
+ }
+ else
+ {
+ return string.Join(", ", cache.HostNames);
+ }
+ }
+ catch
+ {
+ return "Error";
+ }
+ }
+ }
+}