11// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4- //! Compatibility rewriting for protobuf records written before time fields used WKTs .
4+ //! Compatibility rewriting for protobuf records written by earlier releases .
55
66use prost:: Message ;
77use prost_reflect:: { DescriptorPool , Kind , MessageDescriptor } ;
@@ -24,6 +24,14 @@ enum Conversion {
2424 DurationSeconds { new_tag : u32 } ,
2525 DurationString { new_tag : u32 } ,
2626 TimestampMap { new_tag : u32 } ,
27+ StringEnum { tag : u32 , kind : LegacyEnum } ,
28+ }
29+
30+ #[ derive( Clone , Copy ) ]
31+ enum LegacyEnum {
32+ TlsMode ,
33+ EnforcementMode ,
34+ AccessPreset ,
2735}
2836
2937pub ( super ) fn migrate ( object_type : & str , payload : & [ u8 ] ) -> PersistenceResult < Vec < u8 > > {
@@ -78,7 +86,6 @@ fn rewrite_message(descriptor: &MessageDescriptor, input: &[u8]) -> PersistenceR
7886 ) ?;
7987 } else if wire_type == 2
8088 && let Some ( field) = descriptor. get_field ( field_number)
81- && !field. is_map ( )
8289 && let Kind :: Message ( child) = field. kind ( )
8390 {
8491 let rewritten = rewrite_message ( & child, & input[ payload_start..payload_end] ) ?;
@@ -163,10 +170,92 @@ fn rewrite_legacy_field(
163170 write_embedded ( output, new_tag, & rewritten) ;
164171 }
165172 }
173+ Conversion :: StringEnum { tag, kind } => {
174+ rewrite_string_enum ( output, tag, kind, wire_type, payload) ?;
175+ }
166176 }
167177 Ok ( ( ) )
168178}
169179
180+ fn rewrite_string_enum (
181+ output : & mut Vec < u8 > ,
182+ tag : u32 ,
183+ kind : LegacyEnum ,
184+ wire_type : u8 ,
185+ payload : & [ u8 ] ,
186+ ) -> PersistenceResult < ( ) > {
187+ let value = match wire_type {
188+ // Current records already encode these fields as enum varints. Rewrite
189+ // them canonically so the migration remains idempotent.
190+ 0 => {
191+ let ( value, consumed) = read_varint ( payload) ?;
192+ if consumed != payload. len ( ) {
193+ return Err ( PersistenceError :: Decode (
194+ "invalid network endpoint enum value" . into ( ) ,
195+ ) ) ;
196+ }
197+ value
198+ }
199+ // v0.1.0-pre.4 and earlier encoded the same field numbers as strings.
200+ 2 => {
201+ let value = std:: str:: from_utf8 ( payload) . map_err ( |error| {
202+ PersistenceError :: Decode ( format ! (
203+ "legacy network endpoint mode is not UTF-8: {error}"
204+ ) )
205+ } ) ?;
206+ legacy_enum_value ( kind, value) . ok_or_else ( || {
207+ PersistenceError :: Decode ( format ! (
208+ "unsupported legacy network endpoint {} value '{value}'" ,
209+ legacy_enum_name( kind)
210+ ) )
211+ } ) ?
212+ }
213+ _ => {
214+ return Err ( PersistenceError :: Decode ( format ! (
215+ "network endpoint {} field has wire type {wire_type}, expected varint or string" ,
216+ legacy_enum_name( kind)
217+ ) ) ) ;
218+ }
219+ } ;
220+
221+ write_key ( output, tag, 0 ) ;
222+ write_varint ( output, value) ;
223+ Ok ( ( ) )
224+ }
225+
226+ fn legacy_enum_value ( kind : LegacyEnum , value : & str ) -> Option < u64 > {
227+ match kind {
228+ LegacyEnum :: TlsMode => match value {
229+ "" => Some ( 0 ) ,
230+ "skip" => Some ( 1 ) ,
231+ "terminate" => Some ( 2 ) ,
232+ "passthrough" => Some ( 3 ) ,
233+ _ => None ,
234+ } ,
235+ LegacyEnum :: EnforcementMode => match value {
236+ "" => Some ( 0 ) ,
237+ "enforce" => Some ( 1 ) ,
238+ "audit" => Some ( 2 ) ,
239+ _ => None ,
240+ } ,
241+ LegacyEnum :: AccessPreset => match value {
242+ "" => Some ( 0 ) ,
243+ "read-only" => Some ( 1 ) ,
244+ "read-write" => Some ( 2 ) ,
245+ "full" => Some ( 3 ) ,
246+ _ => None ,
247+ } ,
248+ }
249+ }
250+
251+ fn legacy_enum_name ( kind : LegacyEnum ) -> & ' static str {
252+ match kind {
253+ LegacyEnum :: TlsMode => "tls" ,
254+ LegacyEnum :: EnforcementMode => "enforcement" ,
255+ LegacyEnum :: AccessPreset => "access" ,
256+ }
257+ }
258+
170259fn parse_legacy_duration ( value : & str ) -> PersistenceResult < std:: time:: Duration > {
171260 let ( number, millis_multiplier) = value
172261 . strip_suffix ( "ms" )
@@ -219,9 +308,10 @@ fn rewrite_timestamp_map_entry(input: &[u8]) -> PersistenceResult<Option<Vec<u8>
219308
220309fn conversion ( message : & str , field : u32 ) -> Option < Conversion > {
221310 use Conversion :: {
222- DurationSeconds as D , DurationString as DS , Timestamp as T , TimestampMap as M ,
223- TimestampString as TS ,
311+ DurationSeconds as D , DurationString as DS , StringEnum as E , Timestamp as T ,
312+ TimestampMap as M , TimestampString as TS ,
224313 } ;
314+ use LegacyEnum :: { AccessPreset as Access , EnforcementMode as Enforcement , TlsMode as Tls } ;
225315 match ( message, field) {
226316 ( "openshell.datamodel.v1.ObjectMeta" , 3 ) => Some ( T { new_tag : 103 } ) ,
227317 ( "openshell.datamodel.v1.ObjectMeta" , 8 ) => Some ( T { new_tag : 108 } ) ,
@@ -242,6 +332,15 @@ fn conversion(message: &str, field: u32) -> Option<Conversion> {
242332 Some ( D { new_tag : 116 } )
243333 }
244334 ( "openshell.sandbox.v1.MiddlewareBinding" , 4 ) => Some ( DS { new_tag : 104 } ) ,
335+ ( "openshell.sandbox.v1.NetworkEndpoint" , 4 ) => Some ( E { tag : 4 , kind : Tls } ) ,
336+ ( "openshell.sandbox.v1.NetworkEndpoint" , 5 ) => Some ( E {
337+ tag : 5 ,
338+ kind : Enforcement ,
339+ } ) ,
340+ ( "openshell.sandbox.v1.NetworkEndpoint" , 6 ) => Some ( E {
341+ tag : 6 ,
342+ kind : Access ,
343+ } ) ,
245344 _ => None ,
246345 }
247346}
@@ -324,7 +423,7 @@ fn require_wire_type(actual: u8, expected: u8) -> PersistenceResult<()> {
324423 Ok ( ( ) )
325424 } else {
326425 Err ( PersistenceError :: Decode ( format ! (
327- "legacy time field has wire type {actual}, expected {expected}"
426+ "legacy field has wire type {actual}, expected {expected}"
328427 ) ) )
329428 }
330429}
@@ -336,7 +435,8 @@ mod tests {
336435 StoredProviderCredentialRefreshState , StoredProviderCredentialRefreshStateV2 ,
337436 } ;
338437 use openshell_core:: proto:: {
339- EndpointStatus , Provider , SandboxCondition , SandboxWorkloadTemplate , SshSession ,
438+ EndpointStatus , NetworkAccessPreset , NetworkEnforcementMode , NetworkTlsMode , Provider ,
439+ Sandbox , SandboxCondition , SandboxWorkloadTemplate , SshSession ,
340440 } ;
341441 use std:: collections:: HashMap ;
342442
@@ -412,6 +512,94 @@ mod tests {
412512 metadata : Option < LegacyObjectMeta > ,
413513 }
414514
515+ #[ derive( Clone , PartialEq , Message ) ]
516+ struct LegacySandbox {
517+ #[ prost( message, optional, tag = "2" ) ]
518+ spec : Option < LegacySandboxSpec > ,
519+ }
520+
521+ #[ derive( Clone , PartialEq , Message ) ]
522+ struct LegacySandboxSpec {
523+ #[ prost( message, optional, tag = "7" ) ]
524+ policy : Option < LegacySandboxPolicy > ,
525+ }
526+
527+ #[ derive( Clone , PartialEq , Message ) ]
528+ struct LegacySandboxPolicy {
529+ #[ prost( map = "string, message" , tag = "5" ) ]
530+ network_policies : HashMap < String , LegacyNetworkPolicyRule > ,
531+ }
532+
533+ #[ derive( Clone , PartialEq , Message ) ]
534+ struct LegacyNetworkPolicyRule {
535+ #[ prost( message, repeated, tag = "2" ) ]
536+ endpoints : Vec < LegacyNetworkEndpoint > ,
537+ }
538+
539+ #[ derive( Clone , PartialEq , Message ) ]
540+ struct LegacyNetworkEndpoint {
541+ #[ prost( string, tag = "1" ) ]
542+ host : String ,
543+ #[ prost( string, tag = "4" ) ]
544+ tls : String ,
545+ #[ prost( string, tag = "5" ) ]
546+ enforcement : String ,
547+ #[ prost( string, tag = "6" ) ]
548+ access : String ,
549+ }
550+
551+ fn legacy_sandbox_with_endpoint ( endpoint : LegacyNetworkEndpoint ) -> LegacySandbox {
552+ LegacySandbox {
553+ spec : Some ( LegacySandboxSpec {
554+ policy : Some ( LegacySandboxPolicy {
555+ network_policies : HashMap :: from ( [ (
556+ "api" . into ( ) ,
557+ LegacyNetworkPolicyRule {
558+ endpoints : vec ! [ endpoint] ,
559+ } ,
560+ ) ] ) ,
561+ } ) ,
562+ } ) ,
563+ }
564+ }
565+
566+ #[ test]
567+ #[ allow( deprecated) ]
568+ fn migrates_pre4_network_endpoint_strings_to_enums ( ) {
569+ let legacy = legacy_sandbox_with_endpoint ( LegacyNetworkEndpoint {
570+ host : "api.example.com" . into ( ) ,
571+ tls : "terminate" . into ( ) ,
572+ enforcement : "enforce" . into ( ) ,
573+ access : "read-write" . into ( ) ,
574+ } ) ;
575+
576+ let migrated = migrate ( "sandbox" , & legacy. encode_to_vec ( ) ) . unwrap ( ) ;
577+ let sandbox = Sandbox :: decode ( migrated. as_slice ( ) ) . unwrap ( ) ;
578+ let endpoint = & sandbox. spec . unwrap ( ) . policy . unwrap ( ) . network_policies [ "api" ] . endpoints [ 0 ] ;
579+
580+ assert_eq ! ( endpoint. tls, NetworkTlsMode :: Terminate as i32 ) ;
581+ assert_eq ! ( endpoint. enforcement, NetworkEnforcementMode :: Enforce as i32 ) ;
582+ assert_eq ! ( endpoint. access, NetworkAccessPreset :: ReadWrite as i32 ) ;
583+ assert_eq ! ( migrate( "sandbox" , & migrated) . unwrap( ) , migrated) ;
584+ }
585+
586+ #[ test]
587+ fn rejects_unknown_pre4_network_endpoint_strings ( ) {
588+ let legacy = legacy_sandbox_with_endpoint ( LegacyNetworkEndpoint {
589+ host : "api.example.com" . into ( ) ,
590+ tls : String :: new ( ) ,
591+ enforcement : "observe" . into ( ) ,
592+ access : String :: new ( ) ,
593+ } ) ;
594+
595+ let error = migrate ( "sandbox" , & legacy. encode_to_vec ( ) ) . unwrap_err ( ) ;
596+ assert ! (
597+ error
598+ . to_string( )
599+ . contains( "unsupported legacy network endpoint enforcement value 'observe'" )
600+ ) ;
601+ }
602+
415603 #[ test]
416604 fn migrates_nested_metadata_and_timestamp_maps ( ) {
417605 let legacy = LegacyProvider {
0 commit comments