asuran 0.1.6

Deduplicating, encrypting, fast, and tamper evident archive format
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
447
448
449
450
451
452
453
454
455
456
use super::util::LockedFile;
use super::SFTPConnection;
use crate::repository::backend::common::sync_backend::SyncManifest;
use crate::repository::backend::common::{ManifestID, ManifestTransaction};
use crate::repository::backend::BackendError;
use crate::repository::{ChunkSettings, Key};
use crate::{manifest::StoredArchive, repository::backend::Result};

use chrono::prelude::*;
use petgraph::Graph;
use semver::Version;
use serde_cbor as cbor;
use ssh2::FileStat;
use uuid::Uuid;

use std::collections::{HashMap, HashSet};
use std::io::{Seek, SeekFrom};
use std::path::PathBuf;
use std::rc::Rc;

#[derive(Debug)]
pub struct SFTPManifest {
    connection: SFTPConnection,
    known_entries: HashMap<ManifestID, ManifestTransaction>,
    verified_memo_pad: HashSet<ManifestID>,
    heads: Vec<ManifestID>,
    file: LockedFile,
    key: Key,
    chunk_settings: ChunkSettings,
    path: PathBuf,
    seen_versions: HashSet<(Version, Uuid)>,
}

impl SFTPManifest {
    /// Will attempt to open or create a manifest at the location pointed to by the path variable of
    /// the given settings at the given server
    #[allow(clippy::too_many_lines)]
    #[allow(clippy::filter_map)]
    pub fn connect(
        settings: impl Into<SFTPConnection>,
        key: &Key,
        chunk_settings: Option<ChunkSettings>,
    ) -> Result<Self> {
        let connection = settings.into().with_connection()?;
        let sftp = connection
            .sftp()
            .expect("Connected successful, but no sftp session?");
        let repository_path = PathBuf::from(&connection.settings().path);
        // Create a new seen versions set
        let mut seen_versions = HashSet::new();
        seen_versions.insert((crate::VERSION_STRUCT.clone(), *crate::IMPLEMENTATION_UUID));
        // Create repository path if it does not exist
        if sftp.stat(&repository_path).is_err() {
            sftp.mkdir(&repository_path, 0o775)?;
        }
        // Construct the path of the manifest folder
        let manifest_path = repository_path.join("manifest");
        // Check to see if it exists
        if let Ok(file_stat) = sftp.stat(&manifest_path) {
            // If it is a file, and not a folder, return failure
            if file_stat.file_type().is_file() {
                return Err(BackendError::ManifestError(format!(
                    "Failed to load manifest, {:?} is a file, not a directory",
                    manifest_path
                )));
            }
        } else {
            // Create the manifest directory
            sftp.mkdir(&manifest_path, 0o775)?;
        }

        // Get the list of manifest files and sort them by ID
        let mut items = sftp
            .readdir(&manifest_path)?
            .into_iter()
            // Make sure its a file
            .filter(|(_path, file_stat)| file_stat.file_type().is_file())
            // Now that we only have files, drop the file_stat
            .map(|(path, _file_stat)| path)
            // Make sure the file name component is a number, and map to (number, path)
            .filter_map(|path| {
                path.file_name()
                    .and_then(|x| x.to_string_lossy().parse::<u64>().ok())
                    .map(|x| (x, path))
            })
            .collect::<Vec<_>>();
        // Sort the list of files by id
        items.sort_by(|a, b| a.0.cmp(&b.0));

        // Collect all known transactions
        let mut known_entries = HashMap::new();
        for (_, path) in &items {
            // Open the file
            let mut file = sftp.open(path)?;
            // Keep deserializing transactions until we hit an error
            let de = cbor::Deserializer::from_reader(&mut file);
            let mut de = de.into_iter::<ManifestTransaction>();
            while let Some(tx) = de.next().and_then(std::result::Result::ok) {
                seen_versions.insert((tx.version(), tx.uuid()));
                known_entries.insert(tx.tag(), tx);
            }
        }

        let mut file = None;
        // Attempt to find an unlocked file
        for (_, path) in &items {
            let locked_file = LockedFile::open_read_write(path, Rc::clone(&sftp))?;
            if let Some(f) = locked_file {
                file = Some(f);
                break;
            }
        }

        // If we were unable to find an unlocked file, go ahead and make one
        let file = file.unwrap_or_else(|| {
            let id = if items.is_empty() {
                0
            } else {
                items[items.len() - 1].0 + 1
            };
            let path = manifest_path.join(id.to_string());
            LockedFile::open_read_write(path, Rc::clone(&sftp))
                .expect("Unable to create new lock file (IO error)")
                .expect("Somehow, our newly created lock file is already locked")
        });

        let sfile_path = manifest_path.join("chunk.settings");
        let chunk_settings = if let Some(chunk_settings) = chunk_settings {
            // Attempt to open the chunk settings file and update it
            let mut sfile = LockedFile::open_read_write(&sfile_path, Rc::clone(&sftp))?
                .ok_or_else(|| {
                    BackendError::ManifestError("Unable to lock chunk.settings".to_string())
                })?;
            // Clear out the file
            sftp.setstat(
                &sfile_path,
                FileStat {
                    size: Some(0),
                    uid: None,
                    gid: None,
                    perm: None,
                    atime: None,
                    mtime: None,
                },
            )?;
            // Write out new chunksettings
            cbor::ser::to_writer(&mut sfile, &chunk_settings)?;
            chunk_settings
        } else {
            let mut sfile = sftp.open(&sfile_path)?;
            cbor::de::from_reader(&mut sfile)?
        };

        // Construct the manifest
        let mut manifest = SFTPManifest {
            connection,
            known_entries,
            verified_memo_pad: HashSet::new(),
            heads: Vec::new(),
            file,
            key: key.clone(),
            chunk_settings,
            path: manifest_path,
            seen_versions,
        };
        // Build the list of heads
        manifest.build_heads();
        // Verify each head
        for head in manifest.heads.clone() {
            if !manifest.verify_tx(head) {
                return Err(BackendError::ManifestError(format!(
                    "Manifest Transaction failed verification! {:?}",
                    manifest.known_entries.get(&head).ok_or_else(|| BackendError::Unknown("Failed to get the head of the known entries list while reporting an error".to_string()))?
                )));
            }
        }

        Ok(manifest)
    }

