Skip to content

Commit

Permalink
Merge pull request Sunbird-RC#317 from holashchand/issue-1008
Browse files Browse the repository at this point in the history
[BUG][LTS-2.X]: Fixed not getting private fields in Get Entity response
  • Loading branch information
challabeehyv authored May 9, 2024
2 parents 31440c9 + c5b76fd commit b30bbc3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ResponseEntity<Response> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public ResponseEntity<Object> 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");
Expand Down Expand Up @@ -696,7 +696,7 @@ public ResponseEntity<Object> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -1187,7 +1190,7 @@ private List<AttestationPolicy> 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));
Expand Down Expand Up @@ -1254,7 +1257,7 @@ public List<AttestationPolicy> findAttestationPolicyByEntityAndCreatedBy(String
" }\n" +
" }\n" +
"}");
searchEntity(searchRequest, userId, false);
searchEntity(searchRequest, userId);
return Collections.emptyList();
}

Expand Down Expand Up @@ -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<Object> ServiceNotEnabledResponse(String message, Response response, ResponseParams responseParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand All @@ -145,22 +149,21 @@ 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()) {
ObjectNode result = JsonNodeFactory.instance.objectNode();
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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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", ""));
}
Expand Down

0 comments on commit b30bbc3

Please sign in to comment.