summaryrefslogtreecommitdiffstats
path: root/src/main.lib/Services/NotificationService.cs
blob: 7795face0cced173de9f67c6f89d993426ee81a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using MimeKit;
using PKISharp.WACS.Clients;
using PKISharp.WACS.DomainObjects;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace PKISharp.WACS.Services
{
    internal 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 created notification
        /// </summary>
        /// <param name="runLevel"></param>
        /// <param name="renewal"></param>
        internal async Task NotifyCreated(Renewal renewal, IEnumerable<MemoryEntry> log)
        {
            // Do not send emails when running interactively
            _log.Information(
                LogType.All, 
                "Certificate {friendlyName} created", 
                renewal.LastFriendlyName);
            if (_settings.Notification.EmailOnSuccess)
            {
                await _email.Send(
                    $"Certificate {renewal.LastFriendlyName} created",
                    @$"<p>Certificate <b>{HttpUtility.HtmlEncode(renewal.LastFriendlyName)}</b> succesfully created.</p> 
                    {NotificationInformation(renewal)}
                    {RenderLog(log)}",
                    MessagePriority.Normal);
            }
        }

        /// <summary>
        /// Handle success notification
        /// </summary>
        /// <param name="runLevel"></param>
        /// <param name="renewal"></param>
        internal async Task NotifySuccess(Renewal renewal, IEnumerable<MemoryEntry> log)
        {
            // Do not send emails when running interactively
            _log.Information(
                LogType.All, 
                "Renewal for {friendlyName} succeeded",
                renewal.LastFriendlyName);
            if (_settings.Notification.EmailOnSuccess)
            {
                await _email.Send(
                    $"Certificate renewal {renewal.LastFriendlyName} completed",
                    @$"<p>Certificate <b>{HttpUtility.HtmlEncode(renewal.LastFriendlyName)}</b> succesfully renewed.</p> 
                    {NotificationInformation(renewal)}
                    {RenderLog(log)}",
                    MessagePriority.NonUrgent);
            }
        }

        /// <summary>
        /// Handle failure notification
        /// </summary>
        /// <param name="runLevel"></param>
        /// <param name="renewal"></param>
        internal async Task NotifyFailure(
            RunLevel runLevel, 
            Renewal renewal, 
            List<string> errors,
            IEnumerable<MemoryEntry> log)
        {
            // Do not send emails when running interactively       
            _log.Error("Renewal for {friendlyName} failed, will retry on next run", renewal.LastFriendlyName);
            if (errors.Count == 0)
            {
                errors.Add("No specific error reason provided.");
            }
            if (runLevel.HasFlag(RunLevel.Unattended))
            {
                await _email.Send(
                    $"Error processing certificate renewal {renewal.LastFriendlyName}",
                    @$"<p>Renewal for <b>{HttpUtility.HtmlEncode(renewal.LastFriendlyName)}</b> failed, will retry on next run.<br><br>Error(s):
                    <ul><li>{string.Join("</li><li>", errors.Select(x => HttpUtility.HtmlEncode(x)))}</li></ul></p>
                    {NotificationInformation(renewal)}
                    {RenderLog(log)}",
                    MessagePriority.Urgent);
            }
        }

        private string RenderLog(IEnumerable<MemoryEntry> log) => @$"<p>Log output:<ul><li>{string.Join("</li><li>", log.Select(x => RenderLogEntry(x)))}</ul></p>";

        private string RenderLogEntry(MemoryEntry log)
        {
            var color = $"00000";
            switch (log.Level)
            {
                case LogEventLevel.Error:
                case LogEventLevel.Fatal:
                    color = "#8B0000";
                    break;

                case LogEventLevel.Warning:
                    color = "#CCCC00";
                    break;

                case LogEventLevel.Information:
                    color = "#000000";
                    break;

                case LogEventLevel.Debug:
                case LogEventLevel.Verbose:
                    color = "#a9a9a9";
                    break;
            }
            return $"<span style=\"color:{color}\">{log.Level} - {HttpUtility.HtmlEncode(log.Message)}</span>";
        }

        private string NotificationInformation(Renewal renewal)
        {
            try
            {
                var extraMessage = @$"<p>
                    <table>
                        <tr><td><b>Hosts</b><td><td></td></tr>
                        <tr><td colspan=""2"">{NotificationHosts(renewal)}</td></tr>
                        <tr><td colspan=""2"">&nbsp;</td></tr>
                        <tr><td><b>Plugins</b></td><td></td></tr>
                        <tr><td>Target: </td><td> {renewal.TargetPluginOptions.Name}</td></tr>";
                extraMessage += @$"<tr><td>Validation: </td><td> {renewal.ValidationPluginOptions.Name}</td></tr>";
                if (renewal.OrderPluginOptions != null)
                {
                    extraMessage += @$"<tr><td>Order: </td><td> {renewal.OrderPluginOptions.Name}</td></tr>";
                }
                if (renewal.CsrPluginOptions != null)
                {
                    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 infos = _certificateService.CachedInfos(renewal);
                if (infos == null || infos.Count() == 0)
                {
                    return "Unknown";
                }
                else
                {
                    return string.Join(", ", infos.SelectMany(i => i.SanNames).Distinct());
                }
            }
            catch
            {
                return "Error";
            }
        }
    }
}