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

The following examples show how to use com.amazonaws.services.identitymanagement.model.ListAttachedRolePoliciesResult. 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: PolicyProviderTest.java    From fullstop with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetRolePolicies() throws Exception {
    when(clientMock.listAttachedRolePolicies(any()))
            .thenReturn(new ListAttachedRolePoliciesResult().withAttachedPolicies(
                    new AttachedPolicy().withPolicyName("bar1"),
                    new AttachedPolicy().withPolicyName("bar2")));
    when(clientMock.listRolePolicies(any()))
            .thenReturn(new ListRolePoliciesResult().withPolicyNames("foo", "bar"));
    when(clientMock.getRolePolicy(any()))
            .thenReturn(new GetRolePolicyResult().withPolicyDocument("%7B%22hello%22%3A%22world%22%7D"));

    final RolePolicies rolePolicies = policyProvider.getRolePolicies("foo", Region.getRegion(US_EAST_1), "123456789012");
    assertThat(rolePolicies).isNotNull();
    assertThat(rolePolicies.getAttachedPolicyNames()).containsOnly("bar1", "bar2");
    assertThat(rolePolicies.getInlinePolicyNames()).containsOnly("foo", "bar");
    assertThat(rolePolicies.getMainPolicy()).isEqualTo("{\"hello\":\"world\"}");

    verify(clientMock).listAttachedRolePolicies(any());
    verify(clientMock).listRolePolicies(any());
    verify(clientMock).getRolePolicy(any());
}
 
Example #2
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 #3
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 #4
Source File: PolicyProviderTest.java    From fullstop with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRolePoliciesWhenMainPolicyIsMissing() throws Exception {
    when(clientMock.listAttachedRolePolicies(any())).thenReturn(new ListAttachedRolePoliciesResult());
    when(clientMock.listRolePolicies(any())).thenReturn(new ListRolePoliciesResult());

    final RolePolicies rolePolicies = policyProvider.getRolePolicies("foo", Region.getRegion(US_EAST_1), "123456789012");
    assertThat(rolePolicies).isNotNull();
    assertThat(rolePolicies.getAttachedPolicyNames()).isEmpty();
    assertThat(rolePolicies.getInlinePolicyNames()).isEmpty();

    verify(clientMock).listAttachedRolePolicies(any());
    verify(clientMock).listRolePolicies(any());
}
 
Example #5
Source File: IAMUtilsTest.java    From pacbot with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("static-access")
@Test
public void getActionsByRolePolicyTest() throws Exception {
	 AttachedPolicy attachedPolicies = new AttachedPolicy();
     attachedPolicies.setPolicyName("IAMFullAccess");
     List<AttachedPolicy> policies = new ArrayList<>();
     policies.add(attachedPolicies);
     
     PolicyVersion versions = new PolicyVersion();
     versions.setIsDefaultVersion(true);
     versions.setVersionId("123");
     versions.setDocument("{\"ag\":\"aws-all\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"iam:*\"],\"Resource\":[\"iam:*\"]}],\"from\":0,\"searchtext\":\"\",\"size\":25}");
    ListPolicyVersionsResult policyVersions = new ListPolicyVersionsResult();
    policyVersions.setVersions(Arrays.asList(versions));
    
    
    ListAttachedRolePoliciesResult attachedRolePoliciesResult = new ListAttachedRolePoliciesResult();
    attachedRolePoliciesResult.setAttachedPolicies(policies);
    attachedRolePoliciesResult.setIsTruncated(false);
    
    ListRolePoliciesResult rolePoliciesResult = new ListRolePoliciesResult();
    rolePoliciesResult.setPolicyNames(Arrays.asList("123"));
    rolePoliciesResult.setIsTruncated(false);
    
    GetRolePolicyResult policyResult = new GetRolePolicyResult();
    
    policyResult.setPolicyName("123");
    policyResult.setPolicyDocument("{\"ag\":\"aws-all\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"iam:*\"],\"Resource\":[\"iam:*\"]}],\"from\":0,\"searchtext\":\"\",\"size\":25}");
    policyResult.setRoleName("123");
     
    GetPolicyVersionResult versionResult = new GetPolicyVersionResult();
    versionResult.setPolicyVersion(versions);
    when(iamClient.listAttachedRolePolicies(anyObject())).thenReturn(attachedRolePoliciesResult);
    when(iamClient.listRolePolicies(anyObject())).thenReturn(rolePoliciesResult);
    when(iamClient.getRolePolicy(anyObject())).thenReturn(policyResult);
    when(iamClient.listPolicyVersions(anyObject())).thenReturn(policyVersions);
    when(iamClient.getPolicyVersion(anyObject())).thenReturn(versionResult);
    mockStatic(URLDecoder.class);
    when(URLDecoder.decode(anyString(),anyString())).thenReturn("qeqwehgj");
    assertThat(iamUtils.getAllowedActionsByRolePolicy(iamClient,"133"),is(notNullValue()));
    
}
 
Example #6
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);
}