mirror of
https://github.com/fatedier/frp.git
synced 2026-11-05 07:21:36 +08:00
refactor(validation): inline annotation qualified name check (#5601)
Replace the single use of k8s.io/apimachinery/pkg/util/validation .IsQualifiedName (annotation key validation in pkg/config/v1/validation) with a local implementation in pkg/util/validation. Newer apimachinery versions link klog only for IP/CIDR warning helpers, which grows frpc and frps, and this was the only frp import of that package. The local IsQualifiedName keeps the exact behavior and error messages of apimachinery v0.28.8, including the qualified-name (label key) format and the DNS-1123 subdomain prefix rules. Annotation keys are still lowercased before validation, so the accepted set is unchanged. Verified against apimachinery v0.28.8 on 228,220 inputs (exhaustive short strings, boundary lengths and seeded random data, including lowercased variants): every error message matches. No dependency versions change; go.mod and go.sum are untouched.
This commit is contained in:
@@ -20,9 +20,8 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
|
||||
v1 "github.com/fatedier/frp/pkg/config/v1"
|
||||
utilvalidation "github.com/fatedier/frp/pkg/util/validation"
|
||||
)
|
||||
|
||||
func validateProxyBaseConfigForClient(c *v1.ProxyBaseConfig) error {
|
||||
@@ -248,7 +247,7 @@ func ValidateAnnotations(annotations map[string]string) error {
|
||||
|
||||
var errs error
|
||||
for k := range annotations {
|
||||
for _, msg := range validation.IsQualifiedName(strings.ToLower(k)) {
|
||||
for _, msg := range utilvalidation.IsQualifiedName(strings.ToLower(k)) {
|
||||
errs = AppendError(errs, fmt.Errorf("annotation key %s is invalid: %s", k, msg))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,3 +74,18 @@ func TestValidateDomainConfigForServerRejectsSubdomainHostCaseInsensitively(t *t
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAnnotationsQualifiedName(t *testing.T) {
|
||||
require.NoError(t, ValidateAnnotations(map[string]string{
|
||||
"frp.e2e.test/foo": "value1",
|
||||
"app.kubernetes.io/name": "value2",
|
||||
}))
|
||||
// The call site lowercases keys before validation.
|
||||
require.NoError(t, ValidateAnnotations(map[string]string{"Example.COM/MyName": "value"}))
|
||||
|
||||
err := ValidateAnnotations(map[string]string{"bad key": "value"})
|
||||
require.ErrorContains(t, err, "annotation key bad key is invalid: name part must consist of alphanumeric characters")
|
||||
|
||||
err = ValidateAnnotations(map[string]string{"example.com/": "value"})
|
||||
require.ErrorContains(t, err, "name part must be non-empty")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user