Skip to content

Commit 06ef533

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
fix(media): support IRSA/credential-chain S3 auth and configurable signing region (#1406)
Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]>
1 parent f061ae9 commit 06ef533

9 files changed

Lines changed: 240 additions & 11 deletions

File tree

‎crates/buzz-media/src/config.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ fn default_max_file_bytes() -> u64 {
88
104_857_600 // 100 MB
99
}
1010

11+
fn default_s3_region() -> String {
12+
"us-east-1".to_string()
13+
}
14+
1115
/// Configuration for media storage (S3/MinIO).
1216
#[derive(Debug, Clone, serde::Deserialize)]
1317
pub struct MediaConfig {
@@ -19,6 +23,14 @@ pub struct MediaConfig {
1923
pub s3_secret_key: String,
2024
/// S3 bucket name.
2125
pub s3_bucket: String,
26+
/// AWS region for SigV4 request signing (e.g. "us-west-2").
27+
///
28+
/// Must match the region of `s3_endpoint` for real AWS S3, otherwise
29+
/// requests are signed with the wrong credential scope and AWS rejects
30+
/// them. Defaults to "us-east-1" to preserve MinIO/local behavior, where
31+
/// the value is not meaningfully checked.
32+
#[serde(default = "default_s3_region")]
33+
pub s3_region: String,
2234
/// Maximum upload size for images (bytes). Default: 50 MB.
2335
pub max_image_bytes: u64,
2436
/// Maximum upload size for animated GIFs (bytes). Default: 10 MB.

‎crates/buzz-media/src/storage.rs‎

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,43 @@ pub struct MediaStorage {
2222

2323
impl MediaStorage {
2424
/// Create a new storage client from config.
25+
///
26+
/// Credential selection:
27+
/// - If both `s3_access_key` and `s3_secret_key` are non-empty, use them as
28+
/// static credentials (MinIO/local/dev, or any static-key deployment).
29+
/// - Otherwise, fall back to the AWS default credential chain via
30+
/// [`Credentials::default`]: environment, shared profile, web-identity
31+
/// token (IRSA on EKS — `AssumeRoleWithWebIdentity`), container, and
32+
/// instance-metadata providers, in that order. This lets the relay use
33+
/// the pod's IAM role without long-lived static keys.
2534
pub fn new(config: &MediaConfig) -> Result<Self, MediaError> {
2635
let region = Region::Custom {
27-
region: "us-east-1".into(),
36+
region: config.s3_region.clone(),
2837
endpoint: config.s3_endpoint.clone(),
2938
};
30-
let creds = Credentials::new(
31-
Some(&config.s3_access_key),
32-
Some(&config.s3_secret_key),
33-
None,
34-
None,
35-
None,
36-
)
39+
let creds = match (
40+
config.s3_access_key.is_empty(),
41+
config.s3_secret_key.is_empty(),
42+
) {
43+
(false, false) => Credentials::new(
44+
Some(&config.s3_access_key),
45+
Some(&config.s3_secret_key),
46+
None,
47+
None,
48+
None,
49+
),
50+
(true, true) => {
51+
// No static keys configured: resolve from the AWS credential chain
52+
// (IRSA web-identity, env, profile, instance metadata).
53+
Credentials::default()
54+
}
55+
_ => {
56+
return Err(MediaError::StorageError(
57+
"s3_access_key and s3_secret_key must be configured together, or both empty to use the AWS credential chain"
58+
.to_string(),
59+
));
60+
}
61+
}
3762
.map_err(|e| MediaError::StorageError(e.to_string()))?;
3863
let bucket = Bucket::new(&config.s3_bucket, region, creds)
3964
.map_err(|e| MediaError::StorageError(e.to_string()))?
@@ -219,6 +244,55 @@ mod tests {
219244
)
220245
}
221246

247+
fn storage_config(access: &str, secret: &str) -> crate::config::MediaConfig {
248+
crate::config::MediaConfig {
249+
s3_endpoint: "http://localhost:9000".to_string(),
250+
s3_access_key: access.to_string(),
251+
s3_secret_key: secret.to_string(),
252+
s3_bucket: "buzz-media".to_string(),
253+
s3_region: "us-west-2".to_string(),
254+
max_image_bytes: 50 * 1024 * 1024,
255+
max_gif_bytes: 10 * 1024 * 1024,
256+
max_video_bytes: 524_288_000,
257+
max_file_bytes: 104_857_600,
258+
public_base_url: "http://localhost:3000/media".to_string(),
259+
}
260+
}
261+
262+
/// Static keys present: builds a client without touching the AWS
263+
/// credential chain (no env/metadata access), and the signing region
264+
/// comes from config rather than a hardcoded "us-east-1".
265+
#[test]
266+
fn static_keys_build_client_with_configured_region() {
267+
let storage = MediaStorage::new(&storage_config("buzz_dev", "buzz_dev_secret"))
268+
.expect("static creds should build a client");
269+
match storage.bucket.region {
270+
Region::Custom { ref region, .. } => assert_eq!(region, "us-west-2"),
271+
other => panic!("expected Custom region, got {other:?}"),
272+
}
273+
}
274+
275+
#[test]
276+
fn partial_static_keys_are_rejected() {
277+
let err = match MediaStorage::new(&storage_config("buzz_dev", "")) {
278+
Ok(_) => panic!("partial static creds must not silently use credential chain"),
279+
Err(err) => err,
280+
};
281+
assert!(
282+
err.to_string().contains("must be configured together"),
283+
"unexpected error: {err}"
284+
);
285+
286+
let err = match MediaStorage::new(&storage_config("", "buzz_dev_secret")) {
287+
Ok(_) => panic!("partial static creds must not silently use credential chain"),
288+
Err(err) => err,
289+
};
290+
assert!(
291+
err.to_string().contains("must be configured together"),
292+
"unexpected error: {err}"
293+
);
294+
}
295+
222296
#[test]
223297
fn sidecar_keys_are_community_scoped() {
224298
let a = tenant(1);

‎crates/buzz-media/src/upload.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ mod tests {
461461
s3_access_key: String::new(),
462462
s3_secret_key: String::new(),
463463
s3_bucket: String::new(),
464+
s3_region: "us-east-1".to_string(),
464465
max_image_bytes: 50 * 1024 * 1024,
465466
max_gif_bytes: 10 * 1024 * 1024,
466467
max_video_bytes: 524_288_000,

‎crates/buzz-media/src/validation.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ mod tests {
416416
s3_access_key: String::new(),
417417
s3_secret_key: String::new(),
418418
s3_bucket: String::new(),
419+
s3_region: "us-east-1".to_string(),
419420
max_image_bytes: 50 * 1024 * 1024,
420421
max_gif_bytes: 10 * 1024 * 1024,
421422
max_video_bytes: 524_288_000,
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//! Live round-trip test for the **static-credentials** S3 path against a local
2+
//! MinIO, guarded by `#[ignore]`.
3+
//!
4+
//! This is the path local/dev and any static-key deployment uses
5+
//! (`s3_access_key`/`s3_secret_key` both non-empty -> `Credentials::new`). It
6+
//! exists to prove that adding the IRSA/credential-chain fallback did **not**
7+
//! regress hardcoded credentials.
8+
//!
9+
//! Run it against the docker-compose MinIO (creds `buzz_dev`/`buzz_dev_secret`,
10+
//! bucket `buzz-media`, endpoint `http://localhost:9000`):
11+
//!
12+
//! ```bash
13+
//! docker compose up -d minio minio-init
14+
//! cargo test -p buzz-media --test static_creds_minio -- --ignored
15+
//! ```
16+
//!
17+
//! Overridable via `BUZZ_S3_ENDPOINT` / `BUZZ_S3_ACCESS_KEY` /
18+
//! `BUZZ_S3_SECRET_KEY` / `BUZZ_S3_BUCKET`.
19+
20+
use buzz_media::config::MediaConfig;
21+
use buzz_media::storage::MediaStorage;
22+
23+
fn minio_config() -> MediaConfig {
24+
MediaConfig {
25+
s3_endpoint: std::env::var("BUZZ_S3_ENDPOINT")
26+
.unwrap_or_else(|_| "http://localhost:9000".to_string()),
27+
s3_access_key: std::env::var("BUZZ_S3_ACCESS_KEY")
28+
.unwrap_or_else(|_| "buzz_dev".to_string()),
29+
s3_secret_key: std::env::var("BUZZ_S3_SECRET_KEY")
30+
.unwrap_or_else(|_| "buzz_dev_secret".to_string()),
31+
s3_bucket: std::env::var("BUZZ_S3_BUCKET").unwrap_or_else(|_| "buzz-media".to_string()),
32+
s3_region: "us-east-1".to_string(),
33+
max_image_bytes: 50 * 1024 * 1024,
34+
max_gif_bytes: 10 * 1024 * 1024,
35+
max_video_bytes: 524_288_000,
36+
max_file_bytes: 104_857_600,
37+
public_base_url: "http://localhost:3000/media".to_string(),
38+
}
39+
}
40+
41+
#[tokio::test]
42+
#[ignore = "requires a live MinIO (docker compose up -d minio minio-init)"]
43+
async fn static_creds_round_trip_against_minio() {
44+
let storage =
45+
MediaStorage::new(&minio_config()).expect("static creds should build a storage client");
46+
47+
let key = format!("_test/static-creds-{}.bin", std::process::id());
48+
let body = b"hardcoded-creds-still-work";
49+
50+
// PUT
51+
storage
52+
.put(&key, body, "application/octet-stream")
53+
.await
54+
.expect("put with static creds should succeed");
55+
56+
// HEAD -> exists with correct size
57+
assert!(storage.head(&key).await.expect("head should succeed"));
58+
let meta = storage
59+
.head_with_metadata(&key)
60+
.await
61+
.expect("head_with_metadata should succeed")
62+
.expect("object should exist");
63+
assert_eq!(meta.size, body.len() as u64);
64+
65+
// GET round-trips the bytes
66+
let got = storage.get(&key).await.expect("get should succeed");
67+
assert_eq!(got, body);
68+
69+
// DELETE, then HEAD reports absence
70+
storage.delete(&key).await.expect("delete should succeed");
71+
assert!(!storage.head(&key).await.expect("head after delete"));
72+
}

‎crates/buzz-relay/src/api/git/hydrate.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ mod tests {
459459
"buzz_dev",
460460
"buzz_dev_secret",
461461
"buzz-git",
462+
"us-east-1",
462463
)
463464
.expect("connect minio")
464465
}

‎crates/buzz-relay/src/api/git/store.rs‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ pub enum StoreError {
9191
/// Any other backend / transport error.
9292
#[error("s3 backend error: {0}")]
9393
Backend(#[from] S3Error),
94+
/// Invalid storage configuration detected at client construction.
95+
#[error("git store config error: {0}")]
96+
Config(String),
9497
/// Conformance probe failed — backend does not satisfy A1/A2/A3.
9598
#[error(transparent)]
9699
Probe(ProbeFailure),
@@ -172,18 +175,40 @@ impl GitStore {
172175
/// Build a client against an S3-compatible endpoint (e.g. MinIO).
173176
///
174177
/// Uses path-style addressing for MinIO compatibility; AWS S3 accepts both.
178+
///
179+
/// Credential selection mirrors [`buzz_media::MediaStorage::new`]:
180+
/// - both `access_key` and `secret_key` non-empty → static credentials
181+
/// (MinIO/local/dev, or any static-key deployment);
182+
/// - both empty → the AWS default credential chain via
183+
/// [`Credentials::default`] (env, profile, web-identity/IRSA, container,
184+
/// instance metadata), so the relay can use its pod IAM role;
185+
/// - exactly one empty → a config error, to surface a half-configured
186+
/// static deployment instead of silently falling back to the chain.
175187
pub fn new(
176188
endpoint: &str,
177189
access_key: &str,
178190
secret_key: &str,
179191
bucket_name: &str,
192+
region: &str,
180193
) -> Result<Self, StoreError> {
181194
let region = Region::Custom {
182-
region: "us-east-1".into(),
195+
region: region.into(),
183196
endpoint: endpoint.into(),
184197
};
185-
let creds = Credentials::new(Some(access_key), Some(secret_key), None, None, None)
186-
.map_err(|e| StoreError::Backend(S3Error::Credentials(e)))?;
198+
let creds = match (access_key.is_empty(), secret_key.is_empty()) {
199+
(false, false) => Credentials::new(Some(access_key), Some(secret_key), None, None, None),
200+
(true, true) => {
201+
// No static keys configured: resolve from the AWS credential
202+
// chain (IRSA web-identity, env, profile, instance metadata).
203+
Credentials::default()
204+
}
205+
_ => {
206+
return Err(StoreError::Config(
207+
"s3 access_key and secret_key must be configured together, or both empty to use the AWS credential chain".to_string(),
208+
));
209+
}
210+
}
211+
.map_err(|e| StoreError::Backend(S3Error::Credentials(e)))?;
187212
let bucket = Bucket::new(bucket_name, region, creds)
188213
.map_err(StoreError::Backend)?
189214
.with_path_style();
@@ -896,6 +921,44 @@ mod tests {
896921
Err(StoreError::Backend(S3Error::HttpFailWithBody(403, _)))
897922
));
898923
}
924+
925+
#[test]
926+
fn static_keys_build_store_with_configured_region() {
927+
let store = GitStore::new(
928+
"http://localhost:9000",
929+
"buzz_dev",
930+
"buzz_dev_secret",
931+
"buzz-git",
932+
"us-west-2",
933+
)
934+
.expect("static creds should build a git store");
935+
match store.bucket.region {
936+
Region::Custom { ref region, .. } => assert_eq!(region, "us-west-2"),
937+
ref other => panic!("expected Custom region, got {other:?}"),
938+
}
939+
}
940+
941+
#[test]
942+
fn partial_static_keys_are_rejected() {
943+
for (access, secret) in [("buzz_dev", ""), ("", "buzz_dev_secret")] {
944+
let err = match GitStore::new(
945+
"http://localhost:9000",
946+
access,
947+
secret,
948+
"buzz-git",
949+
"us-east-1",
950+
) {
951+
Ok(_) => {
952+
panic!("partial static creds must not silently use the credential chain")
953+
}
954+
Err(err) => err,
955+
};
956+
assert!(
957+
matches!(err, StoreError::Config(_)),
958+
"expected Config error, got {err:?}"
959+
);
960+
}
961+
}
899962
}
900963

901964
#[cfg(test)]
@@ -920,6 +983,7 @@ mod probe {
920983
"buzz_dev",
921984
"buzz_dev_secret",
922985
"buzz-git",
986+
"us-east-1",
923987
)
924988
.expect("connect minio")
925989
}

‎crates/buzz-relay/src/config.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ impl Config {
296296
s3_secret_key: std::env::var("BUZZ_S3_SECRET_KEY")
297297
.unwrap_or_else(|_| "buzz_dev_secret".to_string()),
298298
s3_bucket: std::env::var("BUZZ_S3_BUCKET").unwrap_or_else(|_| "buzz-media".to_string()),
299+
s3_region: std::env::var("BUZZ_S3_REGION")
300+
.or_else(|_| std::env::var("AWS_REGION"))
301+
.unwrap_or_else(|_| "us-east-1".to_string()),
299302
max_image_bytes: std::env::var("BUZZ_MAX_IMAGE_BYTES")
300303
.ok()
301304
.and_then(|v| v.parse().ok())

‎crates/buzz-relay/src/state.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ impl AppState {
370370
&config.media.s3_access_key,
371371
&config.media.s3_secret_key,
372372
&config.media.s3_bucket,
373+
&config.media.s3_region,
373374
)
374375
.expect("media storage was already constructed with this S3 config");
375376
let nip98_replay: Arc<dyn Nip98ReplayGuard> =

0 commit comments

Comments
 (0)