Documents in a bucket of Garage, still encrypted by the app
Asked for on November 19, 2026: the uploaded files land in an S3 bucket of the fleet's Garage instead of a directory on cnx01, as the fleet's other apps keep theirs. The app encrypts each file exactly as before (AES-256-GCM under a key from HKDF, the document's id authenticated), so the bucket holds the same VISADOC1 files and Garage never sees a key or a document in the clear. Garage's SSE-C stays off: its key would come from the same secret. src/s3.rs is a small S3 client on reqwest and ring, as in proplan: SigV4 (checked against AWS's two published examples), path-style PUT, GET, HEAD, DELETE and ListObjectsV2 page by page, three attempts when the server is away or busy. It asks before it writes, so no object is replaced; it sends If-None-Match: * as well, which Garage 2.5 ignores (seen in tests/garage.rs). The store (src/files.rs) keeps the files as documents/<id>.vdoc in the bucket, or as <id>.vdoc in a directory for development and the tests. Its calls are async now. VISA_S3_BUCKET, _ENDPOINT, _REGION and _ACCESS_KEY_ID name the bucket, the secret comes from VISA_S3_SECRET_ACCESS_KEY_FILE or the credential s3-secret; the bucket and a directory together are refused. At the start the app checks the bucket in the background and only warns. New commands: `visa files-migrate --from DIR` copies the files of a directory into the store, each opened and compared with its upload before and after, and may run again; `visa files-mirror --to DIR` makes a directory hold exactly the bucket's files, still encrypted, for the backups, and a restore is files-migrate from it. Tests: an S3 server inside the test process that checks every request's signature and pages its listings (tests/common/fake_s3.rs); uploads, views, downloads and deletions through it, the check finding swapped, missing and stray objects, the move from a directory (again, missing, other bytes, damaged), the mirror and a restore from it, a busy server and a wrong key. tests/garage.rs runs the store against Garage 2.5.0, the version cnx01 runs, which scripts/dev-garage.sh starts in Docker; VISA_DEV_S3=1 scripts/dev.sh runs the dev server on it. The 14 demo documents moved into it with files-migrate and a browser upload landed there.
This commit is contained in:
1 parent
17426dba12
commit
0370e0154f
20 files changed
+2558
-218
No files matched your search
+121
-15
@@ -1,16 +1,19 @@
|
||||
//! Documents: scans and photos of a person's papers, and the company
|
||||
//! documents the extension packs need. A row holds what is known about a
|
||||
//! file; the file itself lies encrypted in the store (`files.rs`). A
|
||||
//! file; the file itself lies encrypted in the store (`files.rs`), on the
|
||||
//! server a bucket of the fleet's Garage. A
|
||||
//! person's document may belong to one of their records (a passport, an
|
||||
//! arrival, a 90-day report ...), and then shows next to that record too.
|
||||
|
||||
use std::collections::BTreeSet;
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result, bail, ensure};
|
||||
use chrono::{DateTime, NaiveDate, Utc};
|
||||
use sqlx::{PgPool, Postgres, Transaction};
|
||||
|
||||
use crate::files::{self, Store};
|
||||
use crate::s3::Put;
|
||||
|
||||
/// Whose document it is.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -498,18 +501,14 @@ pub struct Check {
|
||||
/// size and checksum recorded at the upload; lists files that belong to no
|
||||
/// document.
|
||||
pub async fn check(db: &PgPool, store: &Store) -> Result<Check> {
|
||||
let rows: Vec<(i64, i32, String)> =
|
||||
sqlx::query_as("SELECT id, size_bytes, sha256 FROM documents ORDER BY id")
|
||||
.fetch_all(db)
|
||||
.await?;
|
||||
let (ids, other) = store.entries()?;
|
||||
let rows = uploads(db).await?;
|
||||
let entries = store.entries().await?;
|
||||
let mut out = Check {
|
||||
documents: rows.len(),
|
||||
..Check::default()
|
||||
};
|
||||
let known: BTreeSet<i64> = rows.iter().map(|r| r.0).collect();
|
||||
for (id, size, sha) in &rows {
|
||||
match store.get(*id) {
|
||||
for (id, (size, sha)) in &rows {
|
||||
match store.get(*id).await {
|
||||
Ok(bytes) if bytes.len() == *size as usize && files::sha256_hex(&bytes) == *sha => {}
|
||||
Ok(_) => out.problems.push(format!(
|
||||
"document {id}: the file does not match the size and checksum of its upload"
|
||||
@@ -517,17 +516,124 @@ pub async fn check(db: &PgPool, store: &Store) -> Result<Check> {
|
||||
Err(e) => out.problems.push(format!("document {id}: {e:#}")),
|
||||
}
|
||||
}
|
||||
for id in ids.iter().filter(|id| !known.contains(id)) {
|
||||
out.problems
|
||||
.push(format!("{id}.vdoc: a file without its document"));
|
||||
for (id, _) in entries
|
||||
.files
|
||||
.iter()
|
||||
.filter(|(id, _)| !rows.contains_key(id))
|
||||
{
|
||||
out.problems.push(format!(
|
||||
"{}: a file without its document",
|
||||
files::file_name(*id)
|
||||
));
|
||||
}
|
||||
for name in other {
|
||||
for name in entries.other {
|
||||
out.problems
|
||||
.push(format!("{name}: not a file of the store"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Every document's size and SHA-256 as recorded at its upload.
|
||||
async fn uploads(db: &PgPool) -> Result<BTreeMap<i64, (i32, String)>> {
|
||||
let rows: Vec<(i64, i32, String)> =
|
||||
sqlx::query_as("SELECT id, size_bytes, sha256 FROM documents ORDER BY id")
|
||||
.fetch_all(db)
|
||||
.await?;
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.map(|(id, size, sha)| (id, (size, sha)))
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// What `visa files-migrate` did.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Moved {
|
||||
/// Files copied into the store.
|
||||
pub copied: usize,
|
||||
/// Files the store had already, with the same bytes.
|
||||
pub there: usize,
|
||||
/// Files in the directory that belong to no document, left there.
|
||||
pub strays: Vec<String>,
|
||||
/// Documents whose file is neither in the directory nor in the store.
|
||||
pub missing: Vec<i64>,
|
||||
}
|
||||
|
||||
/// Copies the documents' files from a files directory (where they lay
|
||||
/// before the bucket, or a mirror the backups hold) into the store, as they
|
||||
/// are: still encrypted. Each file is opened with the key and compared with
|
||||
/// the size and checksum of its upload before it is copied, and read back
|
||||
/// from the store and compared again after. A file the store has already
|
||||
/// with the same bytes is counted and skipped, so the copy can run again; a
|
||||
/// file the store has with other bytes, or one that does not open, stops it
|
||||
/// with an error. Files of no document stay in the directory, listed.
|
||||
pub async fn migrate(db: &PgPool, from: &Path, to: &Store) -> Result<Moved> {
|
||||
if let Some(d) = to.dir() {
|
||||
ensure!(
|
||||
!files::same_dir(d, from),
|
||||
"{} is the store itself: files-migrate copies from another directory",
|
||||
from.display()
|
||||
);
|
||||
}
|
||||
ensure!(from.is_dir(), "{} is not a directory", from.display());
|
||||
let src = Store::in_dir(from.to_path_buf(), to.file_key().clone())?;
|
||||
let rows = uploads(db).await?;
|
||||
let present = src.entries().await?;
|
||||
let in_dir: BTreeSet<i64> = present.files.iter().map(|(id, _)| *id).collect();
|
||||
let mut out = Moved::default();
|
||||
for (id, (size, sha)) in &rows {
|
||||
let matches =
|
||||
|bytes: &[u8]| bytes.len() == *size as usize && files::sha256_hex(bytes) == *sha;
|
||||
if !in_dir.contains(id) {
|
||||
if to.get_sealed(*id).await?.is_some() {
|
||||
out.there += 1;
|
||||
} else {
|
||||
out.missing.push(*id);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let plain = src
|
||||
.get(*id)
|
||||
.await
|
||||
.with_context(|| format!("document {id}: its file in the directory"))?;
|
||||
if !matches(&plain) {
|
||||
bail!(
|
||||
"document {id}: the file in {} does not match the size and checksum of its upload",
|
||||
from.display()
|
||||
);
|
||||
}
|
||||
let sealed = src
|
||||
.get_sealed(*id)
|
||||
.await?
|
||||
.with_context(|| format!("document {id}: its file went missing"))?;
|
||||
match to
|
||||
.put_sealed(*id, sealed)
|
||||
.await
|
||||
.with_context(|| format!("document {id}: copying its file"))?
|
||||
{
|
||||
Put::Written => out.copied += 1,
|
||||
Put::Same => out.there += 1,
|
||||
}
|
||||
let back = to
|
||||
.get(*id)
|
||||
.await
|
||||
.with_context(|| format!("document {id}: reading the copy back"))?;
|
||||
if !matches(&back) {
|
||||
bail!(
|
||||
"document {id}: the copy in {} does not match its upload",
|
||||
to.describe()
|
||||
);
|
||||
}
|
||||
}
|
||||
out.strays = present
|
||||
.files
|
||||
.iter()
|
||||
.filter(|(id, _)| !rows.contains_key(id))
|
||||
.map(|(id, _)| files::file_name(*id))
|
||||
.chain(present.other)
|
||||
.collect();
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user