Skip to content

Commit

Permalink
Update superAdminAWSCredentialsProvider for bwc
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chu <[email protected]>
  • Loading branch information
noCharger committed Mar 28, 2024
1 parent f0d72df commit ecdfa33
Showing 1 changed file with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,24 @@ public IRestHighLevelClient createClient() {
signer.setServiceName("es");
signer.setRegionName(options.getRegion());

// Initialize and attempt to instantiate custom AWSCredentialsProviders.
// Use DefaultAWSCredentialsProviderChain by default.
final AtomicReference<AWSCredentialsProvider> customAWSCredentialsProvider =
initializeAndInstantiateProvider(options.getCustomAwsCredentialsProvider());
new AtomicReference<>(new DefaultAWSCredentialsProviderChain());
String customProviderClass = options.getCustomAwsCredentialsProvider();
if (!Strings.isNullOrEmpty(customProviderClass)) {
instantiateProvider(customProviderClass, customAWSCredentialsProvider);
}

// Use the customAWSCredentialsProvider for superAdminAWSCredentialsProvider by default,
// unless a separate superAdmin provider class name is specified
String superAdminProviderClass = options.getSuperAdminAwsCredentialsProvider();
final AtomicReference<AWSCredentialsProvider> superAdminAWSCredentialsProvider =
initializeAndInstantiateProvider(options.getSuperAdminAwsCredentialsProvider());
new AtomicReference<>(new DefaultAWSCredentialsProviderChain());
if (Strings.isNullOrEmpty(superAdminProviderClass)) {
superAdminAWSCredentialsProvider.set(customAWSCredentialsProvider.get());
} else {
instantiateProvider(superAdminProviderClass, superAdminAWSCredentialsProvider);
}

restClientBuilder.setHttpClientConfigCallback(builder -> {
HttpAsyncClientBuilder delegate = builder.addInterceptorLast(
Expand Down Expand Up @@ -295,23 +308,17 @@ public IRestHighLevelClient createClient() {
}

/**
* Initializes and possibly instantiates an AWS credentials provider. If a provider class name is provided,
* this method attempts to instantiate the provider using reflection. Otherwise, it defaults to using the
* {@link DefaultAWSCredentialsProviderChain}.
* Attempts to instantiate the AWS credential provider using reflection.
*/
private AtomicReference<AWSCredentialsProvider> initializeAndInstantiateProvider(String providerClass) {
AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
if (!Strings.isNullOrEmpty(providerClass)) {
try {
Class<?> clazz = Class.forName(providerClass);
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
provider = (AWSCredentialsProvider) constructor.newInstance();
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate AWSCredentialsProvider: " + providerClass, e);
}
private void instantiateProvider(String providerClass, AtomicReference<AWSCredentialsProvider> provider) {
try {
Class<?> awsCredentialsProviderClass = Class.forName(providerClass);
Constructor<?> ctor = awsCredentialsProviderClass.getDeclaredConstructor();
ctor.setAccessible(true);
provider.set((AWSCredentialsProvider) ctor.newInstance());
} catch (Exception e) {
throw new RuntimeException("Failed to instantiate AWSCredentialsProvider: " + providerClass, e);
}
return new AtomicReference<>(provider);
}

/*
Expand Down

0 comments on commit ecdfa33

Please sign in to comment.