Skip to content

Commit de0645c

Browse files
committed
fix(compute): preserve protobuf configuration integers
An integer driver setting such as Kubernetes `tolerationSeconds` can become a fractional JSON number during protobuf decoding and fail typed configuration parsing. This preserves whole-number values through the shared converter and the actual Kubernetes rendering path. Signed-off-by: Shiju <shiju@nvidia.com>
1 parent cc0642f commit de0645c

6 files changed

Lines changed: 226 additions & 15 deletions

File tree

‎crates/openshell-core/src/proto_struct.rs‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ pub enum ProtoStructError {
1313
UnrepresentableNumber(serde_json::Number),
1414
}
1515

16+
/// A non-finite protobuf number cannot be used as typed configuration.
17+
#[derive(Debug, thiserror::Error)]
18+
#[error("protobuf configuration contains a non-finite number")]
19+
pub struct TypedProtoStructError;
20+
1621
/// Convert a JSON object into a protobuf Struct.
1722
pub fn json_object_to_struct(
1823
object: serde_json::Map<String, serde_json::Value>,
@@ -87,6 +92,77 @@ pub fn struct_to_json_value(config: &prost_types::Struct) -> serde_json::Value {
8792
serde_json::Value::Object(struct_to_json_object(config))
8893
}
8994

95+
/// Convert protobuf configuration into JSON for typed serde decoding.
96+
///
97+
/// Finite integral doubles in the JSON integer range become integers; fractions
98+
/// and larger finite numbers remain floats. This preserves exactly represented
99+
/// large integers accepted by [`json_value_to_proto`]. A receiver cannot detect
100+
/// precision already lost before transport; the producer must enforce exactness.
101+
/// General-purpose conversions keep their existing float and null behavior.
102+
///
103+
/// # Errors
104+
///
105+
/// Reject non-finite numbers anywhere in the configuration instead of turning
106+
/// them into null, which could silently clear an optional constraint.
107+
pub fn struct_to_typed_json_value(
108+
config: &prost_types::Struct,
109+
) -> Result<serde_json::Value, TypedProtoStructError> {
110+
config
111+
.fields
112+
.iter()
113+
.map(|(key, value)| typed_value_to_json(value).map(|value| (key.clone(), value)))
114+
.collect::<Result<serde_json::Map<_, _>, _>>()
115+
.map(serde_json::Value::Object)
116+
}
117+
118+
// The casts below are preceded by finite, integral and exclusive range guards.
119+
#[allow(
120+
clippy::cast_possible_truncation,
121+
clippy::cast_precision_loss,
122+
clippy::cast_sign_loss
123+
)]
124+
fn typed_value_to_json(
125+
value: &prost_types::Value,
126+
) -> Result<serde_json::Value, TypedProtoStructError> {
127+
use prost_types::value::Kind;
128+
use serde_json::{Number, Value};
129+
130+
Ok(match value.kind.as_ref() {
131+
Some(Kind::NumberValue(number)) => {
132+
if !number.is_finite() {
133+
return Err(TypedProtoStructError);
134+
}
135+
// Exclusive upper bounds are intentional: u64::MAX and i64::MAX
136+
// round UP when converted to f64. Guard before any integer cast.
137+
let integer = if number.fract() == 0.0 {
138+
if *number >= 0.0 && *number < u64::MAX as f64 {
139+
Some(Number::from(*number as u64))
140+
} else if *number >= i64::MIN as f64 && *number < 0.0 {
141+
Some(Number::from(*number as i64))
142+
} else {
143+
None
144+
}
145+
} else {
146+
None
147+
};
148+
Value::Number(
149+
integer
150+
.or_else(|| Number::from_f64(*number))
151+
.ok_or(TypedProtoStructError)?,
152+
)
153+
}
154+
Some(Kind::StructValue(config)) => struct_to_typed_json_value(config)?,
155+
Some(Kind::ListValue(list)) => Value::Array(
156+
list.values
157+
.iter()
158+
.map(typed_value_to_json)
159+
.collect::<Result<_, _>>()?,
160+
),
161+
// Only numeric values differ from the compatible general conversion.
162+
_ => value_to_json(value),
163+
})
164+
}
165+
90166
/// Convert a protobuf Value into a JSON value for typed serde decoding.
91167
#[must_use]
92168
pub fn value_to_json(value: &prost_types::Value) -> serde_json::Value {
@@ -177,6 +253,123 @@ mod tests {
177253
);
178254
}
179255

