Skip to content

Commit 2ed9c38

Browse files
Bound every CSI map the same way, with headroom over the spec limit
ExternalVolume.volume_context on the public API still capped the driver's map per entry while atelet had moved to a total-size bound, so the same driver response was judged by two different rules depending on the hop. Both now go through resources.ValidateCSIMap. The bound is 40 KiB, ten times the CSI spec's limit for map fields: it exists to keep input finite, not to hold drivers to the spec's exact number, and the headroom means a future revision of that number does not turn compliant drivers away.
1 parent c8b8c1a commit 2ed9c38

10 files changed

Lines changed: 44 additions & 54 deletions

File tree

‎cmd/ateapi/internal/apivalidation/actor.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,7 @@ func ValidateCustom_ExternalVolume_VolumeType(_ context.Context, _ operation.Ope
140140
func ValidateCustom_ExternalVolume_StorageVolumeId(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
141141
return resources.ValidateStorageVolumeID(fldPath, *value)
142142
}
143+
144+
func ValidateCustom_ExternalVolume_VolumeContext(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[string]string) field.ErrorList {
145+
return resources.ValidateCSIMap(fldPath, value)
146+
}

‎cmd/ateapi/internal/apivalidation/validation_test.go‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -946,25 +946,25 @@ func TestValidateExternalVolume(t *testing.T) {
946946
name: "storage volume id at the bound",
947947
obj: valid(func(v *ateapipb.ExternalVolume) { v.StorageVolumeId = strings.Repeat("x", 256) }),
948948
}, {
949-
name: "too many volume_context entries",
949+
name: "volume_context with many entries, as a CSI driver may return",
950950
obj: valid(func(v *ateapipb.ExternalVolume) {
951-
ctxMap := make(map[string]string, 33)
952-
for i := 0; i < 33; i++ {
953-
ctxMap[fmt.Sprintf("key-%d", i)] = "v"
951+
ctxMap := make(map[string]string, 100)
952+
for i := range 100 {
953+
ctxMap[fmt.Sprintf("key-%03d", i)] = "v"
954954
}
955955
v.VolumeContext = ctxMap
956956
}),
957-
want: field.ErrorList{field.TooMany(field.NewPath("volume_context"), 33, 32).WithOrigin("maxProperties")},
958957
}, {
959-
name: "volume_context key too long",
960-
obj: valid(func(v *ateapipb.ExternalVolume) { v.VolumeContext = map[string]string{strings.Repeat("k", 129): "v"} }),
961-
want: field.ErrorList{field.TooLong(field.NewPath("volume_context"), nil, 128).WithOrigin("maxLength")},
958+
name: "volume_context with one long value, as a CSI driver may return",
959+
obj: valid(func(v *ateapipb.ExternalVolume) {
960+
v.VolumeContext = map[string]string{"attachment": strings.Repeat("v", 4000)}
961+
}),
962962
}, {
963-
name: "volume_context value too long",
963+
name: "volume_context over the size bound",
964964
obj: valid(func(v *ateapipb.ExternalVolume) {
965-
v.VolumeContext = map[string]string{"attachment": strings.Repeat("v", 257)}
965+
v.VolumeContext = map[string]string{"attachment": strings.Repeat("v", 40*1024)}
966966
}),
967-
want: field.ErrorList{field.TooLong(field.NewPath("volume_context").Key("attachment"), nil, 256).WithOrigin("maxLength")},
967+
want: field.ErrorList{field.TooLong(field.NewPath("volume_context"), nil, 40*1024)},
968968
}}
969969
for _, tt := range tests {
970970
t.Run(tt.name, func(t *testing.T) {

‎cmd/ateapi/internal/apivalidation/zz_generated.validation.go‎

Lines changed: 2 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cmd/atelet/internal/apivalidation/validation_test.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,11 +1284,11 @@ func TestValidateVolume(t *testing.T) {
12841284
e.VolumeContext = map[string]string{"k": strings.Repeat("v", 4000)}
12851285
}),
12861286
}, {
1287-
name: "external: volume_context over the CSI size limit",
1287+
name: "external: volume_context over the size bound",
12881288
obj: external(func(e *ateletpb.ExternalVolumeSource) {
1289-
e.VolumeContext = map[string]string{"k": strings.Repeat("v", 4096)}
1289+
e.VolumeContext = map[string]string{"k": strings.Repeat("v", 40*1024)}
12901290
}),
1291-
want: field.ErrorList{field.TooLong(extPath.Child("volume_context"), nil, 4096)},
1291+
want: field.ErrorList{field.TooLong(extPath.Child("volume_context"), nil, 40*1024)},
12921292
}, {
12931293
name: "external: publish_context with many entries, as a CSI driver may return",
12941294
obj: external(func(e *ateletpb.ExternalVolumeSource) {
@@ -1298,11 +1298,11 @@ func TestValidateVolume(t *testing.T) {
12981298
}
12991299
}),
13001300
}, {
1301-
name: "external: publish_context over the CSI size limit",
1301+
name: "external: publish_context over the size bound",
13021302
obj: external(func(e *ateletpb.ExternalVolumeSource) {
1303-
e.PublishContext = map[string]string{"a": strings.Repeat("v", 2048), "b": strings.Repeat("v", 2048)}
1303+
e.PublishContext = map[string]string{"a": strings.Repeat("v", 20*1024), "b": strings.Repeat("v", 20*1024)}
13041304
}),
1305-
want: field.ErrorList{field.TooLong(extPath.Child("publish_context"), nil, 4096)},
1305+
want: field.ErrorList{field.TooLong(extPath.Child("publish_context"), nil, 40*1024)},
13061306
},
13071307

13081308
// ImageVolumeSource.

‎internal/proto/ateletpb/atelet.pb.go‎

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎internal/proto/ateletpb/atelet.proto‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,17 @@ message ExternalVolumeSource {
402402
string volume_type = 2;
403403

404404
// volume_context is the driver's provisioning metadata. Its entries are the
405-
// driver's own, so the map is only held to the CSI size limit.
405+
// driver's own, so the map is only bounded, not validated.
406406
//
407407
// +k8s:optional
408-
// +k8s:customValidation # at most 4 KiB in total, the CSI limit for map fields
408+
// +k8s:customValidation # at most 40 KiB in total, ten times the CSI limit for map fields
409409
map<string, string> volume_context = 3;
410410

411411
// publish_context is the driver's attachment metadata for this node, which
412412
// the node plugin needs to complete the mount. Bounded like volume_context.
413413
//
414414
// +k8s:optional
415-
// +k8s:customValidation # at most 4 KiB in total, the CSI limit for map fields
415+
// +k8s:customValidation # at most 40 KiB in total, ten times the CSI limit for map fields
416416
map<string, string> publish_context = 4;
417417
}
418418

‎internal/resources/validate.go‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,15 @@ func ValidateVolumeType(fldPath *field.Path, volumeType string) field.ErrorList
491491
return errs
492492
}
493493

494-
// csiMapMaxBytes is the CSI size limit for map<string, string> fields. A
495-
// compliant driver never produces a larger volume or publish context, and a
496-
// tighter bound would refuse one that is within its rights.
497-
const csiMapMaxBytes = 4096
494+
// csiMapMaxBytes bounds a map a CSI driver produced. The CSI spec caps
495+
// map<string, string> fields at 4 KiB; this allows ten times that so a future
496+
// revision of the limit does not turn compliant drivers away. It is a
497+
// guardrail against unbounded input, not a contract drivers are held to.
498+
const csiMapMaxBytes = 40 * 1024
498499

499500
// ValidateCSIMap bounds a map a CSI driver produced (a volume or publish
500-
// context) at the CSI size limit, counting keys and values. The entries are
501-
// the driver's own and are not validated further.
501+
// context) at csiMapMaxBytes, counting keys and values. The entries are the
502+
// driver's own and are not validated further.
502503
func ValidateCSIMap(fldPath *field.Path, m map[string]string) field.ErrorList {
503504
size := 0
504505
for k, v := range m {

‎internal/resources/validate_test.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,10 @@ func TestValidateCSIMap(t *testing.T) {
592592
{name: "nil"},
593593
{name: "one long value", m: map[string]string{"token": strings.Repeat("x", 4000)}},
594594
{name: "many small entries", m: many},
595-
{name: "exactly the limit", m: map[string]string{"k": strings.Repeat("x", 4095)}},
596-
{name: "one byte over", m: map[string]string{"k": strings.Repeat("x", 4096)}, want: field.ErrorList{field.TooLong(path, nil, 4096)}},
597-
{name: "over across entries", m: map[string]string{"a": strings.Repeat("x", 2048), "b": strings.Repeat("y", 2048)}, want: field.ErrorList{field.TooLong(path, nil, 4096)}},
595+
{name: "over the CSI spec limit, within the headroom", m: map[string]string{"k": strings.Repeat("x", 8*1024)}},
596+
{name: "exactly the bound", m: map[string]string{"k": strings.Repeat("x", 40*1024-1)}},
597+
{name: "one byte over", m: map[string]string{"k": strings.Repeat("x", 40*1024)}, want: field.ErrorList{field.TooLong(path, nil, 40*1024)}},
598+
{name: "over across entries", m: map[string]string{"a": strings.Repeat("x", 20*1024), "b": strings.Repeat("y", 20*1024)}, want: field.ErrorList{field.TooLong(path, nil, 40*1024)}},
598599
}
599600
for _, tt := range tests {
600601
t.Run(tt.name, func(t *testing.T) {

‎pkg/proto/ateapipb/ateapi.pb.go‎

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pkg/proto/ateapipb/ateapi.proto‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,11 @@ message ExternalVolume {
452452

453453
// volume_context contains metadata returned by the CSI driver during volume
454454
// provisioning, needed by the node plugin for mounting (e.g. attachment
455-
// info). Keys and values are the driver's own, so they are only bounded,
455+
// info). Keys and values are the driver's own, so the map is only bounded,
456456
// not validated.
457457
//
458458
// +k8s:optional
459-
// +k8s:maxProperties=32
460-
// +k8s:eachKey=+k8s:maxLength=128
461-
// +k8s:eachVal=+k8s:maxLength=256
459+
// +k8s:customValidation # at most 40 KiB in total, ten times the CSI limit for map fields
462460
map<string, string> volume_context = 5;
463461
}
464462

0 commit comments

Comments
 (0)