From 855ae2fe783901e1fb68e34c9b3426b90121a5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 17 Oct 2023 11:50:08 +0100 Subject: [PATCH] nym-connect config upgrades --- .../desktop/src-tauri/src/config/mod.rs | 1 + .../src/config/old_config_v1_1_20_2.rs | 13 +++--- .../src/config/old_config_v1_1_30.rs | 40 +++++++++++++++++ .../desktop/src-tauri/src/config/upgrade.rs | 45 ++++++++++++++----- 4 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 nym-connect/desktop/src-tauri/src/config/old_config_v1_1_30.rs diff --git a/nym-connect/desktop/src-tauri/src/config/mod.rs b/nym-connect/desktop/src-tauri/src/config/mod.rs index c62e48ce26..3426cf36b2 100644 --- a/nym-connect/desktop/src-tauri/src/config/mod.rs +++ b/nym-connect/desktop/src-tauri/src/config/mod.rs @@ -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; diff --git a/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_20_2.rs b/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_20_2.rs index f4effd9e23..d87f41ae0f 100644 --- a/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_20_2.rs +++ b/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_20_2.rs @@ -1,19 +1,20 @@ // Copyright 2023 - Nym Technologies SA // 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)] @@ -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()?, }, diff --git a/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_30.rs b/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_30.rs new file mode 100644 index 0000000000..a18ae5831c --- /dev/null +++ b/nym-connect/desktop/src-tauri/src/config/old_config_v1_1_30.rs @@ -0,0 +1,40 @@ +// Copyright 2023 - Nym Technologies SA +// 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 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>(path: P) -> io::Result { + read_config_from_toml_file(path) + } + + pub fn read_from_default_path>(id: P) -> io::Result { + Self::read_from_toml_file(default_config_filepath(id)) + } +} diff --git a/nym-connect/desktop/src-tauri/src/config/upgrade.rs b/nym-connect/desktop/src-tauri/src/config/upgrade.rs index bdaf138a00..5ec509e733 100644 --- a/nym-connect/desktop/src-tauri/src/config/upgrade.rs +++ b/nym-connect/desktop/src-tauri/src/config/upgrade.rs @@ -1,6 +1,8 @@ // Copyright 2022-2023 - Nym Technologies SA // 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, @@ -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 { @@ -52,9 +56,10 @@ fn try_upgrade_v1_1_13_config(id: &str) -> Result { 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) } @@ -72,9 +77,10 @@ fn try_upgrade_v1_1_20_config(id: &str) -> Result { 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) } @@ -89,9 +95,25 @@ fn try_upgrade_v1_1_20_2_config(id: &str) -> Result { 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 { + // 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) } @@ -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(()) }