oxedyne/fe2o3/fe2o3_o3db_sync/src/gateway.rs
4.6 KiB, 11 runs
created by r1870400018:67325, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Opening an Ozone store under the gateway's own parameterisation, for an operator tool, and that |
| 2 | //! parameterisation for a process that starts its own store ([`rest_schemes`], [`Ns`]). |
| 3 | //! |
| 4 | //! Steel and Daimond keep their stores with 16-byte `u128` user ids, AES-256-GCM at rest and CRC-32 |
| 5 | //! checksums. A store written under a different parameterisation will not read back. The tools |
| 6 | //! that open such a store (`o3db_migrate`, `o3db_sweep`, `jdat_store_check`) all open it through |
| 7 | //! here, so a key is read, checked and used in one way. |
| 8 | |
| 9 | use crate::{ |
| 10 | prelude::*, |
| 11 | data::core::RestSchemesInput, |
| 12 | }; |
| 13 | |
| 14 | use oxedyne_fe2o3_crypto::enc::EncryptionScheme; |
| 15 | use oxedyne_fe2o3_hash::{ |
| 16 | csum::ChecksumScheme, |
| 17 | hash::HashScheme, |
| 18 | }; |
| 19 | use oxedyne_fe2o3_jdat::id::IdDat; |
| 20 | |
| 21 | use std::{ |
| 22 | fs, |
| 23 | path::Path, |
| 24 | thread, |
| 25 | time::Duration, |
| 26 | }; |
| 27 | |
| 28 | |
| 29 | pub type Uid = IdDat<16, u128>; |
| 30 | pub type Db = O3db<16, Uid, EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>; |
| 31 | /// A path namespace over a [`Db`]. |
| 32 | pub type Ns = crate::ns::Ns<16, Uid, EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>; |
| 33 | |
| 34 | pub const DB_KEY_LEN: usize = 32; |
| 35 | |
| 36 | /// Reads the 32-byte at-rest key, failing loudly if it is absent or the wrong size. There is |
| 37 | /// deliberately no fallback: a wrong key cannot decrypt a byte. |
| 38 | pub fn load_key(path: &Path) -> Outcome<[u8; DB_KEY_LEN]> { |
| 39 | if !path.exists() { |
| 40 | return Err(err!( |
| 41 | "No database key at {:?}. Supply the store's at-rest key with --key; \ |
| 42 | without it the store cannot be read.", path; |
| 43 | Missing, Key, Input)); |
| 44 | } |
| 45 | let bytes = res!(fs::read(path)); |
| 46 | if bytes.len() != DB_KEY_LEN { |
| 47 | return Err(err!( |
| 48 | "The database key at {:?} is {} bytes, but must be {}.", |
| 49 | path, bytes.len(), DB_KEY_LEN; |
| 50 | Invalid, Input, Key)); |
| 51 | } |
| 52 | let mut key = [0u8; DB_KEY_LEN]; |
| 53 | key.copy_from_slice(&bytes); |
| 54 | Ok(key) |
| 55 | } |
| 56 | |
| 57 | /// The schemes a store is kept under, from its at-rest `key`: AES-256-GCM at rest, CRC-32 |
| 58 | /// checksums, and the default hashers. For a process that starts a store of its own, so it keeps |
| 59 | /// it exactly as the gateway and its tools read it. |
| 60 | pub fn rest_schemes(key: &[u8; DB_KEY_LEN]) |
| 61 | -> Outcome<RestSchemesInput<EncryptionScheme, HashScheme, HashScheme, ChecksumScheme>> |
| 62 | { |
| 63 | let aes_gcm = res!(EncryptionScheme::new_aes_256_gcm_with_key(&key[..])); |
| 64 | let crc32 = ChecksumScheme::new_crc32(); |
| 65 | Ok(RestSchemesInput::new( |
| 66 | Some(aes_gcm), |
| 67 | None::<HashScheme>, |
| 68 | None::<HashScheme>, |
| 69 | Some(crc32), |
| 70 | )) |
| 71 | } |
| 72 | |
| 73 | /// Opens and starts a store, leaving garbage collection off unless asked, and says how many of its |
| 74 | /// bots answered a ping. `cfg_opt` is the configuration of a store being created, and None reads |
| 75 | /// the store's own. The store is returned whatever the count, as `o3db_migrate` and `o3db_sweep` |
| 76 | /// opened it before this helper existed; a tool that reads and reports on the store calls |
| 77 | /// [`require_answer`] with the count. |
| 78 | pub fn open_store( |
| 79 | root: &Path, |
| 80 | cfg_opt: Option<OzoneConfig>, |
| 81 | key: &[u8; DB_KEY_LEN], |
| 82 | gc_on: bool, |
| 83 | label: &str, |
| 84 | ) |
| 85 | -> Outcome<(Db, usize)> |
| 86 | { |
| 87 | let schms_input = res!(rest_schemes(key)); |
| 88 | let mut db: Db = res!(O3db::new(root, cfg_opt, schms_input, Uid::default())); |
| 89 | res!(db.start(label.to_string())); |
| 90 | // State the collector explicitly, so that a read never collects. |
| 91 | res!(ok!(db.updated_api()).activate_gc(gc_on)); |
| 92 | thread::sleep(Duration::from_millis(500)); |
| 93 | let (_, msgs) = res!(db.api().ping_bots(constant::USER_REQUEST_WAIT)); |
| 94 | info!("{}: {} bots responded.", label, msgs.len()); |
| 95 | Ok((db, msgs.len())) |
| 96 | } |
| 97 | |
| 98 | /// Fails when no bot answered (`n` from [`open_store`]). A scan of a store that does not answer |
| 99 | /// finds nothing, and a report that says "clean" of it would be false. |
| 100 | pub fn require_answer( |
| 101 | n: usize, |
| 102 | label: &str, |
| 103 | root: &Path, |
| 104 | ) |
| 105 | -> Outcome<usize> |
| 106 | { |
| 107 | if n == 0 { |
| 108 | return Err(err!("{}: no bot of the store at {:?} answered.", label, root; Missing, Data)); |
| 109 | } |
| 110 | Ok(n) |
| 111 | } |
| 112 | |
| 113 | #[cfg(test)] |
| 114 | mod tests { |
| 115 | use super::*; |
| 116 | |
| 117 | /// A caller that starts its own store gets the gateway's schemes from the key alone: AES-256-GCM |
| 118 | /// at rest, CRC-32 checksums, and the default hashers. |
| 119 | #[test] |
| 120 | fn rest_schemes_are_the_gateways() { |
| 121 | let schemes = match rest_schemes(&[7u8; DB_KEY_LEN]) { |
| 122 | Ok(s) => s, |
| 123 | Err(e) => panic!("the gateway's schemes from a key: {}", e), |
| 124 | }; |
| 125 | assert!(schemes.enc.is_some(), "an encrypter"); |
| 126 | assert!(schemes.hash.is_none() && schemes.prnd.is_none(), "the default hashers"); |
| 127 | assert!(schemes.csum.is_some(), "a checksummer"); |
| 128 | } |
| 129 | } |