Skip to content

Commit

Permalink
Merge pull request #325 from holashchand/issue-985
Browse files Browse the repository at this point in the history
[BUG]: Resolved hardcoded uuid property name as osid
  • Loading branch information
challabeehyv authored May 27, 2024
2 parents b3ded1d + 0acdc0e commit d1a5b26
Show file tree
Hide file tree
Showing 43 changed files with 296 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public String getAttestationId() {
return attestationId;
}

public void setAttestationId(String attestationOSID) {
this.attestationId = attestationOSID;
public void setAttestationId(String attestationUUID) {
this.attestationId = attestationUUID;
}

public String getAttestationName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
Expand All @@ -14,7 +15,8 @@
public class ClaimsAuthorizer {

private static final String ATTESTOR = "ATTESTOR";
private static final String UUID_PROPERTY_NAME = "osid";
@Value("${uuid-property-name}")
private String uuidPropertyName;
private static final Logger logger = LoggerFactory.getLogger(ClaimsAuthorizer.class);

private final ConditionResolverService conditionResolverService;
Expand Down Expand Up @@ -42,8 +44,8 @@ public boolean isAuthorizedAttestor(Claim claim, JsonNode attestorNode) {
}

public boolean isAuthorizedRequestor(Claim claim, JsonNode attestorNode) {
if(!attestorNode.isNull() && attestorNode.has(UUID_PROPERTY_NAME)) {
String userEntityId = attestorNode.get(UUID_PROPERTY_NAME).asText();
if(!attestorNode.isNull() && attestorNode.has(uuidPropertyName)) {
String userEntityId = attestorNode.get(uuidPropertyName).asText();
return claim.getEntityId().equals(userEntityId);
}
return false;
Expand Down
4 changes: 3 additions & 1 deletion java/claim/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
sunbirdrc.url=${sunbirdrc_url:http://localhost:8081}
sunbirdrc.url=${sunbirdrc_url:http://localhost:8081}

uuid-property-name=${uuid_property_name:osid}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
@JsonSerialize
public class ESMessage {
String indexName;
String osid;
String uuidPropertyValue;
JsonNode input;

public ESMessage() {
}

/*public ESMessage(String indexName, String osid, JsonNode input) {
setIndexName(indexName);
setOsid(osid);
setInput(input);
}*/

public String getIndexName() {
return indexName;
}
Expand All @@ -26,12 +20,12 @@ public void setIndexName(String indexName) {
this.indexName = indexName;
}

public String getOsid() {
return osid;
public String getUuidPropertyValue() {
return uuidPropertyValue;
}

public void setOsid(String osid) {
this.osid = osid;
public void setUuidPropertyValue(String uuidPropertyValue) {
this.uuidPropertyValue = uuidPropertyValue;
}

public JsonNode getInput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,17 @@ public RestStatus addEntity(String index, String entityId, JsonNode inputEntity)
* Reads the document from Elastic search
*
* @param index - ElasticSearch Index
* @param osid - which maps to document
* @param uuidPropertyValue - which maps to document
* @return
*/
@Override
@Retryable(value = {IOException.class, ConnectException.class}, maxAttemptsExpression = "#{${service.retry.maxAttempts}}",
backoff = @Backoff(delayExpression = "#{${service.retry.backoff.delay}}"))
public Map<String, Object> readEntity(String index, String osid) throws IOException {
logger.debug("readEntity starts with index {} and entityId {}", index, osid);
public Map<String, Object> readEntity(String index, String uuidPropertyValue) throws IOException {
logger.debug("readEntity starts with index {} and entityId {}", index, uuidPropertyValue);

GetResponse response = null;
response = getClient(index).get(new GetRequest(index, searchType, osid), RequestOptions.DEFAULT);
response = getClient(index).get(new GetRequest(index, searchType, uuidPropertyValue), RequestOptions.DEFAULT);
return response.getSourceAsMap();
}

Expand All @@ -230,19 +230,19 @@ public Map<String, Object> readEntity(String index, String osid) throws IOExcept
* Updates the document with updated inputEntity
*
* @param index - ElasticSearch Index
* @param osid - which maps to document
* @param uuidPropertyValue - which maps to document
* @param inputEntity - input json document for updating
* @return
*/
@Override
public RestStatus updateEntity(String index, String osid, JsonNode inputEntity) {
logger.debug("updateEntity starts with index {} and entityId {}", index, osid);
public RestStatus updateEntity(String index, String uuidPropertyValue, JsonNode inputEntity) {
logger.debug("updateEntity starts with index {} and entityId {}", index, uuidPropertyValue);
UpdateResponse response = null;
try {
Map<String, Object> inputMap = JSONUtil.convertJsonNodeToMap(inputEntity);
logger.debug("updateEntity inputMap {}", inputMap);
logger.debug("updateEntity inputEntity {}", inputEntity);
response = getClient(index.toLowerCase()).update(new UpdateRequest(index.toLowerCase(), searchType, osid).doc(inputMap), RequestOptions.DEFAULT);
response = getClient(index.toLowerCase()).update(new UpdateRequest(index.toLowerCase(), searchType, uuidPropertyValue).doc(inputMap), RequestOptions.DEFAULT);
} catch (IOException e) {
logger.error("Exception in updating a record to ElasticSearch: {}", ExceptionUtils.getStackTrace(e));
}
Expand All @@ -253,20 +253,20 @@ public RestStatus updateEntity(String index, String osid, JsonNode inputEntity)
* Updates the document status to inactive into elastic-search
*
* @param index - ElasticSearch Index
* @param osid - which maps to document
* @param uuidPropertyValue - which maps to document
* @return
*/
@Override
public RestStatus deleteEntity(String index, String osid) {
public RestStatus deleteEntity(String index, String uuidPropertyValue) {
DocWriteResponse response = null;
try {
String indexL = index.toLowerCase();
Map<String, Object> readMap = readEntity(indexL, osid);
Map<String, Object> readMap = readEntity(indexL, uuidPropertyValue);
if (isHardDeleteEnabled) {
response = getClient(indexL).delete(new DeleteRequest(indexL, searchType, osid), RequestOptions.DEFAULT);
response = getClient(indexL).delete(new DeleteRequest(indexL, searchType, uuidPropertyValue), RequestOptions.DEFAULT);
} else {
readMap.put(Constants.STATUS_KEYWORD, Constants.STATUS_INACTIVE);
response = getClient(indexL).update(new UpdateRequest(indexL, searchType, osid).doc(readMap), RequestOptions.DEFAULT);
response = getClient(indexL).update(new UpdateRequest(indexL, searchType, uuidPropertyValue).doc(readMap), RequestOptions.DEFAULT);
}
} catch (NullPointerException | IOException e) {
logger.error("exception in deleteEntity {}", ExceptionUtils.getStackTrace(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ public interface IElasticService extends HealthIndicator {
*/
RestStatus addEntity(String index, String id, JsonNode inputEntity);

/** Reads document with respect to input osid from ES
/** Reads document with respect to input uuidPropertyValue from ES
* @param index - ElasticSearch Index
* @param osid - which maps to document
* @param uuidPropertyValue - which maps to document
* @return
*/
Map<String, Object> readEntity(String index, String osid) throws IOException;
Map<String, Object> readEntity(String index, String uuidPropertyValue) throws IOException;

/** updates document with respect to input osid to ES
/** updates document with respect to input uuidPropertyValue to ES
* @param index - ElasticSearch Index
* @param inputEntity - input json document for updating
* @param osid - which maps to document
* @param uuidPropertyValue - which maps to document
* @return
*/
RestStatus updateEntity(String index, String osid, JsonNode inputEntity);
RestStatus updateEntity(String index, String uuidPropertyValue, JsonNode inputEntity);

/** deletes document with respect to input osid from ES
/** deletes document with respect to input uuidPropertyValue from ES
* @param index - ElasticSearch Index
* @param osid - which maps to document
* @param uuidPropertyValue - which maps to document
*/
RestStatus deleteEntity(String index, String osid);
RestStatus deleteEntity(String index, String uuidPropertyValue);

/** searches documents from ES based on query
* @param index - ElasticSearch Index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,23 +481,23 @@ public static String readValFromJsonTree(String path, JsonNode input) {
return typeList.get(0);
}

public static String getOSIDFromArrNode(JsonNode resultNode, JsonNode requestBody, List<String> fieldsToRemove) {
public static String getUUIDPropertyValueFromArrNode(String uuidPropertyName, String propertiesUUIDKeyName, JsonNode resultNode, JsonNode requestBody) {
if (resultNode.isArray()) {
ArrayNode arrayNode = (ArrayNode) resultNode;
JsonNode matchingClaim = searchClaimOsIdFromRequestProperties(arrayNode,requestBody);
return matchingClaim!=null?matchingClaim.get("osid").asText():"";
JsonNode matchingClaim = searchClaimUUIDPropertyValueFromRequestProperties(propertiesUUIDKeyName, arrayNode,requestBody);
return matchingClaim != null ? matchingClaim.get(uuidPropertyName).asText() : "";
}
return "";
}

private static JsonNode searchClaimOsIdFromRequestProperties(ArrayNode arrayNode, JsonNode requestBody) {
if (requestBody.get("propertiesOSID") != null) {
Map<String, List<String>> requestBodyProperty = objectMapper.convertValue(requestBody.get("propertiesOSID"), Map.class);
private static JsonNode searchClaimUUIDPropertyValueFromRequestProperties(String propertiesUUIDKeyName, ArrayNode arrayNode, JsonNode requestBody) {
if (requestBody.get(propertiesUUIDKeyName) != null) {
Map<String, List<String>> requestBodyProperty = objectMapper.convertValue(requestBody.get(propertiesUUIDKeyName), Map.class);
Iterator<JsonNode> claimIterator = arrayNode.elements();
while (claimIterator.hasNext()) {
JsonNode claimEntry = claimIterator.next();
if (claimEntry.get("propertiesOSID") != null) {
JsonNode property = claimEntry.get("propertiesOSID");
if (claimEntry.get(propertiesUUIDKeyName) != null) {
JsonNode property = claimEntry.get(propertiesUUIDKeyName);
Map<String, JsonNode> claimEntryProperty = objectMapper.convertValue(property, Map.class);
if (isRequestBodyPropertyPresentInClaim(requestBodyProperty, claimEntryProperty)) {
return claimEntry;
Expand All @@ -518,16 +518,16 @@ private static JsonNode searchClaimOsIdFromRequestProperties(ArrayNode arrayNode

private static boolean isRequestBodyPropertyPresentInClaim(Map<String,List<String>> requestBodyProperty,Map<String,JsonNode> claimEntryProperty){
for(Map.Entry<String,List<String>> entry: requestBodyProperty.entrySet()){
List<String> requestBodyPropertyOSID = entry.getValue();
List<String> requestBodyPropertyUUID = entry.getValue();
String key = entry.getKey();
if(!((List<String>) objectMapper.convertValue(claimEntryProperty.get(key), List.class)).containsAll(requestBodyPropertyOSID)){
if(!((List<String>) objectMapper.convertValue(claimEntryProperty.get(key), List.class)).containsAll(requestBodyPropertyUUID)){
return false;
}
}
return true;
}

public static JsonNode extractPropertyDataFromEntity(JsonNode entityNode, Map<String, String> attestationProperties, Map<String, List<String>> propertiesOSIDMapper) {
public static JsonNode extractPropertyDataFromEntity(String uuidPropertyName, JsonNode entityNode, Map<String, String> attestationProperties, Map<String, List<String>> propertiesUUIDMapper) {
if(attestationProperties == null) {
return JsonNodeFactory.instance.nullNode();
}
Expand All @@ -539,13 +539,13 @@ public static JsonNode extractPropertyDataFromEntity(JsonNode entityNode, Map<St
Object read = documentContext.read(path);
JsonNode readNode = new ObjectMapper().convertValue(read, JsonNode.class);
result.set(key, readNode);
if(readNode.isArray() && propertiesOSIDMapper != null && propertiesOSIDMapper.containsKey(key)) {
List<String> osids = propertiesOSIDMapper.get(key);
if(readNode.isArray() && propertiesUUIDMapper != null && propertiesUUIDMapper.containsKey(key)) {
List<String> uuidPropertyValues = propertiesUUIDMapper.get(key);
ArrayNode arrayNode = (ArrayNode) readNode;
ArrayNode filteredArrNode = JsonNodeFactory.instance.arrayNode();

for(JsonNode node :arrayNode) {
if(node.has("osid") && osids.contains(node.get("osid").asText())) {
if(node.has(uuidPropertyName) && uuidPropertyValues.contains(node.get(uuidPropertyName).asText())) {
filteredArrNode.add(node);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void shouldAbleToFetchPropertiesFromEntity() throws IOException {
put("educationDetails", new ArrayList<>(Arrays.asList("1", "2")));
}
};
JsonNode actualPropertyData = JSONUtil.extractPropertyDataFromEntity(entityNode, attestationProperties, properyOSIDMapper);
JsonNode actualPropertyData = JSONUtil.extractPropertyDataFromEntity("osid", entityNode, attestationProperties, properyOSIDMapper);
assertEquals(expectedPropertyNode, actualPropertyData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onReceive(MessageProtos.Message request) throws Throwable {
PluginResponseMessage pluginResponseMessage = PluginResponseMessageCreator.createPluginResponseMessage(pluginRequestMessage);
if (fileBytes != null) {
pluginResponseMessage.setFiles(Collections.singletonList(PluginFile.builder().file(fileBytes)
.fileName(String.format("%s.pdf", pluginRequestMessage.getAttestationOSID())).build()));
.fileName(String.format("%s.pdf", pluginRequestMessage.getAttestationUUID())).build()));
pluginResponseMessage.setStatus(Action.GRANT_CLAIM.name());
ObjectNode responseNode = objectMapper.createObjectNode();
responseNode.set("status", JsonNodeFactory.instance.textNode("true"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import dev.sunbirdrc.pojos.PluginRequestMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.sunbird.akka.core.BaseActor;
import org.sunbird.akka.core.MessageProtos;

Expand All @@ -28,8 +26,8 @@ public void onReceive(MessageProtos.Message request) throws Throwable {
logger.info("Received request message {} ", pluginRequestMessage);
JsonNode additionalInput = pluginRequestMessage.getAdditionalInputs();
FetchCredentialsDto fetchCredentialsDto = FetchCredentialsDto.builder()
.osid(pluginRequestMessage.getSourceOSID())
.attestationOsid(pluginRequestMessage.getAttestationOSID())
.uuidPropertyValue(pluginRequestMessage.getSourceUUID())
.attestationUuid(pluginRequestMessage.getAttestationUUID())
.uid(additionalInput.get("uid").asText())
.otp(additionalInput.get("otp").asText())
.transactionId(additionalInput.get("transactionId").asText())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.sunbird.akka.core.ActorCache;
import org.sunbird.akka.core.MessageProtos;
import org.sunbird.akka.core.Router;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
Expand All @@ -40,6 +38,9 @@ public class MosipCallbackController {
@Autowired
private ObjectMapper objectMapper;

@Value("${database.uuidPropertyName}")
private String uuidPropertyName;

@RequestMapping(value = "/plugin/mosip/callback", method = RequestMethod.POST)
public ResponseEntity callbackHandler(@RequestHeader Map<String, String> headers, @RequestParam Map queryParams, @RequestBody String request) throws JsonProcessingException {
try {
Expand All @@ -49,16 +50,16 @@ public ResponseEntity callbackHandler(@RequestHeader Map<String, String> headers
byte[] bytes = mosipServices.fetchMosipPdf(headers, request);
if (bytes != null) {

String attestationOSID = requestBody.get("event").get("data").get("attestationOsid").asText();
String attestationUUID = requestBody.get("event").get("data").get("attestationOsid").asText();
PluginResponseMessage pluginResponseMessage = PluginResponseMessage.builder().policyName("attestation-MOSIP")
.sourceEntity("User")
.sourceOSID(requestBody.get("event").get("data").get("osid").asText())
.attestationOSID(attestationOSID)
.sourceUUID(requestBody.get("event").get("data").get(uuidPropertyName).asText())
.attestationUUID(attestationUUID)
.attestorPlugin("did:external:MosipActor")
.additionalData(JsonNodeFactory.instance.nullNode())
.date(new Date())
.validUntil(new Date())
.version("").files(Collections.singletonList(PluginFile.builder().file(bytes).fileName(String.format("%s.pdf",attestationOSID)).build())).build();
.version("").files(Collections.singletonList(PluginFile.builder().file(bytes).fileName(String.format("%s.pdf",attestationUUID)).build())).build();
pluginResponseMessage.setStatus(Action.GRANT_CLAIM.name());
pluginResponseMessage.setResponse(requestBody.get("event").get("data").toString());
LOGGER.info("{}", pluginResponseMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class FetchCredentialsDto {
String otp;
String uid;
String transactionId;
String osid;
String attestationOsid;
String uuidPropertyValue;
String attestationUuid;
}
Loading

0 comments on commit d1a5b26

Please sign in to comment.