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
30 changes: 25 additions & 5 deletions crates/openshell-policy-runtime-interface/src/policy_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,26 @@ pub struct PolicyGenerationGuard {
revalidator: Option<Arc<dyn ConnectionRevalidator>>,
}

/// The policy that authorized a request no longer applies.
#[derive(Debug)]
pub struct StalePolicyGeneration {
captured: u64,
current: u64,
}

impl std::fmt::Display for StalePolicyGeneration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"policy generation is stale [captured_generation:{} current_generation:{}]",
self.captured, self.current
)
}
}

impl std::error::Error for StalePolicyGeneration {}
impl miette::Diagnostic for StalePolicyGeneration {}

/// Builds a [`PolicyGenerationGuard`] for any state holding a plain
/// `Arc<AtomicU64>` generation counter and matching `watch::Sender<u64>`.
///
Expand Down Expand Up @@ -445,11 +465,11 @@ impl PolicyGenerationGuard {

pub fn ensure_current(&self) -> Result<()> {
if self.is_stale() {
return Err(miette::miette!(
"policy generation is stale [captured_generation:{} current_generation:{}]",
self.captured_generation(),
self.current_generation(),
));
return Err(StalePolicyGeneration {
captured: self.captured_generation(),
current: self.current_generation(),
}
.into());
}
Ok(())
}
Expand Down
21 changes: 15 additions & 6 deletions crates/openshell-supervisor-middleware/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@
use crate::{
EXTERNAL_FINDING_LABEL, MAX_MIDDLEWARE_FINDING_BYTES, MAX_MIDDLEWARE_FINDINGS_PER_STAGE,
MAX_MIDDLEWARE_METADATA_BYTES, MAX_MIDDLEWARE_METADATA_ENTRIES, MAX_MIDDLEWARE_REASON_BYTES,
MAX_MIDDLEWARE_REASON_CODE_BYTES, MiddlewareDiagnosticPolicy, is_stable_reason_code,
MAX_MIDDLEWARE_REASON_CODE_BYTES, MiddlewareDiagnosticPolicy, PlatformFailure,
is_stable_reason_code,
};
use openshell_core::proto::MiddlewareDiagnostics;
use prost::Message;

pub fn validate_diagnostics(
diagnostics: Option<&MiddlewareDiagnostics>,
) -> Result<MiddlewareDiagnostics, &'static str> {
) -> Result<MiddlewareDiagnostics, PlatformFailure> {
let diagnostics = diagnostics.cloned().unwrap_or_default();
if diagnostics.reason.len() > MAX_MIDDLEWARE_REASON_BYTES {
return Err("diagnostics_reason_over_capacity");
return Err(PlatformFailure::Capacity(
"diagnostics_reason_over_capacity",
));
}
if !diagnostics.reason_code.is_empty()
&& (diagnostics.reason_code.len() > MAX_MIDDLEWARE_REASON_CODE_BYTES
|| !is_stable_reason_code(&diagnostics.reason_code))
{
return Err("diagnostics_reason_code_invalid");
return Err(PlatformFailure::InvalidResult(
"diagnostics_reason_code_invalid",
));
}
if diagnostics.findings.len() > MAX_MIDDLEWARE_FINDINGS_PER_STAGE
|| diagnostics
.findings
.iter()
.any(|finding| finding.encoded_len() > MAX_MIDDLEWARE_FINDING_BYTES)
{
return Err("diagnostics_findings_over_capacity");
return Err(PlatformFailure::Capacity(
"diagnostics_findings_over_capacity",
));
}
if diagnostics.metadata.len() > MAX_MIDDLEWARE_METADATA_ENTRIES
|| diagnostics
Expand All @@ -38,7 +45,9 @@ pub fn validate_diagnostics(
.fold(0usize, usize::saturating_add)
> MAX_MIDDLEWARE_METADATA_BYTES
{
return Err("diagnostics_metadata_over_capacity");
return Err(PlatformFailure::Capacity(
"diagnostics_metadata_over_capacity",
));
}
Ok(diagnostics)
}
Expand Down
9 changes: 5 additions & 4 deletions crates/openshell-supervisor-middleware/src/http_hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub use response::{HttpResponseDelivery, HttpResponsePipeline, HttpResponsePipel
use crate::{
DescribedChainEntry, HttpHookVersion, MAX_MIDDLEWARE_CONTEXT_BYTES,
MAX_MIDDLEWARE_HEADER_BYTES, MAX_MIDDLEWARE_HEADERS, MAX_MIDDLEWARE_TARGET_BYTES,
PlatformFailure,
};

/// HTTP hook version a described chain runs on.
Expand Down Expand Up @@ -95,25 +96,25 @@ fn validate_head_input(
context: &RequestContext,
target: &HttpRequestTarget,
headers: &[HttpHeader],
) -> Result<(), &'static str> {
) -> Result<(), PlatformFailure> {
if context.encoded_len() > MAX_MIDDLEWARE_CONTEXT_BYTES
|| target.encoded_len() > MAX_MIDDLEWARE_TARGET_BYTES
{
return Err("head_input_over_capacity");
return Err(PlatformFailure::HeadInputOverCapacity);
}
validate_header_input(headers)
}

/// Platform limits on one header list shown to every stage.
fn validate_header_input(headers: &[HttpHeader]) -> Result<(), &'static str> {
fn validate_header_input(headers: &[HttpHeader]) -> Result<(), PlatformFailure> {
if headers.len() > MAX_MIDDLEWARE_HEADERS
|| headers
.iter()
.map(prost::Message::encoded_len)
.fold(0usize, usize::saturating_add)
> MAX_MIDDLEWARE_HEADER_BYTES
{
return Err("head_input_over_capacity");
return Err(PlatformFailure::HeadInputOverCapacity);
}
Ok(())
}
Loading
Loading