Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom email sender #695

Merged
merged 4 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ config :swoosh, :api_client, false
# configure the recipient for alert notifications
config :trento, :alerting,
enabled: true,
sender: "[email protected]",
recipient: "[email protected]"

# Configure esbuild (the version is required)
Expand Down
3 changes: 2 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ if config_env() == :prod do

config :trento, :alerting,
enabled: System.get_env("ENABLE_ALERTING", "false") == "true",
recipient: System.get_env("ALERT_RECIPIENT") || "[email protected]"
sender: System.get_env("ALERT_SENDER", "[email protected]"),
recipient: System.get_env("ALERT_RECIPIENT", "[email protected]")

config :trento, Trento.Mailer,
adapter: Swoosh.Adapters.SMTP,
Expand Down
17 changes: 16 additions & 1 deletion docs/alerting/alerting.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Currently **SMTP** is the **only supported delivery mechanism** for notification

```
ENABLE_ALERTING=true
[email protected]
[email protected]

SMTP_SERVER=your.smtp-server.com
Expand All @@ -32,4 +33,18 @@ SMTP_PASSWORD=password
```

## Enabling Alerting at a later stage
If your current Trento installation has Alerting disabled, you can enable it by... (TBD)
If your current Trento installation has Alerting disabled, you can enable it by upgrading the helm deployment.

```
helm upgrade
--install <THE_DEPLOYMENT>
--set trento-web.adminUser.password=<ADMIN_PASSWORD>
--set-file trento-runner.privateKey=<PRIVATE_SSH_KEY>
--set trento-web.alerting.enabled=true
--set trento-web.alerting.smtpServer=<SMTP_SERVER>
--set trento-web.alerting.smtpPort=<SMTP_PORT>
--set trento-web.alerting.smtpUser=<SMTP_USER>
--set trento-web.alerting.smtpPassword=<SMTP_PASSWORD>
--set trento-web.alerting.sender=<ALERT_SENDER>
--set trento-web.alerting.recipient=<ALERT_RECIPIENT>
```
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Trento.Application.UseCases.Alerting.EmailAlert do

def alert(component, identified_by, identifier, reason) do
new()
|> from({"Trento Alerts", "[email protected]"})
|> from({"Trento Alerts", Application.fetch_env!(:trento, :alerting)[:sender]})
|> to({"Trento Admin", Application.fetch_env!(:trento, :alerting)[:recipient]})
|> subject("Trento Alert: #{component} #{identifier} needs attention.")
|> render_body("critical.html", %{
Expand Down
31 changes: 29 additions & 2 deletions test/trento/application/usecases/alerting_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Trento.Application.UseCases.AlertingTest do
@moduledoc false
use ExUnit.Case, async: true
use Trento.DataCase

Expand All @@ -10,12 +11,16 @@ defmodule Trento.Application.UseCases.AlertingTest do

@moduletag :integration

@some_sender "[email protected]"
@some_recipient "[email protected]"

describe "Enabling/Disabling Alerting Feature" do
setup do
on_exit(fn ->
Application.put_env(:trento, :alerting,
enabled: true,
recipient: "[email protected]"
sender: @some_sender,
recipient: @some_recipient
)
end)
end
Expand All @@ -30,7 +35,12 @@ defmodule Trento.Application.UseCases.AlertingTest do
end

test "An error should be raised when alerting is enabled but no recipient was provided" do
Application.put_env(:trento, :alerting, enabled: true)
Application.put_env(:trento, :alerting,
enabled: true,
sender: @some_sender
# no recipient set
)

sap_system_id = Faker.UUID.v4()
insert(:sap_system, id: sap_system_id)

Expand All @@ -40,6 +50,23 @@ defmodule Trento.Application.UseCases.AlertingTest do

assert_no_email_sent()
end

test "An error should be raised when alerting is enabled but no sender was provided" do
Application.put_env(:trento, :alerting,
enabled: true,
# no sender set
recipient: @some_recipient
)

sap_system_id = Faker.UUID.v4()
insert(:sap_system, id: sap_system_id)

assert_raise ArgumentError,
~r/Unexpected tuple format, {"Trento Alerts", nil} cannot be formatted into a Recipient./,
fn -> Alerting.notify_critical_sap_system_health(sap_system_id) end

assert_no_email_sent()
end
end

describe "Alerting the configured recipient about crucial facts with email notifications" do
Expand Down