Created
November 14, 2019 13:05
-
-
Save FRosner/6f5839d27ca8d109da93fef3d96ac8c9 to your computer and use it in GitHub Desktop.
Full Kotlin example of https://gist.github.com/adrian-baker/81ec8e7cd8f8e15d343157ac9116faac to work around https://github.com/aws/aws-sdk-java/issues/803
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
import com.amazonaws.auth.AWSCredentialsProvider | |
import com.amazonaws.auth.AWSSessionCredentials | |
import com.amazonaws.auth.BasicAWSCredentials | |
import com.amazonaws.auth.profile.internal.AllProfiles | |
import com.amazonaws.auth.profile.internal.AwsProfileNameLoader | |
import com.amazonaws.auth.profile.internal.BasicProfile | |
import com.amazonaws.auth.profile.internal.BasicProfileConfigLoader | |
import com.amazonaws.auth.profile.internal.ProfileAssumeRoleCredentialsProvider | |
import com.amazonaws.auth.profile.internal.ProfileStaticCredentialsProvider | |
import com.amazonaws.auth.profile.internal.securitytoken.STSProfileCredentialsServiceLoader | |
import com.amazonaws.profile.path.AwsProfileFileLocationProvider | |
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials | |
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials | |
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient | |
import java.util.function.BinaryOperator | |
import java.util.function.Function | |
import java.util.stream.Collectors | |
import java.util.stream.Stream | |
fun main() { | |
val credentialsProvider = credentialsProvider() | |
val cloudWatch = CloudWatchAsyncClient.builder().credentialsProvider { | |
val credentials = credentialsProvider.credentials | |
when (credentials) { | |
is AWSSessionCredentials -> AwsSessionCredentials.create(credentials.awsAccessKeyId, credentials.awsSecretKey, credentials.sessionToken) | |
is BasicAWSCredentials -> AwsBasicCredentials.create(credentials.awsAccessKeyId, credentials.awsSecretKey) | |
else -> throw RuntimeException("Unknown credentials type ${credentials::class}") | |
} | |
}.build() | |
} | |
fun credentialsProvider(): AWSCredentialsProvider { | |
val profileName = AwsProfileNameLoader.INSTANCE.loadProfileName() | |
val keyFunction: Function<BasicProfile, String> = Function(BasicProfile::getProfileName) | |
val valueFunction: Function<BasicProfile, BasicProfile> = Function.identity() | |
val conflictResolutionFunction: BinaryOperator<BasicProfile> = BinaryOperator { left, right -> | |
val mergedProperties = HashMap(left.properties) | |
mergedProperties.putAll(right.properties) | |
BasicProfile(left.profileName, mergedProperties) | |
} | |
val profileStream = Stream.concat( | |
BasicProfileConfigLoader.INSTANCE.loadProfiles( | |
AwsProfileFileLocationProvider.DEFAULT_CONFIG_LOCATION_PROVIDER.location | |
).profiles.values.stream(), | |
BasicProfileConfigLoader.INSTANCE.loadProfiles( | |
AwsProfileFileLocationProvider.DEFAULT_CREDENTIALS_LOCATION_PROVIDER.location | |
).profiles.values.stream() | |
).map { BasicProfile(it.profileName.removePrefix("profile "), it.properties) } | |
.collect(Collectors.toMap(keyFunction, valueFunction, conflictResolutionFunction)) | |
val allProfiles = AllProfiles(profileStream) | |
val profile = allProfiles.getProfile(profileName) ?: throw RuntimeException("Profile $profileName not found in [${allProfiles.profiles.keys.joinToString(", ")}]") | |
return if (profile.isRoleBasedProfile) { | |
ProfileAssumeRoleCredentialsProvider(STSProfileCredentialsServiceLoader.getInstance(), allProfiles, profile) | |
} else { | |
ProfileStaticCredentialsProvider(profile) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment