software.amazon.awssdk.services.iam.IamClient Java Examples

The following examples show how to use software.amazon.awssdk.services.iam.IamClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UpdateUser.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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 < 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String curName = args[0];
        String newName = args[1];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        updateIAMUser(iam, curName, newName) ;
    }
 
Example #2
Source File: DetachRolePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void detachPolicy(IamClient iam, String roleName, String policyArn ) {

        try {

            DetachRolePolicyRequest request = DetachRolePolicyRequest.builder()
                .roleName(roleName)
                .policyArn(policyArn).build();

             DetachRolePolicyResponse response = iam.detachRolePolicy(request);

             System.out.println("Successfully detached policy " + policyArn +
                " from role " + roleName);

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        // snippet-end:[iam.java2.detach_role_policy.main]
    }
 
Example #3
Source File: DeleteAccountAlias.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply an account alias\n" +
                        "Ex: DeleteAccountAlias <account-alias>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String alias = args[0];
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        deleteIAMAccountAlias(iam, alias) ;
    }
 
Example #4
Source File: UpdateServerCertificate.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply the current certificate name and\n" +
                        "a new name. Ex:\n\n" +
                        "UpdateServerCertificate <current-name> <new-name>\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String curName = args[0];
        String newName = args[1];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder().region(region).build();

        updateCertificate(iam, curName, newName) ;
    }
 
Example #5
Source File: UpdateServerCertificate.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void updateCertificate(IamClient iam, String curName, String newName) {

        try {
            UpdateServerCertificateRequest request =
                UpdateServerCertificateRequest.builder()
                        .serverCertificateName(curName)
                        .newServerCertificateName(newName)
                        .build();

            UpdateServerCertificateResponse response =
                iam.updateServerCertificate(request);


            System.out.printf("Successfully updated server certificate to name %s",
                newName);

        } catch (IamException e) {
             System.err.println(e.awsErrorDetails().errorMessage());
             System.exit(1);
        }
        System.out.println("Done");
    }
 
Example #6
Source File: CreateAccessKey.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply an IAM user that you can obtain from the AWS Console\n" +
                        "Ex: CreateAccessKey <user>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String user = args[0];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient
                .builder()
                .region(region)
                .build();

        String keyId = createIAMAccessKey(iam, user);
        System.out.println("The Key Id is " +keyId);
    }
 
Example #7
Source File: CreateAccessKey.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String createIAMAccessKey(IamClient iam,String user) {

        try {
            CreateAccessKeyRequest request = CreateAccessKeyRequest.builder()
                .userName(user).build();

            CreateAccessKeyResponse response = iam.createAccessKey(request);
           String keyId = response.accessKey().accessKeyId();
           return keyId;

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "";
    }
 
Example #8
Source File: AccessKeyLastUsed.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply an access key id that you can ontain from the AWS Console\n" +
                        "Ex: AccessKeyLastUsed <access-key-id>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String accessId = args[0];

        //Create an IamClient object
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        getAccessKeyLastUsed(iam, accessId) ;
    }
 
Example #9
Source File: AccessKeyLastUsed.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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");
        // snippet-end:[iam.java2.access_key_last_used.main]
    }
 
Example #10
Source File: CreatePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a unique policy name\n" +
                        "Ex: CreatePolicy <policy-name>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String policyName = args[0];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder().region(region).build();

        String result = createIAMPolicy(iam, policyName);
        System.out.println("Successfully created a policy with this ARN value: " +result);
    }
 
Example #11
Source File: CreatePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static String createIAMPolicy(IamClient iam, String policyName ) {

        try {
              CreatePolicyRequest request = CreatePolicyRequest.builder()
                .policyName(policyName)
                .policyDocument(PolicyDocument).build();

              CreatePolicyResponse response = iam.createPolicy(request);

              return response.policy().arn();
         } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        return "" ;
    }
 
