Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
feat: Added ability to get metadata for a single sdb by name (#200)
Browse files Browse the repository at this point in the history
* feat: Added ability to get metadata for a single sdb by name

* chore: switch to openjdk
  • Loading branch information
fieldju authored Aug 7, 2019
1 parent 42c7702 commit cb53f6d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public GetSDBMetadata(MetadataService metadataService,
this.paginationService = paginationService;
}

@SuppressWarnings("ConstantConditions") // it lies
@Override
public CompletableFuture<ResponseInfo<SDBMetadataResult>> doExecute(final RequestInfo<Void> request,
final Executor longRunningTaskExecutor,
Expand All @@ -64,10 +63,13 @@ public CompletableFuture<ResponseInfo<SDBMetadataResult>> doExecute(final Reques
}

private FullResponseInfo<SDBMetadataResult> getMetadata(RequestInfo<Void> 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
Expand Down
40 changes: 25 additions & 15 deletions src/main/java/com/nike/cerberus/service/MetadataService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@
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;
import org.slf4j.Logger;
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
Expand Down Expand Up @@ -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<SDBMetadata> sdbMetadataList = getSDBMetadataList(limit, offset);
List<SDBMetadata> sdbMetadataList = getSDBMetadataList(limit, offset, sdbNameFilter);
result.setSafeDepositBoxMetadata(sdbMetadataList);
result.setSdbCountInResult(sdbMetadataList.size());

Expand All @@ -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<SDBMetadata> getSDBMetadataList(int limit, int offset) {
protected List<SDBMetadata> getSDBMetadataList(int limit, int offset, String sdbNameFilter) {
List<SDBMetadata> sdbs = new LinkedList<>();

// Collect the categories.
Map<String, String> catIdToStringMap = categoryService.getCategoryIdToCategoryNameMap();
// Collect the roles
Map<String, String> roleIdToStringMap = roleService.getRoleIdToStringMap();
// Collect The SDB Records
List<SafeDepositBoxV2> safeDepositBoxes = safeDepositBoxService.getSafeDepositBoxes(limit, offset);

List<SafeDepositBoxV2> 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 -> {
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/nike/cerberus/service/SafeDepositBoxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,6 @@ public SafeDepositBoxV1 getSDBAndValidatePrincipalAssociationV1(CerberusPrincipa
*/
public SafeDepositBoxV2 getSDBAndValidatePrincipalAssociationV2(CerberusPrincipal principal, String sdbId) {

Optional<SafeDepositBoxRecord> 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);

Expand All @@ -213,7 +203,7 @@ public SafeDepositBoxV2 getSDBAndValidatePrincipalAssociationV2(CerberusPrincipa
.build();
}

return getSDBFromRecordV2(safeDepositBoxRecord);
return getSafeDepositBoxDangerouslyWithoutPermissionValidation(sdbId);
}

protected SafeDepositBoxV2 getSDBFromRecordV2(SafeDepositBoxRecord safeDepositBoxRecord) {
Expand Down Expand Up @@ -794,4 +784,15 @@ public Set<String> getSecureDataVersionPathsForSdb(String sdbId) {

return versionPaths;
}

public SafeDepositBoxV2 getSafeDepositBoxDangerouslyWithoutPermissionValidation(String sdbId) {
Optional<SafeDepositBoxRecord> safeDepositBoxRecordOptional = safeDepositBoxDao.getSafeDepositBox(sdbId);

return getSDBFromRecordV2(safeDepositBoxRecordOptional
.orElseThrow(() ->
ApiException.newBuilder()
.withApiErrors(DefaultApiError.ENTITY_NOT_FOUND)
.build())
);
}
}
10 changes: 5 additions & 5 deletions src/test/java/com/nike/cerberus/service/MetadataServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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<SDBMetadata> actual = metadataService.getSDBMetadataList(1,0);
List<SDBMetadata> 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());
Expand Down

0 comments on commit cb53f6d

Please sign in to comment.