diff options
| author | David Lönnhager <david.l@mullvad.net> | 2023-02-13 09:39:50 +0100 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-02-13 14:45:33 +0100 |
| commit | 826ddba5b16879b25c9e69fd670256d8f72b47fe (patch) | |
| tree | b8aa0d0f365363196e141025e427d202c60039c1 | |
| parent | 5a5cbe51e108d935ebc4668ea0cc925b8a0a2b86 (diff) | |
| download | mullvadvpn-826ddba5b16879b25c9e69fd670256d8f72b47fe.tar.xz mullvadvpn-826ddba5b16879b25c9e69fd670256d8f72b47fe.zip | |
Rename settings error variants
| -rw-r--r-- | mullvad-daemon/src/migrations/mod.rs | 12 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v2.rs | 4 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v3.rs | 2 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v4.rs | 22 | ||||
| -rw-r--r-- | mullvad-daemon/src/migrations/v5.rs | 11 |
5 files changed, 28 insertions, 23 deletions
diff --git a/mullvad-daemon/src/migrations/mod.rs b/mullvad-daemon/src/migrations/mod.rs index 2987b36dfe..c6bfc5babe 100644 --- a/mullvad-daemon/src/migrations/mod.rs +++ b/mullvad-daemon/src/migrations/mod.rs @@ -59,11 +59,11 @@ pub enum Error { #[error(display = "Failed to read the settings")] Read(#[error(source)] io::Error), - #[error(display = "Malformed settings")] - Parse(#[error(source)] serde_json::Error), + #[error(display = "Failed to deserialize settings")] + Deserialize(#[error(source)] serde_json::Error), - #[error(display = "Unable to read any version of the settings")] - NoMatchingVersion, + #[error(display = "Unexpected settings format")] + InvalidSettingsContent, #[error(display = "Unable to serialize settings to JSON")] Serialize(#[error(source)] serde_json::Error), @@ -132,10 +132,10 @@ pub(crate) async fn migrate_all( let settings_bytes = fs::read(&path).await.map_err(Error::Read)?; let mut settings: serde_json::Value = - serde_json::from_reader(&settings_bytes[..]).map_err(Error::Parse)?; + serde_json::from_reader(&settings_bytes[..]).map_err(Error::Deserialize)?; if !settings.is_object() { - return Err(Error::NoMatchingVersion); + return Err(Error::InvalidSettingsContent); } let old_settings = settings.clone(); diff --git a/mullvad-daemon/src/migrations/v2.rs b/mullvad-daemon/src/migrations/v2.rs index d4acbf77a7..6676250065 100644 --- a/mullvad-daemon/src/migrations/v2.rs +++ b/mullvad-daemon/src/migrations/v2.rs @@ -27,7 +27,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { { settings .as_object_mut() - .ok_or(Error::NoMatchingVersion)? + .ok_or(Error::InvalidSettingsContent)? .remove("show_beta_releases"); } @@ -55,7 +55,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { settings["tunnel_options"]["wireguard"]["rotation_interval"] = serde_json::json!(new_ivl); settings["tunnel_options"]["wireguard"] .as_object_mut() - .ok_or(Error::NoMatchingVersion)? + .ok_or(Error::InvalidSettingsContent)? .remove("automatic_rotation"); } diff --git a/mullvad-daemon/src/migrations/v3.rs b/mullvad-daemon/src/migrations/v3.rs index 0f746a07d6..887a45976d 100644 --- a/mullvad-daemon/src/migrations/v3.rs +++ b/mullvad-daemon/src/migrations/v3.rs @@ -61,7 +61,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { DnsState::Default }; let addresses = if let Some(addrs) = options.get("addresses") { - serde_json::from_value(addrs.clone()).map_err(Error::Parse)? + serde_json::from_value(addrs.clone()).map_err(|_| Error::InvalidSettingsContent)? } else { vec![] }; diff --git a/mullvad-daemon/src/migrations/v4.rs b/mullvad-daemon/src/migrations/v4.rs index 77b72bcb3e..1c30c7d220 100644 --- a/mullvad-daemon/src/migrations/v4.rs +++ b/mullvad-daemon/src/migrations/v4.rs @@ -43,7 +43,8 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { if let Some(constraints) = wireguard_constraints { let (port, protocol): (Constraint<u16>, TransportProtocol) = if let Some(port) = constraints.get("port") { - let port_constraint = serde_json::from_value(port.clone()).map_err(Error::Parse)?; + let port_constraint = serde_json::from_value(port.clone()) + .map_err(|_| Error::InvalidSettingsContent)?; match port_constraint { Constraint::Any => (Constraint::Any, TransportProtocol::Udp), Constraint::Only(port) => (Constraint::Only(port), wg_protocol_from_port(port)), @@ -63,7 +64,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { settings["relay_settings"]["normal"]["wireguard_constraints"] .as_object_mut() - .ok_or(Error::NoMatchingVersion)? + .ok_or(Error::InvalidSettingsContent)? .remove("protocol"); } @@ -76,16 +77,17 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { if let Some(constraints) = openvpn_constraints { let port: Constraint<u16> = if let Some(port) = constraints.get("port") { - serde_json::from_value(port.clone()).map_err(Error::Parse)? + serde_json::from_value(port.clone()).map_err(|_| Error::InvalidSettingsContent)? + } else { + Constraint::Any + }; + let transport_constraint: Constraint<TransportProtocol> = if let Some(protocol) = + constraints.get("protocol") + { + serde_json::from_value(protocol.clone()).map_err(|_| Error::InvalidSettingsContent)? } else { Constraint::Any }; - let transport_constraint: Constraint<TransportProtocol> = - if let Some(protocol) = constraints.get("protocol") { - serde_json::from_value(protocol.clone()).map_err(Error::Parse)? - } else { - Constraint::Any - }; let port = match (port, transport_constraint) { (Constraint::Only(port), Constraint::Any) => Constraint::Only(TransportPort { @@ -102,7 +104,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<()> { serde_json::json!(port); settings["relay_settings"]["normal"]["openvpn_constraints"] .as_object_mut() - .ok_or(Error::NoMatchingVersion)? + .ok_or(Error::InvalidSettingsContent)? .remove("protocol"); } diff --git a/mullvad-daemon/src/migrations/v5.rs b/mullvad-daemon/src/migrations/v5.rs index 1e0a4a8724..2c2942cd4e 100644 --- a/mullvad-daemon/src/migrations/v5.rs +++ b/mullvad-daemon/src/migrations/v5.rs @@ -79,7 +79,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<Option<MigrationData> // "Null" is no longer valid. It is not an option. wireguard_constraints .as_object_mut() - .ok_or(Error::NoMatchingVersion)? + .ok_or(Error::InvalidSettingsContent)? .remove("entry_location"); } else { wireguard_constraints["use_multihop"] = serde_json::json!(true); @@ -95,7 +95,7 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<Option<MigrationData> // if let Some(port) = wireguard_constraints.get("port") { let port_constraint: Constraint<TransportPort> = - serde_json::from_value(port.clone()).map_err(Error::Parse)?; + serde_json::from_value(port.clone()).map_err(|_| Error::InvalidSettingsContent)?; if let Some(transport_port) = port_constraint.option() { let (port, obfuscation_settings) = match transport_port.protocol { TransportProtocol::Udp => (serde_json::json!(transport_port.port), None), @@ -116,7 +116,8 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<Option<MigrationData> let migration_data = if let Some(token) = settings.get("account_token").filter(|t| !t.is_null()) { - let token: AccountToken = serde_json::from_value(token.clone()).map_err(Error::Parse)?; + let token: AccountToken = + serde_json::from_value(token.clone()).map_err(|_| Error::InvalidSettingsContent)?; let migration_data = if let Some(wg_data) = settings.get("wireguard").filter(|wg| !wg.is_null()) { Some(MigrationData { @@ -130,7 +131,9 @@ pub fn migrate(settings: &mut serde_json::Value) -> Result<Option<MigrationData> }) }; - let settings_map = settings.as_object_mut().ok_or(Error::NoMatchingVersion)?; + let settings_map = settings + .as_object_mut() + .ok_or(Error::InvalidSettingsContent)?; settings_map.remove("account_token"); settings_map.remove("wireguard"); |
