11use crate :: opa_client:: { send_request_to_opa, ForwardingError } ;
22use crate :: state:: AppState ;
3+ use log:: { debug, info} ;
34use serde:: { Deserialize , Serialize } ;
45use std:: collections:: HashMap ;
6+ use std:: fmt:: Display ;
57use utoipa:: ToSchema ;
68
79/// Send an allowed query to OPA and get the result
810pub async fn query_allowed (
911 state : & AppState ,
1012 query : & AllowedQuery ,
1113) -> Result < AllowedResult , ForwardingError > {
12- send_request_to_opa :: < AllowedResult , _ > ( state, "/v1/data/permit/root" , query) . await
14+ let result =
15+ send_request_to_opa :: < AllowedResult , _ > ( state, "/v1/data/permit/root" , query) . await ;
16+ if let Ok ( response) = & result {
17+ if state. config . debug . unwrap_or ( false ) {
18+ info ! (
19+ "permit.check(\" {user}\" , \" {action}\" , \" {resource}\" ) -> {result}" ,
20+ user = query. user,
21+ action = query. action,
22+ resource = query. resource,
23+ result = response. allow,
24+ ) ;
25+ debug ! (
26+ "Query: {}\n Result: {}" ,
27+ serde_json:: to_string_pretty( query) . unwrap_or( "Serialization error" . to_string( ) ) ,
28+ serde_json:: to_string_pretty( response) . unwrap_or( "Serialization error" . to_string( ) ) ,
29+ ) ;
30+ }
31+ }
32+ result
1333}
1434
1535#[ derive( Debug , Serialize , Deserialize , ToSchema , Clone , PartialEq ) ]
@@ -30,6 +50,12 @@ pub struct User {
3050 pub attributes : HashMap < String , serde_json:: Value > ,
3151}
3252
53+ impl Display for User {
54+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
55+ write ! ( f, "{}" , self . key)
56+ }
57+ }
58+
3359#[ derive( Debug , Serialize , Deserialize , ToSchema , Clone , PartialEq ) ]
3460pub struct Resource {
3561 /// Type of the resource
@@ -48,6 +74,18 @@ pub struct Resource {
4874 pub context : HashMap < String , serde_json:: Value > ,
4975}
5076
77+ impl Display for Resource {
78+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
79+ if let Some ( key) = & self . key {
80+ write ! ( f, "{}:{}" , self . r#type, key)
81+ } else if let Some ( tenant) = & self . tenant {
82+ write ! ( f, "{}@{}" , self . r#type, tenant)
83+ } else {
84+ write ! ( f, "{}" , self . r#type)
85+ }
86+ }
87+ }
88+
5189/// Authorization query parameters for the allowed endpoint
5290#[ derive( Debug , Serialize , Deserialize , ToSchema , Clone , PartialEq ) ]
5391pub struct AllowedQuery {
0 commit comments