Skip to content

Commit b855d8d

Browse files
authored
fix(policy): reserve provider rule namespace (NVIDIA#1991)
Closes NVIDIA#1982 Reject user-authored _provider_* network policy keys at gateway boundaries, strip provider-derived rules from sandbox sync, and document the round-trip workflow. Signed-off-by: John Myers <[email protected]>
1 parent a242f84 commit b855d8d

12 files changed

Lines changed: 754 additions & 88 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,10 +1693,14 @@ enum PolicyCommands {
16931693
#[arg(long = "rev", default_value_t = 0)]
16941694
rev: u32,
16951695

1696-
/// Include the full policy payload.
1697-
#[arg(long)]
1696+
/// Include the effective policy payload, including provider-composed entries.
1697+
#[arg(long, conflicts_with = "base")]
16981698
full: bool,
16991699

1700+
/// Include the base policy payload without provider-composed entries.
1701+
#[arg(long)]
1702+
base: bool,
1703+
17001704
/// Output format.
17011705
#[arg(short = 'o', long = "output", value_enum, default_value_t = PolicyGetOutput::Table)]
17021706
output: PolicyGetOutput,
@@ -2378,14 +2382,16 @@ async fn main() -> Result<()> {
23782382
name,
23792383
rev,
23802384
full,
2385+
base,
23812386
output,
23822387
global,
23832388
} => {
2389+
let view = run::PolicyGetView::from_flags(base, full);
23842390
if global {
23852391
run::sandbox_policy_get_global(
23862392
&ctx.endpoint,
23872393
rev,
2388-
full,
2394+
view,
23892395
output.as_str(),
23902396
&tls,
23912397
)
@@ -2396,7 +2402,7 @@ async fn main() -> Result<()> {
23962402
&ctx.endpoint,
23972403
&name,
23982404
rev,
2399-
full,
2405+
view,
24002406
output.as_str(),
24012407
&tls,
24022408
)
@@ -4366,17 +4372,42 @@ mod tests {
43664372
Some(Commands::Policy {
43674373
command:
43684374
Some(PolicyCommands::Get {
4369-
name, full, output, ..
4375+
name,
4376+
full,
4377+
base,
4378+
output,
4379+
..
43704380
}),
43714381
}) => {
43724382
assert_eq!(name.as_deref(), Some("my-sandbox"));
43734383
assert!(full);
4384+
assert!(!base);
43744385
assert!(matches!(output, PolicyGetOutput::Json));
43754386
}
43764387
other => panic!("expected policy get command, got: {other:?}"),
43774388
}
43784389
}
43794390

4391+
#[test]
4392+
fn policy_get_base_output_parses() {
4393+
let cli = Cli::try_parse_from(["openshell", "policy", "get", "my-sandbox", "--base"])
4394+
.expect("policy get --base should parse");
4395+
4396+
match cli.command {
4397+
Some(Commands::Policy {
4398+
command:
4399+
Some(PolicyCommands::Get {
4400+
name, full, base, ..
4401+
}),
4402+
}) => {
4403+
assert_eq!(name.as_deref(), Some("my-sandbox"));
4404+
assert!(!full);
4405+
assert!(base);
4406+
}
4407+
other => panic!("expected policy get command, got: {other:?}"),
4408+
}
4409+
}
4410+
43804411
#[test]
43814412
fn policy_delete_global_parses() {
43824413
let cli = Cli::try_parse_from(["openshell", "policy", "delete", "--global", "--yes"])

‎crates/openshell-cli/src/run.rs‎

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ use openshell_providers::{
6363
profile_to_json, profile_to_yaml, profiles_to_json, profiles_to_yaml,
6464
};
6565
use owo_colors::OwoColorize;
66+
use std::borrow::Cow;
6667
use std::collections::{HashMap, HashSet};
6768
use std::io::{ErrorKind, IsTerminal, Read, Write};
6869
use std::path::{Path, PathBuf};
@@ -80,6 +81,27 @@ pub use openshell_core::forward::{
8081
ForwardSpec, find_forward_by_port, list_forwards, stop_forward, stop_forwards_for_sandbox,
8182
};
8283

84+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
85+
pub enum PolicyGetView {
86+
Metadata,
87+
Base,
88+
Full,
89+
}
90+
91+
impl PolicyGetView {
92+
pub fn from_flags(base: bool, full: bool) -> Self {
93+
match (base, full) {
94+
(true, _) => Self::Base,
95+
(false, true) => Self::Full,
96+
(false, false) => Self::Metadata,
97+
}
98+
}
99+
100+
fn includes_policy(self) -> bool {
101+
matches!(self, Self::Base | Self::Full)
102+
}
103+
}
104+
83105
#[derive(Debug, PartialEq, Eq)]
84106
enum SandboxUploadPlan {
85107
GitAware {
@@ -6919,7 +6941,7 @@ pub async fn sandbox_policy_get(
69196941
server: &str,
69206942
name: &str,
69216943
version: u32,
6922-
full: bool,
6944+
view: PolicyGetView,
69236945
output: &str,
69246946
tls: &TlsOptions,
69256947
) -> Result<()> {
@@ -6929,7 +6951,7 @@ pub async fn sandbox_policy_get(
69296951
server,
69306952
name,
69316953
version,
6932-
full,
6954+
view,
69336955
output,
69346956
tls,
69356957
(&mut stdout, &mut stderr),
@@ -6953,7 +6975,7 @@ pub async fn sandbox_policy_get_to_writer<W, E>(
69536975
server: &str,
69546976
name: &str,
69556977
version: u32,
6956-
full: bool,
6978+
view: PolicyGetView,
69576979
output: &str,
69586980
tls: &TlsOptions,
69596981
writers: (&mut W, &mut E),
@@ -6963,7 +6985,7 @@ where
69636985
E: Write + Send,
69646986
{
69656987
if version == 0 {
6966-
return sandbox_policy_get_effective_to_writer(server, name, full, output, tls, writers)
6988+
return sandbox_policy_get_effective_to_writer(server, name, view, output, tls, writers)
69676989
.await;
69686990
}
69696991

@@ -6990,7 +7012,7 @@ where
69907012
Some(inner.active_version),
69917013
&rev,
69927014
status,
6993-
full,
7015+
view,
69947016
)?;
69957017
writeln!(
69967018
stdout,
@@ -7018,10 +7040,11 @@ where
70187040
writeln!(stdout, "Error: {}", rev.load_error).into_diagnostic()?;
70197041
}
70207042

7021-
if full {
7043+
if view.includes_policy() {
70227044
if let Some(ref policy) = rev.policy {
70237045
writeln!(stdout, "---").into_diagnostic()?;
7024-
let yaml_str = openshell_policy::serialize_sandbox_policy(policy)
7046+
let policy = policy_for_view(policy, view);
7047+
let yaml_str = openshell_policy::serialize_sandbox_policy(policy.as_ref())
70257048
.wrap_err("failed to serialize policy to YAML")?;
70267049
write!(stdout, "{yaml_str}").into_diagnostic()?;
70277050
} else {
@@ -7039,7 +7062,7 @@ where
70397062
async fn sandbox_policy_get_effective_to_writer<W, E>(
70407063
server: &str,
70417064
name: &str,
7042-
full: bool,
7065+
view: PolicyGetView,
70437066
output: &str,
70447067
tls: &TlsOptions,
70457068
writers: (&mut W, &mut E),
@@ -7112,10 +7135,11 @@ where
71127135
serde_json::json!(config.global_policy_version),
71137136
);
71147137
}
7115-
if full {
7138+
if view.includes_policy() {
7139+
let policy = policy_for_view(policy, view);
71167140
obj.insert(
71177141
"policy".to_string(),
7118-
openshell_policy::sandbox_policy_to_json_value(policy)?,
7142+
openshell_policy::sandbox_policy_to_json_value(policy.as_ref())?,
71197143
);
71207144
}
71217145
writeln!(
@@ -7135,9 +7159,10 @@ where
71357159
writeln!(stdout, "Global: {}", config.global_policy_version)
71367160
.into_diagnostic()?;
71377161
}
7138-
if full {
7162+
if view.includes_policy() {
71397163
writeln!(stdout, "---").into_diagnostic()?;
7140-
let yaml_str = openshell_policy::serialize_sandbox_policy(policy)
7164+
let policy = policy_for_view(policy, view);
7165+
let yaml_str = openshell_policy::serialize_sandbox_policy(policy.as_ref())
71417166
.wrap_err("failed to serialize policy to YAML")?;
71427167
write!(stdout, "{yaml_str}").into_diagnostic()?;
71437168
}
@@ -7151,7 +7176,7 @@ where
71517176
pub async fn sandbox_policy_get_global(
71527177
server: &str,
71537178
version: u32,
7154-
full: bool,
7179+
view: PolicyGetView,
71557180
output: &str,
71567181
tls: &TlsOptions,
71577182
) -> Result<()> {
@@ -7171,7 +7196,7 @@ pub async fn sandbox_policy_get_global(
71717196
let status = PolicyStatus::try_from(rev.status).unwrap_or(PolicyStatus::Unspecified);
71727197
match output {
71737198
"json" => {
7174-
let obj = policy_revision_to_json("global", None, None, &rev, status, full)?;
7199+
let obj = policy_revision_to_json("global", None, None, &rev, status, view)?;
71757200
println!("{}", serde_json::to_string_pretty(&obj).into_diagnostic()?);
71767201
return Ok(());
71777202
}
@@ -7190,10 +7215,11 @@ pub async fn sandbox_policy_get_global(
71907215
println!("Loaded: {} ms", rev.loaded_at_ms);
71917216
}
71927217

7193-
if full {
7218+
if view.includes_policy() {
71947219
if let Some(ref policy) = rev.policy {
71957220
println!("---");
7196-
let yaml_str = openshell_policy::serialize_sandbox_policy(policy)
7221+
let policy = policy_for_view(policy, view);
7222+
let yaml_str = openshell_policy::serialize_sandbox_policy(policy.as_ref())
71977223
.wrap_err("failed to serialize policy to YAML")?;
71987224
print!("{yaml_str}");
71997225
} else {
@@ -7223,7 +7249,7 @@ fn policy_revision_to_json(
72237249
active_version: Option<u32>,
72247250
rev: &openshell_core::proto::SandboxPolicyRevision,
72257251
status: PolicyStatus,
7226-
full: bool,
7252+
view: PolicyGetView,
72277253
) -> Result<serde_json::Value> {
72287254
let mut obj = serde_json::Map::new();
72297255
obj.insert("scope".to_string(), serde_json::json!(scope));
@@ -7257,16 +7283,31 @@ fn policy_revision_to_json(
72577283
if !rev.load_error.is_empty() {
72587284
obj.insert("load_error".to_string(), serde_json::json!(rev.load_error));
72597285
}
7260-
if full {
7286+
if view.includes_policy() {
72617287
let policy = match rev.policy.as_ref() {
7262-
Some(policy) => openshell_policy::sandbox_policy_to_json_value(policy)?,
7288+
Some(policy) => {
7289+
let policy = policy_for_view(policy, view);
7290+
openshell_policy::sandbox_policy_to_json_value(policy.as_ref())?
7291+
}
72637292
None => serde_json::Value::Null,
72647293
};
72657294
obj.insert("policy".to_string(), policy);
72667295
}
72677296
Ok(serde_json::Value::Object(obj))
72687297
}
72697298

7299+
fn policy_for_view(policy: &SandboxPolicy, view: PolicyGetView) -> Cow<'_, SandboxPolicy> {
7300+
if view != PolicyGetView::Base {
7301+
return Cow::Borrowed(policy);
7302+
}
7303+
7304+
let mut base_policy = policy.clone();
7305+
base_policy
7306+
.network_policies
7307+
.retain(|name, _| !openshell_policy::is_provider_rule_name(name));
7308+
Cow::Owned(base_policy)
7309+
}
7310+
72707311
pub async fn sandbox_policy_list(
72717312
server: &str,
72727313
name: &str,

0 commit comments

Comments
 (0)