diff --git a/lib/wanda/catalog.ex b/lib/wanda/catalog.ex index a466bc371..8f406ebf1 100644 --- a/lib/wanda/catalog.ex +++ b/lib/wanda/catalog.ex @@ -11,6 +11,8 @@ defmodule Wanda.Catalog do Value } + require Logger + @default_severity :critical @doc """ @@ -22,18 +24,24 @@ defmodule Wanda.Catalog do |> Path.join("/*") |> Path.wildcard() |> Enum.map(&Path.basename(&1, ".yaml")) - |> Enum.map(&get_check(&1)) + |> get_checks() end @doc """ Get a check from the catalog. """ - @spec get_check(String.t()) :: Check.t() + @spec get_check(String.t()) :: {:ok, Check.t()} | {:error, any} def get_check(check_id) do - get_catalog_path() - |> Path.join("#{check_id}.yaml") - |> YamlElixir.read_from_file!() - |> map_check() + case get_catalog_path() + |> Path.join("#{check_id}.yaml") + |> YamlElixir.read_from_file() do + {:ok, file_content} -> + {:ok, map_check(file_content)} + + {:error, reason} = error -> + Logger.error("Error getting file with ID #{check_id}: #{inspect(reason)}") + error + end end @doc """ @@ -41,7 +49,13 @@ defmodule Wanda.Catalog do """ @spec get_checks([String.t()]) :: [Check.t()] def get_checks(checks_id) do - Enum.map(checks_id, &get_check/1) + checks_id + |> Enum.flat_map(fn check_id -> + case get_check(check_id) do + {:ok, check} -> [check] + {:error, _} -> [] + end + end) end defp get_catalog_path do diff --git a/test/catalog_test.exs b/test/catalog_test.exs index 79426c5c7..d2da4eabc 100644 --- a/test/catalog_test.exs +++ b/test/catalog_test.exs @@ -34,77 +34,83 @@ defmodule Wanda.CatalogTest do end test "should load a check from a yaml file properly" do - assert %Check{ - id: "expect_check", - name: "Test check", - group: "Test", - description: "Just a check\n", - remediation: "## Remediation\nRemediation text\n", - severity: :critical, - facts: [ - %Fact{ - name: "jedi", - gatherer: "wandalorian", - argument: "-o" - }, - %Fact{ - name: "other_fact", - gatherer: "no_args_gatherer", - argument: "" - } - ], - values: [ - %Value{ - conditions: [ - %Condition{expression: "some_expression", value: 10}, - %Condition{expression: "some_other_expression", value: 15} - ], - default: 5, - name: "expected_value" - }, - %Value{ - conditions: [ - %Condition{expression: "some_third_expression", value: 5} - ], - default: 10, - name: "expected_higher_value" - } - ], - expectations: [ - %Expectation{ - name: "some_expectation", - type: :expect, - expression: "facts.jedi == values.expected_value" - }, - %Expectation{ - name: "some_other_expectation", - type: :expect, - expression: "facts.jedi > values.expected_higher_value" - } - ] - } = Catalog.get_check("expect_check") + assert {:ok, + %Check{ + id: "expect_check", + name: "Test check", + group: "Test", + description: "Just a check\n", + remediation: "## Remediation\nRemediation text\n", + severity: :critical, + facts: [ + %Fact{ + name: "jedi", + gatherer: "wandalorian", + argument: "-o" + }, + %Fact{ + name: "other_fact", + gatherer: "no_args_gatherer", + argument: "" + } + ], + values: [ + %Value{ + conditions: [ + %Condition{expression: "some_expression", value: 10}, + %Condition{expression: "some_other_expression", value: 15} + ], + default: 5, + name: "expected_value" + }, + %Value{ + conditions: [ + %Condition{expression: "some_third_expression", value: 5} + ], + default: 10, + name: "expected_higher_value" + } + ], + expectations: [ + %Expectation{ + name: "some_expectation", + type: :expect, + expression: "facts.jedi == values.expected_value" + }, + %Expectation{ + name: "some_other_expectation", + type: :expect, + expression: "facts.jedi > values.expected_higher_value" + } + ] + }} = Catalog.get_check("expect_check") end test "should load a expect_same expectation type" do - assert %Check{ - values: [], - expectations: [ - %Expectation{ - name: "some_expectation", - type: :expect_same, - expression: "facts.jedi" - } - ] - } = Catalog.get_check("expect_same_check") + assert {:ok, + %Check{ + values: [], + expectations: [ + %Expectation{ + name: "some_expectation", + type: :expect_same, + expression: "facts.jedi" + } + ] + }} = Catalog.get_check("expect_same_check") end test "should load a warning severity" do - assert %Check{severity: :warning} = Catalog.get_check("warning_severity_check") + assert {:ok, %Check{severity: :warning}} = Catalog.get_check("warning_severity_check") + end + + test "should return an error for non-existent check" do + assert {:error, _} = Catalog.get_check("non_existent_check") end test "should load multiple checks" do assert [%Check{id: "expect_check"}, %Check{id: "expect_same_check"}] = - Catalog.get_checks(["expect_check", "expect_same_check"]) + Catalog.get_checks(["expect_check", "non_existent_check", "expect_same_check"]) end end end