    /// Gets the heads from a list of transactions
    fn build_heads(&mut self) {
        // Create the graph
        let mut graph: Graph<ManifestID, ()> = Graph::new();
        let mut index_map = HashMap::new();
        // Add each transaction to our map
        for tx in self.known_entries.values() {
            let tag = tx.tag();
            let id = graph.add_node(tag);
            index_map.insert(tag, id);
        }
        // Go through each transaction in the graph, adding an edge in the new -> old direction
        // These unwraps are safe because we just added these entries to our hashmap
        for tx in self.known_entries.values() {
            let id = index_map.get(&tx.tag()).unwrap();
            for other_tx in tx.previous_heads() {
                let other_id = index_map.get(&other_tx).unwrap();
                graph.update_edge(*id, *other_id, ());
            }
        }
        // reverse all the nodes, so they now point from old to new
        graph.reverse();
        // Find all nodes with no outgoing edges, these are our heads
        let mut heads = Vec::new();
        for (tag, id) in &index_map {
            let mut edges = graph.edges(*id);
            if edges.next() == None {
                heads.push(*tag);
            }
        }

        self.heads = heads;
    }

    /// Verifies a transaction and all of its parents
    fn verify_tx(&mut self, id: ManifestID) -> bool {
        if self.verified_memo_pad.contains(&id) {
            true
        } else {
            let tx = self
                .known_entries
                .get(&id)
                .expect("Item in verified memo pad was not in known_entries")
                .clone();
            if tx.verify(&self.key) {
                self.verified_memo_pad.insert(id);
                for parent in tx.previous_heads() {
                    if !self.verify_tx(*parent) {
                        return false;
                    }
                }
                true
            } else {
                false
            }
        }
    }
}

