summaryrefslogtreecommitdiff
path: root/src/config.rs
blob: b30cfa375041e5832e01cf32d072dec04d4bc25c (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
use crate::passwords::{passwords_filename, PasswordError, Passwords};

use bytesize::MIB;
use log::{error, trace};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

const DEFAULT_CHUNK_SIZE: usize = MIB as usize;
const DEVNULL: &str = "/dev/null";

#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
struct TentativeClientConfig {
    server_url: String,
    verify_tls_cert: Option<bool>,
    chunk_size: Option<usize>,
    roots: Vec<PathBuf>,
    log: Option<PathBuf>,
    encrypt: Option<bool>,
    exclude_cache_tag_directories: Option<bool>,
}

#[derive(Debug, Serialize, Clone)]
pub enum ClientConfig {
    Plain(ClientConfigWithoutPasswords),
    WithPasswords(ClientConfigWithoutPasswords, Passwords),
}

impl ClientConfig {
    pub fn read_without_passwords(filename: &Path) -> Result<Self, ClientConfigError> {
        let config = ClientConfigWithoutPasswords::read_config(filename)?;
        Ok(ClientConfig::Plain(config))
    }

    pub fn read_with_passwords(filename: &Path) -> Result<Self, ClientConfigError> {
        let config = ClientConfigWithoutPasswords::read_config(filename)?;
        if config.encrypt {
            let passwords = Passwords::load(&passwords_filename(filename))
                .map_err(ClientConfigError::PasswordsMissing)?;
            Ok(ClientConfig::WithPasswords(config, passwords))
        } else {
            Ok(ClientConfig::Plain(config))
        }
    }

    pub fn config(&self) -> &ClientConfigWithoutPasswords {
        match self {
            Self::Plain(config) => &config,
            Self::WithPasswords(config, _) => &config,
        }
    }
}

#[derive(Debug, Serialize, Clone)]
pub struct ClientConfigWithoutPasswords {
    pub filename: PathBuf,
    pub server_url: String,
    pub verify_tls_cert: bool,
    pub chunk_size: usize,
    pub roots: Vec<PathBuf>,
    pub log: PathBuf,
    pub encrypt: bool,
    pub exclude_cache_tag_directories: bool,
}

#[derive(Debug, thiserror::Error)]
pub enum ClientConfigError {
    #[error("server_url is empty")]
    ServerUrlIsEmpty,

    #[error("No backup roots in config; at least one is needed")]
    NoBackupRoot,

    #[error("server URL doesn't use https: {0}")]
    NotHttps(String),

    #[error("No passwords are set: you may need to run 'obnam init': {0}")]
    PasswordsMissing(PasswordError),

    #[error(transparent)]
    IoError(#[from] std::io::Error),

    #[error(transparent)]
    SerdeYamlError(#[from] serde_yaml::Error),
}

pub type ClientConfigResult<T> = Result<T, ClientConfigError>;

impl ClientConfigWithoutPasswords {
    pub fn read_config(filename: &Path) -> ClientConfigResult<Self> {
        trace!("read_config: filename={:?}", filename);
        let config = std::fs::read_to_string(filename)?;
        let tentative: TentativeClientConfig = serde_yaml::from_str(&config)?;
        let roots = tentative
            .roots
            .iter()
            .map(|path| expand_tilde(path))
            .collect();
        let log = tentative
            .log
            .map(|path| expand_tilde(&path))
            .unwrap_or_else(|| PathBuf::from(DEVNULL));
        let encrypt = tentative.encrypt.or(Some(false)).unwrap();
        let exclude_cache_tag_directories = tentative.exclude_cache_tag_directories.unwrap_or(true);

        let config = Self {
            chunk_size: tentative.chunk_size.or(Some(DEFAULT_CHUNK_SIZE)).unwrap(),
            encrypt,
            filename: filename.to_path_buf(),
            roots,
            server_url: tentative.server_url,
            verify_tls_cert: tentative.verify_tls_cert.or(Some(false)).unwrap(),
            log,
            exclude_cache_tag_directories,
        };

        config.check()?;
        Ok(config)
    }

    fn check(&self) -> Result<(), ClientConfigError> {
        if self.server_url.is_empty() {
            return Err(ClientConfigError::ServerUrlIsEmpty);
        }
        if !self.server_url.starts_with("https://") {
            return Err(ClientConfigError::NotHttps(self.server_url.to_string()));
        }
        if self.roots.is_empty() {
            return Err(ClientConfigError::NoBackupRoot);
        }
        Ok(())
    }
}

fn expand_tilde(path: &Path) -> PathBuf {
    if path.starts_with("~/") {
        if let Some(home) = std::env::var_os("HOME") {
            let mut expanded = PathBuf::from(home);
            for comp in path.components().skip(1) {
                expanded.push(comp);
            }
            expanded
        } else {
            path.to_path_buf()
        }
    } else {
        path.to_path_buf()
    }
}