Search in sources :

Example 11 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project cloudbreak by hortonworks.

the class AwsSetup method validateInstanceProfileCreation.

private void validateInstanceProfileCreation(AwsCredentialView awsCredentialView) {
    GetRoleRequest roleRequest = new GetRoleRequest();
    String roleName = awsCredentialView.getRoleArn().split("/")[1];
    LOGGER.info("Start validate {} role for S3 access.", roleName);
    roleRequest.withRoleName(roleName);
    AmazonIdentityManagement client = awsClient.createAmazonIdentityManagement(awsCredentialView);
    try {
        ListRolePoliciesRequest listRolePoliciesRequest = new ListRolePoliciesRequest();
        listRolePoliciesRequest.setRoleName(roleName);
        ListRolePoliciesResult listRolePoliciesResult = client.listRolePolicies(listRolePoliciesRequest);
        for (String s : listRolePoliciesResult.getPolicyNames()) {
            if (checkIamOrS3Statement(roleName, client, s)) {
                LOGGER.info("Validation successful for s3 or iam access.");
                return;
            }
        }
        ListAttachedRolePoliciesRequest listAttachedRolePoliciesRequest = new ListAttachedRolePoliciesRequest();
        listAttachedRolePoliciesRequest.setRoleName(roleName);
        ListAttachedRolePoliciesResult listAttachedRolePoliciesResult = client.listAttachedRolePolicies(listAttachedRolePoliciesRequest);
        for (AttachedPolicy attachedPolicy : listAttachedRolePoliciesResult.getAttachedPolicies()) {
            if (checkIamOrS3Access(client, attachedPolicy)) {
                LOGGER.info("Validation successful for s3 or iam access.");
                return;
            }
        }
    } catch (AmazonServiceException ase) {
        if (ase.getStatusCode() == UNAUTHORIZED) {
            String policyMEssage = "Could not get policies on the role because the arn role do not have enough permission: %s";
            LOGGER.info(String.format(policyMEssage, ase.getErrorMessage()));
            throw new CloudConnectorException(String.format(policyMEssage, ase.getErrorMessage()));
        } else {
            LOGGER.info(ase.getMessage());
            throw new CloudConnectorException(ase.getErrorMessage());
        }
    } catch (Exception e) {
        LOGGER.info(e.getMessage());
        throw new CloudConnectorException(e.getMessage());
    }
    LOGGER.info("Could not get policies on the role because the arn role do not have enough permission.");
    throw new CloudConnectorException("Could not get policies on the role because the arn role do not have enough permission.");
}
Also used : ListAttachedRolePoliciesResult(com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesResult) ListRolePoliciesResult(com.amazonaws.services.identitymanagement.model.ListRolePoliciesResult) AttachedPolicy(com.amazonaws.services.identitymanagement.model.AttachedPolicy) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) GetRoleRequest(com.amazonaws.services.identitymanagement.model.GetRoleRequest) ListAttachedRolePoliciesRequest(com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesRequest) ListRolePoliciesRequest(com.amazonaws.services.identitymanagement.model.ListRolePoliciesRequest) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)

Example 12 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class UpdateUser method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply the current username and a new\n" + "username. Ex:\n\n" + "UpdateUser <current-name> <new-name>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String cur_name = args[0];
    String new_name = args[1];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    UpdateUserRequest request = new UpdateUserRequest().withUserName(cur_name).withNewUserName(new_name);
    UpdateUserResult response = iam.updateUser(request);
    System.out.printf("Successfully updated user to username %s", new_name);
}
Also used : UpdateUserRequest(com.amazonaws.services.identitymanagement.model.UpdateUserRequest) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) UpdateUserResult(com.amazonaws.services.identitymanagement.model.UpdateUserResult)

Example 13 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class GetPolicy method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a policy arn\n" + "Ex: GetPolicy <policy-arn>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String policy_arn = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    GetPolicyRequest request = new GetPolicyRequest().withPolicyArn(policy_arn);
    GetPolicyResult response = iam.getPolicy(request);
    System.out.format("Successfully retrieved policy %s", response.getPolicy().getPolicyName());
}
Also used : GetPolicyRequest(com.amazonaws.services.identitymanagement.model.GetPolicyRequest) GetPolicyResult(com.amazonaws.services.identitymanagement.model.GetPolicyResult) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement)

