Skip to content

Commit

Permalink
Refactor/agency client instance (hyperledger#516)
Browse files Browse the repository at this point in the history
* Inline TryFrom

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Stabilize key rotation integration test

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix compile error

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Try to access agency-client as instance

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Use AgencyClient instance methods

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Split aries-vcx integration tests into multiple files

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Turn internal agency function into impl trait methods

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Remove usage of global agency settings

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Further eliminate usage of global agency client settings

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Eliminate global wallet handle from agency-client

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Cleanups, validate agency-client config, remove unused errors

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Small file reorg in agency client

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Move DownloadedMessage structures to /api module

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix failing tests

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Simplify agency-client error handling

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>

* Fix test

Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas authored Jul 19, 2022
1 parent 08512fb commit 88bf722
Show file tree
Hide file tree
Showing 68 changed files with 1,882 additions and 2,030 deletions.
1 change: 0 additions & 1 deletion agency_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2018"
[features]
test_utils = []
general_test = ["test_utils"]
to_restore = []

[dependencies]
async-trait = "0.1.53"
Expand Down
165 changes: 75 additions & 90 deletions agency_client/src/agency_client.rs
Original file line number Diff line number Diff line change
@@ -1,134 +1,117 @@
use serde_json::Value;
use url::Url;

use crate::agency_settings;
use crate::error::{AgencyClientError, AgencyClientErrorKind, AgencyClientResult};
use crate::{AgencyClientError, AgencyClientErrorKind, validation};
use crate::error::AgencyClientResult;
use crate::configuration::AgencyClientConfig;
use crate::testing::mocking;
use crate::utils::{error_utils, validation};
use crate::utils::error_utils;

#[derive(Default, Deserialize)]
// todo: remove Default
#[derive(Default, Deserialize, Clone)]
pub struct AgencyClient {
wallet_handle: i32,
agency_url: String,
agency_did: String,
agency_pwdid: String,
agency_vk: String,
agent_pwdid: String,
agent_vk: String,
my_pwdid: String,
my_vk: String,
pub agency_url: String,
pub agency_did: String,
pub agency_pwdid: String,
pub agency_vk: String,
pub agent_pwdid: String,
pub agent_vk: String,
pub my_pwdid: String,
pub my_vk: String,
}

pub fn validate_optional_config_val<F, S, E>(val: Option<&String>, err: AgencyClientErrorKind, closure: F) -> AgencyClientResult<()>
where F: Fn(&str) -> Result<S, E> {
if val.is_none() { return Ok(()); }

closure(val.as_ref().ok_or(AgencyClientError::from(AgencyClientErrorKind::InvalidConfiguration))?)
.or(Err(AgencyClientError::from(err)))?;

Ok(())
}

pub fn validate_mandotory_config_val<F, S, E>(val: &str, err: AgencyClientErrorKind, closure: F) -> AgencyClientResult<()>
where F: Fn(&str) -> Result<S, E> {
closure(val)
.or(Err(AgencyClientError::from(err)))?;

Ok(())
}


impl AgencyClient {
pub fn get_wallet_handle(&self) -> AgencyClientResult<i32> { Ok(self.wallet_handle) }
pub fn get_agency_url(&self) -> AgencyClientResult<String> { Ok(self.agency_url.clone()) }
pub fn get_agency_did(&self) -> AgencyClientResult<String> { Ok(self.agency_did.clone()) }
pub fn get_agency_pwdid(&self) -> AgencyClientResult<String> { Ok(self.agency_pwdid.clone()) }
pub fn get_agency_vk(&self) -> AgencyClientResult<String> { Ok(self.agency_vk.clone()) }
pub fn get_agent_pwdid(&self) -> AgencyClientResult<String> { Ok(self.agent_pwdid.clone()) }
pub fn get_agent_vk(&self) -> AgencyClientResult<String> { Ok(self.agent_vk.clone()) }
pub fn get_my_pwdid(&self) -> AgencyClientResult<String> { Ok(self.my_pwdid.clone()) }
pub fn get_my_vk(&self) -> AgencyClientResult<String> { Ok(self.my_vk.clone()) }
pub fn get_wallet_handle(&self) -> i32 { self.wallet_handle }
pub fn get_agency_url_config(&self) -> String { self.agency_url.clone() }
pub fn get_agency_url_full(&self) -> String {
format!("{}/agency/msg", self.agency_url.clone())
}

pub fn get_agency_did(&self) -> String { self.agency_did.clone() }
pub fn get_agency_pwdid(&self) -> String { self.agency_pwdid.clone() }
pub fn get_agency_vk(&self) -> String { self.agency_vk.clone() }

pub fn get_agent_pwdid(&self) -> String { self.agent_pwdid.clone() }
pub fn get_agent_vk(&self) -> String { self.agent_vk.clone() }

pub fn get_my_pwdid(&self) -> String { self.my_pwdid.clone() }
pub fn get_my_vk(&self) -> String { self.my_vk.clone() }

pub fn set_wallet_handle(&mut self, wh: i32) {
self.wallet_handle = wh;
crate::utils::wallet::set_wallet_handle(indy::WalletHandle(wh));
}

pub fn reset_wallet_handle(&mut self) {
self.wallet_handle = indy::INVALID_WALLET_HANDLE.0;
crate::utils::wallet::reset_wallet_handle();
}
pub fn set_agency_url(&mut self, url: &str) {
let url = format!("{}/agency/msg", url);
agency_settings::set_config_value(agency_settings::CONFIG_AGENCY_ENDPOINT, &url);
self.agency_url = url;
self.agency_url = url.to_string();
}
pub fn set_agency_did(&mut self, did: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_AGENCY_DID, did);
self.agency_did = did.to_string();
}
pub fn set_agency_pwdid(&mut self, pwdid: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_REMOTE_TO_SDK_DID, pwdid);
self.agency_pwdid = pwdid.to_string();
}
pub fn set_agency_vk(&mut self, vk: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_AGENCY_VERKEY, vk);
self.agency_vk = vk.to_string();
}
pub fn set_agent_pwdid(&mut self, pwdid: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_REMOTE_TO_SDK_DID, pwdid);
self.agent_pwdid = pwdid.to_string();
}
pub fn set_agent_vk(&mut self, vk: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_REMOTE_TO_SDK_VERKEY, vk);
self.agent_vk = vk.to_string();
}
pub fn set_my_pwdid(&mut self, pwdid: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_SDK_TO_REMOTE_DID, pwdid);
self.my_pwdid = pwdid.to_string();
}
pub fn set_my_vk(&mut self, vk: &str) {
agency_settings::set_config_value(agency_settings::CONFIG_SDK_TO_REMOTE_VERKEY, vk);
self.my_vk = vk.to_string();
}

