Skip to content

Commit 65ed2b9

Browse files
committed
refactor(sandbox): group policy poll loop state
1 parent 6f79bbb commit 65ed2b9

1 file changed

Lines changed: 39 additions & 33 deletions

File tree

  • crates/openshell-sandbox/src

crates/openshell-sandbox/src/lib.rs

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -934,20 +934,19 @@ pub async fn run_sandbox(
934934
.ok()
935935
.and_then(|v| v.parse().ok())
936936
.unwrap_or(10);
937+
let poll_ctx = PolicyPollLoopContext {
938+
endpoint: poll_endpoint,
939+
sandbox_id: poll_id,
940+
opa_engine: poll_engine,
941+
entrypoint_pid: poll_pid,
942+
interval_secs: poll_interval_secs,
943+
ocsf_enabled: poll_ocsf_enabled,
944+
provider_credentials: poll_provider_credentials,
945+
policy_local_ctx: Some(poll_policy_local),
946+
};
937947

938948
tokio::spawn(async move {
939-
if let Err(e) = run_policy_poll_loop(
940-
&poll_endpoint,
941-
&poll_id,
942-
&poll_engine,
943-
&poll_pid,
944-
poll_interval_secs,
945-
&poll_ocsf_enabled,
946-
poll_provider_credentials,
947-
Some(poll_policy_local),
948-
)
949-
.await
950-
{
949+
if let Err(e) = run_policy_poll_loop(poll_ctx).await {
951950
ocsf_emit!(
952951
AppLifecycleBuilder::new(ocsf_ctx())
953952
.activity(ActivityId::Fail)
@@ -2281,31 +2280,33 @@ async fn flush_proposals_to_gateway(
22812280
///
22822281
/// When the entrypoint PID is available, policy reloads include symlink
22832282
/// resolution for binary paths via the container filesystem.
2284-
async fn run_policy_poll_loop(
2285-
endpoint: &str,
2286-
sandbox_id: &str,
2287-
opa_engine: &Arc<OpaEngine>,
2288-
entrypoint_pid: &Arc<AtomicU32>,
2283+
struct PolicyPollLoopContext {
2284+
endpoint: String,
2285+
sandbox_id: String,
2286+
opa_engine: Arc<OpaEngine>,
2287+
entrypoint_pid: Arc<AtomicU32>,
22892288
interval_secs: u64,
2290-
ocsf_enabled: &std::sync::atomic::AtomicBool,
2289+
ocsf_enabled: Arc<std::sync::atomic::AtomicBool>,
22912290
provider_credentials: provider_credentials::ProviderCredentialState,
22922291
policy_local_ctx: Option<Arc<policy_local::PolicyLocalContext>>,
2293-
) -> Result<()> {
2292+
}
2293+
2294+
async fn run_policy_poll_loop(ctx: PolicyPollLoopContext) -> Result<()> {
22942295
use crate::grpc_client::CachedOpenShellClient;
22952296
use openshell_core::proto::PolicySource;
22962297
use std::sync::atomic::Ordering;
22972298

2298-
let client = CachedOpenShellClient::connect(endpoint).await?;
2299+
let client = CachedOpenShellClient::connect(&ctx.endpoint).await?;
22992300
let mut current_config_revision: u64 = 0;
2300-
let mut current_provider_env_revision: u64 = provider_credentials.snapshot().revision;
2301+
let mut current_provider_env_revision: u64 = ctx.provider_credentials.snapshot().revision;
23012302
let mut current_policy_hash = String::new();
23022303
let mut current_settings: std::collections::HashMap<
23032304
String,
23042305
openshell_core::proto::EffectiveSetting,
23052306
> = std::collections::HashMap::new();
23062307

23072308
// Initialize revision from the first poll.
2308-
match client.poll_settings(sandbox_id).await {
2309+
match client.poll_settings(&ctx.sandbox_id).await {
23092310
Ok(result) => {
23102311
current_config_revision = result.config_revision;
23112312
current_policy_hash = result.policy_hash.clone();
@@ -2320,11 +2321,11 @@ async fn run_policy_poll_loop(
23202321
}
23212322
}
23222323

2323-
let interval = Duration::from_secs(interval_secs);
2324+
let interval = Duration::from_secs(ctx.interval_secs);
23242325
loop {
23252326
tokio::time::sleep(interval).await;
23262327

2327-
let result = match client.poll_settings(sandbox_id).await {
2328+
let result = match client.poll_settings(&ctx.sandbox_id).await {
23282329
Ok(r) => r,
23292330
Err(e) => {
23302331
debug!(error = %e, "Settings poll: server unreachable, will retry");
@@ -2357,9 +2358,9 @@ async fn run_policy_poll_loop(
23572358
.build());
23582359

23592360
if provider_env_changed {
2360-
match grpc_client::fetch_provider_environment(endpoint, sandbox_id).await {
2361+
match grpc_client::fetch_provider_environment(&ctx.endpoint, &ctx.sandbox_id).await {
23612362
Ok(env_result) => {
2362-
let env_count = provider_credentials.install_environment(
2363+
let env_count = ctx.provider_credentials.install_environment(
23632364
env_result.provider_env_revision,
23642365
env_result.environment,
23652366
);
@@ -2404,11 +2405,11 @@ async fn run_policy_poll_loop(
24042405
continue;
24052406
};
24062407

2407-
let pid = entrypoint_pid.load(Ordering::Acquire);
2408-
match opa_engine.reload_from_proto_with_pid(policy, pid) {
2408+
let pid = ctx.entrypoint_pid.load(Ordering::Acquire);
2409+
match ctx.opa_engine.reload_from_proto_with_pid(policy, pid) {
24092410
Ok(()) => {
2410-
if let Some(ctx) = policy_local_ctx.as_ref() {
2411-
ctx.set_current_policy(policy.clone()).await;
2411+
if let Some(policy_local_ctx) = ctx.policy_local_ctx.as_ref() {
2412+
policy_local_ctx.set_current_policy(policy.clone()).await;
24122413
}
24132414
if result.global_policy_version > 0 {
24142415
ocsf_emit!(ConfigStateChangeBuilder::new(ocsf_ctx())
@@ -2440,7 +2441,7 @@ async fn run_policy_poll_loop(
24402441
if result.version > 0
24412442
&& result.policy_source == PolicySource::Sandbox
24422443
&& let Err(e) = client
2443-
.report_policy_status(sandbox_id, result.version, true, "")
2444+
.report_policy_status(&ctx.sandbox_id, result.version, true, "")
24442445
.await
24452446
{
24462447
warn!(error = %e, "Failed to report policy load success");
@@ -2461,7 +2462,12 @@ async fn run_policy_poll_loop(
24612462
if result.version > 0
24622463
&& result.policy_source == PolicySource::Sandbox
24632464
&& let Err(report_err) = client
2464-
.report_policy_status(sandbox_id, result.version, false, &e.to_string())
2465+
.report_policy_status(
2466+
&ctx.sandbox_id,
2467+
result.version,
2468+
false,
2469+
&e.to_string(),
2470+
)
24652471
.await
24662472
{
24672473
warn!(error = %report_err, "Failed to report policy load failure");
@@ -2472,7 +2478,7 @@ async fn run_policy_poll_loop(
24722478

24732479
// Apply OCSF JSON toggle from the `ocsf_json_enabled` setting.
24742480
let new_ocsf = extract_bool_setting(&result.settings, "ocsf_json_enabled").unwrap_or(false);
2475-
let prev_ocsf = ocsf_enabled.swap(new_ocsf, Ordering::Relaxed);
2481+
let prev_ocsf = ctx.ocsf_enabled.swap(new_ocsf, Ordering::Relaxed);
24762482
if new_ocsf != prev_ocsf {
24772483
info!(ocsf_json_enabled = new_ocsf, "OCSF JSONL logging toggled");
24782484
}

0 commit comments

Comments
 (0)