Validate field sizes on a `CreateSandboxRequest` before persisting. Returns `INVALID_ARGUMENT` on the first field that exceeds its limit.
(
name: &str,
spec: &openshell_core::proto::SandboxSpec,
)
| 91 | /// |
| 92 | /// Returns `INVALID_ARGUMENT` on the first field that exceeds its limit. |
| 93 | pub(super) fn validate_sandbox_spec( |
| 94 | name: &str, |
| 95 | spec: &openshell_core::proto::SandboxSpec, |
| 96 | ) -> Result<(), Status> { |
| 97 | // --- request.name --- |
| 98 | if name.len() > MAX_NAME_LEN { |
| 99 | return Err(Status::invalid_argument(format!( |
| 100 | "name exceeds maximum length ({} > {MAX_NAME_LEN})", |
| 101 | name.len() |
| 102 | ))); |
| 103 | } |
| 104 | |
| 105 | // --- spec.providers --- |
| 106 | if spec.providers.len() > MAX_PROVIDERS { |
| 107 | return Err(Status::invalid_argument(format!( |
| 108 | "providers list exceeds maximum ({} > {MAX_PROVIDERS})", |
| 109 | spec.providers.len() |
| 110 | ))); |
| 111 | } |
| 112 | |
| 113 | // --- spec.log_level --- |
| 114 | if spec.log_level.len() > MAX_LOG_LEVEL_LEN { |
| 115 | return Err(Status::invalid_argument(format!( |
| 116 | "log_level exceeds maximum length ({} > {MAX_LOG_LEVEL_LEN})", |
| 117 | spec.log_level.len() |
| 118 | ))); |
| 119 | } |
| 120 | |
| 121 | // --- spec.environment --- |
| 122 | validate_string_map( |
| 123 | &spec.environment, |
| 124 | MAX_ENVIRONMENT_ENTRIES, |
| 125 | MAX_MAP_KEY_LEN, |
| 126 | MAX_MAP_VALUE_LEN, |
| 127 | "spec.environment", |
| 128 | )?; |
| 129 | validate_env_entries(&spec.environment, "spec.environment")?; |
| 130 | |
| 131 | // --- spec.template --- |
| 132 | if let Some(ref tmpl) = spec.template { |
| 133 | validate_sandbox_template(tmpl)?; |
| 134 | validate_env_entries(&tmpl.environment, "spec.template.environment")?; |
| 135 | } |
| 136 | |
| 137 | // --- spec.resource_requirements.gpu --- |
| 138 | validate_gpu_request_fields(spec)?; |
| 139 | |
| 140 | // --- spec.policy serialized size --- |
| 141 | if let Some(ref policy) = spec.policy { |
| 142 | let size = policy.encoded_len(); |
| 143 | if size > MAX_POLICY_SIZE { |
| 144 | return Err(Status::invalid_argument(format!( |
| 145 | "policy serialized size exceeds maximum ({size} > {MAX_POLICY_SIZE})" |
| 146 | ))); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | Ok(()) |