diff --git a/config/config.exs b/config/config.exs index 14188a8059..6273007033 100644 --- a/config/config.exs +++ b/config/config.exs @@ -32,6 +32,7 @@ config :swoosh, :api_client, false # configure the recipient for alert notifications config :trento, :alerting, enabled: true, + sender: "alerts@trento-project.io", recipient: "admin@trento.io" # Configure esbuild (the version is required) diff --git a/config/runtime.exs b/config/runtime.exs index a90c3cba6b..c098e49070 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -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") || "admin@trento-project.io" + sender: System.get_env("ALERT_SENDER", "alerts@trento-project.io"), + recipient: System.get_env("ALERT_RECIPIENT", "admin@trento-project.io") config :trento, Trento.Mailer, adapter: Swoosh.Adapters.SMTP, diff --git a/docs/alerting/alerting.md b/docs/alerting/alerting.md index 64b4fa5935..6c8e2448e6 100644 --- a/docs/alerting/alerting.md +++ b/docs/alerting/alerting.md @@ -23,6 +23,7 @@ Currently **SMTP** is the **only supported delivery mechanism** for notification ``` ENABLE_ALERTING=true +ALERT_SENDER=sender@yourmail.com ALERT_RECIPIENT=recipient@yourmail.com SMTP_SERVER=your.smtp-server.com @@ -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) \ No newline at end of file +If your current Trento installation has Alerting disabled, you can enable it by upgrading the helm deployment. + +``` +helm upgrade + --install + --set trento-web.adminUser.password= + --set-file trento-runner.privateKey= + --set trento-web.alerting.enabled=true + --set trento-web.alerting.smtpServer= + --set trento-web.alerting.smtpPort= + --set trento-web.alerting.smtpUser= + --set trento-web.alerting.smtpPassword= + --set trento-web.alerting.sender= + --set trento-web.alerting.recipient= +``` \ No newline at end of file diff --git a/lib/trento/application/usecases/alerting/email/email_alert.ex b/lib/trento/application/usecases/alerting/email/email_alert.ex index 6ec95fb85b..cceba05e96 100644 --- a/lib/trento/application/usecases/alerting/email/email_alert.ex +++ b/lib/trento/application/usecases/alerting/email/email_alert.ex @@ -7,7 +7,7 @@ defmodule Trento.Application.UseCases.Alerting.EmailAlert do def alert(component, identified_by, identifier, reason) do new() - |> from({"Trento Alerts", "alerts@trento-project.io"}) + |> 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", %{ diff --git a/test/trento/application/usecases/alerting_test.exs b/test/trento/application/usecases/alerting_test.exs index e56d1cead6..8211eebaf7 100644 --- a/test/trento/application/usecases/alerting_test.exs +++ b/test/trento/application/usecases/alerting_test.exs @@ -1,4 +1,5 @@ defmodule Trento.Application.UseCases.AlertingTest do + @moduledoc false use ExUnit.Case, async: true use Trento.DataCase @@ -10,12 +11,16 @@ defmodule Trento.Application.UseCases.AlertingTest do @moduletag :integration + @some_sender "some.sender@email.com" + @some_recipient "some.recipient@email.com" + describe "Enabling/Disabling Alerting Feature" do setup do on_exit(fn -> Application.put_env(:trento, :alerting, enabled: true, - recipient: "some.recipient@email.com" + sender: @some_sender, + recipient: @some_recipient ) end) end @@ -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) @@ -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