Skip to content

Commit 60a6d04

Browse files
authored
🐛 Add regex validation for signer (#336)
* Add regex validation for signer Signed-off-by: Jian Qiu <[email protected]> * do not allow start with - Signed-off-by: Jian Qiu <[email protected]> --------- Signed-off-by: Jian Qiu <[email protected]>
1 parent f9e7f50 commit 60a6d04

6 files changed

+42
-2
lines changed

addon/v1alpha1/0000_01_addon.open-cluster-management.io_managedclusteraddons.crd.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ spec:
314314
will use to create csr.
315315
maxLength: 571
316316
minLength: 5
317+
pattern: ^([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]+\/[a-z0-9-\.]+$
317318
type: string
318319
subject:
319320
description: 'subject is the user subject of the addon agent

addon/v1alpha1/0000_03_addon.open-cluster-management.io_addontemplates.crd.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ spec:
330330
agent will use to create csr.
331331
maxLength: 571
332332
minLength: 5
333+
pattern: ^([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]+\/[a-z0-9-\.]+$
333334
type: string
334335
signingCA:
335336
description: 'SigningCA represents the reference of the

addon/v1alpha1/types_addontemplate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ type CustomSignerRegistrationConfig struct {
157157
// +required
158158
// +kubebuilder:validation:MaxLength=571
159159
// +kubebuilder:validation:MinLength=5
160+
// +kubebuilder:validation:Pattern=^([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]+\/[a-z0-9-\.]+$
160161
SignerName string `json:"signerName"`
161162

162163
// Subject is the user subject of the addon agent to be registered to the hub.

addon/v1alpha1/types_managedclusteraddon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type RegistrationConfig struct {
5656
// +required
5757
// +kubebuilder:validation:MaxLength=571
5858
// +kubebuilder:validation:MinLength=5
59+
// +kubebuilder:validation:Pattern=^([a-z0-9][a-z0-9-]*[a-z0-9]\.)+[a-z]+\/[a-z0-9-\.]+$
5960
SignerName string `json:"signerName"`
6061

6162
// subject is the user subject of the addon agent to be registered to the hub.

test/integration-test.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ TEST_TMP :=/tmp
22

33
export KUBEBUILDER_ASSETS ?=$(TEST_TMP)/kubebuilder/bin
44

5-
K8S_VERSION ?=1.23.1
5+
K8S_VERSION ?=1.29.3
66
GOHOSTOS ?=$(shell go env GOHOSTOS)
77
GOHOSTARCH =amd64
88
KB_TOOLS_ARCHIVE_NAME :=kubebuilder-tools-$(K8S_VERSION)-$(GOHOSTOS)-$(GOHOSTARCH).tar.gz

test/integration/api/managedclusteraddon_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var _ = ginkgo.Describe("ManagedClusterAddOn API test", func() {
107107

108108
mca.Status.Registrations = []addonv1alpha1.RegistrationConfig{
109109
{
110-
SignerName: "addontest",
110+
SignerName: "open-cluster-management.io/addontest",
111111
},
112112
}
113113

@@ -119,6 +119,42 @@ var _ = ginkgo.Describe("ManagedClusterAddOn API test", func() {
119119
gomega.Expect(err).ToNot(gomega.HaveOccurred())
120120
})
121121

122+
ginkgo.It("Update failed with wrong signer name in the ManagedClusterAddOn", func() {
123+
managedClusterAddOn := &addonv1alpha1.ManagedClusterAddOn{
124+
ObjectMeta: metav1.ObjectMeta{
125+
Name: managedClusterAddOnName,
126+
},
127+
Spec: addonv1alpha1.ManagedClusterAddOnSpec{},
128+
}
129+
130+
_, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(testNamespace).Create(
131+
context.TODO(),
132+
managedClusterAddOn,
133+
metav1.CreateOptions{},
134+
)
135+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
136+
137+
mca, err := hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(testNamespace).Get(
138+
context.TODO(),
139+
managedClusterAddOnName,
140+
metav1.GetOptions{},
141+
)
142+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
143+
144+
mca.Status.Registrations = []addonv1alpha1.RegistrationConfig{
145+
{
146+
SignerName: "addontest",
147+
},
148+
}
149+
150+
_, err = hubAddonClient.AddonV1alpha1().ManagedClusterAddOns(testNamespace).UpdateStatus(
151+
context.TODO(),
152+
mca,
153+
metav1.UpdateOptions{},
154+
)
155+
gomega.Expect(err).To(gomega.HaveOccurred())
156+
})
157+
122158
ginkgo.It("Should update the ManagedClusterAddOn status with config", func() {
123159
managedClusterAddOn := &addonv1alpha1.ManagedClusterAddOn{
124160
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)