Skip to content
Merged
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
1 change: 1 addition & 0 deletions crates/openshell-policy-cedar-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ ipnet = "2"
workspace = true

[dev-dependencies]
openshell-ocsf = { path = "../openshell-ocsf", features = ["test-support"] }
tempfile = "3"
4 changes: 2 additions & 2 deletions crates/openshell-policy-cedar-runtime/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use openshell_policy_runtime_interface::{
use openshell_supervisor_middleware::{ChainEntry, ChainRunner, MiddlewareRegistry};

use crate::cedar_only::{CedarL7TunnelEngine, CedarOnlyEngine, StagedCedarPolicy};
use crate::l7::L7RequestInfo;
use crate::l7::websocket::WebSocketAssemblyBudget;
use crate::l7::{L7Decision, L7RequestInfo};
use crate::network_middleware::{ActiveMiddleware, MiddlewareConfigs};
use crate::policy_types::{
EgressAuthorization, EndpointConfig, NetworkInput, PolicyDnsEligibilitySnapshot,
Expand Down Expand Up @@ -157,7 +157,7 @@ impl RequestPolicy for CedarRequestPolicy {
&self,
ctx: &L7PolicyContext,
request: &L7RequestInfo,
) -> Result<(bool, String)> {
) -> Result<L7Decision> {
self.l7.evaluate_request(ctx, request)
}
fn admitted_token_grant_owners(
Expand Down
68 changes: 42 additions & 26 deletions crates/openshell-policy-cedar-runtime/src/cedar_only/l7.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use openshell_policy_cedar::{CedarEngine, CedarEngineError, Decision, HttpReques

use super::routes::{RequestRoute, UNMATCHED_PATH};
use super::{LoadedPolicy, PLACEHOLDER_IDENTITY};
use crate::l7::L7Decision;
use crate::policy_types::GraphqlPersistedQuery;

/// Per-tunnel L7 decision handle for a Cedar-sourced sandbox.
Expand All @@ -32,7 +33,7 @@ pub struct CedarL7TunnelEngine {
}

impl CedarL7TunnelEngine {
/// Evaluates one L7 request and returns `(allowed, deny_reason)`.
/// Evaluates one L7 request and the Cedar policies that decided it.
///
/// # Errors
///
Expand All @@ -42,7 +43,7 @@ impl CedarL7TunnelEngine {
&self,
ctx: &crate::L7PolicyContext,
request: &crate::l7::L7RequestInfo,
) -> Result<(bool, String)> {
) -> Result<L7Decision> {
let guard = self.read_pinned()?;
Self::decide(&guard, ctx, request, StagedAudit::Log)
}
Expand Down Expand Up @@ -82,7 +83,9 @@ impl CedarL7TunnelEngine {
// `evaluate_request` already logged any staged audit decision for
// this request.
for l7_request in &requests {
if !Self::evaluate_one(&guard.cedar, ctx, request, l7_request, StagedAudit::Skip)?.0 {
if !Self::evaluate_one(&guard.cedar, ctx, request, l7_request, StagedAudit::Skip)?
.allowed
{
return Ok(none());
}
}
Expand Down Expand Up @@ -127,28 +130,46 @@ impl CedarL7TunnelEngine {
Ok(guard)
}

/// Decides one L7 request and returns `(allowed, deny_reason)`.
/// Decides one L7 request.
///
/// Same as the YAML path: a GraphQL request is allowed only when every
/// operation is allowed.
/// operation is allowed, by the policies that allowed any operation.
/// Otherwise it is denied with the first denial's reason, naming every
/// policy that denied an operation.
fn decide(
policy: &LoadedPolicy,
ctx: &crate::L7PolicyContext,
request: &crate::l7::L7RequestInfo,
staged_audit: StagedAudit,
) -> Result<(bool, String)> {
) -> Result<L7Decision> {
let requests = match Self::prepare(policy, ctx, request)? {
Prepared::Denied(reason) => return Ok((false, reason)),
Prepared::Denied(reason) => return Ok(L7Decision::deny(reason, Vec::new())),
Prepared::Evaluate(requests) => requests,
};
let mut allowed = L7Decision::allow(Vec::new());
let mut denied: Option<L7Decision> = None;
for l7_request in &requests {
// Operations after a denial only add attribution; the request is
// already blocked, so their staged outcomes are not logged.
let staged_audit = if denied.is_some() {
StagedAudit::Skip
} else {
staged_audit
};
let decision =
Self::evaluate_one(&policy.cedar, ctx, request, l7_request, staged_audit)?;
if !decision.0 {
return Ok(decision);
match Self::evaluate_one(&policy.cedar, ctx, request, l7_request, staged_audit) {
Comment thread
krishicks marked this conversation as resolved.
Resolved
Ok(decision) => decision,
// An error after a denial ends attribution without changing it.
Err(_) if denied.is_some() => break,
Err(error) => return Err(error),
};
match (&mut denied, decision.allowed) {
(_, true) => allowed.merge_policies(decision),
(Some(denied), false) => denied.merge_policies(decision),
(None, false) => denied = Some(decision),
}
}
Ok(Self::decision(true))
Ok(denied.unwrap_or(allowed))
}

/// Builds the Cedar evaluations of one L7 request, or the reason it is
Expand Down Expand Up @@ -293,8 +314,8 @@ impl CedarL7TunnelEngine {
))
}

/// Evaluates one Cedar request and returns `(allowed, deny_reason)`,
/// logging any staged audit decision when `staged_audit` asks for it.
/// Evaluates one Cedar request, logging any staged audit decision when
/// `staged_audit` asks for it.
///
/// A query with too many value combinations to evaluate, or a request
/// field over the engine's length cap, is denied with that reason.
Expand All @@ -304,14 +325,14 @@ impl CedarL7TunnelEngine {
request: &crate::l7::L7RequestInfo,
l7_request: &HttpRequestInput,
staged_audit: StagedAudit,
) -> Result<(bool, String)> {
) -> Result<L7Decision> {
let evaluation = match cedar.evaluate_l7(l7_request) {
Ok(evaluation) => evaluation,
Err(
error @ (CedarEngineError::TooManyQueryCombinations { .. }
| CedarEngineError::RequestFieldTooLong { .. }),
) => {
return Ok((false, error.to_string()));
return Ok(L7Decision::deny(error.to_string(), Vec::new()));
}
Err(error) => return Err(miette::miette!("{error}")),
};
Expand All @@ -325,17 +346,12 @@ impl CedarL7TunnelEngine {
staged
));
}
Ok(Self::decision(evaluation.is_allow()))
}

/// Returns the relay's `(allowed, deny_reason)` pair.
fn decision(allowed: bool) -> (bool, String) {
let reason = if allowed {
String::new()
} else {
"denied by Cedar HttpRequest policy".to_string()
};
(allowed, reason)
Ok(match evaluation.decision {
Decision::Allow { matched_policies } => L7Decision::allow(matched_policies),
Decision::Deny { matched_policies } => {
L7Decision::deny("denied by Cedar HttpRequest policy", matched_policies)
}
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ impl CedarOnlyEngine {

let action = match evaluation.decision.clone() {
Decision::Allow { matched_policies } => NetworkAction::Allow {
matched_policy: matched_policies.into_iter().next(),
matched_policy: matched_policies.into_iter().min(),
},
Decision::Deny { matched_policies } => NetworkAction::Deny {
reason: if matched_policies.is_empty() {
Expand Down
Loading
Loading