rustic_server 0.4.4

rustic server - a REST server built in rust to use with rustic and restic.
Documentation
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! `RusticServer` Config
//!
//! See instructions in `commands.rs` to specify the path to your
//! application's configuration file and/or command-line options
//! for specifying it.

use std::{
    fs::{self},
    net::SocketAddr,
    path::{Path, PathBuf},
};

use clap::{ArgAction, Args, Parser};
use conflate::Merge;
use serde::{Deserialize, Serialize};
use tracing::info;

use crate::error::{AppResult, ErrorKind};

/// `RusticServer` Configuration
#[derive(Clone, Debug, Deserialize, Serialize, Default, Merge, Parser)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", default)]
pub struct RusticServerConfig {
    /// Server settings
    #[command(flatten)]
    pub server: ConnectionSettings,

    /// Storage settings
    #[command(flatten)]
    pub storage: StorageSettings,

    /// Htpasswd settings
    #[command(flatten)]
    pub auth: HtpasswdSettings,

    /// Acl Settings
    #[command(flatten)]
    pub acl: AclSettings,

    /// Optional TLS Settings
    #[command(flatten)]
    pub tls: TlsSettings,

    /// Optional Logging settings
    #[command(flatten)]
    pub log: LogSettings,
}

/// Overwrite the left value with the right value unconditionally.
#[allow(dead_code)]
fn overwrite_left<T>(left: &mut T, right: T) {
    *left = right;
}

#[derive(Clone, Serialize, Deserialize, Debug, Merge, Parser, Copy)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct ConnectionSettings {
    /// IP address and port to bind to
    #[arg(long, env = "RUSTIC_SERVER_LISTEN")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub listen: Option<SocketAddr>,
}

impl Default for ConnectionSettings {
    fn default() -> Self {
        Self {
            listen: Some(default_socket_address()),
        }
    }
}

pub(crate) fn default_socket_address() -> SocketAddr {
    SocketAddr::from(([127, 0, 0, 1], 8000))
}

#[derive(Clone, Serialize, Deserialize, Debug, Default, Merge, Parser)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct LogSettings {
    /// Optional log level (trace, debug, info, warn, error)
    // We don't want to expose this to the CLI, as we use the global verbose flag there
    #[clap(skip)]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub log_level: Option<String>,

    /// Write HTTP requests in the combined log format to the specified filename
    ///
    /// If provided, the application will write logs to the specified file.
    /// If `None`, logging will be disabled or will use a default logging mechanism.
    #[arg(long = "log", env = "RUSTIC_SERVER_LOG_FILE")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub log_file: Option<PathBuf>,
}

impl LogSettings {
    pub const fn is_disabled(&self) -> bool {
        self.log_file.is_none()
    }
}

#[derive(Clone, Serialize, Deserialize, Debug, Merge, Parser)]
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
pub struct StorageSettings {
    /// Path to the data directory
    ///
    /// If `None`, the default directory will be used.
    ///
    /// # Caution
    ///
    /// By default the server persists backup data in the OS temporary directory
    /// (/tmp/rustic on Linux/BSD and others, in %TEMP%\\rustic in Windows, etc).
    #[arg(long = "path", env = "RUSTIC_SERVER_DATA_DIR")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub data_dir: Option<PathBuf>,

    /// Optional maximum size (quota) of a repository in bytes
    #[arg(long = "max-size", env = "RUSTIC_SERVER_QUOTA")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub quota: Option<usize>,
}

pub(crate) fn default_data_dir() -> PathBuf {
    std::env::temp_dir().join("rustic")
}

impl Default for StorageSettings {
    fn default() -> Self {
        Self {
            data_dir: Some(default_data_dir()),
            quota: None,
        }
    }
}

const fn default_true() -> bool {
    true
}

