forked from trinodb/trino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make AWSCredentialProvider injectable for GlueClient
- Loading branch information
1 parent
33056ce
commit f7e0a0d
Showing
9 changed files
with
104 additions
and
50 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...trino-hive/src/main/java/io/trino/plugin/hive/metastore/glue/GlueCredentialsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.plugin.hive.metastore.glue; | ||
|
||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; | ||
import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class GlueCredentialsProvider | ||
implements Provider<AWSCredentialsProvider> | ||
{ | ||
private final AWSCredentialsProvider credentialsProvider; | ||
|
||
@Inject | ||
public GlueCredentialsProvider(GlueHiveMetastoreConfig config) | ||
{ | ||
requireNonNull(config, "config is null"); | ||
if (config.getAwsCredentialsProvider().isPresent()) { | ||
this.credentialsProvider = getCustomAWSCredentialsProvider(config.getAwsCredentialsProvider().get()); | ||
} | ||
else { | ||
AWSCredentialsProvider provider; | ||
if (config.getAwsAccessKey().isPresent() && config.getAwsSecretKey().isPresent()) { | ||
provider = new AWSStaticCredentialsProvider( | ||
new BasicAWSCredentials(config.getAwsAccessKey().get(), config.getAwsSecretKey().get())); | ||
} | ||
else { | ||
provider = DefaultAWSCredentialsProviderChain.getInstance(); | ||
} | ||
if (config.getIamRole().isPresent()) { | ||
provider = new STSAssumeRoleSessionCredentialsProvider | ||
.Builder(config.getIamRole().get(), "trino-session") | ||
.withExternalId(config.getExternalId().orElse(null)) | ||
.withLongLivedCredentialsProvider(provider) | ||
.build(); | ||
} | ||
this.credentialsProvider = provider; | ||
} | ||
} | ||
|
||
@Override | ||
public AWSCredentialsProvider get() | ||
{ | ||
return credentialsProvider; | ||
} | ||
|
||
private static AWSCredentialsProvider getCustomAWSCredentialsProvider(String providerClass) | ||
{ | ||
try { | ||
Object instance = Class.forName(providerClass).getConstructor().newInstance(); | ||
if (!(instance instanceof AWSCredentialsProvider)) { | ||
throw new RuntimeException("Invalid credentials provider class: " + instance.getClass().getName()); | ||
} | ||
return (AWSCredentialsProvider) instance; | ||
} | ||
catch (ReflectiveOperationException e) { | ||
throw new RuntimeException(format("Error creating an instance of %s", providerClass), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters