Skip to content

Add ListClients and DeleteClient operations #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ documentation = "https://docs.rs/crate/parsec-tool"
ansi_term = "0.12.1"
atty = "0.2.14"
clap = "3.0.0-beta.1"
parsec-client = { git = "https://github.com/parallaxsecond/parsec-client-rust", rev = "3c78e3e9fe7f951a76cb4c346c4a8507d0d4a5c3" }
parsec-client = { git = "https://github.com/parallaxsecond/parsec-client-rust", rev = "75902d454c65e44bd8dfebb492683bc260283876" }
structopt = "0.3.17"
thiserror = "1.0.20"
env_logger = "0.8.2"
Expand Down
54 changes: 54 additions & 0 deletions src/subcommands/delete_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

//! Delete all data a client has in the service (admin operation).

pub use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::ParsecToolSubcommand;
use parsec_client::core::interface::operations::{delete_client, NativeOperation, NativeResult};

use parsec_client::core::interface::requests::ProviderID;
use parsec_client::core::operation_client::OperationClient;
use std::convert::TryFrom;
use structopt::StructOpt;

/// Delete all data a client has in the service (admin operation).
#[derive(Debug, StructOpt)]
#[structopt(name = "delete_client")]
pub struct DeleteClientSubcommand {
#[structopt(short = "c", long = "client")]
client: String,
}

impl TryFrom<&DeleteClientSubcommand> for NativeOperation {
type Error = ParsecToolError;

fn try_from(delete_client: &DeleteClientSubcommand) -> Result<Self, Self::Error> {
// Trivially converted to a `NativeOperation`.
Ok(NativeOperation::DeleteClient(delete_client::Operation {
client: delete_client.client.clone(),
}))
}
}

impl ParsecToolSubcommand<'_> for DeleteClientSubcommand {
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&matches.authentication_data()?,
)?;

match native_result {
NativeResult::DeleteClient(_) => (),
_ => {
return Err(ParsecToolError::UnexpectedNativeResult(native_result));
}
};

success!("Client \"{}\" deleted.", self.client);
Ok(())
}
}
55 changes: 55 additions & 0 deletions src/subcommands/list_clients.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2021 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

//! Lists all clients currently having data in the service (admin operation).

pub use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::ParsecToolSubcommand;
use parsec_client::core::interface::operations::{list_clients, NativeOperation, NativeResult};

use parsec_client::core::interface::requests::ProviderID;
use parsec_client::core::operation_client::OperationClient;
use std::convert::TryFrom;
use structopt::StructOpt;

/// Lists all clients currently having data in the service (admin operation).
#[derive(Debug, StructOpt)]
#[structopt(name = "list_clients")]
pub struct ListClientsSubcommand {}

impl TryFrom<&ListClientsSubcommand> for NativeOperation {
type Error = ParsecToolError;

fn try_from(_list_clients_subcommand: &ListClientsSubcommand) -> Result<Self, Self::Error> {
// Trivially converted to a `NativeOperation`.
Ok(NativeOperation::ListClients(list_clients::Operation {}))
}
}

impl ParsecToolSubcommand<'_> for ListClientsSubcommand {
fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> {
let client = OperationClient::new();
let native_result = client.process_operation(
NativeOperation::try_from(self)?,
ProviderID::Core,
&matches.authentication_data()?,
)?;

if let NativeResult::ListClients(result) = native_result {
if result.clients.is_empty() {
info!("No clients in the service.");
return Ok(());
}
info!("Parsec clients:");
for client in result.clients {
eprint_colored!(Blue, "*");
eprint_colored!(Yellow, " '{}'", client);
eprintln!("");
}
Ok(())
} else {
Err(ParsecToolError::UnexpectedNativeResult(native_result))
}
}
}
13 changes: 12 additions & 1 deletion src/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
//! Subcommand implementations. Interacts with parsec-client-rust.

pub mod common;
pub mod delete_client;
pub mod list_authenticators;
pub mod list_clients;
pub mod list_keys;
pub mod list_opcodes;
pub mod list_providers;
Expand All @@ -18,7 +20,8 @@ pub mod psa_generate_random;
use crate::cli::ParsecToolApp;
use crate::error::ParsecToolError;
use crate::subcommands::{
list_authenticators::ListAuthenticatorsSubcommand, list_keys::ListKeysSubcommand,
delete_client::DeleteClientSubcommand, list_authenticators::ListAuthenticatorsSubcommand,
list_clients::ListClientsSubcommand, list_keys::ListKeysSubcommand,
list_opcodes::ListOpcodesSubcommand, list_providers::ListProvidersSubcommand,
ping::PingSubcommand, psa_destroy_key::PsaDestroyKeySubcommand,
psa_export_key::PsaExportKeySubcommand, psa_export_public_key::PsaExportPublicKeySubcommand,
Expand Down Expand Up @@ -61,6 +64,12 @@ pub enum Subcommand {
/// Lists all keys belonging to the application.
ListKeys(ListKeysSubcommand),

/// Lists all clients currently having data in the service (admin operation).
ListClients(ListClientsSubcommand),

/// Delete all data a client has in the service (admin operation).
DeleteClient(DeleteClientSubcommand),

/// Generates a sequence of random bytes.
PsaGenerateRandom(PsaGenerateRandomSubcommand),

Expand All @@ -85,6 +94,8 @@ impl Subcommand {
Subcommand::ListProviders(cmd) => cmd.run(matches),
Subcommand::ListAuthenticators(cmd) => cmd.run(matches),
Subcommand::ListKeys(cmd) => cmd.run(matches),
Subcommand::ListClients(cmd) => cmd.run(matches),
Subcommand::DeleteClient(cmd) => cmd.run(matches),
Subcommand::ListOpcodes(cmd) => cmd.run(matches),
Subcommand::PsaGenerateRandom(cmd) => cmd.run(matches),
Subcommand::PsaExportPublicKey(cmd) => cmd.run(matches),
Expand Down