@@ -22,18 +22,43 @@ pub struct MediaStorage {
2222
2323impl 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 ) ;
0 commit comments