Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public static class AWSOptionsExtensions
/// </summary>
/// <typeparam name="T">The service interface that a service client will be created for.</typeparam>
/// <returns>The service client that implements the service interface.</returns>
public static T CreateServiceClient<T>(this AWSOptions options, IAWSCredentialsFactory credentialsFactory = null)
public static T CreateServiceClient<T>(this AWSOptions options)
where T : class, IAmazonService
{
credentialsFactory = credentialsFactory ?? new DefaultAWSCredentialsFactory(options);
var clientFactory = new ClientFactory<T>(options, credentialsFactory, null);
var credentials = new DefaultAWSCredentials(options, null);
var clientFactory = new ClientFactory<T>(options, credentials, null);

return clientFactory.CreateServiceClient() as T;
}
Expand Down
12 changes: 6 additions & 6 deletions extensions/src/AWSSDK.Extensions.NETCore.Setup/ClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,30 @@ internal class ClientFactory<T>
private static readonly object[] EMPTY_PARAMETERS = Array.Empty<object>();

private readonly AWSOptions _options;
private readonly IAWSCredentialsFactory _credentialsFactory;
private readonly AWSCredentials _credentials;
private readonly ILogger _logger;

/// <summary>
/// Constructs an instance of the ClientFactory
/// </summary>
/// <param name="awsOptions">The AWS options used for creating service clients.</param>
/// <param name="credentialsFactory"></param>
/// <param name="credentials"></param>
/// <param name="logger"></param>
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;
}

/// <summary>
/// Creates the AWS service client that implements the service client interface.
/// </summary>
/// <returns>The AWS service client</returns>
internal IAmazonService CreateServiceClient(AWSCredentials credentials = null)
internal IAmazonService CreateServiceClient()
{
PerformGlobalConfig(_logger, _options);
credentials = credentials ?? _credentialsFactory.Create();
var credentials = _credentials;

if (!string.IsNullOrEmpty(_options?.SessionRoleArn))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,34 @@ namespace AWSSDK.Extensions.NETCore.Setup
/// <summary>
///
/// </summary>
public class DefaultAWSCredentialsFactory : IAWSCredentialsFactory
public class DefaultAWSCredentials : AWSCredentials
{
private readonly AWSOptions _options;
private readonly ILogger _logger;

/// <summary>
/// Creates the AWSCredentials using either the profile indicated from the AWSOptions object
/// of the SDK fallback credentials search.
///
/// </summary>
/// <param name="awsOptions"></param>
/// <param name="logger"></param>
/// <param name="options"></param>
/// <returns></returns>
public DefaultAWSCredentialsFactory(AWSOptions options, ILogger logger = null)
public DefaultAWSCredentials(AWSOptions awsOptions, ILogger logger)
{
_options = options;
_options = awsOptions;
_logger = logger;
}

/// <summary>
/// Creates the AWSCredentials using either AWSOptions.Credentials, AWSOptions.Profile + AWSOptions.ProfilesLocation,
/// or the SDK fallback credentials search.
///
/// </summary>
public AWSCredentials Create()
/// <returns></returns>
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))
{
Expand All @@ -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
{
Expand All @@ -67,7 +65,7 @@ public AWSCredentials Create()
_logger?.LogInformation("Found credentials using the AWS SDK's default credential search");
}

return credentials;
return credentials.GetCredentials();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -153,94 +153,6 @@ public static IServiceCollection TryAddAWSCredentials(
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection AddAWSCredentialsFactory(
this IServiceCollection collection,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.Add(new ServiceDescriptor(typeof(IAWSCredentialsFactory), CreateDefaultCredentialsFactory, lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="implementationFactory"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection AddAWSCredentialsFactory(
this IServiceCollection collection,
Func<IServiceProvider, IAWSCredentialsFactory> implementationFactory,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.Add(new ServiceDescriptor(typeof(IAWSCredentialsFactory), implementationFactory, lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="credentials"></param>
/// <returns></returns>
public static IServiceCollection AddAWSCredentialsFactory(
this IServiceCollection collection,
IAWSCredentialsFactory credentials)
{
collection.Add(new ServiceDescriptor(typeof(IAWSCredentialsFactory), credentials));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection TryAddAWSCredentialsFactory(
this IServiceCollection collection,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.TryAdd(new ServiceDescriptor(typeof(IAWSCredentialsFactory), CreateDefaultCredentialsFactory, lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="implementationFactory"></param>
/// <param name="lifetime"></param>
/// <returns></returns>
public static IServiceCollection TryAddAWSCredentialsFactory(
this IServiceCollection collection,
Func<IServiceProvider, IAWSCredentialsFactory> implementationFactory,
ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
collection.TryAdd(new ServiceDescriptor(typeof(IAWSCredentialsFactory), implementationFactory, lifetime));
return collection;
}

/// <summary>
///
/// </summary>
/// <param name="collection"></param>
/// <param name="credentials"></param>
/// <returns></returns>
public static IServiceCollection TryAddAWSCredentialsFactory(
this IServiceCollection collection,
IAWSCredentialsFactory credentials)
{
collection.TryAdd(new ServiceDescriptor(typeof(IAWSCredentialsFactory), credentials));
return collection;
}

/// <summary>
/// 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.
Expand Down Expand Up @@ -478,23 +390,17 @@ private static object CreateServiceClient<T>(AWSOptions options, IServiceProvide
{
var logger = sp.GetService<ILogger>();
var awsOptions = options ?? sp.GetService<AWSOptions>() ?? new AWSOptions();
var credentialsFactory = sp.GetService<IAWSCredentialsFactory>() ?? new DefaultAWSCredentialsFactory(awsOptions, logger);
var credentialsFactory = sp.GetService<AWSCredentials>() ?? sp.CreateDefaultAWSCredentials(awsOptions);

var factory = new ClientFactory<T>(awsOptions, credentialsFactory, logger);

return factory.CreateServiceClient(sp.GetService<AWSCredentials>());
}

private static IAWSCredentialsFactory CreateDefaultCredentialsFactory(this IServiceProvider sp)
{
var options = sp.GetService<AWSOptions>() ?? new AWSOptions();
return new DefaultAWSCredentialsFactory(options, sp.GetService<ILogger>());
return factory.CreateServiceClient();
}

private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp)
private static AWSCredentials CreateDefaultAWSCredentials(this IServiceProvider sp, AWSOptions options = null)
{
var credentialsFactory = sp.GetService<IAWSCredentialsFactory>() ?? sp.CreateDefaultCredentialsFactory();
return credentialsFactory.Create();
options = options ?? sp.GetService<AWSOptions>() ?? new AWSOptions();
return new DefaultAWSCredentials(options, sp.GetService<ILogger>());
}
}
}