#[derive(Clone, Serialize, Deserialize, Debug, Merge, Args)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", default)]
#[group(id = "tls")]
pub struct TlsSettings {
    /// Disable TLS support
    // This is a bit of a hack to allow us to set the default value to false
    // and disable TLS support by default.
    #[arg(
        long = "tls",
        action=ArgAction::SetFalse,
        default_value = "true",
        help = "Enable TLS support",
        requires = "tls_key",
        requires = "tls_cert",
        env = "RUSTIC_SERVER_DISABLE_TLS"
    )]
    #[serde(default = "default_true")]
    #[merge(strategy = conflate::bool::overwrite_true)]
    pub disable_tls: bool,

    /// Optional path to the TLS key file
    #[arg(long, requires = "disable_tls", env = "RUSTIC_SERVER_TLS_KEY")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub tls_key: Option<PathBuf>,

    /// Optional path to the TLS certificate file
    #[arg(long, requires = "disable_tls", env = "RUSTIC_SERVER_TLS_CERT")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub tls_cert: Option<PathBuf>,
}

impl TlsSettings {
    pub const fn is_disabled(&self) -> bool {
        self.disable_tls
    }
}

impl Default for TlsSettings {
    fn default() -> Self {
        Self {
            disable_tls: true,
            tls_cert: None,
            tls_key: None,
        }
    }
}

#[derive(Clone, Serialize, Deserialize, Debug, Merge, Default, Parser)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", default)]
#[group(id = "auth")]
pub struct HtpasswdSettings {
    /// Disable .htpasswd authentication
    #[arg(long = "no-auth", env = "RUSTIC_SERVER_DISABLE_AUTH")]
    #[serde(default)]
    #[merge(strategy = conflate::bool::overwrite_false)]
    pub disable_auth: bool,

    /// Optional location of .htpasswd file (default: "<data directory>/.htpasswd")
    #[arg(long, env = "RUSTIC_SERVER_HTPASSWD_FILE")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub htpasswd_file: Option<PathBuf>,
}

impl HtpasswdSettings {
    pub fn htpasswd_file_or_default(&self, data_dir: PathBuf) -> AppResult<PathBuf> {
        let default_file_name = ".htpasswd";
        let path = self.htpasswd_file.clone().unwrap_or_else(|| {
            let mut path = data_dir;
            path.push(default_file_name);
            info!(
                "No htpasswd path specified, using default: `{}`",
                path.display()
            );
            path
        });

        if path
            .canonicalize()
            .map_err(|err| {
                ErrorKind::Io.context(format!(
                    "Does the htpasswd file exist at `{}`? We encountered an error: `{}`",
                    path.display(),
                    err
                ))
            })?
            .exists()
        {
            Ok(path)
        } else {
            Err(ErrorKind::Io
                .context(format!(
                    "Could not find `htpasswd` file at: `{}`",
                    path.display()
                ))
                .into())
        }
    }

    pub const fn is_disabled(&self) -> bool {
        self.disable_auth
    }
}

// This assumes that it makes no sense to have one but not the other
// So we if acl_path is given, we require the auth_path too.
#[derive(Clone, Serialize, Deserialize, Debug, Merge, Parser)]
#[serde(deny_unknown_fields, rename_all = "kebab-case", default)]
#[group(id = "acl")]
pub struct AclSettings {
    /// Disable per-repo ACLs
    #[arg(skip)]
    #[serde(default = "default_true")]
    #[merge(strategy = conflate::bool::overwrite_false)]
    pub disable_acl: bool,

    /// Users can only access their private repositories
    #[arg(long, default_value = "true", env = "RUSTIC_SERVER_PRIVATE_REPOS")]
    #[serde(skip)]
    #[merge(strategy = conflate::bool::overwrite_false)]
    pub private_repos: bool,

    /// Enable append only mode
    #[arg(long, env = "RUSTIC_SERVER_APPEND_ONLY")]
    #[merge(strategy = conflate::bool::overwrite_false)]
    pub append_only: bool,

    /// Full path including file name to read from. Governs per-repo ACLs.
    /// (default: "<data directory>/acl.toml")
    #[arg(long, requires = "private_repos", env = "RUSTIC_SERVER_ACL_PATH")]
    #[serde(skip_serializing_if = "Option::is_none")]
    #[merge(strategy = conflate::option::overwrite_with_some)]
    pub acl_path: Option<PathBuf>,
}

