Search in sources :

Example 1 with AmazonIdentityManagementClientBuilder

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder in project photon-model by vmware.

the class AWSEndpointAdapterService method getAccountId.

/**
 * Method gets the aws accountId from the specified credentials.
 * @param privateKeyId
 * @param privateKey
 * @return account ID
 */
private String getAccountId(String privateKeyId, String privateKey) {
    AWSCredentials awsCredentials = new BasicAWSCredentials(privateKeyId, privateKey);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
    AmazonIdentityManagementClientBuilder amazonIdentityManagementClientBuilder = AmazonIdentityManagementClientBuilder.standard().withCredentials(awsStaticCredentialsProvider).withRegion(Regions.DEFAULT_REGION);
    AmazonIdentityManagementClient iamClient = (AmazonIdentityManagementClient) amazonIdentityManagementClientBuilder.build();
    String userId = null;
    try {
        if ((iamClient.getUser() != null) && (iamClient.getUser().getUser() != null) && (iamClient.getUser().getUser().getArn() != null)) {
            return getAccountId(iamClient.getUser().getUser().getArn());
        }
    } catch (AmazonServiceException ex) {
        if (ex.getErrorCode().compareTo("AccessDenied") == 0) {
            String msg = ex.getMessage();
            userId = msg.split(":", 7)[5];
        } else {
            logSevere("Exception getting the accountId %s", ex);
        }
    }
    return userId;
}
Also used : AmazonIdentityManagementClient(com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonIdentityManagementClientBuilder(com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder) AmazonServiceException(com.amazonaws.AmazonServiceException) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AmazonIdentityManagementClient (com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient)1 AmazonIdentityManagementClientBuilder (com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder)1