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
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
36 changes: 34 additions & 2 deletions internal/evaluator/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,14 @@ func (f *LegacyPostEvaluationFilter) CategorizeResults(
}
case "failure":
if getSeverity(result) == severityWarning || !isResultEffective(result, effectiveTime) {
warnings = append(warnings, result)
// If being demoted due to future effective_on, enhance the message

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] maintainability

The same 7-line enhancement block is duplicated in both LegacyPostEvaluationFilter.CategorizeResults and UnifiedPostEvaluationFilter.CategorizeResults. Changes to message format or guards require updating both sites.

Suggested fix: Extract a small helper function (e.g., appendWithEnhancement) to eliminate the duplication.

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

When a deny result has both severity:warning AND a future effective_on, the message claims it will become a failure, but the severity annotation keeps it as a warning even after the date passes. This is misleading for contradictory rule configurations.

Suggested fix: Consider guarding the enhancement to only fire when the demotion reason is exclusively the future effective_on (i.e., getSeverity != severityWarning), or add a code comment noting this edge case.

if !isResultEffective(result, effectiveTime) {
enhancedResult := result
enhancedResult.Message = formatFutureEnforcementMessage(result)
warnings = append(warnings, enhancedResult)
} else {
warnings = append(warnings, result)
}
} else {
failures = append(failures, result)
}
Expand Down Expand Up @@ -1120,7 +1127,14 @@ func (f *UnifiedPostEvaluationFilter) CategorizeResults(
}
case "failure":
if getSeverity(filteredResult) == severityWarning || !isResultEffective(filteredResult, effectiveTime) {
warnings = append(warnings, filteredResult)
// If being demoted due to future effective_on, enhance the message
if !isResultEffective(filteredResult, effectiveTime) {
enhancedResult := filteredResult
enhancedResult.Message = formatFutureEnforcementMessage(filteredResult)
warnings = append(warnings, enhancedResult)
} else {
warnings = append(warnings, filteredResult)
}
} else {
failures = append(failures, filteredResult)
}
Expand Down Expand Up @@ -1204,3 +1218,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.


// Append enforcement notice with the effective_on date as-is
return fmt.Sprintf("%s. This will become a failure starting on %s",
strings.TrimRight(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