use super::{BrokerError, ContextBroker, digest, measure, random_id, valid_label};
use crate::{AnswerDecision, AnswerPolicy, ClaimReview, ContextViewAssessment, TokenCounter};
use serde::{Deserialize, Serialize};
const SCHEMA: &str = "cigar.context-execution-binding.v1";
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionReview {
pub authority_revision: String,
pub reviews: Vec<ClaimReview>,
pub policy: AnswerPolicy,
}
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionBinding {
pub schema: String,
pub id: String,
pub epoch: String,
pub ticket: String,
pub submission_id: String,
pub context_id: String,
pub snapshot_id: String,
pub source_authority_digest: String,
pub review_digest: String,
pub effect_id: String,
pub intent_digest: String,
}
impl std::fmt::Debug for ExecutionBinding {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ExecutionBinding").finish_non_exhaustive()
}
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExecutionHandoff {
pub binding: ExecutionBinding,
pub checked: ContextViewAssessment,
}
#[derive(Serialize)]
pub(super) struct PendingExecution {
binding: ExecutionBinding,
taken: bool,
}
impl ContextBroker {
pub fn host_bind_execution(
&mut self,
ticket_id: &str,
submission_id: &str,
effect_id: &str,
intent_digest: &str,
review: &ExecutionReview,
tokenizer: &impl TokenCounter,
) -> Result<ExecutionBinding, BrokerError> {
if !valid_label(effect_id)
|| intent_digest.len() != 68
|| !intent_digest.starts_with("1220")
|| !intent_digest
.bytes()
.skip(4)
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))
{
return Err(BrokerError::InvalidInput);
}
let review_digest = review_identity(review)?;
let checked = self.host_check_answer(
ticket_id,
submission_id,
&review.reviews,
&review.policy,
tokenizer,
)?;
if checked.assessment.decision != AnswerDecision::Release {
return Err(BrokerError::AccessDenied);
}
let ticket = self
.tickets
.get(ticket_id)
.ok_or(BrokerError::AccessDenied)?;
if ticket.execution.is_some() {
return Err(BrokerError::Conflict);
}
let grant = self
.grants
.get(&ticket.owner)
.ok_or(BrokerError::AccessDenied)?;
let binding = ExecutionBinding {
schema: SCHEMA.into(),
id: random_id()?,
epoch: self.epoch.clone(),
ticket: ticket_id.into(),
submission_id: submission_id.into(),
context_id: checked.context_id,
snapshot_id: checked.assessment.snapshot_id,
source_authority_digest: digest(
"cigar.broker-source-authority.v1",
&(&self.epoch, &ticket.versions),
)?,
review_digest,
effect_id: effect_id.into(),
intent_digest: intent_digest.into(),
};
let pending = PendingExecution {
binding: binding.clone(),
taken: false,
};
let bytes = measure(
&(
&ticket.owner,
&ticket.context,
&ticket.versions,
&ticket.submission,
&pending,
),
grant.spec.limits.max_retained_bytes,
)?;
self.check_retention(&ticket.owner, ticket.bytes, bytes)?;
let ticket = self
.tickets
.get_mut(ticket_id)
.ok_or(BrokerError::AccessDenied)?;
ticket.execution = Some(pending);
ticket.bytes = bytes;
Ok(binding)
}
pub fn host_take_execution_handoff(
&mut self,
binding: &ExecutionBinding,
review: &ExecutionReview,
tokenizer: &impl TokenCounter,
) -> Result<ExecutionHandoff, BrokerError> {
let ticket = self
.tickets
.get(&binding.ticket)
.ok_or(BrokerError::AccessDenied)?;
let pending = ticket.execution.as_ref().ok_or(BrokerError::Stale)?;
if pending.binding != *binding || binding.epoch != self.epoch {
return Err(BrokerError::Stale);
}
if pending.taken {
return Err(BrokerError::Conflict);
}
if binding.review_digest != review_identity(review)? {
return Err(BrokerError::Stale);
}
let checked = self.host_check_answer(
&binding.ticket,
&binding.submission_id,
&review.reviews,
&review.policy,
tokenizer,
)?;
if checked.assessment.decision != AnswerDecision::Release {
return Err(BrokerError::AccessDenied);
}
let pending = self
.tickets
.get_mut(&binding.ticket)
.and_then(|ticket| ticket.execution.as_mut())
.ok_or(BrokerError::Stale)?;
pending.taken = true;
Ok(ExecutionHandoff {
binding: binding.clone(),
checked,
})
}
}
fn review_identity(review: &ExecutionReview) -> Result<String, BrokerError> {
if !valid_label(&review.authority_revision) || review.reviews.len() > 128 {
return Err(BrokerError::InvalidInput);
}
measure(review, 1024 * 1024)?;
let mut reviews = review.reviews.clone();
reviews.sort_by(|a, b| a.claim_key.cmp(&b.claim_key));
Ok(digest(
"cigar.execution-review.v1",
&(&review.authority_revision, &reviews, &review.policy),
)?)
}
#[cfg(test)]
mod tests;