pub fn enable_test_mode(&self) { mocking::enable_agency_mocks() }
pub fn disable_test_mode(&self) { mocking::disable_agency_mocks() }

// TODO: This should eventually become private
pub fn process_config_string(&mut self, config: &str, validate: bool) -> AgencyClientResult<u32> {
trace!("AgencyClient::process_config_string >>> config {:?}, validate: {:?}", config, validate);

let configuration: Value = serde_json::from_str(config)
.map_err(|err| AgencyClientError::from_msg(AgencyClientErrorKind::InvalidJson, format!("Cannot parse config: {}", err)))?;

if let Value::Object(ref map) = configuration {
for (key, value) in map {
trace!("AgencyClient::process_config_string >>> key {:?}, value {:?} ", key, value);
let value = match value {
Value::String(value_) => value_,
_ => {
warn!("Unexpected config value type for key: {}, value: {:?}", key, value);
continue;
}
};
match key.as_ref() {
agency_settings::CONFIG_AGENCY_ENDPOINT => { self.set_agency_url(&value.to_string()); }
agency_settings::CONFIG_AGENCY_DID => { self.set_agency_did(&value.to_string()); }
agency_settings::CONFIG_AGENCY_VERKEY => { self.set_agency_vk(&value.to_string()); }
agency_settings::CONFIG_REMOTE_TO_SDK_DID => { self.set_agent_pwdid(&value.to_string()); }
agency_settings::CONFIG_REMOTE_TO_SDK_VERKEY => { self.set_agent_vk(&value.to_string()); }
agency_settings::CONFIG_SDK_TO_REMOTE_DID => { self.set_my_pwdid(&value.to_string()); }
agency_settings::CONFIG_SDK_TO_REMOTE_VERKEY => { self.set_my_vk(&value.to_string()); }
agency_settings::CONFIG_ENABLE_TEST_MODE => { self.enable_test_mode(); }
_ => { trace!("AgencyClient::process_config_string >>> ignoring key {}", key); }
}
}
}
if validate { self.validate()?; };
Ok(error_utils::SUCCESS.code_num)
}

