summaryrefslogtreecommitdiff
path: root/src/spec.rs
blob: 5f5a28e17ac37122286f2a5fe7019d7180006d48 (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
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
//! Virtual machine specification.

use crate::config::Configuration;
use crate::util::{check_network_names, expand_tilde};

use log::debug;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct OneVmInputSpecification {
    #[serde(default)]
    pub ssh_key_files: Option<Vec<PathBuf>>,

    pub rsa_host_key: Option<String>,
    pub rsa_host_cert: Option<String>,
    pub dsa_host_key: Option<String>,
    pub dsa_host_cert: Option<String>,
    pub ecdsa_host_key: Option<String>,
    pub ecdsa_host_cert: Option<String>,
    pub ed25519_host_key: Option<String>,
    pub ed25519_host_cert: Option<String>,

    pub base: Option<PathBuf>,
    pub image: Option<PathBuf>,
    pub image_size_gib: Option<u64>,
    pub memory_mib: Option<u64>,
    pub cpus: Option<u64>,
    pub generate_host_certificate: Option<bool>,
    pub autostart: Option<bool>,
    pub networks: Option<Vec<String>>,
    pub ca_key: Option<PathBuf>,
    pub user_ca_pubkey: Option<PathBuf>,
    pub allow_authorized_keys: Option<bool>,
}

impl OneVmInputSpecification {
    fn ssh_key_files(&self, config: &Configuration, name: &str) -> Option<Vec<PathBuf>> {
        if let Ok(x) = get(
            &self.ssh_key_files,
            &config.authorized_keys,
            SpecificationError::NoAuthorizedKeys(name.to_string()),
        ) {
            Some(x)
        } else {
            None
        }
    }

    fn user_ca_pubkey(&self, config: &Configuration, name: &str) -> Option<PathBuf> {
        if let Ok(x) = get(
            &self.user_ca_pubkey,
            &config.user_ca_pubkey,
            SpecificationError::NoAuthorizedKeys(name.to_string()),
        ) {
            Some(x)
        } else {
            None
        }
    }

    fn allow_authorized_keys(&self, config: &Configuration) -> bool {
        if let Ok(x) = get(
            &self.allow_authorized_keys,
            &config.default_allow_authorized_keys,
            SpecificationError::NoAuthorizedKeys("".to_string()),
        ) {
            x
        } else {
            true
        }
    }

    fn base_image(
        &self,
        config: &Configuration,
        name: &str,
    ) -> Result<PathBuf, SpecificationError> {
        get(
            &self.base,
            &config.default_base_image,
            SpecificationError::NoBaseImage(name.to_string()),
        )
    }

    fn image(&self, config: &Configuration, name: &str) -> Result<PathBuf, SpecificationError> {
        let default_image = &config
            .image_directory
            .as_ref()
            .map(|dirname| dirname.join(format!("{}.qcow2", name)));

        get(
            &self.image,
            default_image,
            SpecificationError::NoImage(name.to_string()),
        )
    }

    fn image_size_gib(
        &self,
        config: &Configuration,
        name: &str,
    ) -> Result<u64, SpecificationError> {
        get(
            &self.image_size_gib,
            &config.default_image_gib,
            SpecificationError::NoBaseImage(name.to_string()),
        )
    }

    fn memory_mib(&self, config: &Configuration, name: &str) -> Result<u64, SpecificationError> {
        get(
            &self.memory_mib,
            &config.default_memory_mib,
            SpecificationError::NoBaseImage(name.to_string()),
        )
    }

    fn cpus(&self, config: &Configuration, name: &str) -> Result<u64, SpecificationError> {
        get(
            &self.cpus,
            &config.default_cpus,
            SpecificationError::NoBaseImage(name.to_string()),
        )
    }

    fn autostart(&self, config: &Configuration) -> bool {
        if let Some(x) = self.autostart {
            x
        } else if let Some(x) = config.default_autostart {
            x
        } else {
            false
        }
    }

    fn networks(&self, config: &Configuration) -> Vec<String> {
        if let Some(ref x) = self.networks {
            x.clone()
        } else if let Some(ref x) = config.default_networks {
            x.clone()
        } else {
            vec!["default".to_string()]
        }
    }
}

fn get<'a, T>(
    input: &'a Option<T>,
    default: &'a Option<T>,
    error: SpecificationError,
) -> Result<T, SpecificationError>
where
    T: Clone,
{
    if let Some(input) = input {
        Ok((*input).clone())
    } else if let Some(default) = default {
        Ok((*default).clone())
    } else {
        Err(error)
    }
}

