NamespaceIamRoleAuthorizationHelper { public void checkPermissions(NamespaceEntity namespaceEntity, String... requestedIamRoleNames) { checkPermissions(namespaceEntity, Arrays.asList(requestedIamRoleNames)); }  void checkPermissions(NamespaceEntity namespaceEntity, String... requestedIamRoleNames); void checkPermissions(NamespaceEntity namespaceEntity, Collection<String> requestedIamRoleNames);  }
@Test public void checkPermissionsAssertNoErrorWhenNamespaceAuthorizedToAllRoles() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); String iamRoleName1 = "iamRoleName1"; String iamRoleName2 = "iamRoleName2"; Collection<String> requestedIamRoleNames = Arrays.asList(iamRoleName1, iamRoleName2); List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity1 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity1.setIamRoleName(iamRoleName1); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity1); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity2 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity2.setIamRoleName(iamRoleName2); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity2); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsAssertAccessDeniedWhenNamespaceNotAuthorizedToOneRole() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); expectedNamespaceEntity.setCode("namespace"); String iamRoleName1 = "iamRoleName1"; String iamRoleName2 = "iamRoleName2"; Collection<String> requestedIamRoleNames = Arrays.asList(iamRoleName1, iamRoleName2); List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity1 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity1.setIamRoleName(iamRoleName1); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity1); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); try { namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); fail(); } catch (AccessDeniedException e) { assertEquals("The namespace \"namespace\" does not have access to the following IAM roles: [iamRoleName2]", e.getMessage()); } verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsAssertAccessDeniedWhenNamespaceNotAuthorizedToAllRole() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); expectedNamespaceEntity.setCode("namespace"); String iamRoleName1 = "iamRoleName1"; String iamRoleName2 = "iamRoleName2"; Collection<String> requestedIamRoleNames = Arrays.asList(iamRoleName1, iamRoleName2); List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); try { namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); fail(); } catch (AccessDeniedException e) { assertEquals("The namespace \"namespace\" does not have access to the following IAM roles: [iamRoleName1, iamRoleName2]", e.getMessage()); } verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsAssertBlankRequestRoleIgnored() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); String iamRoleName1 = BLANK_TEXT; String iamRoleName2 = "iamRoleName2"; Collection<String> requestedIamRoleNames = Arrays.asList(iamRoleName1, iamRoleName2); List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity1 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity1.setIamRoleName("iamRoleName1"); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity1); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity2 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity2.setIamRoleName(iamRoleName2); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity2); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsAssertDoNothingWhenAuthorizationDisabled() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); Collection<String> requestedIamRoleNames = new ArrayList<>(); when(configurationHelper.getBooleanProperty(any())).thenReturn(false); namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsWithArrayAssertNoErrorWhenNamespaceAuthorizedToAllRoles() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); String iamRoleName1 = "iamRoleName1"; String iamRoleName2 = "iamRoleName2"; List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity1 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity1.setIamRoleName(iamRoleName1); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity1); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity2 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity2.setIamRoleName(iamRoleName2); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity2); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, iamRoleName1, iamRoleName2); verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsAssertRoleNameIsTrimmed() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); String iamRoleName1 = "iamRoleName1"; String iamRoleName2 = "iamRoleName2"; Collection<String> requestedIamRoleNames = Arrays.asList(StringUtils.wrap(iamRoleName1, BLANK_TEXT), StringUtils.wrap(iamRoleName2, BLANK_TEXT)); List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity1 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity1.setIamRoleName(iamRoleName1); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity1); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity2 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity2.setIamRoleName(iamRoleName2); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity2); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }
@Test public void checkPermissionsAssertRoleNameIsCaseInsensitive() { NamespaceEntity expectedNamespaceEntity = new NamespaceEntity(); String iamRoleName1 = "iamRoleName1"; String iamRoleName2 = "iamRoleName2"; Collection<String> requestedIamRoleNames = Arrays.asList(StringUtils.capitalize(iamRoleName1), StringUtils.capitalize(iamRoleName2)); List<NamespaceIamRoleAuthorizationEntity> namespaceIamRoleAuthorizationEntities = new ArrayList<>(); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity1 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity1.setIamRoleName(iamRoleName1); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity1); NamespaceIamRoleAuthorizationEntity namespaceIamRoleAuthorizationEntity2 = new NamespaceIamRoleAuthorizationEntity(); namespaceIamRoleAuthorizationEntity2.setIamRoleName(iamRoleName2); namespaceIamRoleAuthorizationEntities.add(namespaceIamRoleAuthorizationEntity2); when(configurationHelper.getBooleanProperty(any())).thenReturn(true); when(namespaceIamRoleAuthorizationDao.getNamespaceIamRoleAuthorizations(any())).thenReturn(namespaceIamRoleAuthorizationEntities); namespaceIamRoleAuthorizationHelper.checkPermissions(expectedNamespaceEntity, requestedIamRoleNames); verify(configurationHelper).getBooleanProperty(ConfigurationValue.NAMESPACE_IAM_ROLE_AUTHORIZATION_ENABLED); verify(namespaceIamRoleAuthorizationDao).getNamespaceIamRoleAuthorizations(expectedNamespaceEntity); verifyNoMoreInteractions(configurationHelper, namespaceIamRoleAuthorizationDao); }