pub fn validate(&self) -> AgencyClientResult<u32> {
trace!("AgencyClient::validate >>>");

agency_settings::validate_mandotory_config_val(&self.agency_did, AgencyClientErrorKind::InvalidDid, validation::validate_did)?;
agency_settings::validate_mandotory_config_val(&self.agency_vk, AgencyClientErrorKind::InvalidVerkey, validation::validate_verkey)?;

agency_settings::validate_mandotory_config_val(&self.my_pwdid, AgencyClientErrorKind::InvalidDid, validation::validate_did)?;
agency_settings::validate_mandotory_config_val(&self.my_vk, AgencyClientErrorKind::InvalidVerkey, validation::validate_verkey)?;

agency_settings::validate_mandotory_config_val(&self.agent_pwdid, AgencyClientErrorKind::InvalidDid, validation::validate_did)?;
agency_settings::validate_mandotory_config_val(&self.agent_vk, AgencyClientErrorKind::InvalidVerkey, validation::validate_verkey)?;

agency_settings::validate_mandotory_config_val(&self.agency_url, AgencyClientErrorKind::InvalidUrl, Url::parse)?;

Ok(error_utils::SUCCESS.code_num)
pub fn configure(&mut self, config: &AgencyClientConfig) -> AgencyClientResult<()> {
info!("AgencyClient::configure >>> config {:?}", config);

validate_mandotory_config_val(&config.agency_did, AgencyClientErrorKind::InvalidDid, validation::validate_did)?;
validate_mandotory_config_val(&config.agency_verkey, AgencyClientErrorKind::InvalidVerkey, validation::validate_verkey)?;
validate_mandotory_config_val(&config.sdk_to_remote_did, AgencyClientErrorKind::InvalidDid, validation::validate_did)?;
validate_mandotory_config_val(&config.sdk_to_remote_verkey, AgencyClientErrorKind::InvalidVerkey, validation::validate_verkey)?;
validate_mandotory_config_val(&config.remote_to_sdk_did, AgencyClientErrorKind::InvalidDid, validation::validate_did)?;
validate_mandotory_config_val(&config.remote_to_sdk_verkey, AgencyClientErrorKind::InvalidVerkey, validation::validate_verkey)?;
validate_mandotory_config_val(&config.agency_endpoint, AgencyClientErrorKind::InvalidUrl, Url::parse)?;

self.set_agency_url(&config.agency_endpoint);
self.set_agency_did(&config.agency_did);
self.set_agency_vk(&config.agency_verkey);
self.set_agent_pwdid(&config.remote_to_sdk_did);
self.set_agent_vk(&config.remote_to_sdk_verkey);
self.set_my_pwdid(&config.sdk_to_remote_did);
self.set_my_vk(&config.sdk_to_remote_verkey);

Ok(())
}

// TODO: This should be implemented in the module doing the tests
pub fn set_testing_defaults_agency(&mut self) -> u32 {
pub fn set_testing_defaults_agency(&mut self) {
trace!("set_testing_defaults_agency >>>");

let default_did = "VsKV7grR1BUE29mG2Fm2kX";
Expand All @@ -142,12 +125,14 @@ impl AgencyClient {
self.set_agent_vk(default_verkey);
self.set_my_pwdid(default_did);
self.set_my_vk(default_verkey);
}

agency_settings::set_testing_defaults_agency();

error_utils::SUCCESS.code_num
pub fn new() -> AgencyClientResult<Self> {
let agency_client = Self::default();
Ok(agency_client)
}

// todo: use this in favor of `fn new()`
// pub fn new(config: &str, wallet_handle: i32, validate: bool) -> AgencyClientResult<Self> {
// let mut agency_client = Self::default();
// agency_client.process_config_string(config, wallet_handle, validate)?;
Expand Down
152 changes: 0 additions & 152 deletions agency_client/src/agency_settings.rs

This file was deleted.

Loading

0 comments on commit 88bf722

Please sign in to comment.