Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field to Entity for attestation flow actions #339

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion java/apitest/src/test/java/e2e/registry/registry.feature
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ Feature: Registry api tests
And header viewTemplateId = 'student_view_template.json'
When method get
Then status 200
* match response.contactDetails == { mobile: '#notpresent', email: '#present', osid: '#present' }
* match response.contactDetails == { mobile: '#notpresent', email: '#present', osid: '#present', osAttestationStatus : '#present' }

@envnot=fusionauth
Scenario: Create birth certificate schema, issue credentials then revoke the credential and check for CRUD APIS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package dev.sunbirdrc.registry.middleware.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -45,6 +47,12 @@ public void setOsOwner(JsonNode node, List<String> owners) {
JSONUtil.addField((ObjectNode) node, osOwner.toString(), ListUtils.emptyIfNull(owners));
}
},
osAttestationStatus {
@Override
public void setAttestationStatus (JsonNode node, ObjectNode attestationStatus) {
JSONUtil.addNode((ObjectNode) node, osAttestationStatus.toString(), attestationStatus != null ? attestationStatus : new ObjectMapper().createObjectNode());
}
},
credentials {
private String getCredentialPropertyName(String signatureProvider) {
String signatureProperty = _osSignedData.name();
Expand Down Expand Up @@ -117,6 +125,8 @@ public boolean hasCredential(String signatureProvider, JsonNode node) {

public void setOsOwner(JsonNode node, List<String> owner) {};

public void setAttestationStatus(JsonNode node, ObjectNode attestation_status) {};

public static OSSystemFields getByValue(String value) {
for (final OSSystemFields element : EnumSet.allOf(OSSystemFields.class)) {
if (element.toString().equals(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,25 @@ private void createProperty(String entityName, JsonNode inputJson, JsonNode upda
} catch (MiddlewareHaltException me) {
// try a field node since array validation failed
parentNode.set(propertyName, inputJson);

}
updateAttestationStatusInEntity( parentNode, propertyName, inputJson, States.ATTESTATION_REQUESTED.name());
}

private void updateAttestationStatusInEntity (ObjectNode parentNode, String propertyName, JsonNode inputNode , String status) {

try {
ObjectNode attestationStatus = (ObjectNode) parentNode.get(osAttestationStatus.toString());
logger.debug( "value" + attestationStatus);
if (attestationStatus == null) {
attestationStatus = new ObjectMapper().createObjectNode();
}
attestationStatus.put( propertyName, status);
parentNode.set(osAttestationStatus.toString(), attestationStatus) ;
} catch ( Exception e) {
logger.info(String.format("Exception while updating the entity attesttaino status :%s", e.getMessage()));
}

}

private void updateProperty(JsonNode inputJson, String propertyName, ObjectNode parentNode, JsonNode propertyNode) {
Expand All @@ -540,6 +558,7 @@ private void updateProperty(JsonNode inputJson, String propertyName, ObjectNode
} else {
parentNode.set(propertyName, inputJson);
}
updateAttestationStatusInEntity( parentNode, propertyName, inputJson, States.ATTESTATION_REQUESTED.name());
}

private JsonNode getParentNode(String entityName, JsonNode jsonNode, String parentURIPointer) throws Exception {
Expand Down Expand Up @@ -604,9 +623,11 @@ public void updateState(PluginResponseMessage pluginResponseMessage) throws Exce
ObjectNode metaData = JsonNodeFactory.instance.objectNode();
JsonNode additionalData = pluginResponseMessage.getAdditionalData();
Action action = Action.valueOf(pluginResponseMessage.getStatus());
String osState = new String();
boolean skipSignature = true;
switch (action) {
case GRANT_CLAIM:
osState = States.PUBLISHED.name();
Object credentialTemplate = attestationPolicy.getCredentialTemplate();
// checking size greater than 1, bcz empty template contains uuid property field
if (credentialTemplate != null && !credentialTemplate.toString().isEmpty()) {
Expand All @@ -633,6 +654,7 @@ public void updateState(PluginResponseMessage pluginResponseMessage) throws Exce
skipSignature = false;
break;
case SELF_ATTEST:
osState = States.PUBLISHED.name();
String hashOfTheFile = pluginResponseMessage.getResponse();
metaData.put(
ATTESTED_DATA,
Expand All @@ -641,6 +663,7 @@ public void updateState(PluginResponseMessage pluginResponseMessage) throws Exce
skipSignature = false;
break;
case RAISE_CLAIM:
osState = States.ATTESTATION_REQUESTED.name();
metaData.put(
CLAIM_ID,
additionalData.get(CLAIM_ID).asText("")
Expand All @@ -649,6 +672,15 @@ public void updateState(PluginResponseMessage pluginResponseMessage) throws Exce
String propertyURI = attestationName + "/" + attestationUUID;
uploadAttestedFiles(pluginResponseMessage, metaData);
JsonNode nodeToUpdate = entityStateHelper.manageState(attestationPolicy, root, propertyURI, action, metaData);
if (nodeToUpdate.path(sourceEntity).path(attestationName).isArray() && nodeToUpdate.path(sourceEntity).path(attestationName).size() > 0) {
osState = nodeToUpdate.path(sourceEntity).path(attestationName).get(0).path(_osState.name()).asText();
}
if (nodeToUpdate.path(sourceEntity).has(osAttestationStatus.toString())) {
JsonNode osAttestationStatusNode = nodeToUpdate.path(sourceEntity).path(osAttestationStatus.toString());
if (osAttestationStatusNode.isObject()) {
((ObjectNode) nodeToUpdate.path(sourceEntity).path(osAttestationStatus.toString())).put(attestationName, osState.toString());
}
}
updateEntity(nodeToUpdate, userId, true);
triggerNextFLowIfExists(pluginResponseMessage, sourceEntity, attestationPolicy, action, nodeToUpdate, userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dev.sunbirdrc.pojos.attestation.States;
import dev.sunbirdrc.registry.config.GenericConfiguration;
import dev.sunbirdrc.registry.dao.*;
import dev.sunbirdrc.registry.entities.AttestationPolicy;
import dev.sunbirdrc.registry.entities.SchemaStatus;
import dev.sunbirdrc.registry.exception.CustomException;
import dev.sunbirdrc.registry.exception.RecordNotFoundException;
Expand Down Expand Up @@ -227,6 +228,9 @@ public String addEntity(Shard shard, String userId, JsonNode rootNode, boolean s

systemFieldsHelper.ensureCreateAuditFields(vertexLabel, rootNode.get(vertexLabel), userId);

List<AttestationPolicy> attestationPolicies= definitionsManager.getAttestationPolicies(vertexLabel);
generateAttestationStatus(vertexLabel, rootNode.get(vertexLabel), userId, attestationPolicies);

if (!skipSignature) {
generateCredentials(rootNode, null, vertexLabel);
}
Expand Down Expand Up @@ -287,7 +291,13 @@ public String addEntity(Shard shard, String userId, JsonNode rootNode, boolean s
return entityId;
}


private void generateAttestationStatus( String title, JsonNode node, String userId, List<AttestationPolicy> attestationPolicies ) {
ObjectNode attestationStatus = new ObjectMapper().createObjectNode();
for (AttestationPolicy policy : attestationPolicies) {
attestationStatus.put(policy.getName(), "" );
}
OSSystemFields.osAttestationStatus.setAttestationStatus(node, attestationStatus);
};
private void generateCredentials(JsonNode rootNode, JsonNode inputNode, String vertexLabel) throws SignatureException.UnreachableException, SignatureException.CreationException {
Object credentialTemplate = definitionsManager.getCredentialTemplate(vertexLabel);
if (signatureEnabled && credentialTemplate != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.registry.entities.AttestationPolicy;
import dev.sunbirdrc.registry.middleware.util.Constants;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
Expand Down Expand Up @@ -130,6 +131,15 @@ public List<OwnershipsAttributes> getOwnershipAttributes(String entity) {
}
}

public List<AttestationPolicy> getAttestationPolicies (String entity) {
Definition entityDefinition = definitionMap.get(entity);
if (entityDefinition != null) {
return entityDefinition.getOsSchemaConfiguration().getAttestationPolicies();
} else {
return Collections.emptyList();
}
}

public boolean isValidEntityName(String entityName) {
return definitionMap.containsKey(entityName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.registry.entities.AttestationPolicy;
import dev.sunbirdrc.registry.middleware.util.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -145,6 +146,24 @@ public List<OwnershipsAttributes> getOwnershipAttributes(String entity) {
}
}

@Override
public List<AttestationPolicy> getAttestationPolicies(String entity) {
try(Jedis jedis = jedisPool.getResource()) {
String value = jedis.get(SCHEMA + entity);
if(value != null) {
JsonNode schemaJson = objectMapper.readTree(value);
Definition definition = new Definition(schemaJson);
return definition.getOsSchemaConfiguration().getAttestationPolicies();
}
return Collections.emptyList();
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (NullPointerException e) {
return Collections.emptyList();
}
}


@Override
public boolean isValidEntityName(String entityName) {
try(Jedis jedis = jedisPool.getResource()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import dev.sunbirdrc.pojos.OwnershipsAttributes;
import dev.sunbirdrc.pojos.UniqueIdentifierField;
import dev.sunbirdrc.registry.entities.AttestationPolicy;

import java.util.*;

Expand Down Expand Up @@ -79,6 +80,7 @@ default Set<String> getExcludingFieldsForEntity(String entity) {
return excludeFields;
}
List<OwnershipsAttributes> getOwnershipAttributes(String entity);
List<AttestationPolicy> getAttestationPolicies(String entity);
default Object getCredentialTemplate(String entityName) {
return getDefinition(entityName).getOsSchemaConfiguration().getCredentialTemplate();
}
Expand Down
Loading