Java Code Examples for com.amazonaws.services.identitymanagement.model.AccessKeyMetadata#setAccessKeyId()

The following examples show how to use com.amazonaws.services.identitymanagement.model.AccessKeyMetadata#setAccessKeyId() . 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: AccessKeyRotatedRuleTest.java    From pacbot with Apache License 2.0 4 votes vote down vote up
@Test
public void test()throws Exception{
    Date date = new Date(); // Or where ever you get it from
    Date daysAgo = new DateTime(date).minusDays(300).toDate();
    AccessKeyMetadata accessKeyMetadata = new AccessKeyMetadata();
    accessKeyMetadata.setAccessKeyId("123");
    accessKeyMetadata.setCreateDate(daysAgo);
    accessKeyMetadata.setStatus("Active");
   
    List<AccessKeyMetadata> accessKeyMetadatas  = new ArrayList<>();
    accessKeyMetadatas.add(accessKeyMetadata);

    AccessKeyMetadata accessKeyMetadataTest = new AccessKeyMetadata();
    accessKeyMetadataTest.setAccessKeyId("123");
    accessKeyMetadataTest.setCreateDate(new Date());
    accessKeyMetadataTest.setStatus("Inactive");
   
    List<AccessKeyMetadata> accessKeyMetadatasTest  = new ArrayList<>();
    accessKeyMetadatasTest.add(accessKeyMetadataTest);
    
    List<AccessKeyMetadata> emptyAccessKeyMetadatas  = new ArrayList<>();
    
    mockStatic(PacmanUtils.class);
    when(PacmanUtils.doesAllHaveValue(anyString(),anyString())).thenReturn(
            true);
    
    
   
    
    Map<String,Object>map=new HashMap<String, Object>();
    map.put("client", identityManagementClient);
    AccessKeyRotatedRule spy = Mockito.spy(new AccessKeyRotatedRule());
    
    Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject());
    
    mockStatic(IAMUtils.class);
    when(IAMUtils.getAccessKeyInformationForUser(anyString(),anyObject())).thenReturn(accessKeyMetadatas);
    spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "));
    
    
    when(IAMUtils.getAccessKeyInformationForUser(anyString(),anyObject())).thenReturn(accessKeyMetadatasTest);
    spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "));
    
    when(IAMUtils.getAccessKeyInformationForUser(anyString(),anyObject())).thenReturn(emptyAccessKeyMetadatas);
    spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "));
    
    
    assertThatThrownBy( 
            () -> accessKeyRotatedRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class);
    
    
    when(PacmanUtils.doesAllHaveValue(anyString(),anyString())).thenReturn(
            false);
    assertThatThrownBy(
            () -> accessKeyRotatedRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class);
}
 
Example 2
Source File: InventoryUtilTest.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fetch IAM users test.
 *
 * @throws Exception the exception
 */
