Skip to content

Commit 2a56014

Browse files
committed
feat(policy): freeze sandbox policy and provider permissions
Signed-off-by: Shiju <shiju@nvidia.com>
1 parent 659b45f commit 2a56014

73 files changed

Lines changed: 8081 additions & 538 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-cli/src/commands/provider.rs‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use super::provider_readiness::{
55
ProviderMutationExpectation, ProviderWaitOptions, finish_provider_mutation,
6+
warn_frozen_sandboxes_withholding,
67
};
78
use crate::color::Colorize;
89
use crate::commands::common::{
@@ -1784,6 +1785,8 @@ pub async fn provider_profile_import(
17841785
"s"
17851786
}
17861787
);
1788+
// Warnings, such as frozen sandboxes that keep their captured profile.
1789+
print_profile_diagnostics(&diagnostics);
17871790
return Ok(());
17881791
}
17891792
}
@@ -1831,6 +1834,8 @@ pub async fn provider_profile_update(
18311834
diagnostics.extend(response.diagnostics);
18321835
if response.updated {
18331836
println!("Updated provider profile.");
1837+
// Warnings, such as frozen sandboxes that keep their captured profile.
1838+
print_profile_diagnostics(&diagnostics);
18341839
return Ok(());
18351840
}
18361841
}
@@ -2957,7 +2962,15 @@ pub async fn provider_update(options: ProviderUpdateOptions<'_>) -> Result<()> {
29572962
"gateway did not return a provider mutation receipt; saved credentials cannot establish readiness"
29582963
));
29592964
}
2960-
finish_provider_mutation(
2965+
// Only sandboxes the caller selected: this workspace and this provider.
2966+
let attached_sandboxes = response
2967+
.target_receipts
2968+
.iter()
2969+
.filter(|receipt| receipt.workspace == workspace && receipt.provider == name)
2970+
.filter_map(|receipt| receipt.desired.as_ref())
2971+
.map(|desired| desired.sandbox.clone())
2972+
.collect();
2973+
let finished = finish_provider_mutation(
29612974
&client,
29622975
&response.mutation_id,
29632976
response.target_receipts,
@@ -2970,7 +2983,10 @@ pub async fn provider_update(options: ProviderUpdateOptions<'_>) -> Result<()> {
29702983
},
29712984
readiness,
29722985
)
2973-
.await
2986+
.await;
2987+
// The update is saved either way. A frozen sandbox may now be cut off.
2988+
warn_frozen_sandboxes_withholding(server, tls, workspace, name, attached_sandboxes).await;
2989+
finished
29742990
}
29752991

