pub mod auth;
pub mod config;
pub mod dashboard;
pub mod error;
pub mod locks;
pub mod metrics;
pub mod model;
pub mod namespace;
pub mod page;
pub mod range;
pub mod routes;
pub mod state;
pub mod storage;
pub mod tls;
use std::sync::Arc;
use axum::Router;
use crate::auth::Authorizer;
use crate::config::Config;
use crate::locks::LockStore;
use crate::metrics::Metrics;
use crate::state::AppState;
use crate::storage::s3::{Keyspace, S3Config, S3Store};
use crate::storage::{LocalStore, Store};
pub fn app(config: Config) -> Router {
let (store, locks) = backends(&config);
let authorizer = Authorizer::new(&config.auth);
routes::router(Arc::new(AppState {
store,
locks,
config,
authorizer,
metrics: Metrics::new(),
}))
}
pub async fn reclaim(config: &Config) {
let reclaimed = backends(config).0.reclaim(config.staging_max_age).await;
if reclaimed.files > 0 {
tracing::info!(
files = reclaimed.files,
bytes = reclaimed.bytes,
"reclaimed what interrupted uploads left behind"
);
}
}
pub async fn verify_presign(config: &mut Config) {
use crate::storage::s3::probe::{Checksums, checksums};
let crate::config::Storage::Bucket { presign: true, .. } = &config.storage else {
return;
};
let Some(keys) = keyspace(config) else {
return;
};
let refusal = match checksums(&keys).await {
Checksums::Enforced => return,
Checksums::Ignored => {
"this object store accepted an upload whose body did not match the checksum its own \
signature named. A store that does not verify that header lets a client with push \
rights put chosen bytes under a chosen digest, and every repository that later pushes \
that digest would get a marker pointing at them"
}
Checksums::Unknown => {
"this object store could not be asked whether it verifies upload checksums. Handing out \
a write URL is only safe if the store refuses a body that does not match it, and that \
has not been established"
}
};
tracing::error!(
"{refusal}, so LFSX_S3_PRESIGN is being ignored and uploads keep coming through this server"
);
if let crate::config::Storage::Bucket { presign, .. } = &mut config.storage {
*presign = false;
}
}
pub async fn verify_locking(config: &mut Config) {
use crate::storage::s3::probe::{Conditional, conditional_writes};
let Some(keys) = keyspace(config) else {
return;
};
let refusal = match conditional_writes(&keys).await {
Conditional::Enforced => return,
Conditional::Ignored => {
"this object store wrote the same key twice under a condition that should have refused \
the second, so it cannot say which of two clients racing for a lock arrived first"
}
Conditional::Unknown => {
"this object store could not be asked whether it refuses a conditional write, and lock \
uniqueness is exactly that refusal"
}
};
tracing::error!(
"{refusal}, so taking a lock here answers 501. Objects are unaffected, and so is everything \
else this server does"
);
if let crate::config::Storage::Bucket { locking, .. } = &mut config.storage {
*locking = false;
}
}
fn keyspace(config: &Config) -> Option<Keyspace> {
let crate::config::Storage::Bucket {
endpoint,
bucket,
region,
access_key,
secret_key,
path_style,
..
} = &config.storage
else {
return None;
};
Some(
Keyspace::new(&S3Config {
endpoint: endpoint.clone(),
bucket: bucket.clone(),
region: region.clone(),
access_key: access_key.clone(),
secret_key: secret_key.clone(),
path_style: *path_style,
lifetime: std::time::Duration::from_secs(config.action_lifetime.into()),
})
.expect("the bucket configuration is not usable"),
)
}
fn backends(config: &Config) -> (Store, LockStore) {
if let crate::config::Auth::Forge {
anonymous_read: true,
..
} = config.auth
{
tracing::info!(
"anonymous read is on: a request with no credentials is resolved against the forge, so objects in a repository the forge serves publicly can be read by anybody, and the bandwidth is yours. Unset LFSX_ANONYMOUS_READ to require a token whatever the repository's visibility"
);
}
let keys = config.encryption_key_file.as_deref().map(|path| {
std::sync::Arc::new(
crate::storage::crypt::Keyring::load(path)
.expect("the encryption key file is not usable"),
)
});
let local = LocalStore::new(config.storage_root.clone())
.with_max_object_size(config.max_object_size)
.with_compression(config.compression)
.with_encryption(keys);
let (store, lock_backend) = match &config.storage {
crate::config::Storage::Local => (
Store::local(local),
LockStore::local(config.storage_root.clone()),
),
crate::config::Storage::Bucket {
presign, locking, ..
} => {
let keys = keyspace(config).expect("a bucket keyspace for a bucket store");
tracing::warn!(
"objects and locks are stored in a bucket: deduplication, rewriting and verification answer 501, and the lfsx_objects_stored and lfsx_store_bytes gauges are not measured — read capacity from the bucket itself"
);
if *presign {
if config.encryption_key_file.is_some() || config.compression.is_some() {
tracing::warn!(
"LFSX_S3_PRESIGN=true, but a codec is configured, so downloads keep streaming through this server: what sits in the bucket is a frame under the plaintext digest, and a client handed that directly would hash it and reject the object"
);
} else {
tracing::warn!(
"LFSX_S3_PRESIGN=true, downloads are redirected to the bucket, so lfsx_downloaded_bytes stops counting them and the bucket serves the ranges"
);
}
if config.encryption_key_file.is_some() {
tracing::warn!(
"LFSX_ENCRYPTION_KEY_FILE is set, so uploads keep coming through this server rather than going straight to the bucket: an object a client writes itself would arrive unencrypted"
);
} else if config.compression.is_some() {
tracing::warn!(
"LFSX_COMPRESSION is set, and objects clients upload straight to the bucket arrive uncompressed — only what passes through this server is compressed"
);
}
}
(
Store::bucket(S3Store::new(keys.clone(), *presign), local),
LockStore::bucket(keys).with_conditional_writes(*locking),
)
}
};
(store, lock_backend.with_max_age(config.lock_max_age))
}