From db6dc234dfe91dadb3053c5d9a3914b5f896946e Mon Sep 17 00:00:00 2001 From: mprahl Date: Tue, 8 Nov 2022 14:54:07 -0500 Subject: [PATCH] Limit policy status event messages to 1024 characters Relates: https://issues.redhat.com/browse/ACM-2001 Signed-off-by: mprahl --- controllers/configurationpolicy_controller.go | 6 ++++ ...nfigurationpolicy_controller_suite_test.go | 34 ++++++++++--------- .../configurationpolicy_controller_test.go | 25 ++++++++++++++ 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/controllers/configurationpolicy_controller.go b/controllers/configurationpolicy_controller.go index 32faa285..793abbba 100644 --- a/controllers/configurationpolicy_controller.go +++ b/controllers/configurationpolicy_controller.go @@ -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 } diff --git a/controllers/configurationpolicy_controller_suite_test.go b/controllers/configurationpolicy_controller_suite_test.go index 313566de..f66c92d8 100644 --- a/controllers/configurationpolicy_controller_suite_test.go +++ b/controllers/configurationpolicy_controller_suite_test.go @@ -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) { diff --git a/controllers/configurationpolicy_controller_test.go b/controllers/configurationpolicy_controller_test.go index bdd5c55d..d4da06d2 100644 --- a/controllers/configurationpolicy_controller_test.go +++ b/controllers/configurationpolicy_controller_test.go @@ -236,6 +236,8 @@ func TestConvertPolicyStatusToString(t *testing.T) { compliantDetails = append(compliantDetails, compliantDetail) } + samplePolicy := getSamplePolicy() + samplePolicyStatus := policyv1.ConfigurationPolicyStatus{ ComplianceState: "Compliant", CompliancyDetails: compliantDetails, @@ -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{}{