#![forbid(unsafe_code)]
mod allow_all;
pub mod cache;
mod simple;
mod source;
pub use allow_all::AllowAllAuthorizer;
pub use cache::AclCache;
pub use simple::SimpleAclAuthorizer;
pub use source::AclSource;
use std::net::SocketAddr;
use crabka_metadata::{AclOperation, ResourceType};
use crabka_security::Principal;
#[derive(Debug, Clone)]
pub struct AuthorizationRequest<'a> {
pub principal: &'a Principal,
pub host: &'a SocketAddr,
pub resource_type: ResourceType,
pub resource_name: &'a str,
pub operation: AclOperation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuthorizationResult {
Allow,
Deny,
}
pub trait Authorizer: Send + Sync + std::fmt::Debug {
fn authorize(
&self,
source: &dyn AclSource,
req: &AuthorizationRequest<'_>,
) -> AuthorizationResult;
}
#[must_use]
pub fn authorize_topics<'a>(
authorizer: &dyn Authorizer,
source: &dyn AclSource,
principal: &Principal,
host: &SocketAddr,
operation: AclOperation,
topic_names: impl IntoIterator<Item = &'a str>,
) -> std::collections::HashMap<&'a str, AuthorizationResult> {
topic_names
.into_iter()
.map(|name| {
let req = AuthorizationRequest {
principal,
host,
resource_type: ResourceType::Topic,
resource_name: name,
operation,
};
(name, authorizer.authorize(source, &req))
})
.collect()
}