Skip to content

Commit 75902d4

Browse files
authored
Merge pull request #67 from hug-dev/list-clients
Add ListClients and DeleteClient operations
2 parents 72a97b8 + 5a000ef commit 75902d4

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313
documentation = "https://docs.rs/crate/parsec-client"
1414

1515
[dependencies]
16-
parsec-interface = "0.22.0"
16+
parsec-interface = { git = "https://github.com/parallaxsecond/parsec-interface-rs", rev = "c8a59544fac04df347f51d19323f4a0b5bb9580d" }
1717
num = "0.3.0"
1818
log = "0.4.11"
1919
derivative = "2.1.1"

src/core/basic_client.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ use super::operation_client::OperationClient;
55
use crate::auth::Authentication;
66
use crate::error::{ClientErrorKind, Error, Result};
77
use log::{info, warn};
8+
use parsec_interface::operations::delete_client::Operation as DeleteClient;
89
use parsec_interface::operations::list_authenticators::{
910
AuthenticatorInfo, Operation as ListAuthenticators,
1011
};
12+
use parsec_interface::operations::list_clients::Operation as ListClients;
1113
use parsec_interface::operations::list_keys::{KeyInfo, Operation as ListKeys};
1214
use parsec_interface::operations::list_opcodes::Operation as ListOpcodes;
1315
use parsec_interface::operations::list_providers::{Operation as ListProviders, ProviderInfo};
@@ -377,6 +379,39 @@ impl BasicClient {
377379
}
378380
}
379381

382+
/// **[Core Operation, Admin Operation]** Lists all clients currently having
383+
/// data in the service.
384+
pub fn list_clients(&self) -> Result<Vec<String>> {
385+
let res = self.op_client.process_operation(
386+
NativeOperation::ListClients(ListClients {}),
387+
ProviderID::Core,
388+
&self.auth_data,
389+
)?;
390+
if let NativeResult::ListClients(res) = res {
391+
Ok(res.clients)
392+
} else {
393+
// Should really not be reached given the checks we do, but it's not impossible if some
394+
// changes happen in the interface
395+
Err(Error::Client(ClientErrorKind::InvalidServiceResponseType))
396+
}
397+
}
398+
399+
/// **[Core Operation, Admin Operation]** Delete all data a client has in the service.
400+
pub fn delete_client(&self, client: String) -> Result<()> {
401+
let res = self.op_client.process_operation(
402+
NativeOperation::DeleteClient(DeleteClient { client }),
403+
ProviderID::Core,
404+
&self.auth_data,
405+
)?;
406+
if let NativeResult::DeleteClient(_) = res {
407+
Ok(())
408+
} else {
409+
// Should really not be reached given the checks we do, but it's not impossible if some
410+
// changes happen in the interface
411+
Err(Error::Client(ClientErrorKind::InvalidServiceResponseType))
412+
}
413+
}
414+
380415
/// **[Core Operation]** Send a ping request to the service.
381416
///
382417
/// This operation is intended for testing connectivity to the

src/core/testing/core_tests.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,42 @@ fn list_opcodes_test() {
119119
assert!(opcodes.contains(&Opcode::PsaGenerateKey) && opcodes.contains(&Opcode::PsaDestroyKey));
120120
}
121121

122+
#[test]
123+
fn list_clients_test() {
124+
let mut client: TestBasicClient = Default::default();
125+
let mut clients = Vec::new();
126+
clients.push("toto".to_string());
127+
clients.push("tata".to_string());
128+
client.set_mock_read(&get_response_bytes_from_result(NativeResult::ListClients(
129+
operations::list_clients::Result { clients },
130+
)));
131+
let clients = client.list_clients().expect("Failed to retrieve opcodes");
132+
133+
// Check response:
134+
assert_eq!(clients.len(), 2);
135+
assert!(clients.contains(&"toto".to_string()) && clients.contains(&"tata".to_string()));
136+
}
137+
138+
#[test]
139+
fn delete_client_test() {
140+
let mut client: TestBasicClient = Default::default();
141+
client.set_mock_read(&get_response_bytes_from_result(NativeResult::DeleteClient(
142+
operations::delete_client::Result {},
143+
)));
144+
let client_name = String::from("toto");
145+
client
146+
.delete_client(client_name.clone())
147+
.expect("Failed to call destroy key");
148+
149+
// Check request:
150+
let op = get_operation_from_req_bytes(client.get_mock_write());
151+
if let NativeOperation::DeleteClient(op) = op {
152+
assert_eq!(op.client, client_name);
153+
} else {
154+
panic!("Got wrong operation type: {:?}", op);
155+
}
156+
}
157+
122158
#[test]
123159
fn list_keys_test() {
124160
use parsec_interface::operations::psa_key_attributes::{

0 commit comments

Comments
 (0)