com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesRequest Java Examples

The following examples show how to use com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesRequest. 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: IAMUtils.java    From pacbot with Apache License 2.0 5 votes vote down vote up
/**
 * This method will fetch the attached policy a particular role.
 * 
 * @param roleName
 * @param iamClient
 * @return list of AttachedPolicy
 */
public static List<AttachedPolicy> getAttachedPolicyOfIAMRole(final String roleName,
		AmazonIdentityManagementClient iamClient) throws RuleExecutionFailedExeption {
	ListAttachedRolePoliciesRequest attachedUserPoliciesRequest = new ListAttachedRolePoliciesRequest();
	attachedUserPoliciesRequest.setRoleName(roleName);
	ListAttachedRolePoliciesResult rolePoliciesResult = iamClient
			.listAttachedRolePolicies(attachedUserPoliciesRequest);
	return rolePoliciesResult.getAttachedPolicies();
}
 
Example #2
Source File: PolicyProviderImpl.java    From fullstop with Apache License 2.0 5 votes vote down vote up
private Set<String> fetchAttachedPolicyNames(String roleName, AmazonIdentityManagementClient iamClient) {
    return Optional.of(new ListAttachedRolePoliciesRequest().withRoleName(roleName))
            .map(iamClient::listAttachedRolePolicies)
            .map(ListAttachedRolePoliciesResult::getAttachedPolicies)
            .map(attachedPolicies -> attachedPolicies.stream().map(AttachedPolicy::getPolicyName).collect(toSet()))
            .orElseGet(Collections::emptySet);
}
 
Example #3
Source File: AttachRolePolicy.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    final String USAGE =
        "To run this example, supply a role name\n" +
        "Ex: AttachRolePolicy <role-name>\n";

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

    String role_name = args[0];

    final AmazonIdentityManagement iam =
        AmazonIdentityManagementClientBuilder.defaultClient();

    ListAttachedRolePoliciesRequest request =
        new ListAttachedRolePoliciesRequest()
            .withRoleName(role_name);

    List<AttachedPolicy> matching_policies = new ArrayList<>();

    boolean done = false;

    while(!done) {
        ListAttachedRolePoliciesResult response =
            iam.listAttachedRolePolicies(request);

        matching_policies.addAll(
                response.getAttachedPolicies()
                        .stream()
                        .filter(p -> p.getPolicyName().equals(role_name))
                        .collect(Collectors.toList()));

        if(!response.getIsTruncated()) {
            done = true;
        }
        request.setMarker(response.getMarker());
    }

    if (matching_policies.size() > 0) {
        System.out.println(role_name +
                " policy is already attached to this role.");
        return;
    }

    AttachRolePolicyRequest attach_request =
        new AttachRolePolicyRequest()
            .withRoleName(role_name)
            .withPolicyArn(POLICY_ARN);

    iam.attachRolePolicy(attach_request);

    System.out.println("Successfully attached policy " + POLICY_ARN +
            " to role " + role_name);
}