@SuppressWarnings("static-access")
@Test
public void fetchIAMUsersTest() throws Exception {
    
    mockStatic(AmazonIdentityManagementClientBuilder.class);
    AmazonIdentityManagement iamClient = PowerMockito.mock(AmazonIdentityManagement.class);
    AmazonIdentityManagementClientBuilder amazonIdentityManagementClientBuilder = PowerMockito.mock(AmazonIdentityManagementClientBuilder.class);
    AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class);
    PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider);
    when(amazonIdentityManagementClientBuilder.standard()).thenReturn(amazonIdentityManagementClientBuilder);
    when(amazonIdentityManagementClientBuilder.withCredentials(anyObject())).thenReturn(amazonIdentityManagementClientBuilder);
    when(amazonIdentityManagementClientBuilder.withRegion(anyString())).thenReturn(amazonIdentityManagementClientBuilder);
    when(amazonIdentityManagementClientBuilder.build()).thenReturn(iamClient);
    
    ListUsersResult listUsersResult = new ListUsersResult();
    List<User> users = new ArrayList<>();
    User user = new User();
    user.setUserName("name");
    users.add(user);
    listUsersResult.setUsers(users);
    when(iamClient.listUsers(anyObject())).thenReturn(listUsersResult);
    
    ListAccessKeysResult listAccessKeysResult = new ListAccessKeysResult();
    List<AccessKeyMetadata> accessKeyMetadataList = new ArrayList<>();
    AccessKeyMetadata accessKeyMetadata = new AccessKeyMetadata();
    accessKeyMetadata.setAccessKeyId("accessKeyId");
    accessKeyMetadataList.add(accessKeyMetadata);
    listAccessKeysResult.setAccessKeyMetadata(accessKeyMetadataList );
    when(iamClient.listAccessKeys(anyObject())).thenReturn(listAccessKeysResult);
    
    GetAccessKeyLastUsedResult getAccessKeyLastUsedResult = new GetAccessKeyLastUsedResult();
    AccessKeyLastUsed accessKeyLastUsed = new AccessKeyLastUsed();
    accessKeyLastUsed.setLastUsedDate(new Date());
    getAccessKeyLastUsedResult.setAccessKeyLastUsed(accessKeyLastUsed );
    when(iamClient.getAccessKeyLastUsed(anyObject())).thenReturn(getAccessKeyLastUsedResult);
    
    GetLoginProfileResult getLoginProfileResult = new GetLoginProfileResult();
    LoginProfile loginProfile = new LoginProfile();
    loginProfile.setCreateDate(new Date());
    loginProfile.setPasswordResetRequired(false);
    getLoginProfileResult.setLoginProfile(loginProfile );
    when(iamClient.getLoginProfile(anyObject())).thenReturn(getLoginProfileResult );
    
    ListGroupsForUserResult listGroupsForUserResult = new ListGroupsForUserResult();
    List<Group> groups = new ArrayList<>();
    Group group = new Group();
    group.setGroupName("groupName");
    groups.add(group);
    listGroupsForUserResult.setGroups(groups );
    when(iamClient.listGroupsForUser(anyObject())).thenReturn(listGroupsForUserResult );
    
    ListMFADevicesResult listMFADevicesResult = new ListMFADevicesResult();
    listMFADevicesResult.setMFADevices(new ArrayList<>());;
    when(iamClient.listMFADevices(anyObject())).thenReturn(listMFADevicesResult );
    
    assertThat(inventoryUtil.fetchIAMUsers(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"),
            "account","accountName").size(), is(1));
    
    listMFADevicesResult = new ListMFADevicesResult();
    List<MFADevice> mfaDevices = new ArrayList<>();
    mfaDevices.add(new MFADevice());
    listMFADevicesResult.setMFADevices(mfaDevices);
    when(iamClient.listMFADevices(anyObject())).thenReturn(listMFADevicesResult );
    
    assertThat(inventoryUtil.fetchIAMUsers(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"),
            "account","accountName").size(), is(1));
}
 
Example 3
Source File: AwsIamAccountWithPermanentAccessKeysRuleTest.java    From pacbot with Apache License 2.0 2 votes vote down vote up
@Test
public void test()throws Exception{
    AccessKeyMetadata accessKeyMetadata = new AccessKeyMetadata();
    accessKeyMetadata.setAccessKeyId("123");
   
    List<AccessKeyMetadata> accessKeyMetadatas  = new ArrayList<>();
    accessKeyMetadatas.add(accessKeyMetadata);
    
    ListAccessKeysResult keysResult  = new ListAccessKeysResult();
    keysResult.setAccessKeyMetadata(accessKeyMetadatas);
    
    

    
    List<AccessKeyMetadata> emptyAccessKeyMetadatas  = new ArrayList<>();
    ListAccessKeysResult emptyKeysResult  = new ListAccessKeysResult();
    
    mockStatic(PacmanUtils.class);
    when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn(
            true);
    
    
   
    
    Map<String,Object>map=new HashMap<String, Object>();
    map.put("client", identityManagementClient);
    AwsIamAccountWithPermanentAccessKeysRule spy = Mockito.spy(new AwsIamAccountWithPermanentAccessKeysRule());
    
    Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject());
    
    mockStatic(IAMUtils.class);
    when(IAMUtils.getAccessKeyInformationForUser(anyString(),anyObject())).thenReturn(accessKeyMetadatas);
    when(identityManagementClient.listAccessKeys(anyObject())).thenReturn(keysResult);
    spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "));
    
    
    when(IAMUtils.getAccessKeyInformationForUser(anyString(),anyObject())).thenReturn(emptyAccessKeyMetadatas);
    when(identityManagementClient.listAccessKeys(anyObject())).thenReturn(emptyKeysResult);
    spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "));
    
    
    
    spy.execute(CommonTestUtils.getMapString("svc_123 "),CommonTestUtils.getMapString("svc_123 "));
    when(identityManagementClient.listAccessKeys(anyObject())).thenThrow(new RuleExecutionFailedExeption());
    assertThatThrownBy( 
            () -> awsIamAccountWithPermanentAccessKeysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class);
    
    
    when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn(
            false);
    assertThatThrownBy(
            () -> awsIamAccountWithPermanentAccessKeysRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class);
}