From 3101a098b3f740873497dbbfc1b487c73fbc087c Mon Sep 17 00:00:00 2001 From: Chase Miller Date: Mon, 14 Apr 2025 13:39:06 -0400 Subject: [PATCH 1/3] Remove IAWSCredentialsFactory in favor of DefaultAWSCredentials --- .../AWSOptionsExtensions.cs | 6 +- .../ClientFactory.cs | 12 +- ...alsFactory.cs => DefaultAWSCredentials.cs} | 24 ++-- .../IAWSCredentialsFactory.cs | 15 --- .../ServiceCollectionExtensions.cs | 104 +----------------- 5 files changed, 25 insertions(+), 136 deletions(-) rename extensions/src/AWSSDK.Extensions.NETCore.Setup/{DefaultAWSCredentialsFactory.cs => DefaultAWSCredentials.cs} (74%) delete mode 100644 extensions/src/AWSSDK.Extensions.NETCore.Setup/IAWSCredentialsFactory.cs diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptionsExtensions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptionsExtensions.cs index 29225771e992..d7f4341f61f0 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptionsExtensions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/AWSOptionsExtensions.cs @@ -15,11 +15,11 @@ public static class AWSOptionsExtensions /// /// The service interface that a service client will be created for. /// The service client that implements the service interface. - public static T CreateServiceClient(this AWSOptions options, IAWSCredentialsFactory credentialsFactory = null) + public static T CreateServiceClient(this AWSOptions options) where T : class, IAmazonService { - credentialsFactory = credentialsFactory ?? new DefaultAWSCredentialsFactory(options); - var clientFactory = new ClientFactory(options, credentialsFactory, null); + var credentials = new DefaultAWSCredentials(options, null); + var clientFactory = new ClientFactory(options, credentials, null); return clientFactory.CreateServiceClient() as T; } diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs index cb053116150d..503fbee761c5 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs @@ -38,19 +38,19 @@ internal class ClientFactory private static readonly object[] EMPTY_PARAMETERS = Array.Empty(); private readonly AWSOptions _options; - private readonly IAWSCredentialsFactory _credentialsFactory; + private readonly AWSCredentials _credentials; private readonly ILogger _logger; /// /// Constructs an instance of the ClientFactory /// /// The AWS options used for creating service clients. - /// + /// /// - internal ClientFactory(AWSOptions awsOptions, IAWSCredentialsFactory credentialsFactory, ILogger logger) + internal ClientFactory(AWSOptions awsOptions, AWSCredentials credentials, ILogger logger) { _options = awsOptions ?? throw new ArgumentNullException(nameof(awsOptions)); - _credentialsFactory = credentialsFactory ?? throw new ArgumentNullException(nameof(credentialsFactory)); + _credentials = credentials ?? throw new ArgumentNullException(nameof(credentials)); _logger = logger; } @@ -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(AWSCredentials credentials = null) + internal IAmazonService CreateServiceClient() { PerformGlobalConfig(_logger, _options); - credentials = credentials ?? _credentialsFactory.Create(); + var credentials = _credentials; if (!string.IsNullOrEmpty(_options?.SessionRoleArn)) { diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultAWSCredentialsFactory.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultAWSCredentials.cs similarity index 74% rename from extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultAWSCredentialsFactory.cs rename to extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultAWSCredentials.cs index 0d2c88f74e5b..7f7439187535 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultAWSCredentialsFactory.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/DefaultAWSCredentials.cs @@ -9,36 +9,34 @@ namespace AWSSDK.Extensions.NETCore.Setup /// /// /// - public class DefaultAWSCredentialsFactory : IAWSCredentialsFactory + public class DefaultAWSCredentials : AWSCredentials { private readonly AWSOptions _options; private readonly ILogger _logger; /// - /// Creates the AWSCredentials using either the profile indicated from the AWSOptions object - /// of the SDK fallback credentials search. + /// /// + /// /// - /// - /// - public DefaultAWSCredentialsFactory(AWSOptions options, ILogger logger = null) + public DefaultAWSCredentials(AWSOptions awsOptions, ILogger logger) { - _options = options; + _options = awsOptions; _logger = logger; } /// - /// Creates the AWSCredentials using either AWSOptions.Credentials, AWSOptions.Profile + AWSOptions.ProfilesLocation, - /// or the SDK fallback credentials search. + /// /// - public AWSCredentials Create() + /// + public override ImmutableCredentials GetCredentials() { if (_options != null) { if (_options.Credentials != null) { _logger?.LogInformation("Using AWS credentials specified with the AWSOptions.Credentials property"); - return _options.Credentials; + return _options.Credentials.GetCredentials(); } if (!string.IsNullOrEmpty(_options.Profile)) { @@ -47,7 +45,7 @@ public AWSCredentials Create() if (chain.TryGetAWSCredentials(_options.Profile, out result)) { _logger?.LogInformation($"Found AWS credentials for the profile {_options.Profile}"); - return result; + return result.GetCredentials(); } else { @@ -67,7 +65,7 @@ public AWSCredentials Create() _logger?.LogInformation("Found credentials using the AWS SDK's default credential search"); } - return credentials; + return credentials.GetCredentials(); } } } \ No newline at end of file diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/IAWSCredentialsFactory.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/IAWSCredentialsFactory.cs deleted file mode 100644 index 682b1015f3c8..000000000000 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/IAWSCredentialsFactory.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Amazon.Runtime; - -namespace AWSSDK.Extensions.NETCore.Setup -{ - /// - /// - /// - public interface IAWSCredentialsFactory - { - /// - /// Creates AWSCredentials - /// - AWSCredentials Create(); - } -} \ No newline at end of file diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs index 5750ce9f7d8a..fae7be2a6564 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs @@ -153,94 +153,6 @@ public static IServiceCollection TryAddAWSCredentials( return collection; } - /// - /// - /// - /// - /// - /// - public static IServiceCollection AddAWSCredentialsFactory( - this IServiceCollection collection, - ServiceLifetime lifetime = ServiceLifetime.Singleton) - { - collection.Add(new ServiceDescriptor(typeof(IAWSCredentialsFactory), CreateDefaultCredentialsFactory, lifetime)); - return collection; - } - - /// - /// - /// - /// - /// - /// - /// - public static IServiceCollection AddAWSCredentialsFactory( - this IServiceCollection collection, - Func implementationFactory, - ServiceLifetime lifetime = ServiceLifetime.Singleton) - { - collection.Add(new ServiceDescriptor(typeof(IAWSCredentialsFactory), implementationFactory, lifetime)); - return collection; - } - - /// - /// - /// - /// - /// - /// - public static IServiceCollection AddAWSCredentialsFactory( - this IServiceCollection collection, - IAWSCredentialsFactory credentials) - { - collection.Add(new ServiceDescriptor(typeof(IAWSCredentialsFactory), credentials)); - return collection; - } - - /// - /// - /// - /// - /// - /// - public static IServiceCollection TryAddAWSCredentialsFactory( - this IServiceCollection collection, - ServiceLifetime lifetime = ServiceLifetime.Singleton) - { - collection.TryAdd(new ServiceDescriptor(typeof(IAWSCredentialsFactory), CreateDefaultCredentialsFactory, lifetime)); - return collection; - } - - /// - /// - /// - /// - /// - /// - /// - public static IServiceCollection TryAddAWSCredentialsFactory( - this IServiceCollection collection, - Func implementationFactory, - ServiceLifetime lifetime = ServiceLifetime.Singleton) - { - collection.TryAdd(new ServiceDescriptor(typeof(IAWSCredentialsFactory), implementationFactory, lifetime)); - return collection; - } - - /// - /// - /// - /// - /// - /// - public static IServiceCollection TryAddAWSCredentialsFactory( - this IServiceCollection collection, - IAWSCredentialsFactory credentials) - { - collection.TryAdd(new ServiceDescriptor(typeof(IAWSCredentialsFactory), credentials)); - return collection; - } - /// /// Adds the AWSOptions object to the dependency injection framework providing information /// that will be used to construct Amazon service clients if they haven't already been registered. @@ -478,23 +390,17 @@ private static object CreateServiceClient(AWSOptions options, IServiceProvide { var logger = sp.GetService(); var awsOptions = options ?? sp.GetService() ?? new AWSOptions(); - var credentialsFactory = sp.GetService() ?? new DefaultAWSCredentialsFactory(awsOptions, logger); + var credentialsFactory = sp.GetService() ?? sp.CreateDefaultAWSCredentials(options); var factory = new ClientFactory(awsOptions, credentialsFactory, logger); - return factory.CreateServiceClient(sp.GetService()); - } - - private static IAWSCredentialsFactory CreateDefaultCredentialsFactory(this IServiceProvider sp) - { - var options = sp.GetService() ?? new AWSOptions(); - return new DefaultAWSCredentialsFactory(options, sp.GetService()); + return factory.CreateServiceClient(); } - private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp) + private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp, AWSOptions options = null) { - var credentialsFactory = sp.GetService() ?? sp.CreateDefaultCredentialsFactory(); - return credentialsFactory.Create(); + options = options ?? sp.GetService() ?? new AWSOptions(); + return sp.GetService() ?? new DefaultAWSCredentials(options, sp.GetService()); } } } From 08520175d8e93b9fb271288bc5292d90c834a8bf Mon Sep 17 00:00:00 2001 From: Chase Miller Date: Mon, 14 Apr 2025 15:33:17 -0400 Subject: [PATCH 2/3] Fix infinite recursion with AddAWSService() --- .../ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs index fae7be2a6564..cbb4a8e10552 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs @@ -400,7 +400,7 @@ private static object CreateServiceClient(AWSOptions options, IServiceProvide private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp, AWSOptions options = null) { options = options ?? sp.GetService() ?? new AWSOptions(); - return sp.GetService() ?? new DefaultAWSCredentials(options, sp.GetService()); + return new DefaultAWSCredentials(options, sp.GetService()); } } } From af1865855aee3a9f2d9fdf7225007ff4672d9e76 Mon Sep 17 00:00:00 2001 From: Chase Miller Date: Mon, 14 Apr 2025 15:36:53 -0400 Subject: [PATCH 3/3] Fix another bug with AddAWSService() --- .../ServiceCollectionExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs index cbb4a8e10552..35658fcc78ed 100644 --- a/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs +++ b/extensions/src/AWSSDK.Extensions.NETCore.Setup/ServiceCollectionExtensions.cs @@ -390,7 +390,7 @@ private static object CreateServiceClient(AWSOptions options, IServiceProvide { var logger = sp.GetService(); var awsOptions = options ?? sp.GetService() ?? new AWSOptions(); - var credentialsFactory = sp.GetService() ?? sp.CreateDefaultAWSCredentials(options); + var credentialsFactory = sp.GetService() ?? sp.CreateDefaultAWSCredentials(awsOptions); var factory = new ClientFactory(awsOptions, credentialsFactory, logger);