f328f3fa9e
* Squashing all the changes initial router started expanding the API initial empty openapi/swagger populated build-info endpoint wip: populating rest of swagger missing swagger data + using closure capture for immutable state running the api as a proper task in gateway 'run' fixing some version/feature clashes refactored routes structures initial host information endpoint expanded on gateway-related endpoints signing host information moved all models to separate crate unified http api client routes unification + node api client new generic cache and refresher nym-api caching node self described information removed old cache type temporarily wired up NymContractCache to NodeDescriptionProvider caching self reported host info clients using self-described gateway information fixed request timeouts for wasm fixed wasm builds post rebase fixes cargo fmt brought in wg routes into nym-node router added ErrorResponse for wireguard routes basic swagger support for wg endpoints turns out swagger can be happy with strongly typed requests output type support for wg routes using concrete error type for nym node request error fixed the registration test landing page configurability increased configurability fixed build and lints of other crates added default user-agent to http-api-client reduced severity of gateway details lookup failure changed default http port from 80 to 8080 nym-api using new default port for queries added health endpoint nym-api trying multiple ports for the client using camelcase for node status corrected health endpoint description restored and revamped 'force_tls' flag to filter all gateways that support the wss protocol fixed 'pub_key' path param in open api schema derived Debug on 'NymNodeDescription' ensuring valid public ips added init and run flags to set hostname and public ips fixed listening address being pushed to public ip fixed the positional local flag logging remote ip address of the request updated helper function to query for described gateways enabled tls in gateway client removed hack-opts from mix fetch additional changes after rebasing against origin/develop * clippy * wasm-related target locking * more clippy, but this time in tests
250 lines
8.0 KiB
Rust
250 lines
8.0 KiB
Rust
// Copyright 2022-2023 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//! Collection of initialization steps used by client implementations
|
|
|
|
use crate::client::base_client::storage::gateway_details::{
|
|
GatewayDetailsStore, PersistedGatewayDetails,
|
|
};
|
|
use crate::client::key_manager::persistence::KeyStore;
|
|
use crate::client::key_manager::ManagedKeys;
|
|
use crate::config::GatewayEndpointConfig;
|
|
use crate::error::ClientCoreError;
|
|
use crate::init::helpers::{
|
|
choose_gateway_by_latency, get_specified_gateway, uniformly_random_gateway,
|
|
};
|
|
use crate::init::types::{
|
|
CustomGatewayDetails, GatewayDetails, GatewaySelectionSpecification, GatewaySetup,
|
|
InitialisationResult,
|
|
};
|
|
use nym_gateway_client::client::InitGatewayClient;
|
|
use nym_topology::gateway;
|
|
use rand::rngs::OsRng;
|
|
use serde::de::DeserializeOwned;
|
|
use serde::Serialize;
|
|
use std::sync::Arc;
|
|
|
|
pub mod helpers;
|
|
pub mod types;
|
|
|
|
// helpers for error wrapping
|
|
async fn _store_gateway_details<T, D>(
|
|
details_store: &D,
|
|
details: &PersistedGatewayDetails<T>,
|
|
) -> Result<(), ClientCoreError>
|
|
where
|
|
D: GatewayDetailsStore<T>,
|
|
D::StorageError: Send + Sync + 'static,
|
|
T: Serialize + Send + Sync,
|
|
{
|
|
details_store
|
|
.store_gateway_details(details)
|
|
.await
|
|
.map_err(|source| ClientCoreError::GatewayDetailsStoreError {
|
|
source: Box::new(source),
|
|
})
|
|
}
|
|
|
|
async fn _load_gateway_details<T, D>(
|
|
details_store: &D,
|
|
) -> Result<PersistedGatewayDetails<T>, ClientCoreError>
|
|
where
|
|
D: GatewayDetailsStore<T>,
|
|
D::StorageError: Send + Sync + 'static,
|
|
T: DeserializeOwned + Send + Sync,
|
|
{
|
|
details_store
|
|
.load_gateway_details()
|
|
.await
|
|
.map_err(|source| ClientCoreError::UnavailableGatewayDetails {
|
|
source: Box::new(source),
|
|
})
|
|
}
|
|
|
|
async fn _load_managed_keys<K>(key_store: &K) -> Result<ManagedKeys, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
K::StorageError: Send + Sync + 'static,
|
|
{
|
|
ManagedKeys::try_load(key_store)
|
|
.await
|
|
.map_err(|source| ClientCoreError::KeyStoreError {
|
|
source: Box::new(source),
|
|
})
|
|
}
|
|
|
|
fn ensure_valid_details<T>(
|
|
details: &PersistedGatewayDetails<T>,
|
|
loaded_keys: &ManagedKeys,
|
|
) -> Result<(), ClientCoreError> {
|
|
details.validate(loaded_keys.gateway_shared_key().as_deref())
|
|
}
|
|
|
|
async fn setup_new_gateway<T, K, D>(
|
|
key_store: &K,
|
|
details_store: &D,
|
|
overwrite_data: bool,
|
|
selection_specification: GatewaySelectionSpecification<T>,
|
|
available_gateways: Vec<gateway::Node>,
|
|
) -> Result<InitialisationResult<T>, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
D: GatewayDetailsStore<T>,
|
|
K::StorageError: Send + Sync + 'static,
|
|
D::StorageError: Send + Sync + 'static,
|
|
T: DeserializeOwned + Serialize + Send + Sync,
|
|
{
|
|
// if we're setting up new gateway, failing to load existing information is fine.
|
|
// as a matter of fact, it's only potentially a problem if we DO succeed
|
|
if _load_gateway_details(details_store).await.is_ok() && !overwrite_data {
|
|
return Err(ClientCoreError::ForbiddenKeyOverwrite);
|
|
}
|
|
if _load_managed_keys(key_store).await.is_ok() && !overwrite_data {
|
|
return Err(ClientCoreError::ForbiddenKeyOverwrite);
|
|
}
|
|
|
|
let mut rng = OsRng;
|
|
let mut new_keys = ManagedKeys::generate_new(&mut rng);
|
|
|
|
let gateway_details = match selection_specification {
|
|
GatewaySelectionSpecification::UniformRemote { must_use_tls } => {
|
|
let gateway = uniformly_random_gateway(&mut rng, &available_gateways, must_use_tls)?;
|
|
GatewayDetails::Configured(GatewayEndpointConfig::from_node(gateway, must_use_tls)?)
|
|
}
|
|
GatewaySelectionSpecification::RemoteByLatency { must_use_tls } => {
|
|
let gateway =
|
|
choose_gateway_by_latency(&mut rng, &available_gateways, must_use_tls).await?;
|
|
GatewayDetails::Configured(GatewayEndpointConfig::from_node(gateway, must_use_tls)?)
|
|
}
|
|
GatewaySelectionSpecification::Specified {
|
|
must_use_tls,
|
|
identity,
|
|
} => {
|
|
let gateway = get_specified_gateway(&identity, &available_gateways, must_use_tls)?;
|
|
GatewayDetails::Configured(GatewayEndpointConfig::from_node(gateway, must_use_tls)?)
|
|
}
|
|
GatewaySelectionSpecification::Custom {
|
|
gateway_identity,
|
|
additional_data,
|
|
} => GatewayDetails::Custom(CustomGatewayDetails::new(gateway_identity, additional_data)),
|
|
};
|
|
|
|
let registration_result = if let GatewayDetails::Configured(gateway_cfg) = &gateway_details {
|
|
// if we're using a 'normal' gateway setup, do register
|
|
let our_identity = new_keys.identity_keypair();
|
|
Some(helpers::register_with_gateway(gateway_cfg, our_identity).await?)
|
|
} else {
|
|
None
|
|
};
|
|
|
|
let maybe_shared_keys = registration_result
|
|
.as_ref()
|
|
.map(|r| Arc::clone(&r.shared_keys));
|
|
|
|
let persisted_details =
|
|
PersistedGatewayDetails::new(gateway_details, maybe_shared_keys.as_deref())?;
|
|
|
|
// persist the keys
|
|
new_keys
|
|
.deal_with_gateway_key(maybe_shared_keys, key_store)
|
|
.await
|
|
.map_err(|source| ClientCoreError::KeyStoreError {
|
|
source: Box::new(source),
|
|
})?;
|
|
|
|
// persist gateway configs
|
|
_store_gateway_details(details_store, &persisted_details).await?;
|
|
|
|
Ok(InitialisationResult {
|
|
gateway_details: persisted_details.into(),
|
|
managed_keys: new_keys,
|
|
authenticated_ephemeral_client: registration_result
|
|
.map(|r| r.authenticated_ephemeral_client),
|
|
})
|
|
}
|
|
|
|
async fn use_loaded_gateway_details<T, K, D>(
|
|
key_store: &K,
|
|
details_store: &D,
|
|
) -> Result<InitialisationResult<T>, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
D: GatewayDetailsStore<T>,
|
|
K::StorageError: Send + Sync + 'static,
|
|
D::StorageError: Send + Sync + 'static,
|
|
T: DeserializeOwned + Send + Sync,
|
|
{
|
|
let loaded_details = _load_gateway_details(details_store).await?;
|
|
let loaded_keys = _load_managed_keys(key_store).await?;
|
|
|
|
ensure_valid_details(&loaded_details, &loaded_keys)?;
|
|
|
|
// no need to persist anything as we got everything from the storage
|
|
Ok(InitialisationResult::new_loaded(
|
|
loaded_details.into(),
|
|
loaded_keys,
|
|
))
|
|
}
|
|
|
|
fn reuse_gateway_connection<T>(
|
|
authenticated_ephemeral_client: InitGatewayClient,
|
|
gateway_details: GatewayDetails<T>,
|
|
managed_keys: ManagedKeys,
|
|
) -> InitialisationResult<T> {
|
|
InitialisationResult {
|
|
gateway_details,
|
|
managed_keys,
|
|
authenticated_ephemeral_client: Some(authenticated_ephemeral_client),
|
|
}
|
|
}
|
|
|
|
pub async fn setup_gateway<T, K, D>(
|
|
setup: GatewaySetup<T>,
|
|
key_store: &K,
|
|
details_store: &D,
|
|
) -> Result<InitialisationResult<T>, ClientCoreError>
|
|
where
|
|
K: KeyStore,
|
|
D: GatewayDetailsStore<T>,
|
|
K::StorageError: Send + Sync + 'static,
|
|
D::StorageError: Send + Sync + 'static,
|
|
T: DeserializeOwned + Serialize + Send + Sync,
|
|
{
|
|
match setup {
|
|
GatewaySetup::MustLoad => use_loaded_gateway_details(key_store, details_store).await,
|
|
GatewaySetup::New {
|
|
specification,
|
|
available_gateways,
|
|
overwrite_data,
|
|
} => {
|
|
setup_new_gateway(
|
|
key_store,
|
|
details_store,
|
|
overwrite_data,
|
|
specification,
|
|
available_gateways,
|
|
)
|
|
.await
|
|
}
|
|
GatewaySetup::ReuseConnection {
|
|
authenticated_ephemeral_client,
|
|
gateway_details,
|
|
managed_keys,
|
|
} => Ok(reuse_gateway_connection(
|
|
authenticated_ephemeral_client,
|
|
gateway_details,
|
|
managed_keys,
|
|
)),
|
|
}
|
|
}
|
|
|
|
pub fn output_to_json<T: Serialize>(init_results: &T, output_file: &str) {
|
|
match std::fs::File::create(output_file) {
|
|
Ok(file) => match serde_json::to_writer_pretty(file, init_results) {
|
|
Ok(_) => println!("Saved: {output_file}"),
|
|
Err(err) => eprintln!("Could not save {output_file}: {err}"),
|
|
},
|
|
Err(err) => eprintln!("Could not save {output_file}: {err}"),
|
|
}
|
|
}
|