29762992
pub async fn provider_delete(

‎crates/openshell-cli/src/commands/provider_readiness.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ use std::time::Duration;
1919
use tokio::time::Instant;
2020

2121
const PROVIDER_WAIT_CONCURRENCY: usize = 16;
22+
/// Bound on one freeze read behind the frozen-sandbox warning.
23+
const FROZEN_WARNING_TIMEOUT: Duration = Duration::from_secs(10);
2224

2325
/// Output and deadline choices shared by provider mutation and status commands.
2426
#[derive(Clone, Copy)]
@@ -256,6 +258,62 @@ async fn wait_for_mutation_statuses(
256258
results.into_iter().map(|(_, result)| result).collect()
257259
}
258260

261+
/// Name the frozen sandboxes that no longer receive this provider's credentials.
262+
///
263+
/// A frozen sandbox keeps the provider authorization it captured. A provider
264+
/// change that no longer matches it succeeds, and the gateway then withholds
265+
/// the credentials from that sandbox. The readiness reason is the generic
266+
/// `credentials_withheld`, so say which sandbox, why, and what repairs it.
267+
/// Best effort: the mutation or status this follows has already succeeded.
268+
pub(super) async fn warn_frozen_sandboxes_withholding(
269+
server: &str,
270+
tls: &TlsOptions,
271+
workspace: &str,
272+
provider: &str,
273+
sandboxes: Vec<String>,
274+
) {
275+
if sandboxes.is_empty() {
276+
return;
277+
}
278+
let Ok(Some(client)) = crate::tls::frozen_policies_client(server, tls).await else {
279+
return;
280+
};
281+
let mut warnings = stream::iter(sandboxes.into_iter().map(|sandbox| {
282+
let mut client = client.clone();
283+
async move {
284+
// Best effort: a slow gateway costs the warning, not the command.
285+
let state = tokio::time::timeout(
286+
FROZEN_WARNING_TIMEOUT,
287+
client.get_policy(openshell_core::proto::GetFrozenSandboxPolicyRequest {
288+
workspace_scope: Some(openshell_core::proto::workspace_selector(workspace)),
289+
name: sandbox.clone(),
290+
}),
291+
)
292+
.await
293+
.ok()?
294+
.ok()?
295+
.into_inner();
296+
let withheld = state
297+
.withheld_providers
298+
.into_iter()
299+
.filter(|withheld| withheld.provider == provider)
300+
.collect::<Vec<_>>();
301+
let line = crate::run::withheld_provider_lines(&withheld)
302+
.into_iter()
303+
.next()?;
304+
Some(format!("frozen sandbox '{workspace}/{sandbox}': {line}"))
305+
}
306+
}))
307+
.buffer_unordered(PROVIDER_WAIT_CONCURRENCY)
308+
.filter_map(|warning| async move { warning })
309+
.collect::<Vec<_>>()
310+
.await;
311+
warnings.sort();
312+
for warning in warnings {
313+
eprintln!("Warning: {warning}");
314+
}
315+
}
316+
259317
/// Show desired/observed provider state, optionally waiting for that exact state.
260318
pub async fn sandbox_provider_status(
261319
server: &str,
@@ -313,6 +371,10 @@ pub async fn sandbox_provider_status(
313371
};
314372
let complete = result.complete;
315373
print_statuses(&mutation_id, &[result], options.output)?;
374+
// The readiness state lags until the supervisor reports. The freeze
375+
// record already knows whether this provider is withheld.
376+
warn_frozen_sandboxes_withholding(server, tls, workspace, provider, vec![name.to_string()])
377+
.await;
316378
if options.wait && !complete {
317379
return Err(miette!(
318380
"provider readiness wait did not complete; inspect the reported outcome"

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,14 @@ enum SandboxCommands {
16081608
#[arg(long, value_hint = ValueHint::FilePath)]
16091609
policy: Option<String>,
16101610

1611+
/// Freeze the first accepted policy and provider authorization until the sandbox is deleted.
1612+
///
1613+
/// Policy edits, proposal approvals, and provider attach or detach
1614+
/// then fail; recreate the sandbox to change them. Every gateway
1615+
/// replica must support frozen policies.
1616+
#[arg(long)]
1617+
frozen: bool,
1618+
16111619
/// Forward a local port to the sandbox before the initial command or shell starts.
16121620
/// Accepts [`bind_address`:]port (e.g. 8080, 0.0.0.0:8080). Keeps the sandbox alive.
16131621
#[arg(long, conflicts_with = "no_keep")]
@@ -1767,6 +1775,11 @@ enum SandboxCommands {
17671775
#[arg(long)]
17681776
selector: Option<String>,
17691777

1778+
/// Show only sandboxes created with `--frozen`, with their freeze state.
1779+
/// Pages without a frozen sandbox are skipped.
1780+
#[arg(long)]
1781+
frozen: bool,
1782+
17701783
/// Output format.
17711784
#[arg(short = 'o', long = "output", value_enum, default_value_t = OutputFormat::Table, conflicts_with_all = ["ids", "names"])]
17721785
output: OutputFormat,
@@ -3637,6 +3650,7 @@ async fn run_async() -> Result<()> {
36373650
driver_config_json,
36383651
providers,
36393652
policy,
3653+
frozen,
36403654
forward,
36413655
expose,
36423656
expose_authorization_mode,
@@ -3753,6 +3767,7 @@ async fn run_async() -> Result<()> {
37533767
restart_policy: &restart_policy,
37543768
provision_timeout_secs: timeout.map(std::num::NonZeroU32::get),
37553769
create_retry_timeout_secs,
3770+
frozen,
37563771
},
37573772
&cli.workspace,
37583773
&tls,
@@ -3837,6 +3852,7 @@ async fn run_async() -> Result<()> {
38373852
ids,
38383853
names,
38393854
selector,
3855+
frozen,
38403856
output,
38413857
all_workspaces,
38423858
} => {
@@ -3847,6 +3863,11 @@ async fn run_async() -> Result<()> {
38473863
ids,
38483864
names,
38493865
selector.as_deref(),
3866+
if frozen {
3867+
run::SandboxListFilter::Frozen
3868+
} else {
3869+
run::SandboxListFilter::All
3870+
},
38503871
output.as_str(),
38513872
&cli.workspace,
38523873
all_workspaces,
@@ -6741,6 +6762,38 @@ mod tests {
67416762
}
67426763
}
67436764

6765+
#[test]
6766+
fn sandbox_create_frozen_supports_inline_and_named_templates() {
6767+
for args in [
6768+
vec!["openshell", "sandbox", "create", "--frozen"],
6769+
vec![
6770+
"openshell",
6771+
"sandbox",
6772+
"create",
6773+
"--template",
6774+
"agent",
6775+
"--frozen",
6776+
],
6777+
] {
6778+
let cli = Cli::try_parse_from(args).unwrap();
6779+
assert!(matches!(
6780+
cli.command,
6781+
Some(Commands::Sandbox {
6782+
command: Some(SandboxCommands::Create { frozen: true, .. }),
6783+
..
6784+
})
6785+
));
6786+
}
6787+
let cli = Cli::try_parse_from(["openshell", "sandbox", "create"]).unwrap();
6788+
assert!(matches!(
6789+
cli.command,
6790+
Some(Commands::Sandbox {
6791+
command: Some(SandboxCommands::Create { frozen: false, .. }),
6792+
..
6793+
})
6794+
));
6795+
}
6796+
67446797
#[test]
67456798
fn sandbox_create_restart_policy_defaults_to_never() {
67466799
let cli = Cli::try_parse_from(["openshell", "sandbox", "create"]).unwrap();

0 commit comments

Comments
 (0)