Skip to content

Commit

Permalink
Add ability to handle non-existent checks in catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-suse committed Nov 25, 2022
1 parent 8ad6f97 commit 8eda57f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 68 deletions.
28 changes: 21 additions & 7 deletions lib/wanda/catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule Wanda.Catalog do
Value
}

require Logger

@default_severity :critical

@doc """
Expand All @@ -22,26 +24,38 @@ 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 """
Get specific checks from the catalog.
"""
@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
Expand Down
128 changes: 67 additions & 61 deletions test/catalog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 8eda57f

Please sign in to comment.