summaryrefslogtreecommitdiff
path: root/src/authorization/server.rs
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-06-27 16:45:17 -0600
committermo khan <mo@mokhan.ca>2025-06-27 16:45:17 -0600
commitcce3e0f170dfacb6b626a8777255c3183c5c5eb3 (patch)
tree788d88c028001d435b7f03685f4cf675a2fdacc0 /src/authorization/server.rs
parent7bda8947c80fc507b722f321977522bd50377c17 (diff)
refactor: extract authorization::Server type
Diffstat (limited to 'src/authorization/server.rs')
-rw-r--r--src/authorization/server.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/authorization/server.rs b/src/authorization/server.rs
new file mode 100644
index 00000000..f11d0465
--- /dev/null
+++ b/src/authorization/server.rs
@@ -0,0 +1,41 @@
+use super::cedar_authorizer::CedarAuthorizer;
+use super::check_service::CheckService;
+use envoy_types::ext_authz::v3::pb::AuthorizationServer;
+use std::sync::Arc;
+
+pub fn create_router() -> Result<tonic::transport::server::Router, Box<dyn std::error::Error>> {
+ let (_health_reporter, health_service) = tonic_health::server::health_reporter();
+ let authorizer = Arc::new(CedarAuthorizer::default());
+ let check_service = CheckService::new(authorizer);
+ let server = tonic::transport::Server::builder()
+ .add_service(AuthorizationServer::new(check_service))
+ .add_service(health_service)
+ .add_service(
+ tonic_reflection::server::Builder::configure()
+ .register_encoded_file_descriptor_set(tonic_health::pb::FILE_DESCRIPTOR_SET)
+ .build_v1()
+ .unwrap(),
+ );
+ Ok(server)
+}
+
+pub struct Server {
+ router: tonic::transport::server::Router,
+}
+
+impl Server {
+ pub fn new() -> Result<Server, Box<dyn std::error::Error>> {
+ let router = create_router()?;
+ Ok(Server { router: router })
+ }
+
+ pub async fn serve(self, addr: std::net::SocketAddr) -> Result<(), tonic::transport::Error> {
+ self.router.serve(addr).await
+ }
+}
+
+impl Default for Server {
+ fn default() -> Self {
+ Self::new().unwrap()
+ }
+}