Search in sources :

Example 16 with AwsIamRoleRecord

use of com.nike.cerberus.record.AwsIamRoleRecord in project cerberus by Nike-Inc.

the class KmsServiceTest method test_that_getAuthenticationKmsMetadata_returns_AuthKmsKeyMetadata_from_dao_data.

@Test
public void test_that_getAuthenticationKmsMetadata_returns_AuthKmsKeyMetadata_from_dao_data() {
    OffsetDateTime create = OffsetDateTime.now().plus(5, ChronoUnit.MINUTES);
    OffsetDateTime update = OffsetDateTime.now().plus(3, ChronoUnit.MINUTES);
    OffsetDateTime validate = OffsetDateTime.now().plus(7, ChronoUnit.MINUTES);
    List<AwsIamRoleKmsKeyRecord> keyRecords = ImmutableList.of(new AwsIamRoleKmsKeyRecord().setAwsIamRoleId("iam-role-id").setAwsKmsKeyId("key-id").setAwsRegion("us-west-2").setCreatedTs(create).setLastUpdatedTs(update).setLastValidatedTs(validate));
    List<AuthKmsKeyMetadata> expected = ImmutableList.of(new AuthKmsKeyMetadata().setAwsIamRoleArn("iam-role-arn").setAwsKmsKeyId("key-id").setAwsRegion("us-west-2").setCreatedTs(create).setLastUpdatedTs(update).setLastValidatedTs(validate));
    when(awsIamRoleDao.getAllKmsKeys()).thenReturn(Optional.ofNullable(keyRecords));
    when(awsIamRoleDao.getIamRoleById("iam-role-id")).thenReturn(Optional.of(new AwsIamRoleRecord().setAwsIamRoleArn("iam-role-arn")));
    assertArrayEquals(expected.toArray(), kmsService.getAuthenticationKmsMetadata().toArray());
}
Also used : AuthKmsKeyMetadata(com.nike.cerberus.domain.AuthKmsKeyMetadata) OffsetDateTime(java.time.OffsetDateTime) AwsIamRoleKmsKeyRecord(com.nike.cerberus.record.AwsIamRoleKmsKeyRecord) AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) Test(org.junit.Test)

Example 17 with AwsIamRoleRecord

use of com.nike.cerberus.record.AwsIamRoleRecord in project cerberus by Nike-Inc.

the class IamPrincipalPermissionServiceTest method testIamPrincipalPermissionsUpdateWhenIamRoleIsPresent.

@Test
public void testIamPrincipalPermissionsUpdateWhenIamRoleIsPresent() {
    IamPrincipalPermission iamPrincipalPermission = new IamPrincipalPermission();
    iamPrincipalPermission.setIamPrincipalArn("arn");
    AwsIamRoleRecord awsIamRoleRecord = new AwsIamRoleRecord();
    Set<IamPrincipalPermission> iamPrincipalPermissions = new HashSet<>();
    iamPrincipalPermissions.add(iamPrincipalPermission);
    Mockito.when(awsIamRoleDao.getIamRole("arn")).thenReturn(Optional.of(awsIamRoleRecord));
    iamPrincipalPermissionService.updateIamPrincipalPermissions("boxId", iamPrincipalPermissions, "user", OffsetDateTime.MAX);
    Mockito.verify(awsIamRoleDao).updateIamRolePermission(Mockito.any(AwsIamRolePermissionRecord.class));
}
Also used : AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) AwsIamRolePermissionRecord(com.nike.cerberus.record.AwsIamRolePermissionRecord) IamPrincipalPermission(com.nike.cerberus.domain.IamPrincipalPermission) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 18 with AwsIamRoleRecord

use of com.nike.cerberus.record.AwsIamRoleRecord in project cerberus by Nike-Inc.

the class IamPrincipalPermissionServiceTest method testRevokePermissionWhenIamRoleIsPresent.

@Test
public void testRevokePermissionWhenIamRoleIsPresent() {
    IamPrincipalPermission iamPrincipalPermission = Mockito.mock(IamPrincipalPermission.class);
    Mockito.when(iamPrincipalPermission.getIamPrincipalArn()).thenReturn("arn");
    AwsIamRoleRecord awsIamRoleRecord = Mockito.mock(AwsIamRoleRecord.class);
    Mockito.when(awsIamRoleRecord.getId()).thenReturn("id");
    Mockito.when(awsIamRoleDao.getIamRole("arn")).thenReturn(Optional.of(awsIamRoleRecord));
    iamPrincipalPermissionService.revokeIamPrincipalPermission("boxId", iamPrincipalPermission);
    Mockito.verify(awsIamRoleDao).deleteIamRolePermission("boxId", "id");
}
Also used : AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) IamPrincipalPermission(com.nike.cerberus.domain.IamPrincipalPermission) Test(org.junit.Test)

Example 19 with AwsIamRoleRecord

use of com.nike.cerberus.record.AwsIamRoleRecord in project cerberus by Nike-Inc.

the class IamPrincipalPermissionServiceTest method testGetIamPrincipalPermissionsWhenIamRoleIsPresent.

