Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions features/__snapshots__/validate_image.snap
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ Error: success criteria not met
"source": {},
"warnings": [
{
"msg": "Fails in 2099",
"msg": "Fails in 2099. This will become a failure starting on ${TIMESTAMP}",
"metadata": {
"effective_on": "${TIMESTAMP}"
}
Expand Down Expand Up @@ -1407,7 +1407,7 @@ Error: no suitable config file found at git::${GITHOST}/git/happy-config.git?ref
"source": {},
"violations": [
{
"msg": "Fails in 2099",
"msg": "Fails in 2099. This will become a failure starting on ${TIMESTAMP}",
"metadata": {
"effective_on": "${TIMESTAMP}"
}
Expand Down Expand Up @@ -2011,7 +2011,7 @@ For more information about policy issues, see the policy documentation: https://
"source": {},
"violations": [
{
"msg": "Fails in 2099",
"msg": "Fails in 2099. This will become a failure starting on ${TIMESTAMP}",
"metadata": {
"effective_on": "${TIMESTAMP}"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/evaluator/conftest_evaluator_unit_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestConftestEvaluatorEvaluateSeverity(t *testing.T) {
},

{
Message: "not yet effective",
Message: "not yet effective. This will become a failure starting on 3021-01-01T00:00:00Z",
Metadata: map[string]any{
"effective_on": "3021-01-01T00:00:00Z",
},
Expand Down
28 changes: 26 additions & 2 deletions internal/evaluator/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,10 @@ func (f *LegacyPostEvaluationFilter) CategorizeResults(
warnings = append(warnings, result)
}
case "failure":
if getSeverity(result) == severityWarning || !isResultEffective(result, effectiveTime) {
if getSeverity(result) == severityWarning {
warnings = append(warnings, result)
} else if !isResultEffective(result, effectiveTime) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] correctness

Shallow copy via 'enhancedResult := result' shares the underlying Metadata map between original and copy. Currently safe (only Message is modified), but could lead to subtle aliasing bugs if future changes also mutate Metadata on the enhanced result.

result.Message = formatFutureEnforcementMessage(result)
warnings = append(warnings, result)
} else {
failures = append(failures, result)
Expand Down Expand Up @@ -1119,7 +1122,10 @@ func (f *UnifiedPostEvaluationFilter) CategorizeResults(
warnings = append(warnings, filteredResult)
}
case "failure":
if getSeverity(filteredResult) == severityWarning || !isResultEffective(filteredResult, effectiveTime) {
if getSeverity(filteredResult) == severityWarning {
warnings = append(warnings, filteredResult)
} else if !isResultEffective(filteredResult, effectiveTime) {
filteredResult.Message = formatFutureEnforcementMessage(filteredResult)
warnings = append(warnings, filteredResult)
} else {
failures = append(failures, filteredResult)
Expand Down Expand Up @@ -1204,3 +1210,21 @@ func (f *UnifiedPostEvaluationFilter) determineOriginalType(filteredResult Resul

return "unknown"
}

// formatFutureEnforcementMessage enhances a result message to indicate when
// a future effective_on date will cause enforcement to begin.
// Returns the enhanced message if effective_on is present, otherwise returns
// the original message unchanged.
func formatFutureEnforcementMessage(result Result) string {
originalMessage := result.Message

// Check if this result has an effective_on date
effectiveOnStr, ok := result.Metadata[metadataEffectiveOn].(string)
if !ok || effectiveOnStr == "" {
return originalMessage
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[low] correctness

strings.TrimRight(originalMessage, ".") removes all trailing characters in the cutset, not just a single trailing period. If a message ends with "..." (ellipsis), all three dots are stripped. strings.TrimSuffix would be more precise.

Suggested fix: Replace strings.TrimRight(originalMessage, ".") with strings.TrimSuffix(originalMessage, ".") to remove exactly one trailing period.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


// Append enforcement notice with the effective_on date as-is
return fmt.Sprintf("%s. This will become a failure starting on %s",
strings.TrimSuffix(originalMessage, "."), effectiveOnStr)
}
76 changes: 76 additions & 0 deletions internal/evaluator/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1796,3 +1796,79 @@ func TestPipelineIntentionWithMultipleValues(t *testing.T) {
"security package should be included (has included rules)")
})
}

//////////////////////////////////////////////////////////////////////////////
// formatFutureEnforcementMessage tests
//////////////////////////////////////////////////////////////////////////////

func TestFormatFutureEnforcementMessage(t *testing.T) {
tests := []struct {
name string
result Result
expected string
}{
{
name: "with effective_on string",
result: Result{
Message: "The required 'cpe' label is missing",
Metadata: map[string]interface{}{
metadataEffectiveOn: "2024-12-01T00:00:00Z",
},
},
expected: "The required 'cpe' label is missing. This will become a failure starting on 2024-12-01T00:00:00Z",
},
{
name: "missing effective_on metadata",
result: Result{
Message: "Some error message",
Metadata: map[string]interface{}{},
},
expected: "Some error message",
},
{
name: "non-string effective_on",
result: Result{
Message: "Some error message",
Metadata: map[string]interface{}{
metadataEffectiveOn: 12345,
},
},
expected: "Some error message",
},
{
name: "empty string effective_on",
result: Result{
Message: "Some error message",
Metadata: map[string]interface{}{
metadataEffectiveOn: "",
},
},
expected: "Some error message",
},
{
name: "nil metadata",
result: Result{
Message: "Some error message",
Metadata: nil,
},
expected: "Some error message",
},
{
name: "message already ends with period",
result: Result{
Message: "The required 'cpe' label is missing.",
Metadata: map[string]interface{}{
metadataEffectiveOn: "2025-01-15T00:00:00Z",
},
},
expected: "The required 'cpe' label is missing. This will become a failure starting on 2025-01-15T00:00:00Z",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := formatFutureEnforcementMessage(tc.result)
assert.Equal(t, tc.expected, got)
})
}
}
Loading