mirror of
https://github.com/neondatabase/neon.git
synced 2026-01-14 17:02:56 +00:00
Use serde to serialize/deserialize Privilege instead of manual
This commit is contained in:
@@ -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::<Vec<String>>()
|
||||
.join(", "),
|
||||
schema_name,
|
||||
|
||||
@@ -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<Body>, compute: &Arc<ComputeNode>) -> Response<Body
|
||||
let request = hyper::body::to_bytes(req.into_body()).await.unwrap();
|
||||
let request = serde_json::from_slice::<SetRoleGrantsRequest>(&request).unwrap();
|
||||
|
||||
let privileges: Result<Vec<Privilege>, _> = 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 {
|
||||
|
||||
@@ -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<Self, Self::Err> {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String>,
|
||||
pub privileges: Vec<Privilege>,
|
||||
pub role: String,
|
||||
}
|
||||
|
||||
@@ -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<String>,
|
||||
pub privileges: Vec<Privilege>,
|
||||
pub role: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user