@@ -243,75 +243,90 @@ fn canonicalize_headers(header_map: &HeaderMap) -> (String, String) {
243
243
( signed_headers, canonical_headers)
244
244
}
245
245
246
+ /// Provides credentials for use when signing requests
246
247
#[ derive( Debug ) ]
247
248
pub enum CredentialProvider {
248
- Static {
249
- credential : Arc < AwsCredential > ,
250
- } ,
251
- /// <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html>
252
- Instance {
253
- cache : TokenCache < Arc < AwsCredential > > ,
254
- } ,
255
- WebIdentity {
256
- cache : TokenCache < Arc < AwsCredential > > ,
257
- token : String ,
258
- role_arn : String ,
259
- session_name : String ,
260
- endpoint : String ,
261
- } ,
249
+ Static ( StaticCredentialProvider ) ,
250
+ Instance ( InstanceCredentialProvider ) ,
251
+ WebIdentity ( WebIdentityProvider ) ,
262
252
}
263
253
264
254
impl CredentialProvider {
265
- pub async fn get_credential (
266
- & self ,
267
- client : & Client ,
268
- retry_config : & RetryConfig ,
269
- ) -> Result < Arc < AwsCredential > > {
255
+ pub async fn get_credential ( & self ) -> Result < Arc < AwsCredential > > {
270
256
match self {
271
- CredentialProvider :: Static { credential } => Ok ( Arc :: clone ( credential) ) ,
272
- CredentialProvider :: Instance { cache } => {
273
- cache
274
- . get_or_insert_with ( || {
275
- const METADATA_ENDPOINT : & str = "http://169.254.169.254" ;
276
- instance_creds ( client, retry_config, METADATA_ENDPOINT ) . map_err (
277
- |source| crate :: Error :: Generic {
278
- store : "S3" ,
279
- source,
280
- } ,
281
- )
282
- } )
283
- . await
284
- }
285
- CredentialProvider :: WebIdentity {
286
- cache,
287
- token,
288
- role_arn,
289
- session_name,
290
- endpoint,
291
- } => {
292
- cache
293
- . get_or_insert_with ( || {
294
- web_identity (
295
- client,
296
- retry_config,
297
- token,
298
- role_arn,
299
- session_name,
300
- endpoint,
301
- )
302
- . map_err ( |source| {
303
- crate :: Error :: Generic {
304
- store : "S3" ,
305
- source,
306
- }
307
- } )
308
- } )
309
- . await
310
- }
257
+ CredentialProvider :: Static ( s) => Ok ( Arc :: clone ( & s. credential ) ) ,
258
+ CredentialProvider :: Instance ( c) => c. get_credential ( ) . await ,
259
+ CredentialProvider :: WebIdentity ( c) => c. get_credential ( ) . await ,
311
260
}
312
261
}
313
262
}
314
263
264
+ /// A static set of credentials
265
+ #[ derive( Debug ) ]
266
+ pub struct StaticCredentialProvider {
267
+ pub credential : Arc < AwsCredential > ,
268
+ }
269
+
270
+ /// Credentials sourced from the instance metadata service
271
+ ///
272
+ /// <https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html>
273
+ #[ derive( Debug ) ]
274
+ pub struct InstanceCredentialProvider {
275
+ pub cache : TokenCache < Arc < AwsCredential > > ,
276
+ pub client : Client ,
277
+ pub retry_config : RetryConfig ,
278
+ }
279
+
280
+ impl InstanceCredentialProvider {
281
+ async fn get_credential ( & self ) -> Result < Arc < AwsCredential > > {
282
+ self . cache
283
+ . get_or_insert_with ( || {
284
+ const METADATA_ENDPOINT : & str = "http://169.254.169.254" ;
285
+ instance_creds ( & self . client , & self . retry_config , METADATA_ENDPOINT )
286
+ . map_err ( |source| crate :: Error :: Generic {
287
+ store : "S3" ,
288
+ source,
289
+ } )
290
+ } )
291
+ . await
292
+ }
293
+ }
294
+
295
+ /// Credentials sourced using AssumeRoleWithWebIdentity
296
+ ///
297
+ /// <https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html>
298
+ #[ derive( Debug ) ]
299
+ pub struct WebIdentityProvider {
300
+ pub cache : TokenCache < Arc < AwsCredential > > ,
301
+ pub token : String ,
302
+ pub role_arn : String ,
303
+ pub session_name : String ,
304
+ pub endpoint : String ,
305
+ pub client : Client ,
306
+ pub retry_config : RetryConfig ,
307
+ }
308
+
309
+ impl WebIdentityProvider {
310
+ async fn get_credential ( & self ) -> Result < Arc < AwsCredential > > {
311
+ self . cache
312
+ . get_or_insert_with ( || {
313
+ web_identity (
314
+ & self . client ,
315
+ & self . retry_config ,
316
+ & self . token ,
317
+ & self . role_arn ,
318
+ & self . session_name ,
319
+ & self . endpoint ,
320
+ )
321
+ . map_err ( |source| crate :: Error :: Generic {
322
+ store : "S3" ,
323
+ source,
324
+ } )
325
+ } )
326
+ . await
327
+ }
328
+ }
329
+
315
330
#[ derive( Debug , Deserialize ) ]
316
331
#[ serde( rename_all = "PascalCase" ) ]
317
332
struct InstanceCredentials {
0 commit comments