@@ -29,7 +29,7 @@ mod imp {
2929 use clap:: Parser ;
3030 use openshell_driver_mxc:: {
3131 DEFAULT_COMMAND , DEFAULT_CONTAINMENT , DEFAULT_MXC_VERSION , LossItem , MxcMappingOptions ,
32- build_loss_report, map_to_mxc, render_readme,
32+ build_loss_report, map_to_mxc, render_readme, split_policy ,
3333 } ;
3434 use serde_json:: Value ;
3535
@@ -81,11 +81,41 @@ mod imp {
8181 /// Emit `OpenShell` wildcard hosts into `allowedHosts` despite lossiness.
8282 #[ arg( long) ]
8383 allow_wildcards : bool ,
84+
85+ /// Run the lossless split instead of the coarse map.
86+ ///
87+ /// Requires `--proxy-port`. Prints the MXC config (with proxy redirect
88+ /// and empty `allowedHosts`) and the trimmed proxy policy side-by-side.
89+ #[ arg( long) ]
90+ split : bool ,
91+
92+ /// Localhost port the `OpenShell` CONNECT proxy listens on (required with `--split`).
93+ #[ arg( long) ]
94+ proxy_port : Option < u16 > ,
8495 }
8596
8697 pub fn run ( ) -> Result < ( ) > {
8798 let args = Args :: parse ( ) ;
8899
100+ if args. split {
101+ let port = args
102+ . proxy_port
103+ . ok_or_else ( || anyhow ! ( "--proxy-port is required with --split" ) ) ?;
104+ let policy_path = args
105+ . policy
106+ . as_ref ( )
107+ . ok_or_else ( || anyhow ! ( "--policy is required with --split" ) ) ?;
108+ let stem = policy_path
109+ . file_stem ( )
110+ . unwrap_or_default ( )
111+ . to_string_lossy ( )
112+ . into_owned ( ) ;
113+ let slug = args. container_id . clone ( ) . unwrap_or ( stem) ;
114+ let mut opts = build_options ( & args, & slug) ;
115+ opts. proxy_localhost_port = Some ( port) ;
116+ return show_split ( policy_path, & opts) ;
117+ }
118+
89119 if let Some ( examples_root) = & args. examples_root {
90120 let mut examples = discover_example_policies ( examples_root) ?;
91121 if examples. is_empty ( ) {
@@ -211,6 +241,70 @@ mod imp {
211241 Ok ( ( ) )
212242 }
213243
244+ // -----------------------------------------------------------------------
245+ // Lossless-split display
246+ // -----------------------------------------------------------------------
247+
248+ fn show_split ( policy_path : & Path , opts : & MxcMappingOptions ) -> Result < ( ) > {
249+ let content = std:: fs:: read_to_string ( policy_path)
250+ . with_context ( || format ! ( "reading {}" , policy_path. display( ) ) ) ?;
251+ let policy = openshell_policy:: parse_sandbox_policy ( & content)
252+ . map_err ( |e| anyhow ! ( "parsing {}: {e}" , policy_path. display( ) ) ) ?;
253+
254+ let result = split_policy ( & policy, opts)
255+ . ok_or_else ( || anyhow ! ( "split_policy returned None — is --proxy-port set?" ) ) ?;
256+
257+ println ! ( "=== MXC ContainerConfig (filesystem + proxy redirect) ===" ) ;
258+ println ! ( "{}" , serde_json:: to_string_pretty( & result. mxc_config) ?) ;
259+
260+ println ! ( ) ;
261+ println ! ( "=== Proxy policy (preserved for OpenShell fine-grained enforcement) ===" ) ;
262+ if result. proxy_policy . network_policies . is_empty ( ) {
263+ println ! ( " (no network_policies — nothing for the proxy to enforce)" ) ;
264+ } else {
265+ let mut rules: Vec < _ > = result. proxy_policy . network_policies . iter ( ) . collect ( ) ;
266+ rules. sort_by_key ( |( k, _) | k. as_str ( ) ) ;
267+ for ( name, rule) in rules {
268+ println ! ( " rule: {name}" ) ;
269+ for ep in & rule. endpoints {
270+ let ports = if ep. ports . is_empty ( ) {
271+ String :: new ( )
272+ } else {
273+ format ! ( ":{}" , ep. ports. iter( ) . map( |p| p. to_string( ) ) . collect:: <Vec <_>>( ) . join( "," ) )
274+ } ;
275+ println ! (
276+ " endpoint: {}{} protocol={} tls={} access={} enforcement={}" ,
277+ ep. host, ports,
278+ if ep. protocol. is_empty( ) { "-" } else { & ep. protocol } ,
279+ if ep. tls. is_empty( ) { "-" } else { & ep. tls } ,
280+ if ep. access. is_empty( ) { "-" } else { & ep. access } ,
281+ if ep. enforcement. is_empty( ) { "-" } else { & ep. enforcement } ,
282+ ) ;
283+ if !ep. rules . is_empty ( ) {
284+ println ! ( " allow rules: {}" , ep. rules. len( ) ) ;
285+ }
286+ if !ep. deny_rules . is_empty ( ) {
287+ println ! ( " deny rules: {}" , ep. deny_rules. len( ) ) ;
288+ }
289+ }
290+ let binaries: Vec < _ > = rule. binaries . iter ( ) . map ( |b| b. path . as_str ( ) ) . collect ( ) ;
291+ if !binaries. is_empty ( ) {
292+ println ! ( " binaries: {}" , binaries. join( ", " ) ) ;
293+ }
294+ }
295+ }
296+
297+ if !result. loss . is_empty ( ) {
298+ println ! ( ) ;
299+ println ! ( "=== Filesystem loss items ({} item(s)) ===" , result. loss. len( ) ) ;
300+ for item in & result. loss {
301+ println ! ( " [{}] {}: {}" , item. severity, item. path, item. message) ;
302+ }
303+ }
304+
305+ Ok ( ( ) )
306+ }
307+
214308 // -----------------------------------------------------------------------
215309 // File discovery & slug helpers
216310 // -----------------------------------------------------------------------
0 commit comments