Documents in a bucket of Garage, still encrypted by the app
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 52s

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:
grabowski committed 2026-11-19 20:35:32 +07:00
1 parent 17426dba12
commit 0370e0154f
20 files changed
+2558 -218

No files matched your search

+91 -19
View File
@@ -1,13 +1,14 @@
//! `visa [serve|migrate|seed-demo|files-check|help]`: the server and its admin
//! commands.
//! `visa [serve|migrate|seed-demo|files-check|files-migrate|files-mirror|help]`:
//! the server and its admin commands.
use std::path::PathBuf;
use std::process::ExitCode;
use anyhow::{Context, Result, bail, ensure};
use tracing::info;
use tracing::{info, warn};
use visa::config::Config;
use visa::files::Store;
use visa::files::{self, Store};
use visa::{dates, db, documents, seed, web};
const USAGE: &str = "\
@@ -21,6 +22,13 @@ Commands:
seed-demo add invented demo people and documents (local development only)
files-check read every document's file back and compare it with its upload;
list files without a document (exit status 1 on any problem)
files-migrate --from DIR
copy the documents' files from a files directory (the store
before the bucket, or a backup's mirror) into the store, each
checked against its upload; again is harmless
files-mirror --to DIR
make DIR hold exactly the store's files, still encrypted, for
the backups
help show this text
Settings come from the environment; see src/config.rs or README.md.";
@@ -41,6 +49,24 @@ async fn main() -> ExitCode {
}
}
/// The directory after `flag` on the command line.
fn dir_arg(flag: &str) -> Result<PathBuf> {
let args: Vec<String> = std::env::args().skip(2).collect();
match args.as_slice() {
[f, dir] if f == flag && !dir.is_empty() => Ok(PathBuf::from(dir)),
_ => bail!("give the directory as {flag} DIR; `visa help` lists the commands"),
}
}
/// The store the settings name; an error when documents are off.
fn store(cfg: &Config) -> Result<Store> {
let f = cfg
.files
.as_ref()
.context("documents are off here: no VISA_S3_BUCKET (or VISA_FILES_DIR) and file key")?;
Store::open(f)
}
async fn run(cmd: &str) -> Result<()> {
match cmd {
"serve" => serve().await,
@@ -66,22 +92,18 @@ async fn run(cmd: &str) -> Result<()> {
println!("demo people are there already");
}
match &cfg.files {
Some(f) => {
let store = Store::open(f.dir.clone(), f.key.clone())?;
Some(_) => {
let store = store(&cfg)?;
let n = seed::demo_documents(&pool, &store, dates::today()).await?;
println!("demo documents added: {n}");
}
None => println!("no demo documents: documents are off (no VISA_FILES_DIR)"),
None => println!("no demo documents: documents are off (no place for them)"),
}
Ok(())
}
"files-check" => {
let cfg = Config::from_env()?;
let f = cfg
.files
.as_ref()
.context("documents are off here: no VISA_FILES_DIR and file key")?;
let store = Store::open(f.dir.clone(), f.key.clone())?;
let store = store(&cfg)?;
let pool = db::connect(&cfg).await?;
let check = documents::check(&pool, &store).await?;
for p in &check.problems {
@@ -90,13 +112,55 @@ async fn run(cmd: &str) -> Result<()> {
println!(
"{} documents checked in {} (file key {}): {} problems",
check.documents,
store.dir().display(),
store.describe(),
store.key_id(),
check.problems.len()
);
ensure!(check.problems.is_empty(), "the files need attention");
Ok(())
}
"files-migrate" => {
let from = dir_arg("--from")?;
let cfg = Config::from_env()?;
let store = store(&cfg)?;
let pool = db::connect(&cfg).await?;
let moved = documents::migrate(&pool, &from, &store).await?;
for name in &moved.strays {
println!(
"left in {}: {name}, which is no document's file",
from.display()
);
}
for id in &moved.missing {
println!(
"problem: document {id} has no file in {} and none in the store",
from.display()
);
}
println!(
"{} files copied into {}, {} there already (file key {})",
moved.copied,
store.describe(),
moved.there,
store.key_id()
);
Ok(())
}
"files-mirror" => {
let to = dir_arg("--to")?;
let cfg = Config::from_env()?;
let store = store(&cfg)?;
let m = files::mirror(&store, &to).await?;
println!(
"{} mirrored into {}: {} copied, {} kept, {} removed",
store.describe(),
to.display(),
m.copied,
m.kept,
m.removed
);
Ok(())
}
"help" | "--help" | "-h" => {
println!("{USAGE}");
Ok(())
@@ -113,12 +177,20 @@ async fn serve() -> Result<()> {
let login = cfg.login_enabled();
let st = web::AppState::new(cfg, pool)?;
match &st.files {
Some(s) => info!(
"documents in {} (file key {})",
s.dir().display(),
s.key_id()
),
None => info!("documents are off: no VISA_FILES_DIR (local development)"),
Some(s) => {
info!("documents in {} (file key {})", s.describe(), s.key_id());
// A store that is not there yet (Garage starting) costs the
// documents, not the records: the check only warns, and the
// server listens meanwhile.
let s = s.clone();
tokio::spawn(async move {
match s.check().await {
Ok(()) => info!("documents: {} answers", s.describe()),
Err(e) => warn!("documents: {} does not answer yet: {e:#}", s.describe()),
}
});
}
None => info!("documents are off: no place for them (local development)"),
}
web::spawn_background(st.clone());
let app = web::router(st);