Example #12
Source File: DeleteAccountAlias.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteIAMAccountAlias(IamClient iam,String alias ) {

        try {
            DeleteAccountAliasRequest request = DeleteAccountAliasRequest.builder()
                .accountAlias(alias).build();

            DeleteAccountAliasResponse response = iam.deleteAccountAlias(request);
            System.out.println("Successfully deleted account alias " + alias);

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done");
        // snippet-end:[iam.java2.delete_account_alias.main]
    }
 
Example #13
Source File: UpdateUser.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void updateIAMUser(IamClient iam, String curName,String newName ) {

        try {
            UpdateUserRequest request = UpdateUserRequest.builder()
                .userName(curName)
                .newUserName(newName).build();

            UpdateUserResponse response = iam.updateUser(request);
            System.out.printf("Successfully updated user to username %s",
                newName);
        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done");
        }
 
Example #14
Source File: DeleteAccessKey.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a username and access key id\n" +
                        "Ex: DeleteAccessKey <username> <access-key-id>\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }
        String username = args[0];
        String accessKey = args[1];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        deleteKey(iam , username, accessKey);
    }
 
Example #15
Source File: DeleteAccessKey.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteKey(IamClient iam ,String username, String accessKey ) {

        try {
            DeleteAccessKeyRequest request = DeleteAccessKeyRequest.builder()
                .accessKeyId(accessKey)
                .userName(username).build();

            iam.deleteAccessKey(request);
            System.out.println("Successfully deleted access key " + accessKey +
                " from user " + username);

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example #16
Source File: DeletePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a policy ARN value\n" +
                        "Ex: DeletePolicy <policy-arn>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }
        String policyARN = args[0];

        // Create the IamClient object
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        deleteIAMPolicy(iam, policyARN);
    }
 
Example #17
Source File: DeletePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteIAMPolicy(IamClient iam,String policyARN) {

        try {
            DeletePolicyRequest request = DeletePolicyRequest.builder()
                   .policyArn(policyARN)
                   .build();

            iam.deletePolicy(request);
            System.out.println("Successfully deleted the policy");

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done");
        // snippet-end:[iam.java2.delete_policy.main]
    }
 
Example #18
Source File: CreateAccountAlias.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply an alias which has to be digits\n" +
                        "Ex: CreateAccountAlias <alias>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String alias = args[0];
        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        createIAMAccountAlias(iam, alias);
    }
 
Example #19
Source File: CreateAccountAlias.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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);
        }
        System.out.println("Done");
    }
 
Example #20
Source File: DeleteUser.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a username\n" +
                        "Ex: DeleteUser <username>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String username = args[0];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        deleteIAMUser(iam, username);
    }
 
