Skip to content

Instantly share code, notes, and snippets.

@adrian-baker
Created October 6, 2018 21:39
Show Gist options
  • Save adrian-baker/81ec8e7cd8f8e15d343157ac9116faac to your computer and use it in GitHub Desktop.
Save adrian-baker/81ec8e7cd8f8e15d343157ac9116faac to your computer and use it in GitHub Desktop.
public AWSCredentialsProvider getDefaultCredentials() {
final String profileName = AwsProfileNameLoader.INSTANCE.loadProfileName();
final AllProfiles allProfiles = new AllProfiles(Stream.concat(
BasicProfileConfigLoader.INSTANCE.loadProfiles(
AwsProfileFileLocationProvider.DEFAULT_CONFIG_LOCATION_PROVIDER.getLocation()).getProfiles().values().stream(),
BasicProfileConfigLoader.INSTANCE.loadProfiles(
AwsProfileFileLocationProvider.DEFAULT_CREDENTIALS_LOCATION_PROVIDER.getLocation()).getProfiles().values().stream())
.map(profile -> new BasicProfile(profile.getProfileName().replaceFirst("^profile ", ""), profile.getProperties()))
.collect(Collectors.toMap(profile -> profile.getProfileName(), profile -> profile,
(left, right) -> {
final Map<String, String> properties = new HashMap<>(left.getProperties());
properties.putAll(right.getProperties());
return new BasicProfile(left.getProfileName(), properties);
})));
final BasicProfile profile = Optional.ofNullable(allProfiles.getProfile(profileName))
.orElseThrow(() -> new RuntimeException(String.format("Profile '%s' not found in %s",
profileName, allProfiles.getProfiles().keySet())));
if (profile.isRoleBasedProfile()) {
return new ProfileAssumeRoleCredentialsProvider(STSProfileCredentialsServiceLoader.getInstance(), allProfiles, profile);
} else {
return new ProfileStaticCredentialsProvider(profile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment