Skip to content

Add ListClients and DeleteClient operations #67

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 13, 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"
documentation = "https://docs.rs/crate/parsec-client"

[dependencies]
parsec-interface = "0.22.0"
parsec-interface = { git = "https://github.com/parallaxsecond/parsec-interface-rs", rev = "c8a59544fac04df347f51d19323f4a0b5bb9580d" }
num = "0.3.0"
log = "0.4.11"
derivative = "2.1.1"
Expand Down
35 changes: 35 additions & 0 deletions src/core/basic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use super::operation_client::OperationClient;
use crate::auth::Authentication;
use crate::error::{ClientErrorKind, Error, Result};
use log::{info, warn};
use parsec_interface::operations::delete_client::Operation as DeleteClient;
use parsec_interface::operations::list_authenticators::{
AuthenticatorInfo, Operation as ListAuthenticators,
};
use parsec_interface::operations::list_clients::Operation as ListClients;
use parsec_interface::operations::list_keys::{KeyInfo, Operation as ListKeys};
use parsec_interface::operations::list_opcodes::Operation as ListOpcodes;
use parsec_interface::operations::list_providers::{Operation as ListProviders, ProviderInfo};
Expand Down Expand Up @@ -377,6 +379,39 @@ impl BasicClient {
}
}

/// **[Core Operation, Admin Operation]** Lists all clients currently having
/// data in the service.
pub fn list_clients(&self) -> Result<Vec<String>> {
let res = self.op_client.process_operation(
NativeOperation::ListClients(ListClients {}),
ProviderID::Core,
&self.auth_data,
)?;
if let NativeResult::ListClients(res) = res {
Ok(res.clients)
} else {
// Should really not be reached given the checks we do, but it's not impossible if some
// changes happen in the interface
Err(Error::Client(ClientErrorKind::InvalidServiceResponseType))
}
}

/// **[Core Operation, Admin Operation]** Delete all data a client has in the service.
pub fn delete_client(&self, client: String) -> Result<()> {
let res = self.op_client.process_operation(
NativeOperation::DeleteClient(DeleteClient { client }),
ProviderID::Core,
&self.auth_data,
)?;
if let NativeResult::DeleteClient(_) = res {
Ok(())
} else {
// Should really not be reached given the checks we do, but it's not impossible if some
// changes happen in the interface
Err(Error::Client(ClientErrorKind::InvalidServiceResponseType))
}
}

/// **[Core Operation]** Send a ping request to the service.
///
/// This operation is intended for testing connectivity to the
Expand Down
36 changes: 36 additions & 0 deletions src/core/testing/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,42 @@ fn list_opcodes_test() {
assert!(opcodes.contains(&Opcode::PsaGenerateKey) && opcodes.contains(&Opcode::PsaDestroyKey));
}

#[test]
fn list_clients_test() {
let mut client: TestBasicClient = Default::default();
let mut clients = Vec::new();
clients.push("toto".to_string());
clients.push("tata".to_string());
client.set_mock_read(&get_response_bytes_from_result(NativeResult::ListClients(
operations::list_clients::Result { clients },
)));
let clients = client.list_clients().expect("Failed to retrieve opcodes");

// Check response:
assert_eq!(clients.len(), 2);
assert!(clients.contains(&"toto".to_string()) && clients.contains(&"tata".to_string()));
}

#[test]
fn delete_client_test() {
let mut client: TestBasicClient = Default::default();
client.set_mock_read(&get_response_bytes_from_result(NativeResult::DeleteClient(
operations::delete_client::Result {},
)));
let client_name = String::from("toto");
client
.delete_client(client_name.clone())
.expect("Failed to call destroy key");

// Check request:
let op = get_operation_from_req_bytes(client.get_mock_write());
if let NativeOperation::DeleteClient(op) = op {
assert_eq!(op.client, client_name);
} else {
panic!("Got wrong operation type: {:?}", op);
}
}

#[test]
fn list_keys_test() {
use parsec_interface::operations::psa_key_attributes::{
Expand Down