Skip to content

Commit

Permalink
Limit policy status event messages to 1024 characters
Browse files Browse the repository at this point in the history
Relates:
https://issues.redhat.com/browse/ACM-2001

Signed-off-by: mprahl <mprahl@users.noreply.github.com>
  • Loading branch information
mprahl authored and openshift-merge-robot committed Nov 9, 2022
1 parent 33290e5 commit dfed254
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
6 changes: 6 additions & 0 deletions controllers/configurationpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2540,6 +2540,12 @@ func convertPolicyStatusToString(plc *policyv1.ConfigurationPolicy) (results str
}
}

runeResult := []rune(result)

if len(runeResult) > 1024 {
result = string(append(runeResult[:1021], '.', '.', '.'))
}

return result
}

Expand Down
34 changes: 18 additions & 16 deletions controllers/configurationpolicy_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@ import (
policyv1 "open-cluster-management.io/config-policy-controller/api/v1"
)

var samplePolicy = policyv1.ConfigurationPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: policyv1.ConfigurationPolicySpec{
Severity: "low",
NamespaceSelector: policyv1.Target{
Include: []policyv1.NonEmptyString{"default", "kube-*"},
Exclude: []policyv1.NonEmptyString{"kube-system"},
func getSamplePolicy() policyv1.ConfigurationPolicy {
return policyv1.ConfigurationPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
RemediationAction: "inform",
ObjectTemplates: []*policyv1.ObjectTemplate{
{
ComplianceType: "musthave",
ObjectDefinition: runtime.RawExtension{},
Spec: policyv1.ConfigurationPolicySpec{
Severity: "low",
NamespaceSelector: policyv1.Target{
Include: []policyv1.NonEmptyString{"default", "kube-*"},
Exclude: []policyv1.NonEmptyString{"kube-system"},
},
RemediationAction: "inform",
ObjectTemplates: []*policyv1.ObjectTemplate{
{
ComplianceType: "musthave",
ObjectDefinition: runtime.RawExtension{},
},
},
},
},
}
}

func TestMain(m *testing.M) {
Expand Down
25 changes: 25 additions & 0 deletions controllers/configurationpolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func TestConvertPolicyStatusToString(t *testing.T) {
compliantDetails = append(compliantDetails, compliantDetail)
}

samplePolicy := getSamplePolicy()

samplePolicyStatus := policyv1.ConfigurationPolicyStatus{
ComplianceState: "Compliant",
CompliancyDetails: compliantDetails,
Expand All @@ -246,6 +248,29 @@ func TestConvertPolicyStatusToString(t *testing.T) {
assert.NotNil(t, policyInString)
}

func TestConvertPolicyStatusToStringLongMsg(t *testing.T) {
msg := "Do. Or do not. There is no try."
for len([]rune(msg)) < 1024 {
msg += " Do. Or do not. There is no try."
}

samplePolicy := getSamplePolicy()

samplePolicy.Status = policyv1.ConfigurationPolicyStatus{
ComplianceState: "Compliant",
CompliancyDetails: []policyv1.TemplateStatus{
{
ComplianceState: policyv1.NonCompliant,
Conditions: []policyv1.Condition{{Message: msg}},
},
},
}
statusMsg := convertPolicyStatusToString(&samplePolicy)

assert.Contains(t, statusMsg, "...")
assert.Len(t, []rune(statusMsg), 1024)
}

func TestMerge(t *testing.T) {
oldList := []interface{}{
map[string]interface{}{
Expand Down

0 comments on commit dfed254

Please sign in to comment.