From 9c9d5b55dfe91ac08c71fe12018e4641aefa2c53 Mon Sep 17 00:00:00 2001 From: coffeecupjapan Date: Sat, 7 Sep 2024 18:02:39 +0900 Subject: [PATCH] fix regexp --- pkg/webhook/pod/containerregistry_registry.go | 4 +- .../pod/containerregistry_registry_test.go | 48 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pkg/webhook/pod/containerregistry_registry_test.go diff --git a/pkg/webhook/pod/containerregistry_registry.go b/pkg/webhook/pod/containerregistry_registry.go index 120359f3..4ab67e64 100644 --- a/pkg/webhook/pod/containerregistry_registry.go +++ b/pkg/webhook/pod/containerregistry_registry.go @@ -51,7 +51,9 @@ func (r registry) Tag() string { func NewRegistry(value string) Registry { reg := make(registry) - r := regexp.MustCompile(`((?P[a-zA-Z0-9-._]+(:\d+)?)\/)?(?P.*\/)?(?P[a-zA-Z0-9-._]+:(?P[a-zA-Z0-9-._]+))?`) + // registry name should start with a-zA-Z0-9 and end with a-zA-Z0-9 + // @see https://github.com/kubernetes/kubernetes/blob/v1.31.0/pkg/apis/core/validation/validation.go#L7281-L7284 + r := regexp.MustCompile(`((?P[a-zA-Z0-9][a-zA-Z0-9-.]+[a-zA-Z0-9](:\d+)?)\/)?(?P.*\/)?(?P[a-zA-Z0-9-._]+:(?P[a-zA-Z0-9-._]+))?`) match := r.FindStringSubmatch(value) for i, name := range r.SubexpNames() { diff --git a/pkg/webhook/pod/containerregistry_registry_test.go b/pkg/webhook/pod/containerregistry_registry_test.go new file mode 100644 index 00000000..66fd32ac --- /dev/null +++ b/pkg/webhook/pod/containerregistry_registry_test.go @@ -0,0 +1,48 @@ +package pod + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// struct indicates [value(url that should check), bool(expect result)] +type RegistryValueAndExpected struct { + Value string + Expected bool +} + +func checkContainerRegistryContainerRegexp(value string) bool { + checkRegistry := NewRegistry(value) + if checkRegistry.Registry() == "" || checkRegistry.Image() == "" || checkRegistry.Tag() == "" { + return false + } + return true +} + +func TestContainerRegistry_Registry_Regexp(t *testing.T) { + resgistryValueAndExpectValue := []RegistryValueAndExpected{ + {Value: "some-Domain.domain.name/linux:latest", Expected: true}, + {Value: "some-Domain.domain.name/repository/linux:latest", Expected: true}, + {Value: "111.some-Domain.domain.com/linux:latest", Expected: true}, + {Value: "some-Domain.domain.com:8080/linux:latest", Expected: true}, + // check whether registry starts with valid characters + {Value: "-111.some-Domain.domain.com/linux:latest", Expected: false}, + {Value: ".111.some-Domain.domain.com/linux:latest", Expected: false}, + {Value: "_111.some-Domain.domain.com/linux:latest", Expected: false}, + // check whether registry content should not include invalid characters + {Value: "111.some_Domain.domain.com/linux:latest", Expected: false}, + // check whether registry ends with valid characters + {Value: "111.some-Domain.domain.co-/linux:latest", Expected: false}, + {Value: "111.some-Domain.domain.co./linux:latest", Expected: false}, + {Value: "111.some-Domain.domain.co_/linux:latest", Expected: false}, + } + for _, testStruct := range resgistryValueAndExpectValue { + actualValue := checkContainerRegistryContainerRegexp(testStruct.Value) + if actualValue == true { + assert.True(t, actualValue) + } else { + assert.False(t, actualValue) + } + } +}