16
16
using System . Reflection ;
17
17
using Amazon . Runtime ;
18
18
using Amazon . Runtime . CredentialManagement ;
19
-
19
+ using AWSSDK . Extensions . NETCore . Setup ;
20
20
using Microsoft . Extensions . Configuration ;
21
21
using Microsoft . Extensions . DependencyInjection ;
22
22
using Microsoft . Extensions . Logging ;
@@ -34,17 +34,23 @@ internal class ClientFactory
34
34
private static readonly Type [ ] EMPTY_TYPES = Array . Empty < Type > ( ) ;
35
35
private static readonly object [ ] EMPTY_PARAMETERS = Array . Empty < object > ( ) ;
36
36
37
- private Type _serviceInterfaceType ;
38
- private AWSOptions _awsOptions ;
37
+ private readonly Type _serviceInterfaceType ;
38
+ private readonly AWSOptions _awsOptions ;
39
+ private readonly IAWSCredentialsFactory _credentialsFactory ;
40
+ private readonly ILoggerFactory _loggerFactory ;
41
+ private readonly IConfiguration _configuration ;
39
42
40
43
/// <summary>
41
44
/// Constructs an instance of the ClientFactory
42
45
/// </summary>
43
46
/// <param name="type">The type object for the Amazon service client interface, for example IAmazonS3.</param>
44
- internal ClientFactory ( Type type , AWSOptions awsOptions )
47
+ internal ClientFactory ( Type type , AWSOptions awsOptions , IAWSCredentialsFactory credentialsFactory , ILoggerFactory loggerFactory , IConfiguration configuration )
45
48
{
46
49
_serviceInterfaceType = type ;
47
50
_awsOptions = awsOptions ;
51
+ _credentialsFactory = credentialsFactory ;
52
+ _loggerFactory = loggerFactory ;
53
+ _configuration = configuration ;
48
54
}
49
55
50
56
/// <summary>
@@ -53,24 +59,22 @@ internal ClientFactory(Type type, AWSOptions awsOptions)
53
59
/// </summary>
54
60
/// <param name="provider">The dependency injection provider.</param>
55
61
/// <returns>The AWS service client</returns>
56
- internal object CreateServiceClient ( IServiceProvider provider )
62
+ internal object CreateServiceClient ( )
57
63
{
58
- var loggerFactory = provider . GetService < Microsoft . Extensions . Logging . ILoggerFactory > ( ) ;
59
- var logger = loggerFactory ? . CreateLogger ( "AWSSDK" ) ;
64
+ var logger = _loggerFactory ? . CreateLogger ( "AWSSDK" ) ;
60
65
61
- var options = _awsOptions ?? provider . GetService < AWSOptions > ( ) ;
62
- if ( options == null )
66
+ var options = _awsOptions ;
67
+ if ( _awsOptions == null )
63
68
{
64
- var configuration = provider . GetService < IConfiguration > ( ) ;
65
- if ( configuration != null )
69
+ if ( _configuration != null )
66
70
{
67
- options = configuration . GetAWSOptions ( ) ;
71
+ options = _configuration . GetAWSOptions ( ) ;
68
72
if ( options != null )
69
73
logger ? . LogInformation ( "Found AWS options in IConfiguration" ) ;
70
74
}
71
75
}
72
76
73
- return CreateServiceClient ( logger , _serviceInterfaceType , options ) ;
77
+ return CreateServiceClient ( logger , _serviceInterfaceType , options , _credentialsFactory ) ;
74
78
}
75
79
76
80
/// <summary>
@@ -79,10 +83,10 @@ internal object CreateServiceClient(IServiceProvider provider)
79
83
/// </summary>
80
84
/// <param name="provider">The dependency injection provider.</param>
81
85
/// <returns>The AWS service client</returns>
82
- internal static IAmazonService CreateServiceClient ( ILogger logger , Type serviceInterfaceType , AWSOptions options )
86
+ internal static IAmazonService CreateServiceClient ( ILogger logger , Type serviceInterfaceType , AWSOptions options , IAWSCredentialsFactory credentialsFactory )
83
87
{
84
88
PerformGlobalConfig ( logger , options ) ;
85
- var credentials = CreateCredentials ( logger , options ) ;
89
+ var credentials = credentialsFactory . Create ( ) ;
86
90
87
91
if ( ! string . IsNullOrEmpty ( options ? . SessionRoleArn ) )
88
92
{
@@ -160,51 +164,6 @@ private static AmazonServiceClient CreateClient(Type serviceInterfaceType, AWSCr
160
164
return constructor . Invoke ( new object [ ] { credentials , config } ) as AmazonServiceClient ;
161
165
}
162
166
163
- /// <summary>
164
- /// Creates the AWSCredentials using either the profile indicated from the AWSOptions object
165
- /// of the SDK fallback credentials search.
166
- /// </summary>
167
- /// <param name="options"></param>
168
- /// <returns></returns>
169
- private static AWSCredentials CreateCredentials ( ILogger logger , AWSOptions options )
170
- {
171
- if ( options != null )
172
- {
173
- if ( options . Credentials != null )
174
- {
175
- logger ? . LogInformation ( "Using AWS credentials specified with the AWSOptions.Credentials property" ) ;
176
- return options . Credentials ;
177
- }
178
- if ( ! string . IsNullOrEmpty ( options . Profile ) )
179
- {
180
- var chain = new CredentialProfileStoreChain ( options . ProfilesLocation ) ;
181
- AWSCredentials result ;
182
- if ( chain . TryGetAWSCredentials ( options . Profile , out result ) )
183
- {
184
- logger ? . LogInformation ( $ "Found AWS credentials for the profile { options . Profile } ") ;
185
- return result ;
186
- }
187
- else
188
- {
189
- logger ? . LogInformation ( $ "Failed to find AWS credentials for the profile { options . Profile } ") ;
190
- }
191
- }
192
- }
193
-
194
- var credentials = FallbackCredentialsFactory . GetCredentials ( ) ;
195
- if ( credentials == null )
196
- {
197
- logger ? . LogError ( "Last effort to find AWS Credentials with AWS SDK's default credential search failed" ) ;
198
- throw new AmazonClientException ( "Failed to find AWS Credentials for constructing AWS service client" ) ;
199
- }
200
- else
201
- {
202
- logger ? . LogInformation ( "Found credentials using the AWS SDK's default credential search" ) ;
203
- }
204
-
205
- return credentials ;
206
- }
207
-
208
167
/// <summary>
209
168
/// Creates the ClientConfig object for the service client.
210
169
/// </summary>
0 commit comments