diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-18 16:34:43 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-18 16:34:43 -0600 |
| commit | f7d1396c5634e14a7aafdd5c8f6e644aca6e5fb0 (patch) | |
| tree | 161c6b656d3366d64109bc85c637a7a7f6f7e679 | |
| parent | 68558decefd9562a5c8ee3ffa9c197b244e65321 (diff) | |
refactor: remove all the tutorial code
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | Cargo.toml | 7 | ||||
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | build.rs | 5 | ||||
| -rw-r--r-- | mise.toml | 1 | ||||
| -rw-r--r-- | proto/ability.proto | 16 | ||||
| -rw-r--r-- | proto/helloworld.proto | 14 | ||||
| -rw-r--r-- | src/client.rs | 39 | ||||
| -rw-r--r-- | src/server.rs | 57 |
9 files changed, 6 insertions, 145 deletions
@@ -54,7 +54,6 @@ name = "authzd" version = "0.1.0" dependencies = [ "envoy-types", - "prost", "tokio", "tonic", "tonic-build", @@ -4,16 +4,11 @@ version = "0.1.0" edition = "2024" [[bin]] -name = "authzd-server" +name = "authzd" path = "src/server.rs" -[[bin]] -name = "authzd-client" -path = "src/client.rs" - [dependencies] envoy-types = "0.6.0" -prost = "0.13" tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } tonic = "*" @@ -8,19 +8,10 @@ help: clean: @cargo clean -.PHONY: client -client: - @cargo run --bin authzd-client - .PHONY: server server: - @cargo run --bin authzd-server + @cargo run --bin authzd .PHONY: test test: @cargo test - -.PHONY: grpcurl -grpcurl: - @grpcurl -plaintext -import-path ./proto -proto helloworld.proto -d '{"name": "Tonic"}' '[::1]:50051' helloworld.Greeter/SayHello - @grpcurl -plaintext -import-path ./proto -proto ability.proto -d '{"subject": "gid://example/User/1", "permission": "gid://example/Permission/1", "resource": "gid://example/Project/1"}' '[::1]:50051' authz.rpc.Ability/Allowed diff --git a/build.rs b/build.rs deleted file mode 100644 index 3294804..0000000 --- a/build.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() -> Result<(), Box<dyn std::error::Error>> { - tonic_build::compile_protos("proto/helloworld.proto")?; - tonic_build::compile_protos("proto/ability.proto")?; - Ok(()) -} @@ -1,4 +1,3 @@ [tools] cargo = "latest" -grpcurl = "latest" rust = "latest" diff --git a/proto/ability.proto b/proto/ability.proto deleted file mode 100644 index ccef946..0000000 --- a/proto/ability.proto +++ /dev/null @@ -1,16 +0,0 @@ -syntax = "proto3"; -package authz.rpc; - -service Ability { - rpc Allowed (AllowRequest) returns (AllowReply) {} -} - -message AllowRequest { - string subject = 1; - string permission = 2; - string resource = 3; -} - -message AllowReply { - bool result = 1; -} diff --git a/proto/helloworld.proto b/proto/helloworld.proto deleted file mode 100644 index 949b755..0000000 --- a/proto/helloworld.proto +++ /dev/null @@ -1,14 +0,0 @@ -syntax = "proto3"; -package helloworld; - -service Greeter { - rpc SayHello (HelloRequest) returns (HelloReply); -} - -message HelloRequest { - string name = 1; -} - -message HelloReply { - string message = 1; -} diff --git a/src/client.rs b/src/client.rs deleted file mode 100644 index b0cfa42..0000000 --- a/src/client.rs +++ /dev/null @@ -1,39 +0,0 @@ -use authz::AllowRequest; -use authz::ability_client::AbilityClient; -use hello_world::HelloRequest; -use hello_world::greeter_client::GreeterClient; - -pub mod authz { - tonic::include_proto!("authz.rpc"); -} - -pub mod hello_world { - tonic::include_proto!("helloworld"); -} - -#[tokio::main] -async fn main() -> Result<(), Box<dyn std::error::Error>> { - { - let mut client = GreeterClient::connect("http://[::1]:50051").await?; - let request = tonic::Request::new(HelloRequest { - name: "Tonic".into(), - }); - - let response = client.say_hello(request).await?; - println!("RESPONSE={:?}", response); - } - - { - let request = tonic::Request::new(AllowRequest { - subject: "gid://example/User/1".into(), - permission: "gid://example/Permission/1".into(), - resource: "gid://example/Project/1".into(), - }); - - let mut client = AbilityClient::connect("http://[::1]:50051").await?; - let response = client.allowed(request).await?; - println!("RESPONSE={:?}", response); - } - - Ok(()) -} diff --git a/src/server.rs b/src/server.rs index 3b7d55e..f84dc08 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,18 +1,14 @@ -use authz_rpc::ability_server::{Ability, AbilityServer}; -use authz_rpc::{AllowReply, AllowRequest}; use envoy_types::ext_authz::v3::pb::{ Authorization, AuthorizationServer, CheckRequest, CheckResponse, }; use envoy_types::ext_authz::v3::{CheckRequestExt, CheckResponseExt}; -use hello_world::greeter_server::{Greeter, GreeterServer}; -use hello_world::{HelloReply, HelloRequest}; use tonic::{Request, Response, Status, transport::Server}; -#[derive(Default)] -struct MyServer; +#[derive(Debug, Default)] +struct PolicyServer; #[tonic::async_trait] -impl Authorization for MyServer { +impl Authorization for PolicyServer { async fn check( &self, request: Request<CheckRequest>, @@ -35,57 +31,12 @@ impl Authorization for MyServer { } } -pub mod authz_rpc { - tonic::include_proto!("authz.rpc"); -} - -#[derive(Debug, Default)] -pub struct MyAbility {} - -#[tonic::async_trait] -impl Ability for MyAbility { - async fn allowed( - &self, - request: Request<AllowRequest>, - ) -> Result<Response<AllowReply>, Status> { - println!("Got a request: {:?}", request); - - let reply = AllowReply { result: true }; - Ok(Response::new(reply)) - } -} - -pub mod hello_world { - tonic::include_proto!("helloworld"); -} - -#[derive(Debug, Default)] -pub struct MyGreeter {} - -#[tonic::async_trait] -impl Greeter for MyGreeter { - async fn say_hello( - &self, - request: Request<HelloRequest>, - ) -> Result<Response<HelloReply>, Status> { - println!("Got a request: {:?}", request); - - let reply = HelloReply { - message: format!("Hello {}!", request.into_inner().name), - }; - - Ok(Response::new(reply)) - } -} - #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let addr = "[::1]:50051".parse()?; Server::builder() - .add_service(GreeterServer::new(MyGreeter::default())) - .add_service(AbilityServer::new(MyAbility::default())) - .add_service(AuthorizationServer::new(MyServer::default())) + .add_service(AuthorizationServer::new(PolicyServer::default())) .serve(addr) .await?; |
