diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-27 13:24:40 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-27 13:24:40 -0600 |
| commit | 84420606035fd62bbdcacb6231b9181f197d068f (patch) | |
| tree | 0dbbe29a92d090900f30c089181fa1f27f1318b5 /src | |
| parent | e970d1e29aa9a4e1a4ac6419079928b803536825 (diff) | |
refactor: extract create_server function to test it directly
Diffstat (limited to 'src')
| -rw-r--r-- | src/authorization/cedar_authorizer.rs | 2 | ||||
| -rw-r--r-- | src/lib.rs | 20 | ||||
| -rw-r--r-- | src/main.rs | 21 |
3 files changed, 23 insertions, 20 deletions
diff --git a/src/authorization/cedar_authorizer.rs b/src/authorization/cedar_authorizer.rs index c2c594fe..658de7a6 100644 --- a/src/authorization/cedar_authorizer.rs +++ b/src/authorization/cedar_authorizer.rs @@ -53,7 +53,7 @@ impl CedarAuthorizer { } impl Default for CedarAuthorizer { fn default() -> Self { - Self::new_from(fs::canonicalize("/etc/authzd").unwrap().as_path()) + Self::new_from(std::path::Path::new("/etc/authzd")) } } @@ -1,2 +1,22 @@ pub mod authorization; pub use authorization::{Authorizer, CedarAuthorizer, CheckService}; + +use envoy_types::ext_authz::v3::pb::AuthorizationServer; +use std::sync::Arc; +use tonic::transport::Server; + +pub fn create_server() -> Result<tonic::transport::server::Router, Box<dyn std::error::Error>> { + let (_health_reporter, health_service) = tonic_health::server::health_reporter(); + let authorizer = Arc::new(authorization::CedarAuthorizer::default()); + let check_service = authorization::CheckService::new(authorizer); + let server = 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) +} diff --git a/src/main.rs b/src/main.rs index 1a3ff00c..13d313d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,14 @@ -pub mod authorization; +use authzd::create_server; #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { - use envoy_types::ext_authz::v3::pb::AuthorizationServer; - use std::sync::Arc; - use tonic::transport::Server; - tracing_subscriber::fmt().json().init(); let addr = std::env::var("BIND_ADDR") .unwrap_or_else(|_| "[::1]:50051".to_string()) .parse()?; - let (_health_reporter, health_service) = tonic_health::server::health_reporter(); - - let authorizer = Arc::new(authorization::CedarAuthorizer::default()); - let check_service = authorization::CheckService::new(authorizer); - - let server = 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(), - ); + let server = create_server()?; log::info!("Listening on... {addr}"); server.serve(addr).await?; |
