Skip to content

Commit 3d1df27

Browse files
dhaiducekopenshift-merge-robot
authored andcommitted
Fix RegEx to allow any character in a template name
If an invalid character is provided in a template name, our RegEx was filtering it out and not handling it properly. ref: https://issues.redhat.com/browse/ACM-7737 Signed-off-by: Dale Haiducek <[email protected]>
1 parent da52641 commit 3d1df27

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

controllers/statussync/policy_status_sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
184184
// filter events to current policy instance and build map
185185
eventForPolicyMap := make(map[string]*[]historyEvent)
186186
// panic if regexp invalid
187-
rgx := regexp.MustCompile(`(?i)^policy:\s*(?:([a-z0-9.-]+)\s*\/)?([a-z0-9.-]+)`)
187+
rgx := regexp.MustCompile(`(?i)^policy:\s*(?:([a-z0-9.-]+)\s*\/)?(.+)`)
188188
for _, event := range eventList.Items {
189189
// sample event.Reason -- reason: 'policy: calamari/policy-grc-rbactest-example'
190190
reason := rgx.FindString(event.Reason)

test/e2e/case10_error_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
. "github.com/onsi/gomega"
1212
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1313
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
14+
"k8s.io/apimachinery/pkg/runtime"
1415
"k8s.io/apimachinery/pkg/types"
16+
policiesv1 "open-cluster-management.io/governance-policy-propagator/api/v1"
1517
"open-cluster-management.io/governance-policy-propagator/test/utils"
1618
)
1719

@@ -110,6 +112,35 @@ var _ = Describe("Test error handling", func() {
110112
1,
111113
).Should(BeTrue())
112114
})
115+
It("should generate creation err event", func() {
116+
policyName := "case10-invalid-name-error"
117+
statusMsg := "template-error; Failed to create policy template:"
118+
119+
hubApplyPolicy(policyName,
120+
yamlBasePath+"invalid-name-error.yaml")
121+
122+
By("Checking for event with creation err on managed cluster in ns:" + clusterNamespace)
123+
Eventually(
124+
checkForEvent(policyName, statusMsg),
125+
defaultTimeoutSeconds,
126+
1,
127+
).Should(BeTrue())
128+
By("Checking if policy status is noncompliant")
129+
hubPlc := utils.GetWithTimeout(
130+
clientHubDynamic,
131+
gvrPolicy,
132+
policyName,
133+
clusterNamespaceOnHub,
134+
true,
135+
defaultTimeoutSeconds)
136+
var plc *policiesv1.Policy
137+
err := runtime.DefaultUnstructuredConverter.FromUnstructured(hubPlc.Object, &plc)
138+
Expect(err).ToNot(HaveOccurred())
139+
Expect(plc.Status.Details).To(HaveLen(1))
140+
Expect(plc.Status.Details[0].History).To(HaveLen(1))
141+
Expect(plc.Status.Details[0].TemplateMeta.GetName()).To(Equal("case10_invalid-name"))
142+
Expect(plc.Status.Details[0].History[0].Message).To(ContainSubstring(statusMsg))
143+
})
113144
It("should generate unsupported object err event", func() {
114145
hubApplyPolicy("case10-unsupported-object",
115146
yamlBasePath+"unsupported-object-error.yaml")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: case10-invalid-name-error
5+
labels:
6+
policy.open-cluster-management.io/cluster-name: managed
7+
policy.open-cluster-management.io/cluster-namespace: managed
8+
policy.open-cluster-management.io/root-policy: case10-template-name-error
9+
spec:
10+
remediationAction: inform
11+
disabled: false
12+
policy-templates:
13+
- objectDefinition:
14+
apiVersion: policy.open-cluster-management.io/v1
15+
kind: ConfigurationPolicy
16+
metadata:
17+
name: case10_invalid-name
18+
spec:
19+
object-templates:
20+
- complianceType: musthave
21+
objectDefinition:
22+
apiVersion: v1
23+
kind: Pod
24+
metadata:
25+
name: nginx-pod-e2e
26+
namespace: default
27+
spec:
28+
containers:
29+
- name: nginx
30+

0 commit comments

Comments
 (0)