diff --git a/compute_tools/src/compute.rs b/compute_tools/src/compute.rs index 1f19ed3644..f5e65a2cec 100644 --- a/compute_tools/src/compute.rs +++ b/compute_tools/src/compute.rs @@ -1387,7 +1387,7 @@ LIMIT 100", "GRANT {} ON SCHEMA {} TO {}", privileges .iter() - .map(|p| p.to_string()) + .map(|p| serde_json::to_string(p).unwrap()) .collect::>() .join(", "), schema_name, diff --git a/compute_tools/src/http/api.rs b/compute_tools/src/http/api.rs index 17b5d16459..2f7b3feef1 100644 --- a/compute_tools/src/http/api.rs +++ b/compute_tools/src/http/api.rs @@ -2,7 +2,6 @@ use std::convert::Infallible; use std::net::IpAddr; use std::net::Ipv6Addr; use std::net::SocketAddr; -use std::str::FromStr; use std::sync::Arc; use std::thread; @@ -10,7 +9,6 @@ use crate::catalog::SchemaDumpError; use crate::catalog::{get_database_schema, get_dbs_and_roles}; use crate::compute::forward_termination_signal; use crate::compute::{ComputeNode, ComputeState, ParsedSpec}; -use crate::privilege::Privilege; use compute_api::requests::{ConfigurationRequest, SetRoleGrantsRequest}; use compute_api::responses::{ ComputeStatus, ComputeStatusResponse, GenericAPIError, SetRoleGrantsResponse, @@ -184,24 +182,10 @@ async fn routes(req: Request, compute: &Arc) -> Response(&request).unwrap(); - let privileges: Result, _> = request - .privileges - .iter() - .map(|p| Privilege::from_str(p.as_str())) - .collect(); - let privileges = match privileges { - Ok(privs) => privs, - Err(_) => { - let msg = format!("Invalid privilege in request: {:?}", &request.privileges); - error!(msg); - return Response::new(Body::from(msg)); - } - }; - let res = compute.set_role_grants( &request.database, &request.schema, - &privileges, + &request.privileges, &request.role, ); match res { diff --git a/libs/compute_api/src/privilege.rs b/libs/compute_api/src/privilege.rs index dc4a768651..bdb8cfa753 100644 --- a/libs/compute_api/src/privilege.rs +++ b/libs/compute_api/src/privilege.rs @@ -1,6 +1,5 @@ -use std::str::FromStr; - -#[derive(Debug)] +#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)] +#[serde(rename_all = "UPPERCASE")] pub enum Privilege { Select, Insert, @@ -15,47 +14,3 @@ pub enum Privilege { Temporary, Execute, } - -impl FromStr for Privilege { - type Err = &'static str; - - fn from_str(s: &str) -> Result { - match s.to_uppercase().as_str() { - "SELECT" => Ok(Privilege::Select), - "INSERT" => Ok(Privilege::Insert), - "UPDATE" => Ok(Privilege::Update), - "DELETE" => Ok(Privilege::Delete), - "TRUNCATE" => Ok(Privilege::Truncate), - "REFERENCES" => Ok(Privilege::References), - "TRIGGER" => Ok(Privilege::Trigger), - "USAGE" => Ok(Privilege::Usage), - "CREATE" => Ok(Privilege::Create), - "CONNECT" => Ok(Privilege::Connect), - "TEMPORARY" => Ok(Privilege::Temporary), - "EXECUTE" => Ok(Privilege::Execute), - _ => Err("Invalid privilege"), - } - } -} - -impl Privilege { - pub fn as_str(&self) -> &'static str { - match self { - Privilege::Select => "SELECT", - Privilege::Insert => "INSERT", - Privilege::Update => "UPDATE", - Privilege::Delete => "DELETE", - Privilege::Truncate => "TRUNCATE", - Privilege::References => "REFERENCES", - Privilege::Trigger => "TRIGGER", - Privilege::Usage => "USAGE", - Privilege::Create => "CREATE", - Privilege::Connect => "CONNECT", - Privilege::Temporary => "TEMPORARY", - Privilege::Execute => "EXECUTE", - } - } - pub fn to_string(&self) -> String { - self.as_str().to_string() - } -} diff --git a/libs/compute_api/src/requests.rs b/libs/compute_api/src/requests.rs index d2e60cc092..5e14ccfeeb 100644 --- a/libs/compute_api/src/requests.rs +++ b/libs/compute_api/src/requests.rs @@ -1,6 +1,6 @@ //! Structs representing the JSON formats used in the compute_ctl's HTTP API. -use crate::spec::ComputeSpec; +use crate::{privilege::Privilege, spec::ComputeSpec}; use serde::Deserialize; /// Request of the /configure API @@ -17,6 +17,6 @@ pub struct ConfigurationRequest { pub struct SetRoleGrantsRequest { pub database: String, pub schema: String, - pub privileges: Vec, + pub privileges: Vec, pub role: String, } diff --git a/libs/compute_api/src/responses.rs b/libs/compute_api/src/responses.rs index ef21233f87..18ffcd5071 100644 --- a/libs/compute_api/src/responses.rs +++ b/libs/compute_api/src/responses.rs @@ -6,7 +6,10 @@ use std::fmt::Display; use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize, Serializer}; -use crate::spec::{ComputeSpec, Database, Role}; +use crate::{ + privilege::Privilege, + spec::{ComputeSpec, Database, Role}, +}; #[derive(Serialize, Debug, Deserialize)] pub struct GenericAPIError { @@ -173,6 +176,6 @@ pub struct InstalledExtensions { pub struct SetRoleGrantsResponse { pub database: String, pub schema: String, - pub privileges: Vec, + pub privileges: Vec, pub role: String, }