Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions crates/openshell-cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,9 @@ pub async fn sandbox_create(
.await?;

let policy = load_sandbox_policy(policy)?;
if let Some(policy) = &policy {
require_gateway_policy_support(&mut client, policy).await?;
}
let resource_limits = if template.is_none() {
build_sandbox_resource_limits(cpu, memory)?
} else {
Expand Down Expand Up @@ -2068,6 +2071,38 @@ fn load_sandbox_policy(cli_path: Option<&str>) -> Result<Option<SandboxPolicy>>
openshell_policy::load_sandbox_policy(cli_path)
}

/// Refuse to submit `credential_binding.placement` to a gateway that does not
/// advertise it. An older gateway drops the unknown field while decoding and
/// would apply the provider profile placement at that endpoint instead.
async fn require_gateway_policy_support(
client: &mut GrpcClient,
policy: &SandboxPolicy,
) -> Result<()> {
if !openshell_policy::uses_credential_binding_placement(policy) {
return Ok(());
}
let health = client
.health(openshell_core::proto::HealthRequest {})
.await
.into_diagnostic()?
.into_inner();
check_gateway_policy_support(policy, &health.supported_policy_capabilities)
}

fn check_gateway_policy_support(policy: &SandboxPolicy, supported: &[String]) -> Result<()> {
if openshell_policy::uses_credential_binding_placement(policy)
&& !openshell_core::extension_protocol::has_capability(
supported,
openshell_core::extension_protocol::POLICY_CREDENTIAL_BINDING_PLACEMENT_V1,
)
{
return Err(miette::miette!(
"the gateway does not support credential_binding.placement; upgrade every gateway replica before using it"
));
}
Ok(())
}

/// Sync files to or from a sandbox.
///
/// Dispatches to `sandbox_sync_up` or `sandbox_sync_down` based on the
Expand Down Expand Up @@ -6362,6 +6397,7 @@ pub async fn sandbox_policy_set(
.ok_or_else(|| miette::miette!("No policy loaded from {policy_path}"))?;

let mut client = grpc_client(server, tls).await?;
require_gateway_policy_support(&mut client, &policy).await?;

// Get the current revision to detect no-ops and, for a Cedar file, to
// keep the configuration the file cannot carry.
Expand Down Expand Up @@ -7813,6 +7849,26 @@ mod tests {
assert!(update.provider_credential_rules.is_empty());
}

#[test]
fn credential_binding_placement_requires_gateway_support() {
use super::check_gateway_policy_support;

let policy = |placement: &str| {
openshell_policy::parse_sandbox_policy(&format!(
"version: 1\nnetwork_policies:\n api:\n endpoints:\n - {{host: api.example.com, port: 443, credential_binding: {{provider: p{placement}}}}}\n"
))
.expect("policy")
};
let placed = policy(", placement: {auth_style: header, header_name: x-api-key}");
let supported = openshell_core::extension_protocol::gateway_policy_capabilities();

// An older gateway advertises nothing and would drop the field.
assert!(check_gateway_policy_support(&placed, &[]).is_err());
assert!(check_gateway_policy_support(&placed, &supported).is_ok());
// Policies without placement never depend on the capability.
assert!(check_gateway_policy_support(&policy(""), &[]).is_ok());
}

#[test]
fn cedar_policy_set_fails_when_the_current_policy_cannot_be_read() {
use super::current_policy_revision;
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-cli/tests/ensure_providers_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ impl OpenShell for TestOpenShell {
Ok(Response::new(HealthResponse {
status: ServiceStatus::Healthy.into(),
version: "test".to_string(),
supported_policy_capabilities: Vec::new(),
}))
}

Expand Down
1 change: 1 addition & 0 deletions crates/openshell-cli/tests/mtls_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl OpenShell for TestOpenShell {
Ok(Response::new(HealthResponse {
status: ServiceStatus::Healthy.into(),
version: "test".to_string(),
supported_policy_capabilities: Vec::new(),
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl OpenShell for TestOpenShell {
Ok(Response::new(HealthResponse {
status: ServiceStatus::Healthy.into(),
version: "test".to_string(),
supported_policy_capabilities: Vec::new(),
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl OpenShell for TestOpenShell {
Ok(Response::new(HealthResponse {
status: ServiceStatus::Healthy.into(),
version: "test".to_string(),
supported_policy_capabilities: Vec::new(),
}))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl OpenShell for TestOpenShell {
Ok(Response::new(HealthResponse {
status: ServiceStatus::Healthy.into(),
version: "test".to_string(),
supported_policy_capabilities: Vec::new(),
}))
}

Expand Down
18 changes: 18 additions & 0 deletions crates/openshell-core/src/extension_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ pub const PROTOCOL_MINOR: u32 = 0;
/// Policy format implemented by a peer's registered Cedar backend.
pub const POLICY_CEDAR_V1: &str = "openshell.policy.cedar.v1";

/// Peer enforces `credential_binding.placement` on policy endpoints.
pub const POLICY_CREDENTIAL_BINDING_PLACEMENT_V1: &str =
"openshell.policy.credential-binding-placement.v1";

/// Versioned policy features a gateway accepts and preserves in `Health`.
#[must_use]
pub fn gateway_policy_capabilities() -> Vec<String> {
vec![POLICY_CREDENTIAL_BINDING_PLACEMENT_V1.to_string()]
}

/// Versioned policy capabilities a supervisor reports in its hello.
#[must_use]
pub fn supervisor_policy_capabilities(cedar: bool) -> Vec<String> {
let mut capabilities = vec![POLICY_CREDENTIAL_BINDING_PLACEMENT_V1.to_string()];
capabilities.extend(capabilities_if(POLICY_CEDAR_V1, cedar));
capabilities
}

/// Whether a peer advertises the required versioned capability.
#[must_use]
pub fn has_capability(capabilities: &[String], required: &str) -> bool {
Expand Down
2 changes: 2 additions & 0 deletions crates/openshell-core/src/grpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,7 @@ pub async fn fetch_provider_environment_with_stable_placeholders(
sandbox_id: sandbox_id.to_string(),
supports_static_credential_bindings: true,
supports_stable_placeholder_environment_keys: true,
supports_static_credential_endpoint_placements: true,
})
.await
.map_err(grpc_status_error)?;
Expand Down Expand Up @@ -1553,6 +1554,7 @@ mod provider_environment_tests {
host: "api.example.com".to_string(),
port: 443,
path: "/v1/**".to_string(),
placement: None,
}],
credential_identity: "provider:TOKEN".to_string(),
..Default::default()
Expand Down
Loading
Loading