summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src/migrations/v2.rs
blob: 0ea4cdce9ff1a6982c5fa459456144b1dbda48f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#![allow(clippy::identity_op)]
use super::{Error, Result};
use mullvad_types::settings::SettingsVersion;
use std::time::Duration;

// ======================================================
// Section for vendoring types and values that
// this settings version depend on. See `mod.rs`.

pub const MIN_ROTATION_INTERVAL: Duration = Duration::from_secs(1 * 24 * 60 * 60);
pub const MAX_ROTATION_INTERVAL: Duration = Duration::from_secs(7 * 24 * 60 * 60);

// ======================================================

pub fn migrate(settings: &mut serde_json::Value) -> Result<()> {
    if !version_matches(settings) {
        return Ok(());
    }

    log::info!("Migrating settings format to V3");

    // `show_beta_releases` used to be nullable
    if settings
        .get_mut("show_beta_releases")
        .map(|val| val.is_null())
        .unwrap_or(false)
    {
        settings
            .as_object_mut()
            .ok_or(Error::InvalidSettingsContent)?
            .remove("show_beta_releases");
    }

    let automatic_rotation = || -> Option<u64> {
        settings
            .get("tunnel_options")?
            .get("wireguard")?
            .get("automatic_rotation")
            .map(|ivl| ivl.as_u64())?
    }();

    if let Some(interval) = automatic_rotation {
        let new_ivl = match Duration::from_secs(60 * 60 * interval) {
            ivl if ivl < MIN_ROTATION_INTERVAL => {
                log::warn!("Increasing key rotation interval since it is below minimum");
                MIN_ROTATION_INTERVAL
            }
            ivl if ivl > MAX_ROTATION_INTERVAL => {
                log::warn!("Decreasing key rotation interval since it is above maximum");
                MAX_ROTATION_INTERVAL
            }
            ivl => ivl,
        };

        settings["tunnel_options"]["wireguard"]["rotation_interval"] = serde_json::json!(new_ivl);
        settings["tunnel_options"]["wireguard"]
            .as_object_mut()
            .ok_or(Error::InvalidSettingsContent)?
            .remove("automatic_rotation");
    }

    settings["settings_version"] = serde_json::json!(SettingsVersion::V3);

    Ok(())
}

fn version_matches(settings: &serde_json::Value) -> bool {
    settings
        .get("settings_version")
        .map(|version| version == SettingsVersion::V2 as u64)
        .unwrap_or(false)
}

#[cfg(test)]
mod test {
    use super::{migrate, version_matches};

    const V2_SETTINGS: &str = r#"
{
  "account_token": "1234",
  "relay_settings": {
    "normal": {
      "location": {
        "only": {
          "country": "se"
        }
      },
      "tunnel_protocol": "any",
      "wireguard_constraints": {
        "port": "any"
      },
      "openvpn_constraints": {
        "port": {
          "only": 53
        },
        "protocol": {
          "only": "udp"
        }
      }
    }
  },
  "bridge_settings": {
    "normal": {
      "location": "any"
    }
  },
  "bridge_state": "auto",
  "allow_lan": true,
  "block_when_disconnected": false,
  "auto_connect": false,
  "tunnel_options": {
    "openvpn": {
      "mssfix": null
    },
    "wireguard": {
      "mtu": null,
      "automatic_rotation": 10
    },
    "generic": {
      "enable_ipv6": false
    }
  },
  "show_beta_releases": null,
  "settings_version": 2
}
"#;

    pub const V3_SETTINGS: &str = r#"
{
  "account_token": "1234",
  "relay_settings": {
    "normal": {
      "location": {
        "only": {
          "country": "se"
        }
      },
      "tunnel_protocol": "any",
      "wireguard_constraints": {
        "port": "any"
      },
      "openvpn_constraints": {
        "port": {
          "only": 53
        },
        "protocol": {
          "only": "udp"
        }
      }
    }
  },
  "bridge_settings": {
    "normal": {
      "location": "any"
    }
  },
  "bridge_state": "auto",
  "allow_lan": true,
  "block_when_disconnected": false,
  "auto_connect": false,
  "tunnel_options": {
    "openvpn": {
      "mssfix": null
    },
    "wireguard": {
      "mtu": null,
      "rotation_interval": {
          "secs": 86400,
          "nanos": 0
      }
    },
    "generic": {
      "enable_ipv6": false
    }
  },
  "settings_version": 3
}
"#;

    #[test]
    fn test_v2_migration() {
        let mut old_settings = serde_json::from_str(V2_SETTINGS).unwrap();

        assert!(version_matches(&old_settings));

        migrate(&mut old_settings).unwrap();
        let new_settings: serde_json::Value = serde_json::from_str(V3_SETTINGS).unwrap();

        assert_eq!(&old_settings, &new_settings);
    }
}