/// Effective virtual machine specification.
///
/// This is the specification as read from the input file, with the
/// defaults from the configuration file already applied.
#[derive(Debug, Serialize)]
pub struct Specification {
    /// Name of new virtual machine to create.
    pub name: String,

    /// SSH public keys to install in the default user's `authorized_keys` file.
    pub ssh_keys: Vec<String>,

    /// RSA host key to install in new VM.
    pub rsa_host_key: Option<String>,

    /// RSA host certificate.
    pub rsa_host_cert: Option<String>,

    /// DSA host key to install in new VM.
    pub dsa_host_key: Option<String>,

    /// DSA host certificate.
    pub dsa_host_cert: Option<String>,

    /// ECDSA host key to install in new VM.
    pub ecdsa_host_key: Option<String>,

    /// ECDSA host certificate.
    pub ecdsa_host_cert: Option<String>,

    /// Ed25519 host key to install in new VM.
    pub ed25519_host_key: Option<String>,

    /// Ed25519 host certificate.
    pub ed25519_host_cert: Option<String>,

    /// Path to base image.
    pub base: PathBuf,

    /// Path to new VM image, to be created.
    pub image: PathBuf,

    /// Size of new image, in GiB.
    pub image_size_gib: u64,

    /// Size of memory for new VM, in MiB.
    pub memory_mib: u64,

    /// CPUs new VM should have.
    pub cpus: u64,

    /// Should a new host key and certificate be created for new VM?
    pub generate_host_certificate: bool,

    /// Should the VM be started automatically when host starts?
    pub autostart: bool,

    /// Path to CA key for creating host certificate.
    pub ca_key: Option<PathBuf>,

    /// Path to CA publicv key for verifying user certificates.
    pub user_ca_pubkey: Option<PathBuf>,

    /// Allow SSH server to use per-user authorized keys files?
    pub allow_authorized_keys: bool,

    /// List of networks to which host should be added.
    pub networks: Vec<String>,
}

/// Errors from this module.
#[derive(Debug, thiserror::Error)]
pub enum SpecificationError {
    /// No base image file specified.
    #[error("No base image or default base image specified for {0}")]
    NoBaseImage(String),

    /// No image file specified.
    #[error("No image filename specified for {0} and no image_directory in configuration")]
    NoImage(String),

    /// No image size specified.
    #[error("No image size specified for {0} and no default configured")]
    NoImageSize(String),

    /// No memory size specified.
    #[error("No memory size specified for {0} and no default configured")]
    NoMemorySize(String),

    /// No CPU count specified.
    #[error("No CPU count specified for {0} and no default configured")]
    NoCpuCount(String),

    /// No SSH authorized keys or user CA specified.
    #[error("No SSH authorized keys nor user CA specified for {0} and no default configured")]
    NoAuthorizedKeys(String),

