From 75236390bbab46c05bfff786a69d9270a471f7c1 Mon Sep 17 00:00:00 2001 From: Chase Miller Date: Mon, 14 Apr 2025 12:32:00 -0400 Subject: [PATCH] Expose [Try]AddAWSCredentials extension methods --- .../ClientFactory.cs | 4 +- .../ServiceCollectionExtensions.cs | 98 ++++++++++++++++++- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs index 47bd3f1666d3..cb053116150d 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs @@ -58,10 +58,10 @@ internal ClientFactory(AWSOptions awsOptions, IAWSCredentialsFactory credentials /// Creates the AWS service client that implements the service client interface. /// /// The AWS service client - internal IAmazonService CreateServiceClient() + internal IAmazonService CreateServiceClient(AWSCredentials credentials = null) { PerformGlobalConfig(_logger, _options); - var credentials = _credentialsFactory.Create(); + credentials = credentials ?? _credentialsFactory.Create(); if (!string.IsNullOrEmpty(_options?.SessionRoleArn)) { diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs index 4044dfe4bba5..5750ce9f7d8a 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs @@ -65,6 +65,94 @@ public static IServiceCollection AddDefaultAWSOptions( return collection; } + /// + /// + /// + /// + /// + /// + public static IServiceCollection AddAWSCredentials( + this IServiceCollection collection, + ServiceLifetime lifetime = ServiceLifetime.Singleton) + { + collection.Add(new ServiceDescriptor(typeof(AWSCredentials), sp => sp.CreateDefaultAWSCredentials(), lifetime)); + return collection; + } + + /// + /// + /// + /// + /// + /// + public static IServiceCollection AddAWSCredentials( + this IServiceCollection collection, + AWSCredentials credentials) + { + collection.Add(new ServiceDescriptor(typeof(AWSCredentials), credentials)); + return collection; + } + + /// + /// + /// + /// + /// + /// + /// + public static IServiceCollection AddAWSCredentials( + this IServiceCollection collection, + Func credentialsFunc, + ServiceLifetime lifetime = ServiceLifetime.Singleton) + { + collection.Add(new ServiceDescriptor(typeof(AWSCredentials), credentialsFunc, lifetime)); + return collection; + } + + /// + /// + /// + /// + /// + /// + public static IServiceCollection TryAddAWSCredentials( + this IServiceCollection collection, + ServiceLifetime lifetime = ServiceLifetime.Singleton) + { + collection.TryAdd(new ServiceDescriptor(typeof(AWSCredentials), sp => sp.CreateDefaultAWSCredentials(), lifetime)); + return collection; + } + + /// + /// + /// + /// + /// + /// + public static IServiceCollection TryAddAWSCredentials( + this IServiceCollection collection, + AWSCredentials credentials) + { + collection.TryAdd(new ServiceDescriptor(typeof(AWSCredentials), credentials)); + return collection; + } + + /// + /// + /// + /// + /// + /// + /// + public static IServiceCollection TryAddAWSCredentials( + this IServiceCollection collection, + Func credentialsFunc, + ServiceLifetime lifetime = ServiceLifetime.Singleton) + { + collection.TryAdd(new ServiceDescriptor(typeof(AWSCredentials), credentialsFunc, lifetime)); + return collection; + } + /// /// /// @@ -394,13 +482,19 @@ private static object CreateServiceClient(AWSOptions options, IServiceProvide var factory = new ClientFactory(awsOptions, credentialsFactory, logger); - return factory.CreateServiceClient(); + return factory.CreateServiceClient(sp.GetService()); } - private static IAWSCredentialsFactory CreateDefaultCredentialsFactory(IServiceProvider sp) + private static IAWSCredentialsFactory CreateDefaultCredentialsFactory(this IServiceProvider sp) { var options = sp.GetService() ?? new AWSOptions(); return new DefaultAWSCredentialsFactory(options, sp.GetService()); } + + private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp) + { + var credentialsFactory = sp.GetService() ?? sp.CreateDefaultCredentialsFactory(); + return credentialsFactory.Create(); + } } }