Skip to content

Commit

Permalink
Add ability to handle malformed Check files
Browse files Browse the repository at this point in the history
  • Loading branch information
jamie-suse committed Nov 25, 2022
1 parent 8469cd1 commit 2619ef6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
46 changes: 29 additions & 17 deletions lib/wanda/catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ defmodule Wanda.Catalog do
"""
@spec get_check(String.t()) :: {:ok, Check.t()} | {:error, any}
def get_check(check_id) do
case get_catalog_path()
|> Path.join("#{check_id}.yaml")
|> YamlElixir.read_from_file() do
{:ok, file_content} ->
{:ok, map_check(file_content)}
with path <- Path.join(get_catalog_path(), "#{check_id}.yaml"),
{:ok, file_content} <- YamlElixir.read_from_file(path),
{:ok, check} <-
map_check(file_content) do
{:ok, check}
else
{:error, :malformed_error} = error ->
Logger.error(
"Check with ID #{check_id} is malformed. Check if all the required fields are present."
)

error

{:error, reason} = error ->
Logger.error("Error getting file with ID #{check_id}: #{inspect(reason)}")
Logger.error("Error getting Check with ID #{check_id}: #{inspect(reason)}")
error
end
end
Expand Down Expand Up @@ -72,17 +79,22 @@ defmodule Wanda.Catalog do
"expectations" => expectations
} = check
) do
%Check{
id: id,
name: name,
group: group,
description: description,
remediation: remediation,
severity: map_severity(check),
facts: Enum.map(facts, &map_fact/1),
values: map_values(check),
expectations: Enum.map(expectations, &map_expectation/1)
}
{:ok,
%Check{
id: id,
name: name,
group: group,
description: description,
remediation: remediation,
severity: map_severity(check),
facts: Enum.map(facts, &map_fact/1),
values: map_values(check),
expectations: Enum.map(expectations, &map_expectation/1)
}}
end

defp map_check(_) do
{:error, :malformed_check}
end

defp map_severity(%{"severity" => "critical"}), do: :critical
Expand Down
11 changes: 8 additions & 3 deletions test/catalog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ defmodule Wanda.CatalogTest do
test "should return the whole catalog" do
catalog_path = Application.fetch_env!(:wanda, Wanda.Catalog)[:catalog_path]

files =
valid_files =
catalog_path
|> File.ls!()
|> Enum.sort()
|> Enum.filter(fn file -> file != "malformed_check.yaml" end)

catalog = Catalog.get_catalog()
assert length(files) == length(catalog)
assert length(valid_files) == length(catalog)

Enum.with_index(catalog, fn check, index ->
file_name =
files
valid_files
|> Enum.at(index)
|> Path.basename(".yaml")

Expand Down Expand Up @@ -108,6 +109,10 @@ defmodule Wanda.CatalogTest do
assert {:error, _} = Catalog.get_check("non_existent_check")
end

test "should return an error for malformed check" do
assert {:error, :malformed_check} = Catalog.get_check("malformed_check")
end

test "should load multiple checks" do
assert [%Check{id: "expect_check"}, %Check{id: "expect_same_check"}] =
Catalog.get_checks(["expect_check", "non_existent_check", "expect_same_check"])
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/catalog/malformed_check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
id: malformed_check
name: Malformed check
group: Test
description: |
A malformed check
remediation: |
## Remediation
Remediation text
expectations:
- name: some_expectation
expect_same: facts.jedi

0 comments on commit 2619ef6

Please sign in to comment.