impl AclSettings {
    pub fn acl_file_or_default(&self, data_dir: PathBuf) -> AppResult<PathBuf> {
        let default_file_name = "acl.toml";
        let path = self.acl_path.clone().unwrap_or_else(|| {
            let mut path = data_dir;
            path.push(default_file_name);
            info!("No ACL path specified, using default: `{}`", path.display());
            path
        });

        if path
            .canonicalize()
            .map_err(|err| {
                ErrorKind::Io.context(format!(
                    "Does the {default_file_name} file exist at `{}`? We encountered an error: `{err}`",
                    path.display(),
                ))
            })?
            .exists()
        {
            Ok(path)
        } else {
            Err(ErrorKind::Io
                .context(format!(
                    "Could not find `{default_file_name}` file at: `{}`",
                    path.display()
                ))
                .into())
        }
    }

    pub const fn is_disabled(&self) -> bool {
        self.disable_acl || !self.private_repos
    }
}

impl Default for AclSettings {
    fn default() -> Self {
        Self {
            private_repos: true,
            disable_acl: false,
            append_only: true,
            acl_path: None,
        }
    }
}

impl RusticServerConfig {
    pub fn from_file(pth: &Path) -> AppResult<Self> {
        let s = fs::read_to_string(pth)?;

        let config: Self = toml::from_str(&s).map_err(|err| {
            ErrorKind::Io.context(format!(
                "Could not parse file: {} due to {}",
                pth.to_string_lossy(),
                err
            ))
        })?;

        Ok(config)
    }

    pub fn to_file(&self, pth: &Path) -> AppResult<()> {
        let toml_string = toml::to_string(&self).map_err(|err| {
            ErrorKind::Io.context(format!(
                "Could not serialize configuration to toml due to {}",
                err
            ))
        })?;

        fs::write(pth, toml_string)?;

        Ok(())
    }
}

#[cfg(test)]
mod test {
    use std::path::{Path, PathBuf};

    use anyhow::Result;
    use insta::{assert_debug_snapshot, assert_toml_snapshot};
    use rstest::{fixture, rstest};

    use crate::config::RusticServerConfig;

    #[fixture]
    fn rustic_server_config() -> PathBuf {
        Path::new("tests")
            .join("fixtures")
            .join("test_data")
            .join("rustic_server.toml")
    }

    #[rstest]
    #[ignore = "FIXME: This test is not platform agnostic."]
    fn test_default_config_passes() -> Result<()> {
        let config = RusticServerConfig::default();
        assert_toml_snapshot!(config, {
            ".storage.data_dir" => "[directory]",
        });

        Ok(())
    }

    #[rstest]
    #[ignore = "FIXME: This test is not platform agnostic."]
    fn test_config_parsing_from_file_passes(rustic_server_config: PathBuf) -> Result<()> {
        let config = RusticServerConfig::from_file(&rustic_server_config)?;
        assert_toml_snapshot!(config, {
            ".storage.data_dir" => "[directory]",
        });
        Ok(())
    }

    #[test]
    fn test_optional_explicit_parse_config_passes() -> Result<()> {
        let toml_string = r#"
[server]
listen = "127.0.0.1:8000"

[storage]
data-dir = "./test_data/test_repos/"

[auth]
disable-auth = true

[acl]
disable-acl = true

[tls]
disable-tls = true

[log]
log-level = "info"
"#;

        let config: RusticServerConfig = toml::from_str(toml_string)?;
        assert_debug_snapshot!(config);
        Ok(())
    }

    #[test]
    fn test_optional_implicit_parse_config_passes() -> Result<()> {
        let toml_string = r#"
[server]
listen = "127.0.0.1:8000"

[storage]
data-dir = "./test_data/test_repos/"
"#;

        let config: RusticServerConfig = toml::from_str(toml_string)?;
        assert_debug_snapshot!(config);
        Ok(())
    }

    #[test]
    #[ignore = "FIXME: This test is not platform agnostic."]
    fn test_issue_60_parse_config_passes() -> Result<()> {
        let toml_string = r#"
[acl]
disable-acl = true
append-only = false
"#;

        let config: RusticServerConfig = toml::from_str(toml_string)?;
        assert_debug_snapshot!(config);
        Ok(())
    }
}