@Autowired
private CredentialRepository credentialRepository;
public BasicSessionCredentials retrieveSessionCredentials(int durationInSeconds, String externalId, AwsCredential awsCredential) {
BasicSessionCredentials cachedSessionCredentials = getCachedCredentials(awsCredential);
if (cachedSessionCredentials != null) {
return cachedSessionCredentials;
}
AWSSecurityTokenServiceClient client = new AWSSecurityTokenServiceClient();
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest()
.withDurationSeconds(durationInSeconds)
.withExternalId(externalId)
.withRoleArn(awsCredential.getRoleArn())
.withRoleSessionName("hadoop-provisioning");
AssumeRoleResult result = client.assumeRole(assumeRoleRequest);
cacheSessionCredentials(result, durationInSeconds, awsCredential);
return new BasicSessionCredentials(
result.getCredentials().getAccessKeyId(),
result.getCredentials().getSecretAccessKey(),
result.getCredentials().getSessionToken());
}