Skip to content

Commit 2b96be8

Browse files
committed
[platform] Validate computed tenant namespace length in cozystack-api
Reject Tenant creation when the computed workload namespace (parent namespace + "-" + tenant name) would exceed the 63-character DNS-1123 label limit. Previously only the per-name length was checked against the Helm release limit, so a deeply-nested tenant could pass Create() and then fail later as an opaque HelmRelease reconcile error. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent 78ca1f0 commit 2b96be8

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎pkg/registry/apps/application/rest.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation
164164
return nil, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, nameLenErrs)
165165
}
166166

167+
// For Tenant applications, also validate that the computed workload
168+
// namespace fits within the DNS-1123 label limit. A deeply-nested tenant
169+
// can exceed the limit even when its own name passes the per-name Helm
170+
// release length check, because the namespace is formed from the full
171+
// ancestor chain.
172+
if r.kindName == "Tenant" {
173+
if nsErrs := r.validateTenantNamespaceLength(app.Namespace, app.Name); len(nsErrs) > 0 {
174+
return nil, apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, nsErrs)
175+
}
176+
}
177+
167178
// Validate that values don't contain reserved keys (starting with "_")
168179
if err := validateNoInternalKeys(app.Spec); err != nil {
169180
return nil, apierrors.NewBadRequest(err.Error())
@@ -1049,6 +1060,12 @@ func validateNoInternalKeys(values *apiextv1.JSON) error {
10491060
// chart-generated resource suffixes within the 63-char DNS-1035 label limit.
10501061
const maxHelmReleaseName = 53
10511062

1063+
// maxNamespaceName is the DNS-1123 label limit for Kubernetes namespace names.
1064+
// The tenant Helm chart creates a Namespace whose name is the computed
1065+
// workload namespace (parent namespace + "-" + tenant name), so the total
1066+
// must fit inside a single 63-char DNS-1123 label.
1067+
const maxNamespaceName = 63
1068+
10521069
// validateNameLength checks that the application name won't exceed Kubernetes limits.
10531070
// prefix + name must fit within the Helm release name limit (53 chars).
10541071
func (r *REST) validateNameLength(name string) field.ErrorList {
@@ -1070,6 +1087,24 @@ func (r *REST) validateNameLength(name string) field.ErrorList {
10701087
return allErrs
10711088
}
10721089

1090+
// validateTenantNamespaceLength checks that the computed workload namespace
1091+
// for a Tenant application fits within the DNS-1123 label limit. The namespace
1092+
// is formed by dash-joining the parent namespace with the tenant name, so
1093+
// deep nesting can exceed the limit even when each individual name passes the
1094+
// per-name Helm release length check.
1095+
func (r *REST) validateTenantNamespaceLength(currentNamespace, tenantName string) field.ErrorList {
1096+
fldPath := field.NewPath("metadata").Child("name")
1097+
allErrs := field.ErrorList{}
1098+
1099+
computed := r.computeTenantNamespace(currentNamespace, tenantName)
1100+
if len(computed) > maxNamespaceName {
1101+
allErrs = append(allErrs, field.Invalid(fldPath, tenantName,
1102+
fmt.Sprintf("computed tenant namespace %q would be %d characters, which exceeds the %d-character Kubernetes namespace limit; shorten the tenant name or reduce ancestor nesting depth",
1103+
computed, len(computed), maxNamespaceName)))
1104+
}
1105+
return allErrs
1106+
}
1107+
10731108
// convertHelmReleaseToApplication implements the actual conversion logic
10741109
func (r *REST) convertHelmReleaseToApplication(ctx context.Context, hr *helmv2.HelmRelease) (appsv1alpha1.Application, error) {
10751110
// Filter out internal keys (starting with "_") from spec

‎pkg/registry/apps/application/rest_validation_test.go‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package application
1818

1919
import (
20+
"fmt"
2021
"strings"
2122
"testing"
2223

@@ -102,3 +103,101 @@ func TestValidateNameLength(t *testing.T) {
102103
})
103104
}
104105
}
106+
107+
// TestValidateTenantNamespaceLength covers the check that the computed
108+
// workload namespace for a Tenant application fits inside the 63-char
109+
// DNS-1123 label limit. The namespace is formed by dash-joining the
110+
// parent namespace with the tenant name, so deep nesting can exceed the
111+
// limit even when each individual name passes the per-name Helm release
112+
// length check.
113+
//
114+
// The "tenant-root" branches of computeTenantNamespace do not get a
115+
// dedicated overflow case: that branch produces "tenant-<name>" (7 +
116+
// len(name)), so for the computed result to exceed 63 chars the name
117+
// would need to exceed 56 chars, which is already blocked by
118+
// validateNameLength (max 46 for the "tenant-" prefix). The invariant
119+
// holds by arithmetic.
120+
func TestValidateTenantNamespaceLength(t *testing.T) {
121+
tests := []struct {
122+
name string
123+
currentNamespace string
124+
tenantName string
125+
wantError bool
126+
}{
127+
{
128+
name: "tenant-root with short name passes",
129+
currentNamespace: "tenant-root",
130+
tenantName: "alpha",
131+
wantError: false,
132+
},
133+
{
134+
name: "root tenant inside root namespace passes",
135+
currentNamespace: "tenant-root",
136+
tenantName: "root",
137+
wantError: false,
138+
},
139+
{
140+
name: "short parent and short name passes",
141+
currentNamespace: "tenant-foo",
142+
tenantName: "bar",
143+
wantError: false,
144+
},
145+
{
146+
name: "exactly at 63-char limit passes",
147+
currentNamespace: "tenant-" + strings.Repeat("a", 45), // 52 chars
148+
tenantName: strings.Repeat("b", 10), // 10 chars -> 52+1+10 = 63
149+
wantError: false,
150+
},
151+
{
152+
name: "one char over the limit fails",
153+
currentNamespace: "tenant-" + strings.Repeat("a", 45), // 52 chars
154+
tenantName: strings.Repeat("b", 11), // 11 chars -> 52+1+11 = 64
155+
wantError: true,
156+
},
157+
{
158+
name: "deeply nested failure with issue-style names",
159+
currentNamespace: "tenant-" + strings.Repeat("a", 35), // 42 chars
160+
tenantName: strings.Repeat("b", 25), // 25 chars -> 42+1+25 = 68
161+
wantError: true,
162+
},
163+
}
164+
165+
for _, tt := range tests {
166+
t.Run(tt.name, func(t *testing.T) {
167+
r := &REST{
168+
kindName: "Tenant",
169+
releaseConfig: config.ReleaseConfig{
170+
Prefix: "tenant-",
171+
},
172+
}
173+
174+
errs := r.validateTenantNamespaceLength(tt.currentNamespace, tt.tenantName)
175+
176+
if tt.wantError && len(errs) == 0 {
177+
computed := r.computeTenantNamespace(tt.currentNamespace, tt.tenantName)
178+
t.Errorf("expected error for parent=%q name=%q (computed=%q, len=%d), got none",
179+
tt.currentNamespace, tt.tenantName, computed, len(computed))
180+
return
181+
}
182+
if !tt.wantError && len(errs) > 0 {
183+
t.Errorf("unexpected error for parent=%q name=%q: %v",
184+
tt.currentNamespace, tt.tenantName, errs)
185+
return
186+
}
187+
188+
// For failing cases, verify the error message surfaces the
189+
// computed namespace string and its actual length so a
190+
// regression in the message format is caught.
191+
if tt.wantError {
192+
computed := r.computeTenantNamespace(tt.currentNamespace, tt.tenantName)
193+
msg := errs.ToAggregate().Error()
194+
if !strings.Contains(msg, computed) {
195+
t.Errorf("error message must contain computed namespace %q, got: %s", computed, msg)
196+
}
197+
if !strings.Contains(msg, fmt.Sprintf("%d characters", len(computed))) {
198+
t.Errorf("error message must contain the computed length %d, got: %s", len(computed), msg)
199+
}
200+
}
201+
})
202+
}
203+
}

0 commit comments

Comments
 (0)