//! S3 cache download and snapshot upload.
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use aws_credential_types::Credentials;
use aws_sdk_s3::Client;
use aws_sdk_s3::config::{BehaviorVersion, Region, RequestChecksumCalculation};
use aws_sdk_s3::primitives::ByteStream;
use serde::Serialize;
use serde_json::json;
pub use crate::config::S3Config;
use crate::db::DbHandle;
use crate::log::{Level, Logger};
use crate::time::jitter;
const SQLITE_CONTENT_TYPE: &str = "application/vnd.sqlite3";
/// One initial upload attempt plus one per backoff delay.
const UPLOAD_ATTEMPTS: usize = 4;
const UPLOAD_BACKOFF_SECS: [f64; 3] = [1.0, 2.0, 4.0];
const UPLOAD_JITTER_SECS: f64 = 0.25;
/// Object-store seam for snapshot uploads. Production uses [`AwsSink`];
/// tests script put/head outcomes without network.
#[async_trait]
pub trait S3ObjectSink: Send + Sync {
async fn put(&self, cfg: &S3Config, src: &Path) -> Result<()>;
/// Remote object size after upload; `Ok(None)` when the store cannot
/// report it. Verification is skipped when unavailable.
async fn size(&self, cfg: &S3Config, key: &str) -> Result>;
}
struct AwsSink;
#[async_trait]
impl S3ObjectSink for AwsSink {
async fn put(&self, cfg: &S3Config, src: &Path) -> Result<()> {
let body = ByteStream::from_path(src)
.await
.with_context(|| format!("read {}", src.display()))?;
let client = client(cfg).await?;
client
.put_object()
.bucket(&cfg.bucket)
.key(&cfg.path)
.content_type(SQLITE_CONTENT_TYPE)
.body(body)
.send()
.await
.map_err(|err| {
anyhow::Error::new(err).context(format!("s3 put s3://{}/{}", cfg.bucket, cfg.path))
})?;
Ok(())
}
async fn size(&self, cfg: &S3Config, key: &str) -> Result > {
let client = client(cfg).await?;
match client
.head_object()
.bucket(&cfg.bucket)
.key(key)
.send()
.await
{
Ok(object) => Ok(object
.content_length()
.and_then(|length| u64::try_from(length).ok())),
Err(err) => {
let not_found = match &err {
aws_sdk_s3::error::SdkError::ServiceError(ctx) => {
ctx.raw().status().as_u16() == 404
}
_ => false,
};
if not_found {
return Ok(None);
}
Err(anyhow::Error::new(err).context(format!("s3 head s3://{}/{}", cfg.bucket, key)))
}
}
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct S3Stats {
pub download_bytes: u64,
pub upload_bytes: u64,
pub download_seconds: f64,
pub upload_seconds: f64,
}
/// Strip a leading `{bucket}.` label from the endpoint host, preserving the
/// scheme, port, path, and query. Normalizes bucket-prefixed endpoints.
pub fn normalize_endpoint(endpoint: &str, bucket: &str) -> String {
if bucket.is_empty() {
return endpoint.to_owned();
}
let Some((scheme, rest)) = endpoint.split_once("://") else {
return endpoint.to_owned();
};
let split = rest.find(['/', '?', '#']).unwrap_or(rest.len());
let (authority, tail) = rest.split_at(split);
let (userinfo, host_port) = match authority.rsplit_once('@') {
Some((userinfo, host_port)) => (Some(userinfo), host_port),
None => (None, authority),
};
let Some(host) = host_port.strip_prefix(format!("{bucket}.").as_str()) else {
return endpoint.to_owned();
};
let mut normalized = format!("{scheme}://");
if let Some(userinfo) = userinfo {
normalized.push_str(userinfo);
normalized.push('@');
}
normalized.push_str(host);
normalized.push_str(tail);
normalized
}
/// Temporary sibling used while downloading, e.g. `cache.db.download`.
pub fn download_tmp_path(dest: &Path) -> PathBuf {
match dest.extension() {
Some(ext) => dest.with_extension(format!("{}.download", ext.to_string_lossy())),
None => dest.with_extension("download"),
}
}
/// Build an S3 client: static credentials when configured, otherwise the
/// default AWS credential chain.
pub async fn client(cfg: &S3Config) -> Result {
let access_key = cfg.access_key_id.as_deref().filter(|key| !key.is_empty());
let secret_key = cfg
.secret_access_key
.as_deref()
.filter(|key| !key.is_empty());
let mut builder = match (access_key, secret_key) {
(Some(access_key), Some(secret_key)) => aws_sdk_s3::config::Builder::new()
.behavior_version(BehaviorVersion::latest())
.credentials_provider(Credentials::new(
access_key,
secret_key,
None,
None,
"vuln-notif-static",
)),
(None, None) => {
let sdk_config = aws_config::defaults(BehaviorVersion::latest()).load().await;
aws_sdk_s3::config::Builder::from(&sdk_config)
}
_ => anyhow::bail!("s3 access key and secret key must be configured together"),
};
builder = builder
.region(Region::new(cfg.region.clone()))
.request_checksum_calculation(RequestChecksumCalculation::WhenRequired);
if let Some(endpoint) = cfg.endpoint_url.as_deref().filter(|url| !url.is_empty()) {
builder = builder
.endpoint_url(normalize_endpoint(endpoint, &cfg.bucket))
.force_path_style(true);
}
Ok(Client::from_conf(builder.build()))
}
/// Download the remote cache to `dest`; `None` when the object is absent.
pub async fn download(cfg: &S3Config, dest: &Path) -> Result> {
let client = client(cfg).await?;
let object = match client
.get_object()
.bucket(&cfg.bucket)
.key(&cfg.path)
.send()
.await
{
Ok(object) => object,
Err(err) => {
let not_found = match &err {
aws_sdk_s3::error::SdkError::ServiceError(ctx) => {
ctx.err().is_no_such_key() || ctx.raw().status().as_u16() == 404
}
_ => false,
};
if not_found {
return Ok(None);
}
return Err(
anyhow::Error::new(err).context(format!("s3 get s3://{}/{}", cfg.bucket, cfg.path))
);
}
};
let bytes = object
.body
.collect()
.await
.with_context(|| format!("read s3://{}/{} body", cfg.bucket, cfg.path))?
.into_bytes();
if let Some(parent) = dest
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
{
tokio::fs::create_dir_all(parent)
.await
.with_context(|| format!("create {}", parent.display()))?;
}
let tmp = download_tmp_path(dest);
tokio::fs::write(&tmp, &bytes)
.await
.with_context(|| format!("write {}", tmp.display()))?;
if let Err(err) = tokio::fs::rename(&tmp, dest).await {
let _ = tokio::fs::remove_file(&tmp).await;
return Err(anyhow::Error::new(err).context(format!(
"rename {} -> {}",
tmp.display(),
dest.display()
)));
}
Ok(Some(bytes.len() as u64))
}
/// Snapshot the live cache with `VACUUM INTO` and upload it.
///
/// `src` is the live cache path, used only for error context; the snapshot is
/// written to a temp file and removed on every path.
pub async fn snapshot_upload(cfg: &S3Config, db: &DbHandle, src: &Path) -> Result {
let tmp = TempSnapshot::new(temp_snapshot_path());
db.snapshot(&tmp.path)
.await
.with_context(|| format!("snapshot {} to {}", src.display(), tmp.path.display()))?;
let started = std::time::Instant::now();
let upload_bytes = upload(cfg, &tmp.path)
.await
.with_context(|| format!("upload snapshot of {}", src.display()))?;
Ok(S3Stats {
upload_bytes,
upload_seconds: started.elapsed().as_secs_f64(),
..S3Stats::default()
})
}
/// Upload `src` as the configured snapshot object; returns the file size.
pub async fn upload(cfg: &S3Config, src: &Path) -> Result {
upload_with(cfg, src, &AwsSink).await
}
/// Upload through `sink` with bounded exponential retries and a post-upload
/// size check when the store can report it.
///
/// Every failed attempt (including a size mismatch) is retried; the final
/// failure is logged with bucket/path/bytes/error and returned to the caller
/// so a lost snapshot is never silently swallowed.
pub async fn upload_with(cfg: &S3Config, src: &Path, sink: &dyn S3ObjectSink) -> Result {
let size = tokio::fs::metadata(src)
.await
.with_context(|| format!("stat {}", src.display()))?
.len();
let mut last_error: Option = None;
for attempt in 1..=UPLOAD_ATTEMPTS {
let outcome = match sink.put(cfg, src).await {
Ok(()) => match sink.size(cfg, &cfg.path).await {
Ok(Some(remote)) if remote != size => Err(anyhow!(
"uploaded object size mismatch: local {size}, remote {remote}"
)),
// Unverifiable (absent head permission, or no length reported)
// is not a failure; the put already succeeded.
_ => Ok(()),
},
Err(error) => Err(error),
};
match outcome {
Ok(()) => return Ok(size),
Err(error) => last_error = Some(error),
}
if attempt < UPLOAD_ATTEMPTS {
let delay = UPLOAD_BACKOFF_SECS[attempt - 1] + jitter(UPLOAD_JITTER_SECS);
Logger::stderr(Level::Warn).warn(
"s3_upload_retry",
Some("s3"),
json!({
"bucket": cfg.bucket,
"path": cfg.path,
"attempt": attempt,
"sleep_seconds": crate::metrics::round3(delay),
}),
);
tokio::time::sleep(std::time::Duration::from_secs_f64(delay)).await;
}
}
let error = last_error
.unwrap_or_else(|| anyhow!("s3 upload failed without an error"))
.context(format!("s3 put s3://{}/{}", cfg.bucket, cfg.path));
Logger::stderr(Level::Error).error(
"s3_upload_failed",
Some("s3"),
json!({
"bucket": cfg.bucket,
"path": cfg.path,
"bytes": size,
"error": format!("{error:#}"),
}),
);
Err(error)
}
/// Upload raw bytes to one object key (artifact mirror path).
pub async fn put_bytes(cfg: &S3Config, key: &str, bytes: &[u8]) -> Result<()> {
let client = client(cfg).await?;
client
.put_object()
.bucket(&cfg.bucket)
.key(key)
.content_type("application/octet-stream")
.body(ByteStream::from(bytes.to_vec()))
.send()
.await
.map_err(|err| {
anyhow::Error::new(err).context(format!("s3 put s3://{}/{key}", cfg.bucket))
})?;
Ok(())
}
/// Read one object key; `None` when the object is absent.
pub async fn get_bytes(cfg: &S3Config, key: &str) -> Result>> {
let client = client(cfg).await?;
let object = match client
.get_object()
.bucket(&cfg.bucket)
.key(key)
.send()
.await
{
Ok(object) => object,
Err(err) => {
let not_found = match &err {
aws_sdk_s3::error::SdkError::ServiceError(ctx) => {
ctx.err().is_no_such_key() || ctx.raw().status().as_u16() == 404
}
_ => false,
};
if not_found {
return Ok(None);
}
return Err(
anyhow::Error::new(err).context(format!("s3 get s3://{}/{key}", cfg.bucket))
);
}
};
let bytes = object
.body
.collect()
.await
.with_context(|| format!("read s3://{}/{key} body", cfg.bucket))?
.into_bytes();
Ok(Some(bytes.to_vec()))
}
fn temp_snapshot_path() -> PathBuf {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.as_nanos());
let sequence = COUNTER.fetch_add(1, Ordering::Relaxed);
std::env::temp_dir().join(format!(
"vuln-notif-snapshot-{}-{nanos}-{sequence}.db",
std::process::id()
))
}
struct TempSnapshot {
path: PathBuf,
}
impl TempSnapshot {
fn new(path: PathBuf) -> Self {
Self { path }
}
}
impl Drop for TempSnapshot {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}