Example #21
Source File: IAMServiceIntegrationTest.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
@BeforeAll
public static void setUp() throws IOException {

    Region region = Region.AWS_GLOBAL;
    iam =  IamClient.builder().region(region).build();

    try (InputStream input = IAMServiceIntegrationTest.class.getClassLoader().getResourceAsStream("config.properties")) {

        Properties prop = new Properties();
        prop.load(input);
        // Populate the data members required for all tests
        userName = prop.getProperty("userName");
        policyName= prop.getProperty("policyName");
        policyARN= prop.getProperty("policyARN");
        roleName=prop.getProperty("roleName");
        accountAlias=prop.getProperty("accountAlias");

        if (input == null) {
            System.out.println("Sorry, unable to find config.properties");
            return;
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
 
Example #22
Source File: DeleteServerCertificate.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void deleteCert(IamClient iam,String certName ) {

        try {
            DeleteServerCertificateRequest request =
                DeleteServerCertificateRequest.builder()
                        .serverCertificateName(certName).build();

            DeleteServerCertificateResponse response =
                iam.deleteServerCertificate(request);

            System.out.println("Successfully deleted server certificate " +
                    certName);

        } catch (IamException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.println("Done");
    }
 
Example #23
Source File: AwsIamPasswordPolicyScannerTest.java    From clouditor with Apache License 2.0 6 votes vote down vote up
@BeforeAll
static void setUpOnce() {
  discoverAssets(
      IamClient.class,
      AwsIamPasswordPolicyScanner::new,
      api ->
          when(api.getAccountPasswordPolicy())
              .thenReturn(
                  GetAccountPasswordPolicyResponse.builder()
                      .passwordPolicy(
                          PasswordPolicy.builder()
                              .requireUppercaseCharacters(true)
                              .requireSymbols(true)
                              .expirePasswords(false)
                              .passwordReusePrevention(24)
                              .requireLowercaseCharacters(true)
                              .maxPasswordAge(90)
                              .hardExpiry(false)
                              .requireNumbers(true)
                              .minimumPasswordLength(14)
                              .build())
                      .build()));
}
 
Example #24
Source File: IntegrationTestBase.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private static void createLambdaServiceRole() {
    iam = IamClient.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(Region.AWS_GLOBAL)
            .build();

    CreateRoleResponse result = iam.createRole(CreateRoleRequest.builder().roleName(LAMBDA_SERVICE_ROLE_NAME)
                                                                    .assumeRolePolicyDocument(LAMBDA_ASSUME_ROLE_POLICY).build());

    lambdaServiceRoleArn = result.role().arn();

    roleExecutionPolicyArn = iam
            .createPolicy(CreatePolicyRequest.builder().policyName(LAMBDA_SERVICE_ROLE_POLICY_NAME).policyDocument(
                            LAMBDA_ROLE_EXECUTION_POLICY).build()).policy().arn();

    iam.attachRolePolicy(AttachRolePolicyRequest.builder().roleName(LAMBDA_SERVICE_ROLE_NAME).policyArn(
            roleExecutionPolicyArn).build());
}
 
Example #25
Source File: ElbIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Loads the AWS account info for the integration tests and creates an EC2
 * client for tests to use.
 */
@BeforeClass
public static void setUp() throws IOException {
    elb = ElasticLoadBalancingClient.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(REGION)
            .build();
    ec2 = Ec2Client.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(REGION)
            .build();
    iam = IamClient.builder()
            .credentialsProvider(CREDENTIALS_PROVIDER_CHAIN)
            .region(Region.AWS_GLOBAL)
            .build();

    List<ServerCertificateMetadata> serverCertificates = iam.listServerCertificates(
            ListServerCertificatesRequest.builder().build()).serverCertificateMetadataList();
    if (!serverCertificates.isEmpty()) {
        certificateArn = serverCertificates.get(0).arn();
    }
}
 
Example #26
Source File: ListAccessKeys.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        listKeys(iam,username) ;
    }
 
Example #27
Source File: AttachRolePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    final String USAGE =
            "To run this example, supply a role name that you can obtain from the AWS Console\n" +
                    "Ex: AttachRolePolicy <role-name> <policy-arn>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String roleName = args[0];
    String policyArn = args[1];

    // snippet-start:[iam.java2.attach_role_policy.client] 
    Region region = Region.AWS_GLOBAL;
    IamClient iam = IamClient.builder()
            .region(region)
            .build();
    // snippet-end:[iam.java2.attach_role_policy.client]

    attachIAMRolePolicy(iam, roleName, policyArn);
}
 
Example #28
Source File: CreateUser.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
                "To run this example, supply a username\n" +
                        "Ex: CreateUser <username>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String username = args[0];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder()
                .region(region)
                .build();

        String result = createIAMUser(iam, username) ;
        System.out.println("Successfully created user: " +result);

    }
 
Example #29
Source File: GetServerCertificate.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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 certName = args[0];

        Region region = Region.AWS_GLOBAL;
        IamClient iam = IamClient.builder().region(region).build();

        getCertificate(iam, certName );
    }
 
Example #30
Source File: GetServerCertificate.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void getCertificate(IamClient iam,String certName ) {

        try {
            GetServerCertificateRequest request = GetServerCertificateRequest.builder()
                .serverCertificateName(certName).build();

            GetServerCertificateResponse response = iam.getServerCertificate(request);

            System.out.format("Successfully retrieved certificate with body %s",
                response.serverCertificate().certificateBody());

    } catch (IamException e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
        System.out.println("Done");
    }