use super::{
PermissionedProcessRunnerFactory, ProcessActionIntent, ProcessRunner, ProcessRunnerContext,
ProcessRunnerError, ProcessRunnerFuture,
};
use crate::{HostIntegration, PathAccess, PathAccessRule, PermissionRequest, RequestedCapability};
use std::sync::{Arc, RwLock};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcessPathGrantConstraint {
Ordinary,
ReviewRequired,
ProtectedMetadata,
}
#[derive(Debug, Clone)]
pub struct ProcessPathGrant {
rule: PathAccessRule,
constraint: ProcessPathGrantConstraint,
}
pub struct PreparedProcessPermission {
runner: Arc<dyn ProcessRunner>,
grants: Vec<ProcessPathGrant>,
}
impl PreparedProcessPermission {
#[must_use]
pub fn new(runner: Arc<dyn ProcessRunner>, grants: Vec<ProcessPathGrant>) -> Self {
Self { runner, grants }
}
}
impl ProcessPathGrant {
#[must_use]
pub fn new(rule: PathAccessRule, constraint: ProcessPathGrantConstraint) -> Self {
Self { rule, constraint }
}
}
#[derive(Debug, Clone, Default)]
pub struct ProcessSessionPermissionSnapshot {
path_rules: Vec<PathAccessRule>,
host_integrations: Vec<HostIntegration>,
}
impl ProcessSessionPermissionSnapshot {
#[must_use]
pub fn path_rules(&self) -> &[PathAccessRule] {
&self.path_rules
}
#[must_use]
pub fn host_integrations(&self) -> &[HostIntegration] {
&self.host_integrations
}
}
#[derive(Debug, Clone)]
pub struct ProcessSessionPermissionView {
state: Arc<RwLock<ProcessSessionPermissionSnapshot>>,
}
impl ProcessSessionPermissionView {
pub fn snapshot(&self) -> Result<ProcessSessionPermissionSnapshot, ProcessRunnerError> {
self.state.read().map(|state| state.clone()).map_err(|_| {
ProcessRunnerError::infrastructure("runtime session permission state lock was poisoned")
})
}
}
#[derive(Debug, Clone, Default)]
pub struct ProcessSessionPermissions {
state: Arc<RwLock<ProcessSessionPermissionSnapshot>>,
}
impl ProcessSessionPermissions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn from_snapshot(snapshot: ProcessSessionPermissionSnapshot) -> Self {
Self {
state: Arc::new(RwLock::new(snapshot)),
}
}
pub fn from_inherited_view(
view: &ProcessSessionPermissionView,
) -> Result<Self, ProcessRunnerError> {
Ok(Self::from_snapshot(view.snapshot()?))
}
#[must_use]
pub fn view(&self) -> ProcessSessionPermissionView {
ProcessSessionPermissionView {
state: Arc::clone(&self.state),
}
}
#[must_use]
pub fn with_factory<Factory: PermissionedProcessRunnerFactory>(
self,
backend: Factory,
) -> SessionPermissionedProcessRunnerFactory<Factory> {
SessionPermissionedProcessRunnerFactory {
permissions: self,
backend,
}
}
pub fn clear(&self) -> Result<(), ProcessRunnerError> {
let mut state = self.state.write().map_err(|_| {
ProcessRunnerError::infrastructure("runtime session permission state lock was poisoned")
})?;
state.path_rules.clear();
state.host_integrations.clear();
Ok(())
}
fn retain(
&self,
request: &PermissionRequest,
grants: Vec<ProcessPathGrant>,
) -> Result<(), ProcessRunnerError> {
let mut state = self.state.write().map_err(|_| {
ProcessRunnerError::infrastructure("runtime session permission state lock was poisoned")
})?;
state
.path_rules
.extend(grants.into_iter().filter_map(|grant| {
(grant.constraint == ProcessPathGrantConstraint::Ordinary
&& !grant.rule.review_required()
&& grant.rule.access() != PathAccess::Deny)
.then_some(grant.rule)
}));
normalize_grants(&mut state.path_rules);
state
.host_integrations
.extend(
request
.requested()
.iter()
.filter_map(|capability| match capability {
RequestedCapability::HostIntegration(integration) => Some(*integration),
_ => None,
}),
);
state.host_integrations.sort_unstable();
state.host_integrations.dedup();
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct SessionPermissionedProcessRunnerFactory<Factory> {
permissions: ProcessSessionPermissions,
backend: Factory,
}
impl<Factory> SessionPermissionedProcessRunnerFactory<Factory> {
#[must_use]
pub fn backend(&self) -> &Factory {
&self.backend
}
}
impl<Factory: PermissionedProcessRunnerFactory> PermissionedProcessRunnerFactory
for SessionPermissionedProcessRunnerFactory<Factory>
{
fn validate_request(&self, request: &PermissionRequest) -> Result<(), ProcessRunnerError> {
self.backend.validate_request(request)
}
fn request_capabilities_are_satisfied(
&self,
request: &PermissionRequest,
) -> Result<bool, ProcessRunnerError> {
self.backend.request_capabilities_are_satisfied(request)
}
fn runner_for(&self, request: &PermissionRequest) -> Arc<dyn ProcessRunner> {
let result = self
.backend
.prepare_approved_request(request)
.and_then(|prepared| {
self.permissions.retain(request, prepared.grants)?;
Ok(prepared.runner)
});
match result {
Ok(runner) => runner,
Err(error) => Arc::new(RejectedRunner(error)),
}
}
}
struct RejectedRunner(ProcessRunnerError);
impl ProcessRunner for RejectedRunner {
fn run<'a>(
&'a self,
_intent: ProcessActionIntent,
_context: ProcessRunnerContext,
) -> ProcessRunnerFuture<'a> {
Box::pin(async { Err(self.0.clone()) })
}
}
fn normalize_grants(rules: &mut Vec<PathAccessRule>) {
rules.sort_by(|left, right| {
left.path()
.components()
.count()
.cmp(&right.path().components().count())
.then_with(|| left.path().cmp(right.path()))
.then_with(|| {
(left.access() != PathAccess::ReadWrite)
.cmp(&(right.access() != PathAccess::ReadWrite))
})
});
let mut retained = Vec::with_capacity(rules.len());
for rule in rules.drain(..) {
if !retained.iter().any(|ancestor: &PathAccessRule| {
rule.path().starts_with(ancestor.path()) && ancestor.access().covers(rule.access())
}) {
retained.push(rule);
}
}
*rules = retained;
}
#[cfg(test)]
mod tests;