From 5a000efd5f3b04e17e0fad6a6175ce75fe976570 Mon Sep 17 00:00:00 2001 From: Hugues de Valon Date: Wed, 13 Jan 2021 11:13:30 +0000 Subject: [PATCH] Add ListClients and DeleteClient operations Signed-off-by: Hugues de Valon --- Cargo.toml | 2 +- src/core/basic_client.rs | 35 +++++++++++++++++++++++++++++++++ src/core/testing/core_tests.rs | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 28183df..f593bdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/core/basic_client.rs b/src/core/basic_client.rs index f5f94e1..f36ac58 100644 --- a/src/core/basic_client.rs +++ b/src/core/basic_client.rs @@ -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}; @@ -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> { + 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 diff --git a/src/core/testing/core_tests.rs b/src/core/testing/core_tests.rs index 825317c..333d187 100644 --- a/src/core/testing/core_tests.rs +++ b/src/core/testing/core_tests.rs @@ -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::{