protected override void Send(AuditableAction action, Dictionary <string, string> tokens, PowershellNotificationChannelDefinition settings) { PowerShell powershell = this.sessionProvider.GetSession(settings.Script, "Write-AuditLog"); powershell.AddCommand("Write-AuditLog") .AddParameter("tokens", new System.Collections.Hashtable(tokens, StringComparer.OrdinalIgnoreCase)) .AddParameter("isSuccess", action.IsSuccess); Task task = new Task(() => { powershell.Invoke(); powershell.ThrowOnPipelineError(); }); task.Start(); if (!task.Wait(TimeSpan.FromSeconds(settings.TimeOut))) { throw new TimeoutException("The PowerShell script did not complete within the configured time"); } if (task.IsFaulted) { if (task.Exception != null) { throw task.Exception; } throw new AccessManagerException("The task failed"); } }
protected override void Send(AuditableAction action, Dictionary <string, string> tokens, WebhookNotificationChannelDefinition settings) { HttpClient client = new HttpClient(); HttpRequestMessage message = new HttpRequestMessage(); string content = action.IsSuccess ? templates.GetTemplate(settings.TemplateSuccess) : templates.GetTemplate(settings.TemplateFailure); content = TokenReplacer.ReplaceAsJson(tokens, content); message.Content = new StringContent(content, Encoding.UTF8, settings.ContentType); message.RequestUri = new Uri(settings.Url); message.Method = new HttpMethod(settings.HttpMethod); var response = client.SendAsync(message).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); }
private void Send(AuditableAction action, Dictionary <string, string> tokens, T channel, bool rethrowExceptions) { try { this.logger.LogTrace($"Attempting delivery of audit notification to {channel.Id}"); this.Send(action, tokens, channel); this.logger.LogTrace($"Delivery of audit notification to {channel.Id} successful"); } catch (Exception ex) { this.logger.LogEventError(EventIDs.NotificationChannelError, $"Delivery of audit notification to {channel.Id} failed", ex); if (rethrowExceptions) { throw; } } }
protected override void Send(AuditableAction action, Dictionary <string, string> tokens, SmtpNotificationChannelDefinition settings) { string message = action.IsSuccess ? templates.GetTemplate(settings.TemplateSuccess) : templates.GetTemplate(settings.TemplateFailure); string subject = GetSubjectLine(message, action.IsSuccess); message = TokenReplacer.ReplaceAsHtml(tokens, message); subject = TokenReplacer.ReplaceAsPlainText(tokens, subject); HashSet <string> recipients = new HashSet <string>(StringComparer.OrdinalIgnoreCase); settings.EmailAddresses.ForEach(t => recipients.Add(t)); if (recipients.Remove("{user.EmailAddress}")) { if (action?.User?.EmailAddress != null) { recipients.Add(action.User.EmailAddress); } } this.SendEmail(recipients, subject, message); }
public void ProcessNotification(AuditableAction action, Dictionary <string, string> tokens, IImmutableSet <string> notificationChannelIDs) { List <Exception> rethrowableExceptions = new List <Exception>(); foreach (var channel in this.NotificationChannelDefinitions) { if (notificationChannelIDs.Any(t => string.Equals(t, channel.Id, StringComparison.OrdinalIgnoreCase))) { if (!channel.Enabled) { this.logger.LogTrace($"Skipping delivery of audit notification to {channel.Id} as it is currently disabled"); continue; } try { if (channel.Mandatory && action.IsSuccess) { Send(action, tokens, channel, true); } else { this.queue.TryWrite(() => Send(action, tokens, channel, false)); } } catch (Exception ex) { rethrowableExceptions.Add(ex); } } } if (action.IsSuccess && rethrowableExceptions.Count > 0) { throw new AuditLogFailureException(rethrowableExceptions); } }
protected abstract void Send(AuditableAction action, Dictionary <string, string> tokens, T settings);