Search in sources :

Example 1 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project janusgraph by JanusGraph.

the class AwsCodePipelinesCi method main.

public static void main(String[] args) {
    int status = 0;
    try {
        final Options options = new Options();
        OPTIONS.forEach(options::addOption);
        new AwsCodePipelinesCi(new DefaultParser().parse(options, args)).run();
    } catch (ParseException | IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        // EINVAL
        status = 22;
    } catch (IAMException e) {
        log.error(e.getMessage(), e);
        // EPERM
        status = 1;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        // EAGAIN
        status = 11;
    }
    System.exit(status);
}
Also used : Options(org.apache.commons.cli.Options) IAMException(software.amazon.awssdk.services.iam.model.IAMException) ParseException(org.apache.commons.cli.ParseException) BucketLocationConstraint(software.amazon.awssdk.services.s3.model.BucketLocationConstraint) IAMException(software.amazon.awssdk.services.iam.model.IAMException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) DefaultParser(org.apache.commons.cli.DefaultParser)

Example 2 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class DemoUtils method attachIamPolicyToRole.

public static void attachIamPolicyToRole(IamClient iam, String roleName, String policyArn) {
    String policyName = policyArn.substring(policyArn.indexOf("/") + 1);
    try {
        ListAttachedRolePoliciesRequest request = ListAttachedRolePoliciesRequest.builder().roleName(roleName).build();
        ListAttachedRolePoliciesResponse response = iam.listAttachedRolePolicies(request);
        List<AttachedPolicy> attachedPolicies = response.attachedPolicies();
        // Ensure that the policy is not attached to this role
        String polArn;
        for (AttachedPolicy policy : attachedPolicies) {
            polArn = policy.policyArn();
            if (polArn.compareTo(policyArn) == 0) {
                System.out.println("The " + policyName + " policy is already attached to this role.");
                return;
            }
        }
        AttachRolePolicyRequest attachRequest = AttachRolePolicyRequest.builder().roleName(roleName).policyArn(policyArn).build();
        iam.attachRolePolicy(attachRequest);
        System.out.println("Successfully attached policy " + policyArn + " to role " + roleName);
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : AttachRolePolicyRequest(software.amazon.awssdk.services.iam.model.AttachRolePolicyRequest) AttachedPolicy(software.amazon.awssdk.services.iam.model.AttachedPolicy) ListAttachedRolePoliciesResponse(software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesResponse) ListAttachedRolePoliciesRequest(software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 3 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class AccessKeyLastUsed method getAccessKeyLastUsed.

// snippet-start:[iam.java2.access_key_last_used.main]
public static void getAccessKeyLastUsed(IamClient iam, String accessId) {
    try {
        GetAccessKeyLastUsedRequest request = GetAccessKeyLastUsedRequest.builder().accessKeyId(accessId).build();
        GetAccessKeyLastUsedResponse response = iam.getAccessKeyLastUsed(request);
        System.out.println("Access key was last used at: " + response.accessKeyLastUsed().lastUsedDate());
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : GetAccessKeyLastUsedResponse(software.amazon.awssdk.services.iam.model.GetAccessKeyLastUsedResponse) GetAccessKeyLastUsedRequest(software.amazon.awssdk.services.iam.model.GetAccessKeyLastUsedRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 4 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class AttachRolePolicy method attachIAMRolePolicy.

// snippet-start:[iam.java2.attach_role_policy.main]
public static void attachIAMRolePolicy(IamClient iam, String roleName, String policyArn) {
    try {
        ListAttachedRolePoliciesRequest request = ListAttachedRolePoliciesRequest.builder().roleName(roleName).build();
        ListAttachedRolePoliciesResponse response = iam.listAttachedRolePolicies(request);
        List<AttachedPolicy> attachedPolicies = response.attachedPolicies();
        // Ensure that the policy is not attached to this role
        String polArn = "";
        for (AttachedPolicy policy : attachedPolicies) {
            polArn = policy.policyArn();
            if (polArn.compareTo(policyArn) == 0) {
                System.out.println(roleName + " policy is already attached to this role.");
                return;
            }
        }
        // snippet-start:[iam.java2.attach_role_policy.attach]
        AttachRolePolicyRequest attachRequest = AttachRolePolicyRequest.builder().roleName(roleName).policyArn(policyArn).build();
        iam.attachRolePolicy(attachRequest);
        // snippet-end:[iam.java2.attach_role_policy.attach]
        System.out.println("Successfully attached policy " + policyArn + " to role " + roleName);
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : AttachRolePolicyRequest(software.amazon.awssdk.services.iam.model.AttachRolePolicyRequest) AttachedPolicy(software.amazon.awssdk.services.iam.model.AttachedPolicy) ListAttachedRolePoliciesResponse(software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesResponse) ListAttachedRolePoliciesRequest(software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesRequest) IamException(software.amazon.awssdk.services.iam.model.IamException)

Example 5 with IAMException

use of software.amazon.awssdk.services.iam.model.IAMException in project aws-doc-sdk-examples by awsdocs.

the class CreateAccountAlias method createIAMAccountAlias.

// snippet-start:[iam.java2.create_account_alias.main]
public static void createIAMAccountAlias(IamClient iam, String alias) {
    try {
        CreateAccountAliasRequest request = CreateAccountAliasRequest.builder().accountAlias(alias).build();
        iam.createAccountAlias(request);
        System.out.println("Successfully created account alias: " + alias);
    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : IamException(software.amazon.awssdk.services.iam.model.IamException) CreateAccountAliasRequest(software.amazon.awssdk.services.iam.model.CreateAccountAliasRequest)

Aggregations

IamException (software.amazon.awssdk.services.iam.model.IamException)24 AttachedPolicy (software.amazon.awssdk.services.iam.model.AttachedPolicy)3 GetPolicyRequest (software.amazon.awssdk.services.iam.model.GetPolicyRequest)3 GetPolicyResponse (software.amazon.awssdk.services.iam.model.GetPolicyResponse)3 IamWaiter (software.amazon.awssdk.services.iam.waiters.IamWaiter)3 AttachRolePolicyRequest (software.amazon.awssdk.services.iam.model.AttachRolePolicyRequest)2 CreatePolicyRequest (software.amazon.awssdk.services.iam.model.CreatePolicyRequest)2 CreatePolicyResponse (software.amazon.awssdk.services.iam.model.CreatePolicyResponse)2 EntityAlreadyExistsException (software.amazon.awssdk.services.iam.model.EntityAlreadyExistsException)2 ListAttachedRolePoliciesRequest (software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesRequest)2 ListAttachedRolePoliciesResponse (software.amazon.awssdk.services.iam.model.ListAttachedRolePoliciesResponse)2 IOException (java.io.IOException)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 AccessKeyMetadata (software.amazon.awssdk.services.iam.model.AccessKeyMetadata)1 CreateAccessKeyRequest (software.amazon.awssdk.services.iam.model.CreateAccessKeyRequest)1 CreateAccessKeyResponse (software.amazon.awssdk.services.iam.model.CreateAccessKeyResponse)1 CreateAccountAliasRequest (software.amazon.awssdk.services.iam.model.CreateAccountAliasRequest)1 CreateRoleRequest (software.amazon.awssdk.services.iam.model.CreateRoleRequest)1