256+
#[test]
257+
fn typed_configuration_round_trips_recursive_integer_fields() {
258+
#[derive(Debug, Deserialize, PartialEq)]
259+
struct Limits {
260+
max_processes: Option<u32>,
261+
max_resident_bytes: u64,
262+
}
263+
#[derive(Debug, Deserialize, PartialEq)]
264+
struct Config {
265+
service_ports: Vec<u16>,
266+
limits: Limits,
267+
nested: Vec<Limits>,
268+
}
269+
let json = serde_json::json!({
270+
"service_ports": [8080, 65535],
271+
"limits": {"max_processes": 128, "max_resident_bytes": 1_073_741_824},
272+
"nested": [{"max_processes": null, "max_resident_bytes": 0}]
273+
});
274+
let expected: Config = serde_json::from_value(json.clone()).unwrap();
275+
let proto = json_object_to_struct(json.as_object().unwrap().clone()).unwrap();
276+
let actual: Config =
277+
serde_json::from_value(struct_to_typed_json_value(&proto).unwrap()).unwrap();
278+
assert_eq!(actual, expected);
279+
// The general-purpose conversion retains its existing float semantics.
280+
assert!(struct_to_json_value(&proto)["service_ports"][0].is_f64());
281+
}
282+
283+
#[test]
284+
fn typed_configuration_rejects_nonfinite_numbers() {
285+
for number in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
286+
let mut proto = json_object_to_struct(
287+
serde_json::json!({"limits": [{"max_processes": 128}]})
288+
.as_object()
289+
.unwrap()
290+
.clone(),
291+
)
292+
.unwrap();
293+
let Some(prost_types::value::Kind::ListValue(list)) =
294+
proto.fields.get_mut("limits").unwrap().kind.as_mut()
295+
else {
296+
unreachable!();
297+
};
298+
let Some(prost_types::value::Kind::StructValue(limits)) = list.values[0].kind.as_mut()
299+
else {
300+
unreachable!();
301+
};
302+
limits.fields.get_mut("max_processes").unwrap().kind =
303+
Some(prost_types::value::Kind::NumberValue(number));
304+
assert!(struct_to_typed_json_value(&proto).is_err());
305+
assert!(struct_to_json_value(&proto)["limits"][0]["max_processes"].is_null());
306+
}
307+
}
308+
309+
#[test]
310+
fn typed_configuration_preserves_numeric_boundaries() {
311+
for integer in [
312+
0,
313+
9_007_199_254_740_992,
314+
9_007_199_254_740_994,
315+
1_u64 << 63,
316+
u64::MAX - 2047,
317+
] {
318+
let proto = json_object_to_struct(
319+
serde_json::json!({"limit": integer})
320+
.as_object()
321+
.unwrap()
322+
.clone(),
323+
)
324+
.unwrap();
325+
let value = struct_to_typed_json_value(&proto).unwrap();
326+
assert_eq!(
327+
serde_json::from_value::<u64>(value["limit"].clone()).unwrap(),
328+
integer
329+
);
330+
}
331+
let proto = json_object_to_struct(
332+
serde_json::json!({"limit": i64::MIN})
333+
.as_object()
334+
.unwrap()
335+
.clone(),
336+
)
337+
.unwrap();
338+
let value = struct_to_typed_json_value(&proto).unwrap();
339+
assert_eq!(
340+
serde_json::from_value::<i64>(value["limit"].clone()).unwrap(),
341+
i64::MIN
342+
);
343+
// Fractions and doubles outside the integer range must not be truncated
344+
// or saturated into a valid integer configuration value.
345+
for number in [1.5, -1.0, 18_446_744_073_709_551_616.0, f64::MAX] {
346+
let proto = prost_types::Struct {
347+
fields: [(
348+
"limit".to_owned(),
349+
prost_types::Value {
350+
kind: Some(prost_types::value::Kind::NumberValue(number)),
351+
},
352+
)]
353+
.into(),
354+
};
355+
let value = struct_to_typed_json_value(&proto).unwrap();
356+
assert!(serde_json::from_value::<u64>(value["limit"].clone()).is_err());
357+
}
358+
let below_i64_min = prost_types::Struct {
359+
fields: [(
360+
"limit".to_owned(),
361+
prost_types::Value {
362+
kind: Some(prost_types::value::Kind::NumberValue(
363+
-9_223_372_036_854_777_856.0,
364+
)),
365+
},
366+
)]
367+
.into(),
368+
};
369+
let value = struct_to_typed_json_value(&below_i64_min).unwrap();
370+
assert!(serde_json::from_value::<i64>(value["limit"].clone()).is_err());
371+
}
372+
180373
#[derive(Debug, Default, Deserialize)]
181374
#[serde(default)]
182375
struct TestConfig {

‎crates/openshell-driver-docker/src/lib.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use openshell_core::proto::compute::v1::{
5555
WatchSandboxesSandboxEvent, compute_driver_server::ComputeDriver, watch_sandboxes_event,
5656
};
5757
use openshell_core::proto_struct::{
58-
deserialize_optional_non_empty_string_list, struct_to_json_value,
58+
deserialize_optional_non_empty_string_list, struct_to_typed_json_value,
5959
};
6060
use openshell_core::{
6161
AppArmorProfile, Error, ImagePullPolicy, NetworkSupervisorTrustBundle, Result as CoreResult,
@@ -783,8 +783,9 @@ impl DockerSandboxDriverConfig {
783783
return Ok(Self::default());
784784
};
785785

786-
serde_json::from_value(struct_to_json_value(config))
787-
.map_err(|err| format!("invalid docker driver_config: {err}"))
786+
let json = struct_to_typed_json_value(config)
787+
.map_err(|err| format!("invalid docker driver_config: {err}"))?;
788+
serde_json::from_value(json).map_err(|err| format!("invalid docker driver_config: {err}"))
788789
}
789790
}
790791