    /// Error reading specification file.
    #[error("Couldn't read specification file {0}")]
    Read(PathBuf, #[source] std::io::Error),

    /// Error reading SSH public key.
    #[error("Failed to read SSH public key file {0}")]
    SshKeyRead(PathBuf, #[source] std::io::Error),

    /// Network name error.
    #[error(transparent)]
    NetworkNameError(#[from] crate::util::NetworkNameError),

    /// Error parsing string as UTF8.
    #[error(transparent)]
    FromUtf8Error(#[from] std::string::FromUtf8Error),

    /// Error parsing YAML.
    #[error(transparent)]
    YamlError(#[from] serde_yaml::Error),

    /// Error expanding a ~user in a path name.
    #[error(transparent)]
    HomeDirError(#[from] home_dir::Error),
}

impl Specification {
    /// Read all specifications from a file.
    ///
    /// Apply values from the provided configuration so that the
    /// returned specifications are *effective* and the caller doesn't
    /// need to worry about the configuration anymore.
    ///
    /// Also, SSH public keys are read from the files named in the
    /// input specification.
    pub fn from_file(
        config: &Configuration,
        filename: &Path,
    ) -> Result<Vec<Specification>, SpecificationError> {
        debug!("reading specification from {}", filename.display());
        let spec = fs::read(filename)
            .map_err(|err| SpecificationError::Read(filename.to_path_buf(), err))?;
        let input: HashMap<String, OneVmInputSpecification> = serde_yaml::from_slice(&spec)?;
        debug!("specification as read from file: {:#?}", input);

        let mut machines = vec![];
        for (name, machine) in input.iter() {
            let spec = Specification::one_machine(config, name, machine)?;
            debug!("machine with defaults applied: {:#?}", spec);
            machines.push(spec);
        }

        Ok(machines)
    }

    fn one_machine(
        config: &Configuration,
        name: &str,
        input: &OneVmInputSpecification,
    ) -> Result<Specification, SpecificationError> {
        let ssh_keys = if let Some(key_filenames) = input.ssh_key_files(config, name) {
            ssh_keys(&key_filenames)?
        } else {
            vec![]
        };
        let user_ca_pubkey = input.user_ca_pubkey(config, name);
        if ssh_keys.is_empty() && user_ca_pubkey.is_none() {
            return Err(SpecificationError::NoAuthorizedKeys(name.to_string()));
        }
        let user_ca_pubkey = if let Some(filename) = user_ca_pubkey {
            Some(expand_tilde(&filename)?)
        } else {
            None
        };
        let ca_key = if let Some(filename) = &input.ca_key {
            Some(expand_tilde(filename)?)
        } else {
            config.ca_key.clone()
        };
        let gen_cert = if let Some(v) = &input.generate_host_certificate {
            *v
        } else if let Some(v) = &config.default_generate_host_certificate {
            *v
        } else {
            false
        };

        let networks = input.networks(config);
        check_network_names(&networks)?;

        let spec = Specification {
            name: name.to_string(),
            ssh_keys,
            rsa_host_key: input.rsa_host_key.clone(),
            rsa_host_cert: input.rsa_host_cert.clone(),
            dsa_host_key: input.dsa_host_key.clone(),
            dsa_host_cert: input.dsa_host_cert.clone(),
            ecdsa_host_key: input.ecdsa_host_key.clone(),
            ecdsa_host_cert: input.ecdsa_host_cert.clone(),
            ed25519_host_key: input.ed25519_host_key.clone(),
            ed25519_host_cert: input.ed25519_host_cert.clone(),
            base: expand_tilde(&input.base_image(config, name)?)?,
            image: expand_tilde(&input.image(config, name)?)?,
            image_size_gib: input.image_size_gib(config, name)?,
            memory_mib: input.memory_mib(config, name)?,
            cpus: input.cpus(config, name)?,
            generate_host_certificate: gen_cert,
            autostart: input.autostart(config),
            ca_key,
            user_ca_pubkey,
            allow_authorized_keys: input.allow_authorized_keys(config),
            networks,
        };

        debug!("specification as with defaults applied: {:#?}", spec);
        Ok(spec)
    }
}

fn ssh_keys(filenames: &[PathBuf]) -> Result<Vec<String>, SpecificationError> {
    let mut keys = vec![];
    for filename in filenames {
        let filename = expand_tilde(filename)?;
        let key =
            std::fs::read(&filename).map_err(|e| SpecificationError::SshKeyRead(filename, e))?;
        let key = String::from_utf8(key)?;
        let key = key.strip_suffix('\n').unwrap_or(&key);
        keys.push(key.to_string());
    }
    Ok(keys)
}