Files
grabowski 0370e0154f
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 52s
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.
2026-11-19 20:35:32 +07:00

99 lines
3.8 KiB
Rust

//! The store against a real Garage, the version cnx01 runs:
//! `bash scripts/dev-garage.sh` starts one in Docker and writes
//! `.dev/garage.env`; `. .dev/garage.env` before `cargo test` runs this.
//! Without VISA_TEST_S3_* set it is skipped (CI has no Garage; the test
//! server in tests/common/fake_s3.rs covers the same calls there).
use visa::files::{FileKey, Store};
use visa::s3::{Bucket, Put, S3Config};
use crate::common::pdf;
fn garage() -> Option<S3Config> {
let v = |n: &str| std::env::var(n).ok().filter(|s| !s.is_empty());
let secret = std::fs::read_to_string(v("VISA_TEST_S3_SECRET_FILE")?).ok()?;
Some(S3Config {
endpoint: v("VISA_TEST_S3_ENDPOINT")?,
bucket: v("VISA_TEST_S3_BUCKET")?,
region: v("VISA_TEST_S3_REGION").unwrap_or_else(|| "garage".into()),
access_key_id: v("VISA_TEST_S3_ACCESS_KEY_ID")?,
secret_access_key: secret.trim().to_string(),
})
}
/// Ids no other run uses at the same time: the bucket is shared.
fn fresh_ids(n: i64) -> Vec<i64> {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.subsec_nanos() as i64;
let base = 1_000_000_000 + (std::process::id() as i64 % 1000) * 1_000_000 + nanos % 1_000_000;
(0..n).map(|i| base + i * 7).collect()
}
#[tokio::test]
async fn the_store_works_against_a_real_garage() {
let Some(cfg) = garage() else {
eprintln!("VISA_TEST_S3_* not set: skipped (scripts/dev-garage.sh)");
return;
};
let key = FileKey::random();
let store = Store::in_bucket(&cfg, key.clone()).unwrap();
store.check().await.unwrap();
let ids = fresh_ids(3);
for id in &ids {
store
.put(*id, pdf(&format!("document {id}")))
.await
.unwrap();
}
for id in &ids {
assert_eq!(
store.get(*id).await.unwrap(),
pdf(&format!("document {id}"))
);
}
// the listing names them with their sizes, page after page
let entries = store.entries().await.unwrap();
for id in &ids {
let size = store.get_sealed(*id).await.unwrap().unwrap().len() as u64;
assert!(entries.files.contains(&(*id, size)), "{id}: {entries:?}");
}
// never replaced: the same bytes count as stored, others are refused
let sealed = store.get_sealed(ids[0]).await.unwrap().unwrap();
assert_eq!(
store.put_sealed(ids[0], sealed.clone()).await.unwrap(),
Put::Same
);
let e = store.put(ids[0], pdf("other")).await.unwrap_err();
assert!(format!("{e:#}").contains("other bytes"), "{e:#}");
assert_eq!(store.get_sealed(ids[0]).await.unwrap().unwrap(), sealed);
// Garage 2.5 ignores If-None-Match on a PUT and writes (seen on Nov 19,
// 2026): on a scratch object of this run, never on a document's
let bucket = Bucket::new(&cfg).unwrap();
let scratch = format!("scratch/{}.probe", ids[0]);
assert!(bucket.put_if_absent(&scratch, b"first").await.unwrap());
let honored = !bucket.put_if_absent(&scratch, b"second").await.unwrap();
let kept = bucket.get(&scratch).await.unwrap().unwrap();
assert_eq!(
(honored, kept.as_slice()),
(false, b"second".as_slice()),
"Garage now honors If-None-Match: update the comment on Bucket::put_if_absent"
);
bucket.delete(&scratch).await.unwrap();
// a wrong secret is refused, not taken for a missing file
let mut wrong = cfg.clone();
wrong.secret_access_key = "wrong".into();
let e = Store::in_bucket(&wrong, key)
.unwrap()
.get(ids[0])
.await
.unwrap_err();
assert!(format!("{e:#}").contains("HTTP 403"), "{e:#}");
for id in &ids {
store.remove(*id).await.unwrap();
store.remove(*id).await.unwrap();
assert!(store.get_sealed(*id).await.unwrap().is_none());
}
}