Example 14 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class GetServerCertificate method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a certificate name\n" + "Ex: GetServerCertificate <certificate-name>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String cert_name = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    GetServerCertificateRequest request = new GetServerCertificateRequest().withServerCertificateName(cert_name);
    GetServerCertificateResult response = iam.getServerCertificate(request);
    System.out.format("Successfully retrieved certificate with body %s", response.getServerCertificate().getCertificateBody());
}
Also used : GetServerCertificateRequest(com.amazonaws.services.identitymanagement.model.GetServerCertificateRequest) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement) GetServerCertificateResult(com.amazonaws.services.identitymanagement.model.GetServerCertificateResult)

Example 15 with AmazonIdentityManagement

use of com.amazonaws.services.identitymanagement.AmazonIdentityManagement in project aws-doc-sdk-examples by awsdocs.

the class ListAccessKeys method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply an IAM  username\n" + "Ex: ListAccessKeys <username>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String username = args[0];
    final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient();
    boolean done = false;
    while (!done) {
        ListAccessKeysRequest request = new ListAccessKeysRequest().withUserName(username);
        ListAccessKeysResult response = iam.listAccessKeys(request);
        for (AccessKeyMetadata metadata : response.getAccessKeyMetadata()) {
            System.out.format("Retrieved access key %s", metadata.getAccessKeyId());
        }
        request.setMarker(response.getMarker());
        if (!response.getIsTruncated()) {
            done = true;
        }
    }
}
Also used : ListAccessKeysResult(com.amazonaws.services.identitymanagement.model.ListAccessKeysResult) AccessKeyMetadata(com.amazonaws.services.identitymanagement.model.AccessKeyMetadata) ListAccessKeysRequest(com.amazonaws.services.identitymanagement.model.ListAccessKeysRequest) AmazonIdentityManagement(com.amazonaws.services.identitymanagement.AmazonIdentityManagement)

Aggregations

AmazonIdentityManagement (com.amazonaws.services.identitymanagement.AmazonIdentityManagement)22 AmazonServiceException (com.amazonaws.AmazonServiceException)2 AttachedPolicy (com.amazonaws.services.identitymanagement.model.AttachedPolicy)2 ListAttachedRolePoliciesRequest (com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesRequest)2 ListAttachedRolePoliciesResult (com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesResult)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AccessKeyMetadata (com.amazonaws.services.identitymanagement.model.AccessKeyMetadata)1 AttachRolePolicyRequest (com.amazonaws.services.identitymanagement.model.AttachRolePolicyRequest)1 CreateAccessKeyRequest (com.amazonaws.services.identitymanagement.model.CreateAccessKeyRequest)1 CreateAccessKeyResult (com.amazonaws.services.identitymanagement.model.CreateAccessKeyResult)1 CreateAccountAliasRequest (com.amazonaws.services.identitymanagement.model.CreateAccountAliasRequest)1 CreateAccountAliasResult (com.amazonaws.services.identitymanagement.model.CreateAccountAliasResult)1 CreatePolicyRequest (com.amazonaws.services.identitymanagement.model.CreatePolicyRequest)1 CreatePolicyResult (com.amazonaws.services.identitymanagement.model.CreatePolicyResult)1 CreateUserRequest (com.amazonaws.services.identitymanagement.model.CreateUserRequest)1 CreateUserResult (com.amazonaws.services.identitymanagement.model.CreateUserResult)1 DeleteAccessKeyRequest (com.amazonaws.services.identitymanagement.model.DeleteAccessKeyRequest)1 DeleteAccessKeyResult (com.amazonaws.services.identitymanagement.model.DeleteAccessKeyResult)1 DeleteAccountAliasRequest (com.amazonaws.services.identitymanagement.model.DeleteAccountAliasRequest)1