gpt4 book ai didi

java - AWS Java - 如何加载 ~/.aws/config 文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:48:01 26 4
gpt4 key购买 nike

我已经阅读了一段时间的文档。我可以看到 JavaScript 和 Go SDK 的示例,这些示例展示了如何通过将 AWS_SDK_LOAD_CONFIG 环境变量设置为真值来加载配置文件。文档是 herehere分别。

但是,根据我的要求,我必须使用Java。我在 Java SDK 中找不到等效的引用。这让我假设了三件事。

  1. Java 的 SDK 不使用这个变量
    • 我很确定情况可能是这样,因为只是尝试它似乎并没有让它发挥作用。
    • 更新:检查 Java SDKJava SDK V2并使用 ack -i "AWS_SDK_LOAD_CONFIG" 搜索显示两个项目都没有使用此变量。
  2. Java 的 SDK 使用不同的变量
    • 我认为这不太可能,因为它与其他两个 SDK 不一致。
  3. Java 的 SDK 希望您以编程方式执行此操作。
    • 似乎最有可能,但我找不到如何做到这一点。我一定是使用了错误的关键词或忽略了某些东西才会出现这种行为。

为清楚起见,我需要加载的配置文件是 sbx,它存在于我的配置中,但在凭据文件中没有相邻值。这是我的 ~/.aws/config 文件:

[profile shared]
output = json
region = us-west-2
adfs_config.ssl_verification = True
adfs_config.role_arn = ....
adfs_config.adfs_host = ....
adfs_config.adfs_user = ....

[profile sbx]
role_arn = ... (this is different from the adfs_config.role_arn above)
source_profile = shared
region = us-west-2

和 ~/.aws/credentials 文件:(此文件自动使用 aws-adfs 命令填充。

[shared]
aws_access_key_id = ....
aws_secret_access_key = ....
aws_session_token = ....
aws_security_token = ....

最佳答案

我想出了答案。此问题的问题在于默认情况下会加载凭据文件,但它并不总是具有配置文件中可用的所有信息。我们既需要加载又需要展平。

AWS 已经提供了 ProfileAssumeRoleCredentialsProvider,它允许我们从配置文件中承担角色。一旦我们向它提供了它需要的所有信息,它就可以毫无问题地承担角色(假设您的 token 是最新的)

/**
* @author Paul Nelson Baker
* @see <a href="https://github.com/paul-nelson-baker/">GitHub</a>
* @see <a href="https://www.linkedin.com/in/paul-n-baker/">LinkedIn</a>
* @since 2018-11
*/
public class CredentialsChain {

public static final AWSCredentialsProviderChain CREDENTIALS_PROVIDER_CHAIN;

static {
AllProfiles allProfiles = flattenConfigurationFiles(
DEFAULT_CONFIG_LOCATION_PROVIDER.getLocation(), // ~/.aws/config
DEFAULT_CREDENTIALS_LOCATION_PROVIDER.getLocation() // ~/.aws/credentials
);
String currentProfileName = AwsProfileNameLoader.INSTANCE.loadProfileName();
BasicProfile currentProfile = allProfiles.getProfile(currentProfileName);
STSProfileCredentialsService profileCredentialsService = new STSProfileCredentialsService();
// We stick our merged profile provider first, but we still want the default behavior to apply
// so create a new chain with the default chain as the tail provider.
CREDENTIALS_PROVIDER_CHAIN = new AWSCredentialsProviderChain(
new ProfileAssumeRoleCredentialsProvider(profileCredentialsService, allProfiles, currentProfile),
new DefaultAWSCredentialsProviderChain()
);
}

private static AllProfiles flattenConfigurationFiles(File firstFile, File... additionalFiles) {
// Utilize the AWS SDK to load the actual profile objects
List<ProfilesConfigFile> allProfileConfigFiles = Stream.concat(Stream.of(firstFile), Arrays.stream(additionalFiles))
.map(ProfilesConfigFile::new).collect(Collectors.toList());
// Process each file one by one, look at their profiles, and place their values into a single map
// Duplicate profiles will now have the single key/value pairs.
Map<String, Map<String, String>> buildingMap = new LinkedHashMap<>();
for (ProfilesConfigFile currentConfigFile : allProfileConfigFiles) {
for (Entry<String, BasicProfile> currentProfile : currentConfigFile.getAllBasicProfiles().entrySet()) {
// Some profiles are prefixed with "profile " so we want to cull it so we're actually merging the correct data
String currentProfileName = currentProfile.getKey().replaceAll("^profile\\s+", "");
if (!buildingMap.containsKey(currentProfileName)) {
buildingMap.put(currentProfileName, new LinkedHashMap<>());
}
Map<String, String> profileKeyValuePairs = buildingMap.get(currentProfileName);
for (Entry<String, String> overridingEntry : currentProfile.getValue().getProperties().entrySet()) {
profileKeyValuePairs.put(overridingEntry.getKey(), overridingEntry.getValue());
}
}
}
// Take the results, and convert them to AWS SDK Types
Map<String, BasicProfile> finalResult = new LinkedHashMap<>();
for (Entry<String, Map<String, String>> currentFinalProfile : buildingMap.entrySet()) {
String currentProfileName = currentFinalProfile.getKey();
finalResult.put(currentProfileName, new BasicProfile(currentProfileName, currentFinalProfile.getValue()));
}
return new AllProfiles(finalResult);
}

private CredentialsChain() {
}
}

关于java - AWS Java - 如何加载 ~/.aws/config 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49038204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com