Skip to content

Commit

Permalink
nym-connect config upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
jstuczyn committed Oct 17, 2023
1 parent 078365c commit 855ae2f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
1 change: 1 addition & 0 deletions nym-connect/desktop/src-tauri/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use tap::TapFallible;
mod old_config_v1_1_13;
mod old_config_v1_1_20;
mod old_config_v1_1_20_2;
mod old_config_v1_1_30;
mod persistence;
mod template;
mod upgrade;
Expand Down
13 changes: 7 additions & 6 deletions nym-connect/desktop/src-tauri/src/config/old_config_v1_1_20_2.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// Copyright 2023 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::config::default_config_filepath;
use crate::config::old_config_v1_1_30::ConfigV1_1_30;
use crate::config::persistence::NymConnectPaths;
use crate::config::{default_config_filepath, Config};
use crate::error::Result;
use nym_bin_common::logging::LoggingSettings;
use nym_client_core::config::disk_persistence::old_v1_1_20_2::CommonClientPathsV1_1_20_2;
use nym_client_core::config::GatewayEndpointConfig;
use nym_config::read_config_from_toml_file;
pub use nym_socks5_client_core::config::old_config_v1_1_20_2::ConfigV1_1_20_2 as CoreConfigV1_1_20_2;
pub use nym_socks5_client_core::config::old_config_v1_1_30::ConfigV1_1_30 as CoreConfigV1_1_30;
use serde::{Deserialize, Serialize};
use std::io;
use std::path::Path;

pub use nym_socks5_client_core::config::old_config_v1_1_20_2::ConfigV1_1_20_2 as CoreConfigV1_1_20_2;

#[derive(Debug, Deserialize, PartialEq, Eq, Serialize, Clone)]
pub struct SocksClientPathsV1_1_20_2 {
#[serde(flatten)]
Expand Down Expand Up @@ -41,10 +42,10 @@ impl ConfigV1_1_20_2 {

// in this upgrade, gateway endpoint configuration was moved out of the config file,
// so its returned to be stored elsewhere.
pub fn upgrade(self) -> Result<(Config, GatewayEndpointConfig)> {
pub fn upgrade(self) -> Result<(ConfigV1_1_30, GatewayEndpointConfig)> {
let gateway_details = self.core.base.client.gateway_endpoint.clone().into();
let config = Config {
core: CoreConfigV1_1_30::from(self.core).into(),
let config = ConfigV1_1_30 {
core: self.core.into(),
storage_paths: NymConnectPaths {
common_paths: self.storage_paths.common_paths.upgrade_default()?,
},
Expand Down
40 changes: 40 additions & 0 deletions nym-connect/desktop/src-tauri/src/config/old_config_v1_1_30.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2023 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::config::persistence::NymConnectPaths;
use crate::config::{default_config_filepath, Config};
use nym_config::read_config_from_toml_file;
use nym_socks5_client_core::config::old_config_v1_1_30::ConfigV1_1_30 as CoreConfigV1_1_30;
use serde::{Deserialize, Serialize};
use std::io;
use std::path::Path;

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ConfigV1_1_30 {
pub core: CoreConfigV1_1_30,

// I'm leaving a landmine here for when the paths actually do change the next time,
// but propagating the change right now (in ALL clients) would be such a hassle...,
// so sorry for the next person looking at it : )
pub storage_paths: NymConnectPaths,
}

impl From<ConfigV1_1_30> for Config {
fn from(value: ConfigV1_1_30) -> Self {
Config {
core: value.core.into(),
storage_paths: value.storage_paths,
}
}
}

impl ConfigV1_1_30 {
pub fn read_from_toml_file<P: AsRef<Path>>(path: P) -> io::Result<Self> {
read_config_from_toml_file(path)
}

pub fn read_from_default_path<P: AsRef<Path>>(id: P) -> io::Result<Self> {
Self::read_from_toml_file(default_config_filepath(id))
}
}
45 changes: 35 additions & 10 deletions nym-connect/desktop/src-tauri/src/config/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2022-2023 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::config::old_config_v1_1_30::ConfigV1_1_30;
use crate::config::persistence::NymConnectPaths;
use crate::{
config::{
old_config_v1_1_13::OldConfigV1_1_13, old_config_v1_1_20::ConfigV1_1_20,
Expand All @@ -18,10 +20,12 @@ use nym_client_core::{
error::ClientCoreError,
};

fn persist_gateway_details(config: &Config, details: GatewayEndpointConfig) -> Result<()> {
let details_store =
OnDiskGatewayDetails::new(&config.storage_paths.common_paths.gateway_details);
let keys_store = OnDiskKeys::new(config.storage_paths.common_paths.keys.clone());
fn persist_gateway_details(
storage_paths: &NymConnectPaths,
details: GatewayEndpointConfig,
) -> Result<()> {
let details_store = OnDiskGatewayDetails::new(&storage_paths.common_paths.gateway_details);
let keys_store = OnDiskKeys::new(storage_paths.common_paths.keys.clone());
let shared_keys = keys_store.ephemeral_load_gateway_keys().map_err(|source| {
BackendError::ClientCoreError {
source: ClientCoreError::KeyStoreError {
Expand Down Expand Up @@ -52,9 +56,10 @@ fn try_upgrade_v1_1_13_config(id: &str) -> Result<bool> {

let updated_step1: ConfigV1_1_20 = old_config.into();
let updated_step2: ConfigV1_1_20_2 = updated_step1.into();
let (updated, gateway_config) = updated_step2.upgrade()?;
persist_gateway_details(&updated, gateway_config)?;
let (updated_step3, gateway_config) = updated_step2.upgrade()?;
persist_gateway_details(&updated_step3.storage_paths, gateway_config)?;

let updated: Config = updated_step3.into();
updated.save_to_default_location()?;
Ok(true)
}
Expand All @@ -72,9 +77,10 @@ fn try_upgrade_v1_1_20_config(id: &str) -> Result<bool> {
info!("It is going to get updated to the current specification.");

let updated_step1: ConfigV1_1_20_2 = old_config.into();
let (updated, gateway_config) = updated_step1.upgrade()?;
persist_gateway_details(&updated, gateway_config)?;
let (updated_step2, gateway_config) = updated_step1.upgrade()?;
persist_gateway_details(&updated_step2.storage_paths, gateway_config)?;

let updated: Config = updated_step2.into();
updated.save_to_default_location()?;
Ok(true)
}
Expand All @@ -89,9 +95,25 @@ fn try_upgrade_v1_1_20_2_config(id: &str) -> Result<bool> {
info!("It seems the client is using <= v1.1.20_2 config template.");
info!("It is going to get updated to the current specification.");

let (updated, gateway_config) = old_config.upgrade()?;
persist_gateway_details(&updated, gateway_config)?;
let (updated_step1, gateway_config) = old_config.upgrade()?;
persist_gateway_details(&updated_step1.storage_paths, gateway_config)?;

let updated: Config = updated_step1.into();
updated.save_to_default_location()?;
Ok(true)
}

fn try_upgrade_v1_1_30_config(id: &str) -> Result<bool> {
// explicitly load it as v1.1.20_2 (which is incompatible with the current one, i.e. +1.1.21)
let Ok(old_config) = ConfigV1_1_30::read_from_default_path(id) else {
// if we failed to load it, there might have been nothing to upgrade
// or maybe it was an even older file. in either way. just ignore it and carry on with our day
return Ok(false);
};
info!("It seems the client is using <= v1.1.30 config template.");
info!("It is going to get updated to the current specification.");

let updated: Config = old_config.into();
updated.save_to_default_location()?;
Ok(true)
}
Expand All @@ -107,6 +129,9 @@ pub fn try_upgrade_config(id: &str) -> Result<()> {
if try_upgrade_v1_1_20_2_config(id)? {
return Ok(());
}
if try_upgrade_v1_1_30_config(id)? {
return Ok(());
}

Ok(())
}

0 comments on commit 855ae2f

Please sign in to comment.