Add command check-auth
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
mod check_auth;
|
||||
mod create_db;
|
||||
mod create_user;
|
||||
mod drop_db;
|
||||
@@ -10,6 +11,7 @@ mod show_privs;
|
||||
mod show_user;
|
||||
mod unlock_user;
|
||||
|
||||
pub use check_auth::*;
|
||||
pub use create_db::*;
|
||||
pub use create_user::*;
|
||||
pub use drop_db::*;
|
||||
@@ -28,6 +30,10 @@ use crate::core::protocol::{ClientToServerMessageStream, Response};
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
pub enum ClientCommand {
|
||||
/// Check whether you are authorized to manage the specified databases or users.
|
||||
#[command()]
|
||||
CheckAuth(CheckAuthArgs),
|
||||
|
||||
/// Create one or more databases
|
||||
#[command()]
|
||||
CreateDb(CreateDbArgs),
|
||||
@@ -141,6 +147,7 @@ pub async fn handle_command(
|
||||
server_connection: ClientToServerMessageStream,
|
||||
) -> anyhow::Result<()> {
|
||||
match command {
|
||||
ClientCommand::CheckAuth(args) => check_authorization(args, server_connection).await,
|
||||
ClientCommand::CreateDb(args) => create_databases(args, server_connection).await,
|
||||
ClientCommand::DropDb(args) => drop_databases(args, server_connection).await,
|
||||
ClientCommand::ShowDb(args) => show_databases(args, server_connection).await,
|
||||
|
||||
67
src/client/commands/check_auth.rs
Normal file
67
src/client/commands/check_auth.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
use crate::{
|
||||
client::commands::erroneous_server_response,
|
||||
core::{
|
||||
protocol::{
|
||||
ClientToServerMessageStream, Request, Response,
|
||||
print_check_authorization_output_status, print_check_authorization_output_status_json,
|
||||
},
|
||||
types::DbOrUser,
|
||||
},
|
||||
};
|
||||
use clap::Parser;
|
||||
use futures_util::SinkExt;
|
||||
use tokio_stream::StreamExt;
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
pub struct CheckAuthArgs {
|
||||
/// The name of the database(s) or user(s) to check authorization for
|
||||
#[arg(num_args = 1..)]
|
||||
name: Vec<String>,
|
||||
|
||||
/// Assume the names are users, not databases
|
||||
#[arg(short, long)]
|
||||
users: bool,
|
||||
|
||||
/// Print the information as JSON
|
||||
#[arg(short, long)]
|
||||
json: bool,
|
||||
}
|
||||
|
||||
pub async fn check_authorization(
|
||||
args: CheckAuthArgs,
|
||||
mut server_connection: ClientToServerMessageStream,
|
||||
) -> anyhow::Result<()> {
|
||||
if args.name.is_empty() {
|
||||
anyhow::bail!("No database/user names provided");
|
||||
}
|
||||
|
||||
let payload = args
|
||||
.name
|
||||
.into_iter()
|
||||
.map(|name| {
|
||||
if args.users {
|
||||
DbOrUser::User(name.into())
|
||||
} else {
|
||||
DbOrUser::Database(name.into())
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let message = Request::CheckAuthorization(payload);
|
||||
server_connection.send(message).await?;
|
||||
|
||||
let result = match server_connection.next().await {
|
||||
Some(Ok(Response::CheckAuthorization(response))) => response,
|
||||
response => return erroneous_server_response(response),
|
||||
};
|
||||
|
||||
server_connection.send(Request::Exit).await?;
|
||||
|
||||
if args.json {
|
||||
print_check_authorization_output_status_json(&result);
|
||||
} else {
|
||||
print_check_authorization_output_status(&result);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user