impl SyncManifest for SFTPManifest {
    type Iterator = std::vec::IntoIter<StoredArchive>;
    fn last_modification(&mut self) -> Result<chrono::DateTime<chrono::FixedOffset>> {
        if self.heads.is_empty() {
            Ok(Local::now().with_timezone(Local::now().offset()))
        } else {
            let first_head = self
                .known_entries
                .get(&self.heads[0])
                .expect("Item in heads was not in known entries");
            let mut max = first_head.timestamp();
            for id in &self.heads {
                let tx = self.known_entries.get(id).ok_or_else(|| {
                    BackendError::ManifestError("Unable to load timestamp".to_string())
                })?;
                if tx.timestamp() > max {
                    max = tx.timestamp()
                }
            }
            Ok(max)
        }
    }
    fn chunk_settings(&mut self) -> ChunkSettings {
        self.chunk_settings
    }
    fn archive_iterator(&mut self) -> Self::Iterator {
        let mut items = self.known_entries.values().cloned().collect::<Vec<_>>();
        items.sort_by(|a, b| a.timestamp().cmp(&b.timestamp()));
        items.reverse();
        items
            .into_iter()
            .map(StoredArchive::from)
            .collect::<Vec<_>>()
            .into_iter()
    }
    fn write_chunk_settings(&mut self, chunk_settings: ChunkSettings) -> Result<()> {
        let sftp = self.connection.sftp().unwrap();
        let sfile_path = self.path.join("chunk.settings");
        // Attempt to open the chunk settings file and update it
        let mut sfile =
            LockedFile::open_read_write(&sfile_path, Rc::clone(&sftp))?.ok_or_else(|| {
                BackendError::ManifestError("Unable to lock chunk.settings".to_string())
            })?;
        // Clear out the file
        sftp.setstat(
            &sfile_path,
            FileStat {
                size: Some(0),
                uid: None,
                gid: None,
                perm: None,
                atime: None,
                mtime: None,
            },
        )?;
        // Write out new chunksettings
        cbor::ser::to_writer(&mut sfile, &chunk_settings)?;
        self.chunk_settings = chunk_settings;
        Ok(())
    }
    fn write_archive(&mut self, archive: StoredArchive) -> Result<()> {
        // Create the transaction
        let tx = ManifestTransaction::new(
            &self.heads,
            archive.id(),
            archive.timestamp(),
            self.chunk_settings.hmac,
            &self.key,
        );
        // Write the transaction to the file
        let file = &mut self.file;
        file.seek(SeekFrom::End(0))?;
        cbor::ser::to_writer(file, &tx)?;
        // Add the transaction to our entries list
        let id = tx.tag();
        self.known_entries.insert(id, tx);
        // Update our heads to only contain this transaction
        self.heads = vec![id];
        Ok(())
    }
    fn touch(&mut self) -> Result<()> {
        // Touch doesn't actually do anything with this implementation
        Ok(())
    }
    fn seen_versions(&mut self) -> HashSet<(Version, Uuid)> {
        self.seen_versions.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::{Compression, Encryption, HMAC};
    use crate::repository::backend::sftp::SFTPSettings;
    use std::collections::HashSet;
    use std::env;

    fn get_settings(path: String) -> SFTPSettings {
        let hostname = env::var_os("ASURAN_SFTP_HOSTNAME")
            .map(|x| x.into_string().unwrap())
            .expect("Server must be set");
        let username = env::var_os("ASURAN_SFTP_USER")
            .map(|x| x.into_string().unwrap())
            .unwrap_or("asuran".to_string());
        let password = env::var_os("ASURAN_SFTP_PASS")
            .map(|x| x.into_string().unwrap())
            .unwrap_or("asuran".to_string());
        let port = env::var_os("ASURAN_SFTP_PORT")
            .map(|x| x.into_string().unwrap())
            .unwrap_or("22".to_string())
            .parse::<u16>()
            .expect("Unable to parse port");

        SFTPSettings {
            hostname,
            username,
            port: Some(port),
            password: Some(password),
            path,
        }
    }

    fn get_manifest(
        path: impl AsRef<str>,
        key: &Key,
        chunk_settings: Option<ChunkSettings>,
    ) -> SFTPManifest {
        let path = path.as_ref().to_string();
        SFTPManifest::connect(get_settings(path), key, chunk_settings)
            .expect("Unable to connect to manifest")
    }

    #[test]
    fn connect() {
        let key = Key::random(32);
        get_manifest(
            "asuran/manifest-connect",
            &key,
            Some(ChunkSettings::lightweight()),
        );
    }

    #[test]
    #[should_panic]
    fn creation_without_settings() {
        let key = Key::random(32);
        get_manifest("asuran/manifest-creation-without-settings", &key, None);
    }

    #[test]
    fn chunk_settings() {
        let key = Key::random(32);
        let mut manifest = get_manifest(
            "asuran/manifest-chunksettings",
            &key,
            Some(ChunkSettings::lightweight()),
        );
        let settings = ChunkSettings {
            compression: Compression::ZStd { level: 1 },
            encryption: Encryption::new_aes256ctr(),
            hmac: HMAC::Blake3,
        };
        manifest
            .write_chunk_settings(settings)
            .expect("Unable to write chunk settings");
        drop(manifest);
        let mut manifest = get_manifest("asuran/manifest-chunksettings", &key, None);
        let new_settings = manifest.chunk_settings();
        assert!(settings == new_settings)
    }

    #[test]
    fn touch_and_modification() {
        let key = Key::random(32);
        let mut manifest = get_manifest(
            "asuran/manifest_touch",
            &key,
            Some(ChunkSettings::lightweight()),
        );
        manifest.touch().unwrap();
        let x = manifest.last_modification().unwrap();
        drop(manifest);
        let mut manifest = get_manifest("asuran/manifest_touch", &key, None);
        std::thread::sleep(std::time::Duration::from_secs(2));
        manifest.touch().unwrap();
        drop(manifest);
        let mut manifest = get_manifest("asuran/manifest_touch", &key, None);
        let y = manifest.last_modification().unwrap();
        assert!(x != y)
    }

    #[test]
    fn archives() {
        let key = Key::random(32);
        let dummy_archives: HashSet<StoredArchive> =
            (0..10).map(|_| StoredArchive::dummy_archive()).collect();

        let mut manifest = get_manifest(
            "asuran/manifest_archives",
            &key,
            Some(ChunkSettings::lightweight()),
        );

        for archive in &dummy_archives {
            manifest
                .write_archive(archive.clone())
                .expect("Unable to write archive");
        }

        drop(manifest);
        let mut manifest = get_manifest("asuran/manifest_archives", &key, None);

        let output: HashSet<StoredArchive> = manifest.archive_iterator().collect();

        assert!(dummy_archives == output);
    }
}