@Test
public void testGetIamPrincipalPermissionsWhenIamRoleIsPresent() {
    List<AwsIamRolePermissionRecord> awsIamRolePermissionRecords = new ArrayList<>();
    AwsIamRolePermissionRecord awsIamRolePermissionRecord = new AwsIamRolePermissionRecord().setId("id").setCreatedBy("createdBy").setLastUpdatedBy("lastUpdatedBy").setRoleId("roleId").setCreatedTs(OffsetDateTime.MAX).setLastUpdatedTs(OffsetDateTime.MAX);
    awsIamRolePermissionRecords.add(awsIamRolePermissionRecord);
    AwsIamRoleRecord awsIamRoleRecord = new AwsIamRoleRecord().setAwsIamRoleArn("awsIamRoleArn");
    Mockito.when(awsIamRoleDao.getIamRolePermissions("boxId")).thenReturn(awsIamRolePermissionRecords);
    Mockito.when(awsIamRoleDao.getIamRoleById(Mockito.anyString())).thenReturn(Optional.of(awsIamRoleRecord));
    Set<IamPrincipalPermission> boxIds = iamPrincipalPermissionService.getIamPrincipalPermissions("boxId");
    Assert.assertTrue(boxIds.size() == 1);
    boxIds.forEach(iamPrincipalPermission -> {
        Assert.assertEquals("id", iamPrincipalPermission.getId());
        Assert.assertEquals("lastUpdatedBy", iamPrincipalPermission.getLastUpdatedBy());
        Assert.assertEquals("createdBy", iamPrincipalPermission.getCreatedBy());
        Assert.assertEquals("roleId", iamPrincipalPermission.getRoleId());
        Assert.assertEquals("awsIamRoleArn", iamPrincipalPermission.getIamPrincipalArn());
        Assert.assertEquals(OffsetDateTime.MAX, iamPrincipalPermission.getCreatedTs());
        Assert.assertEquals(OffsetDateTime.MAX, iamPrincipalPermission.getLastUpdatedTs());
    });
}
Also used : ArrayList(java.util.ArrayList) AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) AwsIamRolePermissionRecord(com.nike.cerberus.record.AwsIamRolePermissionRecord) IamPrincipalPermission(com.nike.cerberus.domain.IamPrincipalPermission) Test(org.junit.Test)

Example 20 with AwsIamRoleRecord

use of com.nike.cerberus.record.AwsIamRoleRecord in project cerberus by Nike-Inc.

the class AuthenticationServiceTest method test_that_findIamRoleAssociatedWithSdb_returns_generic_role_when_iam_principal_not_found.

@Test
public void test_that_findIamRoleAssociatedWithSdb_returns_generic_role_when_iam_principal_not_found() {
    String accountId = "0000000000";
    String roleName = "role/path";
    String principalArn = String.format("arn:aws:iam::%s:instance-profile/%s", accountId, roleName);
    String roleArn = String.format(AWS_IAM_ROLE_ARN_TEMPLATE, AWS_GLOBAL_PARTITION_NAME, accountId, roleName);
    AwsIamRoleRecord awsIamRoleRecord = mock(AwsIamRoleRecord.class);
    when(awsIamRoleDao.getIamRole(principalArn)).thenReturn(Optional.empty());
    when(awsIamRoleDao.getIamRole(roleArn)).thenReturn(Optional.of(awsIamRoleRecord));
    when(awsIamRoleArnParser.isRoleArn(principalArn)).thenReturn(false);
    when(awsIamRoleArnParser.convertPrincipalArnToRoleArn(principalArn)).thenReturn(roleArn);
    Optional<AwsIamRoleRecord> result = authenticationService.findIamRoleAssociatedWithSdb(principalArn);
    assertEquals(awsIamRoleRecord, result.get());
}
Also used : AwsIamRoleRecord(com.nike.cerberus.record.AwsIamRoleRecord) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Aggregations

AwsIamRoleRecord (com.nike.cerberus.record.AwsIamRoleRecord)24 Test (org.junit.Test)17 IamPrincipalPermission (com.nike.cerberus.domain.IamPrincipalPermission)8 AwsIamRolePermissionRecord (com.nike.cerberus.record.AwsIamRolePermissionRecord)8 Matchers.anyString (org.mockito.Matchers.anyString)6 Role (com.nike.cerberus.domain.Role)3 AwsIamRoleKmsKeyRecord (com.nike.cerberus.record.AwsIamRoleKmsKeyRecord)3 OffsetDateTime (java.time.OffsetDateTime)3 HashSet (java.util.HashSet)3 Transactional (org.springframework.transaction.annotation.Transactional)3 AmazonServiceException (com.amazonaws.AmazonServiceException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 AuthKmsKeyMetadata (com.nike.cerberus.domain.AuthKmsKeyMetadata)1 AuthTokenResponse (com.nike.cerberus.domain.AuthTokenResponse)1 EncryptedAuthDataWrapper (com.nike.cerberus.domain.EncryptedAuthDataWrapper)1 ArrayList (java.util.ArrayList)1