com.distelli.aws.CredProviderAws Maven / Gradle / Ivy
package com.distelli.aws;
import com.distelli.cred.CredProvider;
import com.distelli.cred.CredPair;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
public class CredProviderAws implements CredProvider {
private AWSCredentialsProvider _credProvider;
public CredProviderAws(String profileName) {
_credProvider = new ProfileCredentialsProvider(profileName);
}
public CredProviderAws() {
_credProvider = new DefaultAWSCredentialsProviderChain();
}
@Override
public CredPair getCredPair() {
AWSCredentials awsCreds = _credProvider.getCredentials();
String keyId = awsCreds.getAWSAccessKeyId();
String secret = awsCreds.getAWSSecretKey();
if ( awsCreds instanceof BasicSessionCredentials ) {
secret = secret + "\u001E" + ((BasicSessionCredentials)awsCreds).getSessionToken();
}
return new CredPair()
.withKeyId(keyId)
.withSecret(secret);
}
@Override
public void refresh() {
if ( null == _credProvider ) return;
_credProvider.refresh();
}
}