use std::borrow::Cow;
use std::fmt;
use std::sync::Arc;
use serde::{Deserialize, Serialize};
use crate::attr::Attributes;
#[cfg(feature = "audit")]
use crate::audit::{AuditStatus, Auditor, FailMode};
use crate::condition::Condition;
use crate::decision::Request;
use crate::engine::{Context, Engine};
use crate::explain::Explanation;
use crate::grant::{Effect, Grant, SubjectMatcher};
use crate::impact::{Impact, ImpactOptions};
use crate::rebac::TupleStore;
use crate::resource::{Anchor, Resource};
use crate::revision::{Revised, Revision, RevisionSource};
use crate::subject::{RoleAssignment, Subject};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[allow(clippy::large_enum_variant)]
pub enum Mutation {
Add(Grant),
Remove {
id: String,
},
AssignRole {
subject: Subject,
role: RoleAssignment,
},
RevokeRole {
subject: Subject,
role: RoleAssignment,
},
}
impl Mutation {
pub fn role_change(&self) -> Option<(&Subject, &RoleAssignment, bool)> {
match self {
Mutation::AssignRole { subject, role } => Some((subject, role, true)),
Mutation::RevokeRole { subject, role } => Some((subject, role, false)),
Mutation::Add(_) | Mutation::Remove { .. } => None,
}
}
pub fn kind(&self) -> MutationKind {
match self {
Mutation::Add(_) => MutationKind::Add,
Mutation::Remove { .. } => MutationKind::Remove,
Mutation::AssignRole { .. } => MutationKind::AssignRole,
Mutation::RevokeRole { .. } => MutationKind::RevokeRole,
}
}
pub fn subject_after(&self) -> Option<Subject> {
let (subject, role, assigned) = self.role_change()?;
Some(with_role_change(subject, role, assigned))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MutationKind {
Add,
Remove,
AssignRole,
RevokeRole,
}
impl MutationKind {
pub const ALL: [MutationKind; 4] = [
MutationKind::Add,
MutationKind::Remove,
MutationKind::AssignRole,
MutationKind::RevokeRole,
];
pub fn as_str(self) -> &'static str {
match self {
MutationKind::Add => "add",
MutationKind::Remove => "remove",
MutationKind::AssignRole => "assign_role",
MutationKind::RevokeRole => "revoke_role",
}
}
}
pub(crate) fn with_role_change(subject: &Subject, role: &RoleAssignment, assigned: bool) -> Subject {
let mut next = subject.clone();
if !assigned {
next.roles.retain(|r| r != role);
} else if !next.roles.contains(role) {
next.roles.push(role.clone());
}
next
}
pub(crate) fn same_subject(a: &Subject, b: &Subject) -> bool {
a.id.is_some() && a.id == b.id
}
pub(crate) fn standing_roles(subject: &Subject, now: Option<u64>) -> impl Iterator<Item = &RoleAssignment> {
subject
.roles
.iter()
.filter(move |r| r.check_window().is_ok() && !r.validity().has_ended(now))
}
fn role_label(role: &RoleAssignment) -> String {
match &role.scope {
Some(scope) => format!("{} em {scope}", role.role),
None => role.role.clone(),
}
}
pub struct Proposal<'a> {
pub actor: &'a Subject,
pub mutation: &'a Mutation,
pub before: &'a [Grant],
pub after: &'a [Grant],
pub tuples: Option<&'a dyn TupleStore>,
pub now: Option<u64>,
}
impl<'a> Proposal<'a> {
pub fn grant(&self) -> Option<&'a Grant> {
match self.mutation {
Mutation::Add(grant) => Some(grant),
Mutation::Remove { id } => self.before.iter().find(|g| &g.id == id),
Mutation::AssignRole { .. } | Mutation::RevokeRole { .. } => None,
}
}
pub fn subject_after<'s>(&self, subject: &'s Subject) -> Cow<'s, Subject> {
match self.mutation.role_change() {
Some((target, role, assigned)) if same_subject(target, subject) => {
Cow::Owned(with_role_change(subject, role, assigned))
}
_ => Cow::Borrowed(subject),
}
}
pub fn before_context(&self) -> Context<'_> {
self.context(&self.before)
}
pub fn after_context(&self) -> Context<'_> {
self.context(&self.after)
}
fn context<'s>(&'s self, grants: &'s &'a [Grant]) -> Context<'s> {
let ctx = Context::new(grants);
let ctx = match self.tuples {
Some(tuples) => ctx.with_tuples(tuples),
None => ctx,
};
match self.now {
Some(now) => ctx.at(now),
None => ctx,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Suggestion {
AssignRole { subject_id: String, role: RoleAssignment },
RevokeRole { subject_id: String, role: RoleAssignment },
}
impl Suggestion {
fn of(mutation: &Mutation) -> Option<Self> {
let (subject, role, assigned) = mutation.role_change()?;
let subject_id = subject.id.clone()?;
let role = role.clone();
Some(if assigned {
Suggestion::AssignRole { subject_id, role }
} else {
Suggestion::RevokeRole { subject_id, role }
})
}
pub fn into_mutation(self, subject: Subject) -> Mutation {
match self {
Suggestion::AssignRole { role, .. } => Mutation::AssignRole { subject, role },
Suggestion::RevokeRole { role, .. } => Mutation::RevokeRole { subject, role },
}
}
pub fn subject_id(&self) -> &str {
match self {
Suggestion::AssignRole { subject_id, .. } | Suggestion::RevokeRole { subject_id, .. } => subject_id,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Violation {
pub invariant: String,
pub message: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub suggestion: Option<Box<Suggestion>>,
}
impl Violation {
pub fn new(invariant: impl Into<String>, message: impl Into<String>) -> Self {
Self {
invariant: invariant.into(),
message: message.into(),
suggestion: None,
}
}
}
impl fmt::Display for Violation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {}", self.invariant, self.message)
}
}
pub trait Invariant: Send + Sync {
fn name(&self) -> &str;
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation>;
fn remedy(&self, _engine: &Engine, _proposal: &Proposal<'_>) -> Option<Mutation> {
None
}
}
#[cfg(feature = "audit")]
type CallMode = Option<FailMode>;
#[cfg(not(feature = "audit"))]
type CallMode = Option<std::convert::Infallible>;
#[cfg(feature = "audit")]
type Delivery = Option<AuditStatus>;
#[cfg(not(feature = "audit"))]
type Delivery = Option<std::convert::Infallible>;
type Applied = Result<(Vec<Grant>, Option<Impact>), Vec<Violation>>;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct AppliedWithImpact {
pub grants: Vec<Grant>,
pub impact: Impact,
pub revision: Option<Revision>,
}
#[derive(Default)]
pub struct Governance {
invariants: Vec<Box<dyn Invariant>>,
#[cfg(feature = "audit")]
auditor: Option<Arc<Auditor>>,
revisions: Option<Arc<dyn RevisionSource>>,
}
impl Governance {
pub fn new() -> Self {
Self::default()
}
pub fn with(mut self, invariant: impl Invariant + 'static) -> Self {
self.invariants.push(Box::new(invariant));
self
}
#[cfg(feature = "audit")]
pub fn audited(mut self, auditor: Arc<Auditor>) -> Self {
self.auditor = Some(auditor);
self
}
#[must_use]
pub fn with_revisions(mut self, source: Arc<dyn RevisionSource>) -> Self {
self.revisions = Some(source);
self
}
fn next_revision(&self) -> Option<Revision> {
self.revisions.as_ref().map(|source| source.next())
}
pub fn apply_revised(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
) -> Result<Revised<Vec<Grant>>, Vec<Violation>> {
let grants = self.apply_with(engine, actor, current, mutation, tuples, meta)?;
Ok(Revised::new(grants, self.next_revision()))
}
#[cfg(feature = "audit")]
pub(crate) fn auditor(&self) -> Option<&Arc<Auditor>> {
self.auditor.as_ref()
}
pub fn len(&self) -> usize {
self.invariants.len()
}
pub fn is_empty(&self) -> bool {
self.invariants.is_empty()
}
pub fn apply(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
) -> Result<Vec<Grant>, Vec<Violation>> {
self.apply_with(engine, actor, current, mutation, tuples, &Attributes::new())
}
pub fn at(&self, now: u64) -> GovernanceAt<'_> {
GovernanceAt {
governance: self,
now,
call_mode: None,
}
}
pub fn apply_with(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
) -> Result<Vec<Grant>, Vec<Violation>> {
self.run(engine, actor, current, mutation, Scene { tuples, now: None }, meta)
}
pub fn impact(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
options: &ImpactOptions<'_>,
) -> Result<Impact, Vec<Violation>> {
let scene = Scene {
tuples: options.tuples.map(upcast),
now: options.now,
};
let after = self.evaluate_inner(engine, actor, current, mutation, scene, true)?;
Ok(crate::impact::compute(engine, current, &after, mutation, options))
}
#[allow(clippy::too_many_arguments)]
pub fn apply_with_impact(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
options: &ImpactOptions<'_>,
) -> Result<AppliedWithImpact, Vec<Violation>> {
let scene = Scene {
tuples,
now: options.now,
};
let (result, _) = self.run_impact(engine, actor, current, mutation, scene, meta, None, Some(options));
result.map(|(grants, impact)| AppliedWithImpact {
grants,
impact: impact.unwrap_or_default(),
revision: self.next_revision(),
})
}
fn run(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
scene: Scene<'_>,
meta: &Attributes,
) -> Result<Vec<Grant>, Vec<Violation>> {
self.run_reporting(engine, actor, current, mutation, scene, meta, None)
.0
}
#[allow(clippy::too_many_arguments)]
fn run_reporting(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
scene: Scene<'_>,
meta: &Attributes,
call: CallMode,
) -> (Result<Vec<Grant>, Vec<Violation>>, Delivery) {
let (result, delivery) = self.run_impact(engine, actor, current, mutation, scene, meta, call, None);
(result.map(|(grants, _)| grants), delivery)
}
#[allow(clippy::too_many_arguments)]
fn run_impact(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
scene: Scene<'_>,
meta: &Attributes,
call: CallMode,
options: Option<&ImpactOptions<'_>>,
) -> (Applied, Delivery) {
crate::obs::mutation(
|| {
let evaluated = self.evaluate_inner(engine, actor, current, mutation, scene, true);
let impact = match (&evaluated, options) {
(Ok(after), Some(options)) => {
let options = ImpactOptions {
now: scene.now.or(options.now),
..options.clone()
};
Some(crate::impact::compute(engine, current, after, mutation, &options))
}
_ => None,
};
#[cfg(feature = "audit")]
if let Some(auditor) = &self.auditor {
return match auditor.grant_mutation_impact(
actor,
mutation,
current,
&evaluated,
meta,
call,
impact.as_ref(),
) {
Ok(emitted) => (evaluated.map(|g| (g, impact)), Some(emitted.status)),
Err(error) => (Err(vec![Violation::new("audit", error.to_string())]), None),
};
}
#[cfg(not(feature = "audit"))]
let _ = (meta, call);
(evaluated.map(|g| (g, impact)), None)
},
|(result, _)| result.as_ref().err().map(Vec::as_slice),
)
}
pub(crate) fn evaluate(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
) -> Result<Vec<Grant>, Vec<Violation>> {
self.evaluate_inner(engine, actor, current, mutation, Scene { tuples, now: None }, true)
}
pub(crate) fn evaluate_at(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
now: u64,
) -> Result<Vec<Grant>, Vec<Violation>> {
let scene = Scene { tuples, now: Some(now) };
self.evaluate_inner(engine, actor, current, mutation, scene, true)
}
fn evaluate_inner(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
scene: Scene<'_>,
with_remedies: bool,
) -> Result<Vec<Grant>, Vec<Violation>> {
let after: Cow<'_, [Grant]> = match mutation {
Mutation::Add(grant) => {
if current.iter().any(|g| g.id == grant.id) {
return Err(vec![Violation::new(
"mutation",
format!("grant {} já existe", grant.id),
)]);
}
let mut next = current.to_vec();
next.push(grant.clone());
Cow::Owned(next)
}
Mutation::Remove { id } => {
if !current.iter().any(|g| &g.id == id) {
return Err(vec![Violation::new("mutation", format!("grant {id} não existe"))]);
}
Cow::Owned(current.iter().filter(|g| &g.id != id).cloned().collect())
}
Mutation::AssignRole { subject, role } => {
check_role_mutation(subject, role, true)?;
Cow::Borrowed(current)
}
Mutation::RevokeRole { subject, role } => {
check_role_mutation(subject, role, false)?;
Cow::Borrowed(current)
}
};
let proposal = Proposal {
actor,
mutation,
before: current,
after: &after,
tuples: scene.tuples,
now: scene.now,
};
let mut violations = Vec::new();
for invariant in &self.invariants {
let Err(mut violation) = invariant.check(engine, &proposal) else {
continue;
};
if with_remedies
&& let Some(fix) = invariant.remedy(engine, &proposal)
&& self.evaluate_inner(engine, actor, current, &fix, scene, false).is_ok()
{
violation.suggestion = Suggestion::of(&fix).map(Box::new);
}
violations.push(violation);
}
if violations.is_empty() {
Ok(after.into_owned())
} else {
Err(violations)
}
}
}
#[derive(Clone, Copy)]
struct Scene<'a> {
tuples: Option<&'a dyn TupleStore>,
now: Option<u64>,
}
fn upcast(index: &dyn crate::rebac::TupleIndex) -> &dyn TupleStore {
index
}
pub struct GovernanceAt<'g> {
governance: &'g Governance,
now: u64,
call_mode: CallMode,
}
impl GovernanceAt<'_> {
#[cfg(feature = "audit")]
pub fn fail_mode(mut self, mode: FailMode) -> Self {
self.call_mode = Some(mode);
self
}
#[cfg(feature = "audit")]
pub fn apply_reporting(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
) -> (Result<Vec<Grant>, Vec<Violation>>, Option<AuditStatus>) {
let scene = Scene {
tuples,
now: Some(self.now),
};
self.governance
.run_reporting(engine, actor, current, mutation, scene, meta, self.call_mode)
}
pub fn apply(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
) -> Result<Vec<Grant>, Vec<Violation>> {
self.apply_with(engine, actor, current, mutation, tuples, &Attributes::new())
}
pub fn impact(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
options: &ImpactOptions<'_>,
) -> Result<Impact, Vec<Violation>> {
let options = ImpactOptions {
now: Some(self.now),
..options.clone()
};
self.governance.impact(engine, actor, current, mutation, &options)
}
#[allow(clippy::too_many_arguments)]
pub fn apply_with_impact(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
options: &ImpactOptions<'_>,
) -> Result<AppliedWithImpact, Vec<Violation>> {
let scene = Scene {
tuples,
now: Some(self.now),
};
let (result, _) = self.governance.run_impact(
engine,
actor,
current,
mutation,
scene,
meta,
self.call_mode,
Some(options),
);
result.map(|(grants, impact)| AppliedWithImpact {
grants,
impact: impact.unwrap_or_default(),
revision: self.governance.next_revision(),
})
}
pub fn apply_revised(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
) -> Result<Revised<Vec<Grant>>, Vec<Violation>> {
let grants = self.apply_with(engine, actor, current, mutation, tuples, meta)?;
Ok(Revised::new(grants, self.governance.next_revision()))
}
pub fn apply_with(
&self,
engine: &Engine,
actor: &Subject,
current: &[Grant],
mutation: &Mutation,
tuples: Option<&dyn TupleStore>,
meta: &Attributes,
) -> Result<Vec<Grant>, Vec<Violation>> {
let scene = Scene {
tuples,
now: Some(self.now),
};
self.governance
.run_reporting(engine, actor, current, mutation, scene, meta, self.call_mode)
.0
}
}
fn check_role_mutation(subject: &Subject, role: &RoleAssignment, assigned: bool) -> Result<(), Vec<Violation>> {
let Some(id) = subject.id.as_deref() else {
return Err(vec![Violation::new(
"mutation",
"papel só se atribui a sujeito identificado",
)]);
};
if assigned && let Err(error) = role.check_window() {
return Err(vec![Violation::new("mutation", error.to_string())]);
}
let has = subject.roles.contains(role);
let message = match (assigned, has) {
(true, true) => format!("{id} já tem o papel {}", role_label(role)),
(false, false) => format!("{id} não tem o papel {}", role_label(role)),
_ => return Ok(()),
};
Err(vec![Violation::new("mutation", message)])
}
pub struct CatalogConformance;
impl CatalogConformance {
fn check_role(&self, engine: &Engine, current: &[Grant], role: &RoleAssignment) -> Result<(), Violation> {
let known = engine.roles().contains(&role.role)
|| current
.iter()
.any(|g| matches!(&g.subject, SubjectMatcher::Role { id } if *id == role.role));
if !known {
return Err(Violation::new(
self.name(),
format!("papel desconhecido: {}", role.role),
));
}
match &role.scope {
Some(scope) if !engine.catalog().is_target_kind(&scope.kind) => Err(Violation::new(
self.name(),
format!("escopo {scope} usa tipo fora do catálogo"),
)),
_ => Ok(()),
}
}
}
impl Invariant for CatalogConformance {
fn name(&self) -> &str {
"catalog_conformance"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
match proposal.mutation {
Mutation::Add(grant) => engine
.validate_grant(grant)
.map_err(|e| Violation::new(self.name(), e.to_string())),
Mutation::AssignRole { role, .. } => self.check_role(engine, proposal.before, role),
Mutation::Remove { .. } | Mutation::RevokeRole { .. } => Ok(()),
}
}
}
pub struct NoSelfManagement;
impl Invariant for NoSelfManagement {
fn name(&self) -> &str {
"no_self_management"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
let actor = proposal.actor;
if let Some((subject, _, _)) = proposal.mutation.role_change() {
return if same_subject(subject, actor) {
Err(Violation::new(
self.name(),
"não é permitido gerenciar os próprios papéis",
))
} else {
Ok(())
};
}
let Some(grant) = proposal.grant() else {
return Ok(());
};
let affects_actor = match &grant.subject {
SubjectMatcher::User { id } => actor.id.as_deref() == Some(id.as_str()),
SubjectMatcher::Role { id } => engine
.roles()
.expand(standing_roles(actor, proposal.now).map(|r| r.role.as_str()))
.contains(id),
_ => false,
};
if affects_actor {
Err(Violation::new(
self.name(),
"não é permitido gerenciar as próprias permissões",
))
} else {
Ok(())
}
}
}
pub struct RequirePermission {
key: String,
resource: Resource,
}
impl RequirePermission {
pub fn new(key: impl Into<String>, resource: Resource) -> Self {
Self {
key: key.into(),
resource,
}
}
}
impl Invariant for RequirePermission {
fn name(&self) -> &str {
"require_permission"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
let request = Request::new(proposal.actor, &self.key, &self.resource);
if engine.check(&proposal.before_context(), &request) {
Ok(())
} else {
Err(Violation::new(self.name(), format!("falta a permissão {}", self.key)))
}
}
}
pub struct NoLockout {
critical: Vec<String>,
members: Vec<Subject>,
resource: Resource,
reserve: Option<Reserve>,
}
struct Reserve {
admin_role: RoleAssignment,
subjects: Vec<Subject>,
}
impl NoLockout {
pub fn new<I, S>(critical: I, members: Vec<Subject>, resource: Resource) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
critical: critical.into_iter().map(Into::into).collect(),
members,
resource,
reserve: None,
}
}
#[must_use]
pub fn with_reserve<I>(mut self, admin_role: RoleAssignment, subjects: I) -> Self
where
I: IntoIterator<Item = Subject>,
{
self.reserve = Some(Reserve {
admin_role,
subjects: subjects.into_iter().collect(),
});
self
}
fn uncovered(&self, engine: &Engine, proposal: &Proposal<'_>, promoted: Option<&Subject>) -> Vec<&String> {
let ctx = proposal.after_context();
self.critical
.iter()
.filter(|key| {
!self.members.iter().any(|m| {
let member = match promoted {
Some(p) if same_subject(p, m) => Cow::Borrowed(p),
_ => proposal.subject_after(m),
};
engine.check(&ctx, &Request::new(&member, key, &self.resource))
})
})
.collect()
}
}
impl Invariant for NoLockout {
fn name(&self) -> &str {
"no_lockout"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
match self.uncovered(engine, proposal, None).first() {
Some(first) => Err(Violation::new(
self.name(),
format!("a mudança deixaria ninguém com a permissão {first}"),
)),
None => Ok(()),
}
}
fn remedy(&self, engine: &Engine, proposal: &Proposal<'_>) -> Option<Mutation> {
let reserve = self.reserve.as_ref()?;
reserve.subjects.iter().find_map(|candidate| {
candidate.id.as_ref()?;
if same_subject(candidate, proposal.actor) {
return None;
}
let member = self.members.iter().find(|m| same_subject(m, candidate))?;
let base = proposal.subject_after(member);
if member.roles.contains(&reserve.admin_role) || base.roles.contains(&reserve.admin_role) {
return None;
}
let promoted = with_role_change(&base, &reserve.admin_role, true);
self.uncovered(engine, proposal, Some(&promoted))
.is_empty()
.then(|| Mutation::AssignRole {
subject: member.clone(),
role: reserve.admin_role.clone(),
})
})
}
}
pub struct Forbid {
subject: SubjectMatcher,
permissions: Vec<String>,
}
impl Forbid {
pub fn new<I, S>(subject: SubjectMatcher, permissions: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
subject,
permissions: permissions.into_iter().map(Into::into).collect(),
}
}
}
impl Invariant for Forbid {
fn name(&self) -> &str {
"forbid"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
match proposal.mutation {
Mutation::Add(grant) => self.check_grant(engine, grant),
Mutation::AssignRole { subject, role } => self.check_role(engine, proposal, subject, role),
Mutation::Remove { .. } | Mutation::RevokeRole { .. } => Ok(()),
}
}
}
impl Forbid {
fn forbidden_by<'s>(&'s self, engine: &Engine, permission: &str) -> Option<&'s String> {
self.permissions
.iter()
.find(|p| engine.catalog().implies(permission, p))
}
fn check_grant(&self, engine: &Engine, grant: &Grant) -> Result<(), Violation> {
if grant.effect != Effect::Allow || grant.subject != self.subject {
return Ok(());
}
match self.forbidden_by(engine, &grant.permission) {
Some(forbidden) => Err(Violation::new(
self.name(),
format!(
"{} concederia {forbidden}, proibido para este sujeito",
grant.permission
),
)),
None => Ok(()),
}
}
fn check_role(
&self,
engine: &Engine,
proposal: &Proposal<'_>,
subject: &Subject,
role: &RoleAssignment,
) -> Result<(), Violation> {
let SubjectMatcher::User { id } = &self.subject else {
return Ok(());
};
if subject.id.as_deref() != Some(id.as_str()) {
return Ok(());
}
let roles = engine.roles().expand([role.role.as_str()]);
for grant in proposal
.before
.iter()
.filter(|g| g.effect == Effect::Allow && !g.validity().has_ended(proposal.now))
{
let SubjectMatcher::Role { id: granted } = &grant.subject else {
continue;
};
if !roles.contains(granted) {
continue;
}
if let Some(forbidden) = self.forbidden_by(engine, &grant.permission) {
return Err(Violation::new(
self.name(),
format!(
"o papel {} concederia {forbidden} pelo grant {} ({}), proibido para este sujeito",
role.role, grant.id, grant.permission
),
));
}
}
Ok(())
}
}
fn grant_resource(grant: &Grant, role_scope: Option<&Anchor>) -> Resource {
let mut path: Vec<Anchor> = Vec::new();
let target = Anchor {
kind: grant.target.kind.clone(),
id: grant.target.id.clone().or_else(|| Some(KIND_WILDCARD.to_string())),
};
for anchor in [Some(target), grant.scope.clone(), role_scope.cloned()]
.into_iter()
.flatten()
{
if !anchor.is_global() && !path.contains(&anchor) {
path.push(anchor);
}
}
Resource {
path,
..Resource::default()
}
}
const KIND_WILDCARD: &str = "*";
pub(crate) const PROBE_ID: &str = "strig:probe";
pub(crate) fn matcher_subject(matcher: &SubjectMatcher) -> Option<Subject> {
Some(match matcher {
SubjectMatcher::User { id } => Subject::user(id.clone()),
SubjectMatcher::Role { id } => Subject::user(PROBE_ID).with_role(id.clone()),
SubjectMatcher::Principal { name } => Subject::user(PROBE_ID).with_principal(name.clone()),
SubjectMatcher::Any => Subject::user(PROBE_ID),
SubjectMatcher::Relation { .. } => return None,
})
}
struct Conferred<'a> {
grant: &'a Grant,
resource: Resource,
keys: Vec<String>,
}
fn conferred_keys(engine: &Engine, permission: &str) -> Vec<String> {
let mut keys = vec![permission.to_string()];
keys.extend(engine.catalog().descendants(permission));
keys
}
fn conferred<'a>(engine: &Engine, proposal: &Proposal<'a>) -> Vec<Conferred<'a>> {
let ended = |grant: &Grant| grant.validity().has_ended(proposal.now);
match proposal.mutation {
Mutation::Add(grant) if grant.effect == Effect::Allow && !ended(grant) => vec![Conferred {
grant,
resource: grant_resource(grant, None),
keys: conferred_keys(engine, &grant.permission),
}],
Mutation::AssignRole { role, .. } if !role.validity().has_ended(proposal.now) => {
let roles = engine.roles().expand([role.role.as_str()]);
proposal
.before
.iter()
.filter(|g| g.effect == Effect::Allow && !ended(g))
.filter(|g| matches!(&g.subject, SubjectMatcher::Role { id } if roles.contains(id)))
.map(|grant| Conferred {
grant,
resource: grant_resource(grant, role.scope.as_ref()),
keys: conferred_keys(engine, &grant.permission),
})
.collect()
}
_ => Vec::new(),
}
}
fn unlocked<'a>(engine: &Engine, proposal: &Proposal<'a>) -> Vec<Conferred<'a>> {
let ended = |grant: &Grant| grant.validity().has_ended(proposal.now);
match proposal.mutation {
Mutation::Remove { .. } => proposal
.grant()
.filter(|g| g.effect == Effect::Deny && !ended(g))
.map(|grant| Conferred {
grant,
resource: grant_resource(grant, None),
keys: conferred_keys(engine, &grant.permission),
})
.into_iter()
.collect(),
Mutation::RevokeRole { role, .. } if !role.validity().has_ended(proposal.now) => {
let roles = engine.roles().expand([role.role.as_str()]);
proposal
.before
.iter()
.filter(|g| g.effect == Effect::Deny && !ended(g))
.filter(|g| matches!(&g.subject, SubjectMatcher::Role { id } if roles.contains(id)))
.map(|grant| Conferred {
grant,
resource: grant_resource(grant, role.scope.as_ref()),
keys: conferred_keys(engine, &grant.permission),
})
.collect()
}
_ => Vec::new(),
}
}
pub struct Prerequisites;
impl Prerequisites {
fn missing(
engine: &Engine,
ctx: &Context<'_>,
subject: &Subject,
key: &str,
resource: &Resource,
) -> Option<String> {
if !engine.catalog().has_prerequisites(key)
|| !engine
.decide_ignoring_prerequisites(ctx, &Request::new(subject, key, resource))
.is_allowed()
{
return None;
}
engine
.catalog()
.prerequisites(key)
.find(|p| !engine.check(ctx, &Request::new(subject, p, resource)))
.map(str::to_string)
}
fn violation(&self, key: &str, prerequisite: &str) -> Violation {
Violation::new(self.name(), format!("{key} exige {prerequisite} no mesmo recurso"))
}
fn grants_of<'g>(engine: &Engine, subject: &Subject, grants: &'g [Grant]) -> Vec<(&'g Grant, Option<Anchor>)> {
let mut found = Vec::new();
for grant in grants.iter().filter(|g| g.effect == Effect::Allow) {
match &grant.subject {
SubjectMatcher::User { id } if subject.id.as_deref() == Some(id.as_str()) => {
found.push((grant, None));
}
SubjectMatcher::Role { id } => {
for assignment in &subject.roles {
if engine.roles().expand([assignment.role.as_str()]).contains(id) {
found.push((grant, assignment.scope.clone()));
}
}
}
SubjectMatcher::Principal { name } if subject.principals.contains(name) => {
found.push((grant, None));
}
SubjectMatcher::Any => found.push((grant, None)),
_ => {}
}
}
found
}
fn broken_by(
&self,
engine: &Engine,
proposal: &Proposal<'_>,
ctx: &Context<'_>,
changed: &str,
) -> Result<(), Violation> {
for grant in proposal.after.iter().filter(|g| g.effect == Effect::Allow) {
let Some(subject) = matcher_subject(&grant.subject) else {
continue;
};
let resource = grant_resource(grant, None);
for key in conferred_keys(engine, &grant.permission) {
let relies = engine
.catalog()
.prerequisites(&key)
.any(|p| engine.catalog().implies(changed, p));
if !relies {
continue;
}
if let Some(p) = Self::missing(engine, ctx, &subject, &key, &resource) {
return Err(self.violation(&key, &p));
}
}
}
Ok(())
}
}
impl Invariant for Prerequisites {
fn name(&self) -> &str {
"prerequisites"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
let ctx = proposal.after_context();
match proposal.mutation {
Mutation::Add(grant) if grant.effect == Effect::Deny => {
if grant.validity().has_ended(proposal.now) {
return Ok(());
}
self.broken_by(engine, proposal, &ctx, &grant.permission)
}
Mutation::Add(grant) => {
let Some(subject) = matcher_subject(&grant.subject) else {
return Ok(());
};
for c in conferred(engine, proposal) {
for key in &c.keys {
if let Some(p) = Self::missing(engine, &ctx, &subject, key, &c.resource) {
return Err(self.violation(key, &p));
}
}
}
Ok(())
}
Mutation::Remove { .. } => match proposal.grant().filter(|g| !g.validity().has_ended(proposal.now)) {
Some(removed) => self.broken_by(engine, proposal, &ctx, &removed.permission),
None => Ok(()),
},
Mutation::AssignRole { role, .. } | Mutation::RevokeRole { role, .. }
if role.validity().has_ended(proposal.now) =>
{
Ok(())
}
Mutation::AssignRole { subject, .. } | Mutation::RevokeRole { subject, .. } => {
let after = proposal.subject_after(subject);
for (grant, scope) in Self::grants_of(engine, &after, proposal.after) {
let resource = grant_resource(grant, scope.as_ref());
for key in conferred_keys(engine, &grant.permission) {
if let Some(p) = Self::missing(engine, &ctx, &after, &key, &resource) {
return Err(self.violation(&key, &p));
}
}
}
Ok(())
}
}
}
}
pub struct NonDelegable {
manager: String,
}
impl NonDelegable {
pub fn new(manager: impl Into<String>) -> Self {
Self {
manager: manager.into(),
}
}
}
impl Invariant for NonDelegable {
fn name(&self) -> &str {
"non_delegable"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
let ctx = proposal.before_context();
for c in conferred(engine, proposal) {
let Some(key) = c
.keys
.iter()
.find(|k| engine.catalog().get(k).is_some_and(|d| d.non_delegable))
else {
continue;
};
let request = Request::new(proposal.actor, &self.manager, &c.resource);
if !engine.check(&ctx, &request) {
return Err(Violation::new(
self.name(),
format!("{key} não é delegável: só quem tem {} concede", self.manager),
));
}
}
Ok(())
}
}
pub struct ManagementOnly {
roles: Vec<String>,
}
impl ManagementOnly {
pub fn new<I, S>(roles: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self {
roles: roles.into_iter().map(Into::into).collect(),
}
}
fn is_management(&self, role: &str) -> bool {
self.roles.iter().any(|r| r == role)
}
}
impl Invariant for ManagementOnly {
fn name(&self) -> &str {
"management_only"
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
for c in conferred(engine, proposal) {
let Some(key) = c
.keys
.iter()
.find(|k| engine.catalog().get(k).is_some_and(|d| d.management_only))
else {
continue;
};
let allowed = match proposal.mutation {
Mutation::AssignRole { role, .. } => self.is_management(&role.role),
_ => matches!(&c.grant.subject, SubjectMatcher::Role { id } if self.is_management(id)),
};
if !allowed {
return Err(Violation::new(
self.name(),
format!("{key} só pode ser concedida a papéis de gestão"),
));
}
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Bypass {
pub permission: String,
pub resource: Resource,
}
impl Bypass {
pub fn new(permission: impl Into<String>, resource: Resource) -> Self {
Self {
permission: permission.into(),
resource,
}
}
}
#[derive(Default)]
pub struct NoEscalation {
bypass: Option<Bypass>,
}
impl NoEscalation {
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn bypass(mut self, bypass: Bypass) -> Self {
self.bypass = Some(bypass);
self
}
fn name_of(&self) -> &'static str {
"no_escalation"
}
fn require_held(
&self,
engine: &Engine,
ctx: &Context<'_>,
actor: &Subject,
c: &Conferred<'_>,
carried: Option<&Grant>,
) -> Result<(), Violation> {
for key in &c.keys {
for question in held_questions(engine, key, &c.grant.fields) {
let explanation = engine.explain(ctx, &question.request(actor, key, &c.resource));
if !explanation.decision.is_allowed() {
return Err(Violation::new(
self.name_of(),
match question {
HeldQuestion::Field(field) => {
format!("o ator não tem {key} no campo {field} onde a mudança passaria a valer")
}
HeldQuestion::Whole => format!("o ator não tem {key} onde a mudança passaria a valer"),
},
));
}
let Some(grant) = carried else { continue };
let Some(required) = conditions_behind(&explanation) else {
continue;
};
if !required.iter().any(|r| covers(grant.condition.as_ref(), r)) {
return Err(Violation::new(
self.name_of(),
format!("o ator só tem {key} sob condição: a concessão precisa carregar a mesma condição"),
));
}
}
}
Ok(())
}
}
enum HeldQuestion<'f> {
Whole,
Field(&'f str),
}
impl<'f> HeldQuestion<'f> {
fn request<'a>(&self, actor: &'a Subject, key: &'a str, resource: &'a Resource) -> Request<'a>
where
'f: 'a,
{
let request = Request::new(actor, key, resource);
match self {
HeldQuestion::Whole => request,
HeldQuestion::Field(field) => request.field(field),
}
}
}
fn held_questions<'f>(engine: &Engine, key: &str, fields: &'f [String]) -> Vec<HeldQuestion<'f>> {
if fields.is_empty() {
return vec![HeldQuestion::Whole];
}
let declared = engine.catalog().fields(key);
fields
.iter()
.filter(|f| declared.is_empty() || declared.contains(f))
.map(|f| HeldQuestion::Field(f))
.collect()
}
fn conditions_behind(explanation: &Explanation) -> Option<Vec<&Condition>> {
let mut conditions = Vec::new();
for trace in explanation.candidates().filter(|t| t.grant.effect == Effect::Allow) {
conditions.push(trace.grant.condition.as_ref()?);
}
(!conditions.is_empty()).then_some(conditions)
}
fn covers(candidate: Option<&Condition>, required: &Condition) -> bool {
match candidate {
None => false,
Some(c) if c == required => true,
Some(Condition::All { of }) => of.iter().any(|term| covers(Some(term), required)),
Some(_) => false,
}
}
impl Invariant for NoEscalation {
fn name(&self) -> &str {
self.name_of()
}
fn check(&self, engine: &Engine, proposal: &Proposal<'_>) -> Result<(), Violation> {
let ctx = proposal.before_context();
if let Some(bypass) = &self.bypass
&& engine.check(
&ctx,
&Request::new(proposal.actor, &bypass.permission, &bypass.resource),
)
{
return Ok(());
}
for c in conferred(engine, proposal) {
self.require_held(engine, &ctx, proposal.actor, &c, Some(c.grant))?;
}
for c in unlocked(engine, proposal) {
self.require_held(engine, &ctx, proposal.actor, &c, None)?;
}
Ok(())
}
}