Skip to content

Commit

Permalink
Merge pull request #1225 from lcarva/dupe-trusted-task-as-warning
Browse files Browse the repository at this point in the history
Set dupe entries as warnings in trusted tasks
  • Loading branch information
lcarva authored Nov 19, 2024
2 parents 2143ebf + fcec9f6 commit 381024a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions policy/lib/json/schema.rego
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,8 @@ _prepare_document(doc) := d if {
_severity(e) := "warning" if {
startswith(e.desc, "Additional property")
} else := "failure"

with_severity_for_pattern(issue, severity, pattern) := updated_issue if {
regex.match(pattern, issue.message)
updated_issue := object.union(issue, {"severity": severity})
} else := issue
26 changes: 26 additions & 0 deletions policy/lib/json/schema_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,29 @@ test_validate_schema_unknown_property_warning if {
}),
)
}

test_with_severity_for_pattern if {
# Empty issue
lib.assert_equal(
{},
j.with_severity_for_pattern({}, "warning", ".*"),
)

# Empty message
lib.assert_equal(
{"message": "", "severity": "warning"},
j.with_severity_for_pattern({"message": ""}, "warning", ".*"),
)

# Message not matched
lib.assert_equal(
{"message": "spam"},
j.with_severity_for_pattern({"message": "spam"}, "warning", "bacon"),
)

# Severity overwritten
lib.assert_equal(
{"message": "spam", "severity": "warning"},
j.with_severity_for_pattern({"message": "spam", "severity": "failure"}, "warning", "spam"),
)
}
5 changes: 4 additions & 1 deletion policy/lib/tekton/trusted.rego
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ data_errors contains error if {
}},
},
)
error := {

original_error := {
"message": sprintf("trusted_tasks data has unexpected format: %s", [e.message]),
"severity": e.severity,
}

error := j.with_severity_for_pattern(original_error, "warning", "must be unique")
}

data_errors contains error if {
Expand Down
8 changes: 8 additions & 0 deletions policy/lib/tekton/trusted_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ test_data_errors if {
{"ref": "bad-effective-on", "effective_on": "not-a-date"},
{"ref": "bad-effective-on", "effective_on": "2024-01-01T00:00:00Z", "expires_on": "not-a-date"},
],
"duplicated-entries": [
{"ref": "sha256:digest", "effective_on": "2099-01-01T00:00:00Z"},
{"ref": "sha256:digest", "effective_on": "2099-01-01T00:00:00Z"},
],
}

expected := {
Expand Down Expand Up @@ -118,6 +122,10 @@ test_data_errors if {
"message": `trusted_tasks.bad-dates[1].expires_on is not valid RFC3339 format: "not-a-date"`,
"severity": "failure",
},
{
"message": "trusted_tasks data has unexpected format: duplicated-entries: array items[0,1] must be unique",
"severity": "warning",
},
}

lib.assert_equal(tekton.data_errors, expected) with data.trusted_tasks as tasks
Expand Down

0 comments on commit 381024a

Please sign in to comment.