From 1fd3f91c37d72a95d479cd3ed1f9d8620567c4ae Mon Sep 17 00:00:00 2001 From: Alessio Biancalana Date: Tue, 23 Jul 2024 15:34:27 +0200 Subject: [PATCH] Discover host vulnerabilities only if SUSE Manager is enabled (#2803) --- ...oftware_updates_discovery_event_handler.ex | 13 +++++-- ...e_updates_discovery_event_handler_test.exs | 38 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/lib/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler.ex b/lib/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler.ex index 9fd2e2436c..f8c0b75080 100644 --- a/lib/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler.ex +++ b/lib/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler.ex @@ -14,6 +14,8 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.SoftwareUpdatesDiscovery SoftwareUpdatesHealthChanged } + alias Trento.Settings + alias Trento.SoftwareUpdates.Discovery def handle( @@ -23,9 +25,14 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.SoftwareUpdatesDiscovery }, _ ) do - Discovery.discover_host_software_updates(host_id, fully_qualified_domain_name) - - :ok + case Settings.get_suse_manager_settings() do + {:ok, _} -> + Discovery.discover_host_software_updates(host_id, fully_qualified_domain_name) + :ok + + _ -> + :ok + end end def handle(%SoftwareUpdatesHealthChanged{host_id: host_id}, _) do diff --git a/test/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler_test.exs b/test/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler_test.exs index 41610db63d..fb264c57f2 100644 --- a/test/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler_test.exs +++ b/test/trento/infrastructure/commanded/event_handlers/software_updates_discovery_event_handler_test.exs @@ -32,6 +32,8 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.SoftwareUpdatesDiscovery describe "Discovering software updates" do test "should discover software updates when a SoftwareUpdatesDiscoveryRequested is emitted" do + insert_software_updates_settings() + %SoftwareUpdatesDiscoveryRequested{ host_id: host_id, fully_qualified_domain_name: fully_qualified_domain_name @@ -66,7 +68,43 @@ defmodule Trento.Infrastructure.Commanded.EventHandlers.SoftwareUpdatesDiscovery assert :ok = SoftwareUpdatesDiscoveryEventHandler.handle(event, %{}) end + test "should discover data about hosts just when SUSE Manager's settings are set" do + event = build(:software_updates_discovery_requested_event) + + expect( + SoftwareUpdatesDiscoveryMock, + :get_system_id, + 0, + fn _ -> :ok end + ) + + expect( + SoftwareUpdatesDiscoveryMock, + :get_relevant_patches, + 0, + fn _ -> :ok end + ) + + expect( + SoftwareUpdatesDiscoveryMock, + :get_upgradable_packages, + 0, + fn _ -> :ok end + ) + + expect( + Trento.Commanded.Mock, + :dispatch, + 0, + fn _ -> :ok end + ) + + assert :ok = SoftwareUpdatesDiscoveryEventHandler.handle(event, %{}) + end + test "should pass through failures" do + insert_software_updates_settings() + %SoftwareUpdatesDiscoveryRequested{ fully_qualified_domain_name: fully_qualified_domain_name } = event = build(:software_updates_discovery_requested_event)