From 5d97373048045d23957babd309e61876db519284 Mon Sep 17 00:00:00 2001 From: Holash Chand Date: Wed, 8 May 2024 12:15:54 +0530 Subject: [PATCH 1/2] Fixed not getting private fields in Get Entity response --- .../controller/RegistryController.java | 2 +- .../controller/RegistryEntityController.java | 4 +- .../registry/helper/RegistryHelper.java | 49 ++++++++++--------- .../registry/service/NativeSearchService.java | 13 +++-- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryController.java b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryController.java index c2b5d844e..bce2c06c0 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryController.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryController.java @@ -56,7 +56,7 @@ public ResponseEntity searchEntity(@RequestHeader HttpHeaders header) try { watch.start("RegistryController.searchEntity"); - JsonNode result = registryHelper.searchEntity(payload, null, false); + JsonNode result = registryHelper.searchEntity(payload, null); response.setResult(result); responseParams.setStatus(Response.Status.SUCCESSFUL); diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java index 72b85a060..f5f10ebcc 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/RegistryEntityController.java @@ -220,7 +220,7 @@ public ResponseEntity searchEntity(@PathVariable String entityName, searchNode.set(ENTITY_TYPE, entity); checkEntityNameInDefinitionManager(entityName); if (definitionsManager.getDefinition(entityName).getOsSchemaConfiguration().getEnableSearch()) { - JsonNode result = registryHelper.searchEntity(searchNode, null, false).get(entityName); + JsonNode result = registryHelper.searchEntity(searchNode, null).get(entityName); ObjectNode pageUrls = JSONUtil.getSearchPageUrls(searchNode, searchLimit, searchOffset, result.get(TOTAL_COUNT).asLong(), request.getRequestURL().toString()); ((ObjectNode) result).setAll(pageUrls); watch.stop("RegistryController.searchEntity"); @@ -696,7 +696,7 @@ public ResponseEntity getEntityByToken(@PathVariable String entityName, String userId = registryHelper.getUserId(entityName); if (!Strings.isEmpty(userId)) { JsonNode searchQuery = registryHelper.searchQueryByUserId(entityName, userId, searchToken, viewTemplateId); - JsonNode responseFromDb = registryHelper.searchEntity(searchQuery, userId, true); + JsonNode responseFromDb = registryHelper.searchEntityFromDBWithPrivateFields(searchQuery, userId); JsonNode results = responseFromDb.get(entityName); if (!results.isEmpty()) { ObjectNode pageUrls = JSONUtil.getSearchPageUrls(searchQuery, searchLimit, searchOffset, results.get(TOTAL_COUNT).asLong(), request.getRequestURL().toString()); diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java index e79e37653..69d966e55 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/helper/RegistryHelper.java @@ -350,13 +350,23 @@ public JsonNode readEntity(JsonNode inputJson, String userId) throws Exception { * @return * @throws Exception */ - public JsonNode searchEntity(JsonNode inputJson, String userId, boolean forceNativeSearch) throws Exception { - return searchEntity(inputJson, forceNativeSearch ? nativeSearchService : searchService, userId); + public JsonNode searchEntity(JsonNode inputJson, String userId) throws Exception { + return searchEntity(inputJson, searchService, userId, false); } - private JsonNode searchEntity(JsonNode inputJson, ISearchService service, String userId) throws Exception { + public JsonNode searchEntityFromDBWithPrivateFields(JsonNode inputJson, String userId) throws Exception { + return searchEntity(inputJson, nativeSearchService, userId, true); + } + + private JsonNode searchEntity(JsonNode inputJson, ISearchService service, String userId, boolean skipRemoveNonPublicFields) throws Exception { logger.debug("searchEntity starts"); - ObjectNode resultNode = (ObjectNode) service.search(inputJson, userId); + ObjectNode resultNode; + if(skipRemoveNonPublicFields && service instanceof NativeSearchService) { + resultNode = (ObjectNode) ((NativeSearchService) service).search(inputJson, userId, true); + } else { + resultNode = (ObjectNode) service.search(inputJson, userId); + } + ViewTemplate viewTemplate = viewTemplateManager.getViewTemplate(inputJson); if (viewTemplate != null) { ViewTransformer vTransformer = new ViewTransformer(); @@ -738,12 +748,12 @@ public JsonNode getAuditLog(JsonNode inputJson, String userId) throws Exception newEntityArrNode.add(entityType + auditSuffixSeparator + auditSuffix); ((ObjectNode) queryNode).set(ENTITY_TYPE, newEntityArrNode); - JsonNode resultNode = searchService.search(queryNode, userId); + ObjectNode resultNode = (ObjectNode) searchService.search(queryNode, userId); ViewTemplate viewTemplate = viewTemplateManager.getViewTemplate(inputJson); if (viewTemplate != null) { ViewTransformer vTransformer = new ViewTransformer(); - resultNode = vTransformer.transform(viewTemplate, resultNode); + resultNode.set(ENTITY_LIST, vTransformer.transform(viewTemplate, resultNode.get(ENTITY_LIST))); } logger.debug("get audit log ends"); @@ -826,28 +836,20 @@ private JsonNode getUserInfoFromRegistry(HttpServletRequest request, String enti ObjectNode payload = getSearchByOwnerQuery(entityName, userId); watch.start("RegistryController.searchEntity"); - JsonNode result = searchEntity(payload, userId, false); + JsonNode result = searchEntity(payload, userId); watch.stop("RegistryController.searchEntity"); - if(result != null && result.get(entityName) != null && !result.get(entityName).isEmpty()) { - String uuid = result.get(entityName).get(0).get(uuidPropertyName).asText(); + if(result != null && result.get(entityName) != null && !result.get(entityName).get(ENTITY_LIST).isEmpty()) { + String uuid = result.get(entityName).get(ENTITY_LIST).get(0).get(uuidPropertyName).asText(); JsonNode user = readEntity(userId, entityName, uuid, true, null, false); ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode(); arrayNode.add(user.get(entityName)); - ((ObjectNode) result).set(entityName, arrayNode); + ((ObjectNode) result.get(entityName)).set(ENTITY_LIST, arrayNode); } return result; } throw new Exception("Forbidden"); } - private JsonNode getEntityByUserId(String entityName, String userId) throws Exception { - ObjectNode payload = getSearchByOwnerQuery(entityName, userId); - watch.start("RegistryController.searchEntity"); - JsonNode result = searchEntity(payload, userId, false); - watch.stop("RegistryController.searchEntity"); - return result; - } - @NotNull private ObjectNode getSearchByOwnerQuery(String entityName, String userId) { ObjectNode payload = JsonNodeFactory.instance.objectNode(); @@ -1098,7 +1100,8 @@ public void invalidateClaim(String attestorEntityName, String userId, String cla final String attestorPlugin = "did:internal:ClaimPluginActor"; Action action = Action.SET_TO_DRAFT; ObjectNode additionalInputs = JsonNodeFactory.instance.objectNode(); - JsonNode attestorInfo = getEntityByUserId(attestorEntityName, userId).get(attestorEntityName).get(0); + JsonNode searchQuery = searchQueryByUserId(attestorEntityName, userId, null, null); + JsonNode attestorInfo = searchEntityFromDBWithPrivateFields(searchQuery, userId).get(attestorEntityName).get(ENTITY_LIST).get(0); additionalInputs.put("claimId", claimId); additionalInputs.put("action", action.name()); additionalInputs.put("notes", "Closed due to entity update"); @@ -1187,7 +1190,7 @@ private List getAttestationsFromRegistry(String entityName) { " }\n" + " }\n" + "}"); - JsonNode searchResponse = searchEntity(searchRequest, "", false); + JsonNode searchResponse = searchEntity(searchRequest, ""); return convertJsonNodeToAttestationList(searchResponse); } catch (Exception e) { logger.error("Error fetching attestation policy: {}", ExceptionUtils.getStackTrace(e)); @@ -1254,7 +1257,7 @@ public List findAttestationPolicyByEntityAndCreatedBy(String " }\n" + " }\n" + "}"); - searchEntity(searchRequest, userId, false); + searchEntity(searchRequest, userId); return Collections.emptyList(); } @@ -1343,8 +1346,8 @@ public boolean checkIfCredentialIsRevoked(String signedData, String userId) thro searchNode.set(FILTERS, JsonNodeFactory.instance.objectNode().set(SIGNED_HASH, JsonNodeFactory.instance.objectNode().put("eq", generateHash(signedData)))); - JsonNode searchResponse = searchEntity(searchNode, userId, false); - return searchResponse.get(REVOKED_CREDENTIAL) != null && searchResponse.get(REVOKED_CREDENTIAL).size() > 0; + JsonNode searchResponse = searchEntity(searchNode, userId); + return searchResponse.get(REVOKED_CREDENTIAL) != null && !searchResponse.get(REVOKED_CREDENTIAL).get(ENTITY_LIST).isEmpty(); } public static ResponseEntity ServiceNotEnabledResponse(String message, Response response, ResponseParams responseParams) { diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/service/NativeSearchService.java b/java/registry/src/main/java/dev/sunbirdrc/registry/service/NativeSearchService.java index eafe98e4d..012dd3669 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/service/NativeSearchService.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/service/NativeSearchService.java @@ -86,6 +86,10 @@ public class NativeSearchService implements ISearchService { @Override public JsonNode search(JsonNode inputQueryNode, String userId) throws IOException { + return search(inputQueryNode, userId, false); + } + + public JsonNode search(JsonNode inputQueryNode, String userId, boolean skipRemoveNonPublicFields) throws IOException { ArrayNode result = JsonNodeFactory.instance.arrayNode(); SearchQuery searchQuery = getSearchQuery(inputQueryNode, offset, limit); @@ -125,7 +129,7 @@ public JsonNode search(JsonNode inputQueryNode, String userId) throws IOExceptio String prefix = shard.getShardLabel() + RecordIdentifier.getSeparator(); JSONUtil.addPrefix((ObjectNode) shardResult, prefix, new ArrayList<>(Arrays.asList(uuidPropertyName))); } - result.add(removeNonPublicFields(searchQuery, shardResult)); + result.add(removeNonPublicFields(searchQuery, shardResult, skipRemoveNonPublicFields)); if (tx != null) { transaction.add(tx.hashCode()); } @@ -145,14 +149,13 @@ public JsonNode search(JsonNode inputQueryNode, String userId) throws IOExceptio } catch (Exception e) { logger.error("Exception while auditing: {}", ExceptionUtils.getStackTrace(e)); } - - } + } } return buildResultNode(searchQuery, result); } - private ObjectNode removeNonPublicFields(SearchQuery searchQuery, ObjectNode shardResult) throws Exception { + private ObjectNode removeNonPublicFields(SearchQuery searchQuery, ObjectNode shardResult, boolean skipRemoveNonPublicFields) throws Exception { ObjectNode response = JsonNodeFactory.instance.objectNode(); NumericNode count; for(String entityType: searchQuery.getEntityTypes()) { @@ -160,7 +163,7 @@ private ObjectNode removeNonPublicFields(SearchQuery searchQuery, ObjectNode sha ArrayNode data = JsonNodeFactory.instance.arrayNode(); ArrayNode arrayNode = (ArrayNode) (shardResult.get(entityType).get(ENTITY_LIST)); count = (NumericNode) shardResult.get(entityType).get(TOTAL_COUNT); - if (removeNonPublicFieldsForNativeSearch) { + if (removeNonPublicFieldsForNativeSearch && !skipRemoveNonPublicFields) { for(JsonNode node : arrayNode) { data.add(JSONUtil.removeNodesByPath(node, definitionsManager.getExcludingFieldsForEntity(entityType))); } From c5b76fd0f9a1d5466c73cde2d7c873015ca7a4be Mon Sep 17 00:00:00 2001 From: Holash Chand Date: Wed, 8 May 2024 12:32:29 +0530 Subject: [PATCH 2/2] fixed unit tests --- .../sunbirdrc/registry/helper/RegistryHelperTest.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java b/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java index 0b6ce6ffc..80fb2b884 100644 --- a/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java +++ b/java/registry/src/test/java/dev/sunbirdrc/registry/helper/RegistryHelperTest.java @@ -58,6 +58,7 @@ import java.util.*; import static dev.sunbirdrc.registry.Constants.*; +import static dev.sunbirdrc.registry.middleware.util.Constants.ENTITY_LIST; import static dev.sunbirdrc.registry.middleware.util.Constants.FILTERS; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; @@ -806,14 +807,19 @@ public void shouldNotStoredSignedDataIfNullOrEmptyInRevokedCredentialsRegistry() @Test public void shouldReturnTrueIFSignedDataIsRevoked() throws Exception { - JsonNode searchResponse = JsonNodeFactory.instance.objectNode().set(REVOKED_CREDENTIAL, JsonNodeFactory.instance.arrayNode().add(JsonNodeFactory.instance.objectNode().put("signedData", "xyz"))); + JsonNode searchResponse = JsonNodeFactory.instance.objectNode() + .set(REVOKED_CREDENTIAL, JsonNodeFactory.instance.objectNode() + .set(ENTITY_LIST, JsonNodeFactory.instance.arrayNode() + .add(JsonNodeFactory.instance.objectNode().put("signedData", "xyz")))); when(searchService.search(any(), anyString())).thenReturn(searchResponse); assertTrue(registryHelper.checkIfCredentialIsRevoked("signedData", "")); } @Test public void shouldReturnFalseIfSignedDataIsNotRevoked() throws Exception { - JsonNode searchResponse = JsonNodeFactory.instance.objectNode().set(REVOKED_CREDENTIAL, JsonNodeFactory.instance.arrayNode()); + JsonNode searchResponse = JsonNodeFactory.instance.objectNode().set(REVOKED_CREDENTIAL, + JsonNodeFactory.instance.objectNode() + .set(ENTITY_LIST, JsonNodeFactory.instance.arrayNode())); when(searchService.search(any(), anyString())).thenReturn(searchResponse); assertFalse(registryHelper.checkIfCredentialIsRevoked("signedData", "")); }