diff --git a/src/main/java/com/nike/cerberus/endpoints/admin/GetSDBMetadata.java b/src/main/java/com/nike/cerberus/endpoints/admin/GetSDBMetadata.java index e4416f1ee..9033aa6dd 100644 --- a/src/main/java/com/nike/cerberus/endpoints/admin/GetSDBMetadata.java +++ b/src/main/java/com/nike/cerberus/endpoints/admin/GetSDBMetadata.java @@ -51,7 +51,6 @@ public GetSDBMetadata(MetadataService metadataService, this.paginationService = paginationService; } - @SuppressWarnings("ConstantConditions") // it lies @Override public CompletableFuture> doExecute(final RequestInfo request, final Executor longRunningTaskExecutor, @@ -64,10 +63,13 @@ public CompletableFuture> doExecute(final Reques } private FullResponseInfo getMetadata(RequestInfo request) { - return ResponseInfo.newBuilder(metadataService.getSDBMetadata( + String sdbNameFilter = request.getQueryParamSingle("sdbName"); + return ResponseInfo.newBuilder( + metadataService.getSDBMetadata( paginationService.getLimit(request), - paginationService.getOffset(request))) - .build(); + paginationService.getOffset(request), + sdbNameFilter + )).build(); } @Override diff --git a/src/main/java/com/nike/cerberus/service/MetadataService.java b/src/main/java/com/nike/cerberus/service/MetadataService.java index 9a788a546..fbc264bc5 100644 --- a/src/main/java/com/nike/cerberus/service/MetadataService.java +++ b/src/main/java/com/nike/cerberus/service/MetadataService.java @@ -23,6 +23,7 @@ import com.nike.cerberus.domain.SDBMetadataResult; import com.nike.cerberus.domain.SafeDepositBoxV2; import com.nike.cerberus.domain.UserGroupPermission; +import com.nike.cerberus.error.DefaultApiError; import com.nike.cerberus.error.InvalidCategoryNameApiError; import com.nike.cerberus.error.InvalidRoleNameApiError; import com.nike.cerberus.util.UuidSupplier; @@ -30,13 +31,7 @@ import org.slf4j.LoggerFactory; import javax.inject.Inject; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.*; /** * A service that can perform admin tasks around SDB metadata @@ -174,16 +169,17 @@ private String getRoleIdFromName(String roleName) { * @param offset The int offset for paginating. * @return SDBMetadataResult of meta data. */ - public SDBMetadataResult getSDBMetadata(int limit, int offset) { + public SDBMetadataResult getSDBMetadata(int limit, int offset, String sdbNameFilter) { SDBMetadataResult result = new SDBMetadataResult(); - result.setLimit(limit); - result.setOffset(offset); - result.setTotalSDBCount(safeDepositBoxService.getTotalNumberOfSafeDepositBoxes()); + result.setLimit(Optional.ofNullable(sdbNameFilter).map(it -> 1).orElse(limit)); + result.setOffset(Optional.ofNullable(sdbNameFilter).map(it -> 1).orElse(offset)); + result.setTotalSDBCount(Optional.ofNullable(sdbNameFilter).map(it -> 1) + .orElseGet(safeDepositBoxService::getTotalNumberOfSafeDepositBoxes)); result.setHasNext(result.getTotalSDBCount() > (offset + limit)); if (result.isHasNext()) { result.setNextOffset(offset + limit); } - List sdbMetadataList = getSDBMetadataList(limit, offset); + List sdbMetadataList = getSDBMetadataList(limit, offset, sdbNameFilter); result.setSafeDepositBoxMetadata(sdbMetadataList); result.setSdbCountInResult(sdbMetadataList.size()); @@ -196,15 +192,29 @@ public SDBMetadataResult getSDBMetadata(int limit, int offset) { * @param offset The offset for pagination * @return A list of SDB Metadata */ - protected List getSDBMetadataList(int limit, int offset) { + protected List getSDBMetadataList(int limit, int offset, String sdbNameFilter) { List sdbs = new LinkedList<>(); // Collect the categories. Map catIdToStringMap = categoryService.getCategoryIdToCategoryNameMap(); // Collect the roles Map roleIdToStringMap = roleService.getRoleIdToStringMap(); - // Collect The SDB Records - List safeDepositBoxes = safeDepositBoxService.getSafeDepositBoxes(limit, offset); + + List safeDepositBoxes = + Optional.ofNullable(sdbNameFilter) + .map( + i -> + Collections.singletonList( + safeDepositBoxService + .getSafeDepositBoxDangerouslyWithoutPermissionValidation( + safeDepositBoxService + .getSafeDepositBoxIdByName(sdbNameFilter) + .orElseThrow( + () -> + ApiException.newBuilder() + .withApiErrors(DefaultApiError.ENTITY_NOT_FOUND) + .build())))) + .orElseGet(() -> safeDepositBoxService.getSafeDepositBoxes(limit, offset)); // for each SDB collect the user and iam permissions and add to result safeDepositBoxes.forEach(sdb -> { diff --git a/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java b/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java index 50a01b5f4..ff57272b6 100644 --- a/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java +++ b/src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java @@ -194,16 +194,6 @@ public SafeDepositBoxV1 getSDBAndValidatePrincipalAssociationV1(CerberusPrincipa */ public SafeDepositBoxV2 getSDBAndValidatePrincipalAssociationV2(CerberusPrincipal principal, String sdbId) { - Optional safeDepositBoxRecordOptional = safeDepositBoxDao.getSafeDepositBox(sdbId); - - if (! safeDepositBoxRecordOptional.isPresent()) { - throw ApiException.newBuilder() - .withApiErrors(DefaultApiError.ENTITY_NOT_FOUND) - .build(); - } - - SafeDepositBoxRecord safeDepositBoxRecord = safeDepositBoxRecordOptional.get(); - boolean doesPrincipalHaveReadPerms = sdbPermissionService .doesPrincipalHaveReadPermission(principal, sdbId); @@ -213,7 +203,7 @@ public SafeDepositBoxV2 getSDBAndValidatePrincipalAssociationV2(CerberusPrincipa .build(); } - return getSDBFromRecordV2(safeDepositBoxRecord); + return getSafeDepositBoxDangerouslyWithoutPermissionValidation(sdbId); } protected SafeDepositBoxV2 getSDBFromRecordV2(SafeDepositBoxRecord safeDepositBoxRecord) { @@ -794,4 +784,15 @@ public Set getSecureDataVersionPathsForSdb(String sdbId) { return versionPaths; } + + public SafeDepositBoxV2 getSafeDepositBoxDangerouslyWithoutPermissionValidation(String sdbId) { + Optional safeDepositBoxRecordOptional = safeDepositBoxDao.getSafeDepositBox(sdbId); + + return getSDBFromRecordV2(safeDepositBoxRecordOptional + .orElseThrow(() -> + ApiException.newBuilder() + .withApiErrors(DefaultApiError.ENTITY_NOT_FOUND) + .build()) + ); + } } diff --git a/src/test/java/com/nike/cerberus/service/MetadataServiceTest.java b/src/test/java/com/nike/cerberus/service/MetadataServiceTest.java index cfcb5b4d4..12bc926cd 100644 --- a/src/test/java/com/nike/cerberus/service/MetadataServiceTest.java +++ b/src/test/java/com/nike/cerberus/service/MetadataServiceTest.java @@ -85,9 +85,9 @@ public void test_that_get_sdb_metadata_properly_sets_result_metadata() { when(safeDepositBoxService.getTotalNumberOfSafeDepositBoxes()).thenReturn(totalSDBs); SDBMetadata sdbMD = new SDBMetadata(); - doReturn(Arrays.asList(sdbMD)).when(metadataServiceSpy).getSDBMetadataList(limit, offset); + doReturn(Arrays.asList(sdbMD)).when(metadataServiceSpy).getSDBMetadataList(limit, offset, null); - SDBMetadataResult actual = metadataServiceSpy.getSDBMetadata(limit, offset); + SDBMetadataResult actual = metadataServiceSpy.getSDBMetadata(limit, offset, null); assertEquals("expected actual limit to be passed in limit", limit, actual.getLimit()); assertEquals("expected actual offset to be passed in offset", offset, actual.getOffset()); @@ -104,9 +104,9 @@ public void test_that_get_sdb_metadata_set_has_next_to_no_when_done_paging() { int totalSDBs = 20; when(safeDepositBoxService.getTotalNumberOfSafeDepositBoxes()).thenReturn(totalSDBs); - doReturn(Arrays.asList(new SDBMetadata())).when(metadataServiceSpy).getSDBMetadataList(limit, offset); + doReturn(Arrays.asList(new SDBMetadata())).when(metadataServiceSpy).getSDBMetadataList(limit, offset, null); - SDBMetadataResult actual = metadataServiceSpy.getSDBMetadata(limit, offset); + SDBMetadataResult actual = metadataServiceSpy.getSDBMetadata(limit, offset, null); assertEquals("expected actual limit to be passed in limit", limit, actual.getLimit()); assertEquals("expected actual offset to be passed in offset", offset, actual.getOffset()); @@ -167,7 +167,7 @@ public void test_that_get_sdb_metadata_list_returns_valid_list() { when(safeDepositBoxService.getSafeDepositBoxes(1,0)).thenReturn(Arrays.asList(box)); - List actual = metadataService.getSDBMetadataList(1,0); + List actual = metadataService.getSDBMetadataList(1,0, null); assertEquals("List should have 1 entry", 1, actual.size()); SDBMetadata data = actual.get(0); assertEquals("Name should match record", name, data.getName());