Skip to content

Commit 9430cef

Browse files
committed
fix(policy): reserve provider rule namespace
Closes #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 d93293a commit 9430cef

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
)
@@ -4364,17 +4370,42 @@ mod tests {
43644370
Some(Commands::Policy {
43654371
command:
43664372
Some(PolicyCommands::Get {
4367-
name, full, output, ..
4373+
name,
4374+
full,
4375+
base,
4376+
output,
4377+
..
43684378
}),
43694379
}) => {
43704380
assert_eq!(name.as_deref(), Some("my-sandbox"));
43714381
assert!(full);
4382+
assert!(!base);
43724383
assert!(matches!(output, PolicyGetOutput::Json));
43734384
}
43744385
other => panic!("expected policy get command, got: {other:?}"),
43754386
}
43764387
}
43774388

4389+
#[test]
4390+
fn policy_get_base_output_parses() {
4391+
let cli = Cli::try_parse_from(["openshell", "policy", "get", "my-sandbox", "--base"])
4392+
.expect("policy get --base should parse");
4393+
4394+
match cli.command {
4395+
Some(Commands::Policy {
4396+
command:
4397+
Some(PolicyCommands::Get {
4398+
name, full, base, ..
4399+
}),
4400+
}) => {
4401+
assert_eq!(name.as_deref(), Some("my-sandbox"));
4402+
assert!(!full);
4403+
assert!(base);
4404+
}
4405+
other => panic!("expected policy get command, got: {other:?}"),
4406+
}
4407+
}
4408+
43784409
#[test]
43794410
fn policy_delete_global_parses() {
43804411
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
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 {
@@ -6867,7 +6889,7 @@ pub async fn sandbox_policy_get(
68676889
server: &str,
68686890
name: &str,
68696891
version: u32,
6870-
full: bool,
6892+
view: PolicyGetView,
68716893
output: &str,
68726894
tls: &TlsOptions,
68736895
) -> Result<()> {
@@ -6877,7 +6899,7 @@ pub async fn sandbox_policy_get(
68776899
server,
68786900
name,
68796901
version,
6880-
full,
6902+
view,
68816903
output,
68826904
tls,
68836905
(&mut stdout, &mut stderr),
@@ -6901,7 +6923,7 @@ pub async fn sandbox_policy_get_to_writer<W, E>(
69016923
server: &str,
69026924
name: &str,
69036925
version: u32,
6904-
full: bool,
6926+
view: PolicyGetView,
69056927
output: &str,
69066928
tls: &TlsOptions,
69076929
writers: (&mut W, &mut E),
@@ -6911,7 +6933,7 @@ where
69116933
E: Write + Send,
69126934
{
69136935
if version == 0 {
6914-
return sandbox_policy_get_effective_to_writer(server, name, full, output, tls, writers)
6936+
return sandbox_policy_get_effective_to_writer(server, name, view, output, tls, writers)
69156937
.await;
69166938
}
69176939

@@ -6938,7 +6960,7 @@ where
69386960
Some(inner.active_version),
69396961
&rev,
69406962
status,
6941-
full,
6963+
view,
69426964
)?;
69436965
writeln!(
69446966
stdout,
@@ -6966,10 +6988,11 @@ where
69666988
writeln!(stdout, "Error: {}", rev.load_error).into_diagnostic()?;
69676989
}
69686990

6969-
if full {
6991+
if view.includes_policy() {
69706992
if let Some(ref policy) = rev.policy {
69716993
writeln!(stdout, "---").into_diagnostic()?;
6972-
let yaml_str = openshell_policy::serialize_sandbox_policy(policy)
6994+
let policy = policy_for_view(policy, view);
6995+
let yaml_str = openshell_policy::serialize_sandbox_policy(policy.as_ref())
69736996
.wrap_err("failed to serialize policy to YAML")?;
69746997
write!(stdout, "{yaml_str}").into_diagnostic()?;
69756998
} else {
@@ -6987,7 +7010,7 @@ where
69877010
async fn sandbox_policy_get_effective_to_writer<W, E>(
69887011
server: &str,
69897012
name: &str,
6990-
full: bool,
7013+
view: PolicyGetView,
69917014
output: &str,
69927015
tls: &TlsOptions,
69937016
writers: (&mut W, &mut E),
@@ -7060,10 +7083,11 @@ where
70607083
serde_json::json!(config.global_policy_version),
70617084
);
70627085
}
7063-
if full {
7086+
if view.includes_policy() {
7087+
let policy = policy_for_view(policy, view);
70647088
obj.insert(
70657089
"policy".to_string(),
7066-
openshell_policy::sandbox_policy_to_json_value(policy)?,
7090+
openshell_policy::sandbox_policy_to_json_value(policy.as_ref())?,
70677091
);
70687092
}
70697093
writeln!(
@@ -7083,9 +7107,10 @@ where
70837107
writeln!(stdout, "Global: {}", config.global_policy_version)
70847108
.into_diagnostic()?;
70857109
}
7086-
if full {
7110+
if view.includes_policy() {
70877111
writeln!(stdout, "---").into_diagnostic()?;
7088-
let yaml_str = openshell_policy::serialize_sandbox_policy(policy)
7112+
let policy = policy_for_view(policy, view);
7113+
let yaml_str = openshell_policy::serialize_sandbox_policy(policy.as_ref())
70897114
.wrap_err("failed to serialize policy to YAML")?;
70907115
write!(stdout, "{yaml_str}").into_diagnostic()?;
70917116
}
@@ -7099,7 +7124,7 @@ where
70997124
pub async fn sandbox_policy_get_global(
71007125
server: &str,
71017126
version: u32,
7102-
full: bool,
7127+
view: PolicyGetView,
71037128
output: &str,
71047129
tls: &TlsOptions,
71057130
) -> Result<()> {
@@ -7119,7 +7144,7 @@ pub async fn sandbox_policy_get_global(
71197144
let status = PolicyStatus::try_from(rev.status).unwrap_or(PolicyStatus::Unspecified);
71207145
match output {
71217146
"json" => {
7122-
let obj = policy_revision_to_json("global", None, None, &rev, status, full)?;
7147+
let obj = policy_revision_to_json("global", None, None, &rev, status, view)?;
71237148
println!("{}", serde_json::to_string_pretty(&obj).into_diagnostic()?);
71247149
return Ok(());
71257150
}
@@ -7138,10 +7163,11 @@ pub async fn sandbox_policy_get_global(
71387163
println!("Loaded: {} ms", rev.loaded_at_ms);
71397164
}
71407165

7141-
if full {
7166+
if view.includes_policy() {
71427167
if let Some(ref policy) = rev.policy {
71437168
println!("---");
7144-
let yaml_str = openshell_policy::serialize_sandbox_policy(policy)
7169+
let policy = policy_for_view(policy, view);
7170+
let yaml_str = openshell_policy::serialize_sandbox_policy(policy.as_ref())
71457171
.wrap_err("failed to serialize policy to YAML")?;
71467172
print!("{yaml_str}");
71477173
} else {
@@ -7171,7 +7197,7 @@ fn policy_revision_to_json(
71717197
active_version: Option<u32>,
71727198
rev: &openshell_core::proto::SandboxPolicyRevision,
71737199
status: PolicyStatus,
7174-
full: bool,
7200+
view: PolicyGetView,
71757201
) -> Result<serde_json::Value> {
71767202
let mut obj = serde_json::Map::new();
71777203
obj.insert("scope".to_string(), serde_json::json!(scope));
@@ -7205,16 +7231,31 @@ fn policy_revision_to_json(
72057231
if !rev.load_error.is_empty() {
72067232
obj.insert("load_error".to_string(), serde_json::json!(rev.load_error));
72077233
}
7208-
if full {
7234+
if view.includes_policy() {
72097235
let policy = match rev.policy.as_ref() {
7210-
Some(policy) => openshell_policy::sandbox_policy_to_json_value(policy)?,
7236+
Some(policy) => {
7237+
let policy = policy_for_view(policy, view);
7238+
openshell_policy::sandbox_policy_to_json_value(policy.as_ref())?
7239+
}
72117240
None => serde_json::Value::Null,
72127241
};
72137242
obj.insert("policy".to_string(), policy);
72147243
}
72157244
Ok(serde_json::Value::Object(obj))
72167245
}
72177246

7247+
fn policy_for_view(policy: &SandboxPolicy, view: PolicyGetView) -> Cow<'_, SandboxPolicy> {
7248+
if view != PolicyGetView::Base {
7249+
return Cow::Borrowed(policy);
7250+
}
7251+
7252+
let mut base_policy = policy.clone();
7253+
base_policy
7254+
.network_policies
7255+
.retain(|name, _| !openshell_policy::is_provider_rule_name(name));
7256+
Cow::Owned(base_policy)
7257+
}
7258+
72187259
pub async fn sandbox_policy_list(
72197260
server: &str,
72207261
name: &str,

0 commit comments

Comments
 (0)