1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//! 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<Option<u64>>;
}
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<Option<u64>> {
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<Client> {
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<Option<u64>> {
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<S3Stats> {
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<u64> {
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<u64> {
let size = tokio::fs::metadata(src)
.await
.with_context(|| format!("stat {}", src.display()))?
.len();
let mut last_error: Option<anyhow::Error> = 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<Option<Vec<u8>>> {
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);
}
}