new GatewaySetup variant to reuse the connection

This commit is contained in:
Jędrzej Stuczyński
2023-08-15 14:43:52 +01:00
parent 950919b36c
commit 55f3fc278f
9 changed files with 59 additions and 17 deletions
+1 -1
View File
@@ -174,7 +174,7 @@ pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> {
let details_store =
OnDiskGatewayDetails::new(&config.storage_paths.common_paths.gateway_details);
let init_details = nym_client_core::init::setup_gateway(
&gateway_setup,
gateway_setup,
&key_store,
&details_store,
register_gateway,
+1 -1
View File
@@ -186,7 +186,7 @@ pub(crate) async fn execute(args: &Init) -> Result<(), Socks5ClientError> {
let details_store =
OnDiskGatewayDetails::new(&config.storage_paths.common_paths.gateway_details);
let init_details = nym_client_core::init::setup_gateway(
&gateway_setup,
gateway_setup,
&key_store,
&details_store,
register_gateway,
+1 -1
View File
@@ -89,7 +89,7 @@ async fn setup_gateway(
GatewaySetup::new_fresh(chosen_gateway.clone(), None)
};
setup_gateway_from(&setup, client_store, client_store, false, Some(gateways))
setup_gateway_from(setup, client_store, client_store, false, Some(gateways))
.await
.map_err(Into::into)
}
@@ -455,17 +455,23 @@ where
Ok(mem_store)
}
async fn initialise_keys_and_gateway(&self) -> Result<InitialisationResult, ClientCoreError>
async fn initialise_keys_and_gateway(
setup_method: GatewaySetup,
key_store: &S::KeyStore,
details_store: &S::GatewayDetailsStore,
overwrite_data: bool,
validator_servers: Option<&[Url]>,
) -> Result<InitialisationResult, ClientCoreError>
where
<S::KeyStore as KeyStore>::StorageError: Sync + Send,
<S::GatewayDetailsStore as GatewayDetailsStore>::StorageError: Sync + Send,
{
setup_gateway(
&self.setup_method,
self.client_store.key_store(),
self.client_store.gateway_details_store(),
false,
Some(&self.config.client.nym_api_urls),
setup_method,
key_store,
details_store,
overwrite_data,
validator_servers,
)
.await
}
@@ -481,7 +487,14 @@ where
info!("Starting nym client");
// derive (or load) client keys and gateway configuration
let init_res = self.initialise_keys_and_gateway().await?;
let init_res = Self::initialise_keys_and_gateway(
self.setup_method,
self.client_store.key_store(),
self.client_store.gateway_details_store(),
false,
Some(&self.config.client.nym_api_urls),
)
.await?;
let (reply_storage_backend, credential_store) = self.client_store.into_runtime_stores();
+27 -4
View File
@@ -97,7 +97,6 @@ impl InitialisationDetails {
}
}
#[derive(Debug, Clone)]
pub enum GatewaySetup {
/// The gateway specification MUST BE loaded from the underlying storage.
MustLoad,
@@ -115,6 +114,13 @@ pub enum GatewaySetup {
/// Full gateway configuration
details: PersistedGatewayDetails,
},
ReuseConnection {
/// The authenticated ephemeral client that was created during `init`
authenticated_ephemeral_client: GatewayClient<InitOnly>,
/// Details of this pre-initialised client
details: InitialisationDetails,
},
}
impl From<PersistedGatewayDetails> for GatewaySetup {
@@ -289,7 +295,7 @@ fn ensure_valid_details(
}
pub async fn setup_gateway_from<K, D>(
setup: &GatewaySetup,
setup: GatewaySetup,
key_store: &K,
details_store: &D,
overwrite_data: bool,
@@ -301,6 +307,20 @@ where
K::StorageError: Send + Sync + 'static,
D::StorageError: Send + Sync + 'static,
{
// I don't like how we can't deal with this variant in the match below, but we need to take ownership of internal values.
if let GatewaySetup::ReuseConnection {
authenticated_ephemeral_client,
details,
} = setup
{
// if we have already performed the full setup, forward the details.
// it's up to the caller to ensure persistence
return Ok(InitialisationResult {
details,
authenticated_ephemeral_client: Some(authenticated_ephemeral_client),
});
}
let mut rng = OsRng;
// try load gateway details
@@ -309,7 +329,7 @@ where
// try load keys and decide what to do based on the GatewaySetup
let mut managed_keys = match ManagedKeys::try_load(key_store).await {
Ok(loaded_keys) => {
match setup {
match &setup {
GatewaySetup::MustLoad => {
// get EVERYTHING from the storage
let details = loaded_details?;
@@ -376,6 +396,9 @@ where
return Err(ClientCoreError::ForbiddenKeyOverwrite);
}
}
GatewaySetup::ReuseConnection { .. } => {
unreachable!("the reuse connection variant was already manually covered")
}
}
}
Err(_) => {
@@ -419,7 +442,7 @@ where
}
pub async fn setup_gateway<K, D>(
setup: &GatewaySetup,
setup: GatewaySetup,
key_store: &K,
details_store: &D,
overwrite_data: bool,
@@ -809,6 +809,12 @@ impl GatewayClient<InitOnly, EphemeralCredentialStorage> {
bandwidth_controller: Option<BandwidthController<C, St>>,
shutdown: TaskClient,
) -> GatewayClient<C, St> {
// invariants that can't be broken
// (unless somebody decided to expose some field that wasn't meant to be exposed)
assert!(self.authenticated);
assert!(self.connection.is_available());
assert!(self.shared_key.is_some());
GatewayClient {
authenticated: self.authenticated,
disabled_credentials_mode: self.disabled_credentials_mode,
@@ -183,7 +183,7 @@ pub async fn init_socks5_config(provider_address: String, chosen_gateway_id: Str
let details_store =
OnDiskGatewayDetails::new(&config.storage_paths.common_paths.gateway_details);
let init_details = nym_client_core::init::setup_gateway(
&gateway_setup,
gateway_setup,
&key_store,
&details_store,
register_gateway,
+1 -1
View File
@@ -323,7 +323,7 @@ where
// this will perform necessary key and details load and optional store
let _init_result = nym_client_core::init::setup_gateway(
&gateway_setup,
gateway_setup,
self.storage.key_store(),
self.storage.gateway_details_store(),
!self.config.key_mode.is_keep(),
@@ -149,7 +149,7 @@ pub(crate) async fn execute(args: &Init) -> Result<(), NetworkRequesterError> {
let details_store =
OnDiskGatewayDetails::new(&config.storage_paths.common_paths.gateway_details);
let init_details = nym_client_core::init::setup_gateway(
&gateway_setup,
gateway_setup,
&key_store,
&details_store,
register_gateway,