‎crates/openshell-driver-kubernetes/src/driver.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use openshell_core::proto::compute::v1::{
6969
WatchSandboxesDeletedEvent, WatchSandboxesEvent, WatchSandboxesPlatformEvent,
7070
WatchSandboxesSandboxEvent, watch_sandboxes_event,
7171
};
72-
use openshell_core::proto_struct::{struct_to_json_object, value_to_json};
72+
use openshell_core::proto_struct::{struct_to_typed_json_value, value_to_json};
7373
use openshell_isolation_interface::contract::ResolvedWorkloadIdentity;
7474
use openshell_sandbox_backend::boundary_protocol::{
7575
GatewayVerificationKey, SandboxTlsClientConfig, SandboxTlsServerConfig,
@@ -711,7 +711,8 @@ impl KubernetesSandboxDriverConfig {
711711
return Ok(Self::default());
712712
};
713713

714-
let json = serde_json::Value::Object(struct_to_json_object(config));
714+
let json = struct_to_typed_json_value(config)
715+
.map_err(|err| format!("invalid kubernetes driver_config: {err}"))?;
715716
let config: Self = serde_json::from_value(json)
716717
.map_err(|err| format!("invalid kubernetes driver_config: {err}"))?;
717718
config
@@ -12891,6 +12892,11 @@ mod tests {
1289112892
"key": "nvidia.com/gpu",
1289212893
"operator": "Exists",
1289312894
"effect": "NoSchedule"
12895+
}, {
12896+
"key": "maintenance.example.com/pool",
12897+
"operator": "Exists",
12898+
"effect": "NoExecute",
12899+
"tolerationSeconds": 512
1289412900
}]
1289512901
},
1289612902
"containers": {
@@ -12933,6 +12939,10 @@ mod tests {
1293312939
pod_template["spec"]["tolerations"][0]["key"],
1293412940
serde_json::json!("nvidia.com/gpu")
1293512941
);
12942+
let toleration: k8s_openapi::api::core::v1::Toleration =
12943+
serde_json::from_value(pod_template["spec"]["tolerations"][1].clone())
12944+
.expect("transport preserves the integer expected by the Kubernetes API");
12945+
assert_eq!(toleration.toleration_seconds, Some(512));
1293612946
assert_eq!(
1293712947
pod_template["spec"]["containers"][0]["resources"]["requests"]["vendor.example/gpu-memory"],
1293812948
serde_json::json!("8Gi")

‎crates/openshell-driver-mxc/src/driver.rs‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use openshell_core::proto::compute::v1::{
1414
GetCapabilitiesResponse, WatchSandboxesDeletedEvent, WatchSandboxesEvent,
1515
WatchSandboxesPlatformEvent, WatchSandboxesSandboxEvent, watch_sandboxes_event,
1616
};
17-
use openshell_core::proto_struct::struct_to_json_value;
17+
use openshell_core::proto_struct::struct_to_typed_json_value;
1818
use serde::{Deserialize, Serialize};
1919
use std::collections::HashMap;
2020
use std::net::SocketAddr;
@@ -239,10 +239,12 @@ fn sandbox_config(sandbox: &DriverSandbox) -> Result<MxcSandboxConfig, tonic::St
239239
"mxc requires template.driver_config.mxc with a non-empty command array",
240240
)
241241
})?;
242-
let config: MxcSandboxConfig =
243-
serde_json::from_value(struct_to_json_value(config)).map_err(|error| {
244-
tonic::Status::invalid_argument(format!("invalid mxc driver_config: {error}"))
245-
})?;
242+
let json = struct_to_typed_json_value(config).map_err(|error| {
243+
tonic::Status::invalid_argument(format!("invalid mxc driver_config: {error}"))
244+
})?;
245+
let config: MxcSandboxConfig = serde_json::from_value(json).map_err(|error| {
246+
tonic::Status::invalid_argument(format!("invalid mxc driver_config: {error}"))
247+
})?;
246248
if config.command.is_empty() || config.command[0].is_empty() {
247249
return Err(tonic::Status::invalid_argument(
248250
"mxc driver_config.command must contain a non-empty executable",

‎crates/openshell-driver-podman/src/container.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ impl PodmanSandboxDriverConfig {
127127
return Ok(Self::default());
128128
};
129129

130-
serde_json::from_value(proto_struct::struct_to_json_value(config)).map_err(|err| {
130+
let json = proto_struct::struct_to_typed_json_value(config).map_err(|err| {
131+
ComputeDriverError::InvalidArgument(format!("invalid podman driver_config: {err}"))
132+
})?;
133+
serde_json::from_value(json).map_err(|err| {
131134
ComputeDriverError::InvalidArgument(format!("invalid podman driver_config: {err}"))
132135
})
133136
}
@@ -1044,7 +1047,8 @@ fn podman_driver_config(
10441047
let Some(config) = template.driver_config.as_ref() else {
10451048
return Ok(PodmanSandboxDriverConfig::default());
10461049
};
1047-
let json = Value::Object(proto_struct::struct_to_json_object(config));
1050+
let json = proto_struct::struct_to_typed_json_value(config)
1051+
.map_err(|err| format!("invalid podman driver_config: {err}"))?;
10481052
let config: PodmanSandboxDriverConfig = serde_json::from_value(json)
10491053
.map_err(|err| format!("invalid podman driver_config: {err}"))?;
10501054
validate_podman_driver_mounts(&config.mounts, enable_bind_mounts)?;

‎crates/openshell-driver-vm/src/driver.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use openshell_core::proto::compute::v1::{
7070
WorkloadIdentityRequest, compute_driver_server::ComputeDriver, watch_sandboxes_event,
7171
};
7272
use openshell_core::proto_struct::{
73-
deserialize_optional_non_empty_string_list, struct_to_json_value,
73+
deserialize_optional_non_empty_string_list, struct_to_typed_json_value,
7474
};
7575
use openshell_core::{
7676
NetworkSupervisorTrustBundle, network_trust::MAX_NETWORK_SUPERVISOR_TRUST_BUNDLE_BYTES,
@@ -192,8 +192,9 @@ impl VmSandboxDriverConfig {
192192
return Ok(Self::default());
193193
};
194194

195-
serde_json::from_value(struct_to_json_value(config))
196-
.map_err(|err| format!("invalid vm driver_config: {err}"))
195+
let json = struct_to_typed_json_value(config)
196+
.map_err(|err| format!("invalid vm driver_config: {err}"))?;
197+
serde_json::from_value(json).map_err(|err| format!("invalid vm driver_config: {err}"))
197198
}
198199
}
199200

0 commit comments

Comments
 (0)