Skip to content

Commit

Permalink
fixed errors for audit, notification, encryption, swagger, Api Messag…
Browse files Browse the repository at this point in the history
…e, keycloak
  • Loading branch information
holashchand committed Sep 5, 2023
1 parent 16799e5 commit fa87187
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 34 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ services:
- connectionInfo_uri=jdbc:postgresql://db:5432/registry
- connectionInfo_username=postgres
- connectionInfo_password=postgres
- encryption_enabled=${ENCRYPTION_ENABLED-false}
- event_enabled=${EVENT_ENABLED-false}
- event_topic=events
- event_providerName=dev.sunbirdrc.registry.service.impl.KafkaEventService
Expand Down Expand Up @@ -214,7 +215,7 @@ services:
zookeeper:
condition: service_started
ports:
- "9092:9092"
- "9094:9094"
environment:
KAFKA_BROKER_ID: "1"
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.stereotype.Component;

import java.util.*;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;

import static dev.sunbirdrc.registry.middleware.util.Constants.CONNECTION_FAILURE;
Expand Down Expand Up @@ -130,6 +131,9 @@ private String createOrUpdateRealmGroup(String entityName) {
private RoleRepresentation createOrGetRealmRole(String entityName) {
RolesResource rolesResource = keycloak.realm(realm).roles();
try {
return rolesResource.get(entityName).toRepresentation();
} catch (NotFoundException ex) {
logger.error("Role {} not found. Creating role {}", entityName, entityName);
RoleRepresentation roleRepresentation = new RoleRepresentation();
roleRepresentation.setName(entityName);
rolesResource.create(roleRepresentation);
Expand Down
12 changes: 7 additions & 5 deletions java/pojos/src/main/java/dev/sunbirdrc/pojos/APIMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public APIMessage(HttpServletRequest servletRequest) {
request = new Request();
requestWrapper = new RequestWrapper(servletRequest);
String body = requestWrapper.getBody();
try {
request = new ObjectMapper().readValue(body, Request.class);
} catch (IOException jpe) {
logger.error("Can't read request body", jpe);
request = null;
if(body != null && !body.isEmpty()) {
try {
request = new ObjectMapper().readValue(body, Request.class);
} catch (IOException jpe) {
logger.error("Can't read request body", jpe);
request = null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

import static dev.sunbirdrc.registry.Constants.ATTESTATION_POLICY;

@Configuration
@EnableRetry
Expand Down Expand Up @@ -427,11 +431,14 @@ public IElasticService elasticService() throws IOException {
elasticService.setUserName(username);
elasticService.setPassword(password);
elasticService.setScheme(scheme);
elasticService.init(iDefinitionsManager.getAllKnownDefinitions());
Set<String> indices = new HashSet<>(iDefinitionsManager.getAllKnownDefinitions());
indices.add(ATTESTATION_POLICY);
elasticService.init(indices);
}
return elasticService;
}

@ConditionalOnProperty(name = "notification.service.enabled", havingValue = "true")
@Bean
public NotificationService notificationService() {
return new NotificationService(notificationServiceConnInfo, notificationServiceHealthUrl, notificationServiceEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import dev.sunbirdrc.registry.helper.RegistryHelper;
import dev.sunbirdrc.registry.middleware.util.JSONUtil;
import dev.sunbirdrc.registry.util.IDefinitionsManager;
import dev.sunbirdrc.registry.util.RefResolver;
import io.swagger.models.*;
Expand All @@ -16,6 +17,7 @@
import io.swagger.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -29,12 +31,19 @@
import static dev.sunbirdrc.registry.Constants.TITLE;

@RestController
@ConditionalOnProperty(name = "api-swagger.enabled", havingValue = "true")
public class RegistrySwaggerController {
private final IDefinitionsManager definitionsManager;
private final RefResolver refResolver;
private final ObjectMapper objectMapper;
@Value("${registry.schema.url}")
private String schemaUrl;
@Value("${api-swagger.title}")
private String swaggerTitle;
@Value("${api-swagger.description}")
private String swaggerDescription;
@Value("${api-swagger.version}")
private String swaggerVersion;

private RegistryHelper registryHelper;

Expand Down Expand Up @@ -81,6 +90,7 @@ private ObjectNode generateAPIMethods(Set<String> entities, HttpServletRequest r
doc.set("definitions", definitions);
doc.set("host", getHost(request));
doc.set("schemes", JsonNodeFactory.instance.arrayNode().add(request.getScheme()));
doc.set("info", getApiInfo());
for (String entityName : entities) {
if (Character.isUpperCase(entityName.charAt(0))) {
populateEntityActions(paths, entityName);
Expand Down Expand Up @@ -261,4 +271,12 @@ private void addResponseType(ObjectNode path, Operation operation, String operat
}
});
}

private JsonNode getApiInfo() throws IOException {
Info info = new Info()
.title(swaggerTitle)
.version(swaggerVersion)
.description(swaggerDescription);
return JSONUtil.convertObjectJsonNode(info);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ public JsonNode readInternal(Vertex rootVertex) throws Exception {

printUuidNodeMap();

logger.info("Finished loading information. Start creating the response");
logger.debug("Finished loading information. Start creating the response");

ObjectNode entityNode = JsonNodeFactory.instance.objectNode();
// For the entity Node, now go and replace the array values with actual
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class RegistryHelper {
@Value("${notification.service.enabled}") boolean notificationEnabled;
@Value("${invite.required_validation_enabled}") boolean skipRequiredValidationForInvite = true;
@Value("${invite.signature_enabled}") boolean skipSignatureForInvite = true;
@Autowired
@Autowired(required = false)
private NotificationHelper notificationHelper;
@Autowired
private ShardManager shardManager;
Expand Down Expand Up @@ -128,7 +128,9 @@ public class RegistryHelper {
@Autowired
private DBConnectionInfoMgr dbConnectionInfoMgr;

@Autowired
@Value("${encryption.enabled}")
private boolean encryptionEnabled;
@Autowired(required = false)
private DecryptionHelper decryptionHelper;

@Autowired
Expand Down Expand Up @@ -203,13 +205,13 @@ public JsonNode removeFormatAttr(JsonNode requestBody) {
*/
public String addEntity(JsonNode inputJson, String userId) throws Exception {
String entityId = addEntityHandler(inputJson, userId, false, false);
notificationHelper.sendNotification(inputJson, CREATE);
if(notificationEnabled) notificationHelper.sendNotification(inputJson, CREATE);
return entityId;
}

public String inviteEntity(JsonNode inputJson, String userId) throws Exception {
String entityId = addEntityHandler(inputJson, userId, skipRequiredValidationForInvite, skipSignatureForInvite);
notificationHelper.sendNotification(inputJson, INVITE);
if(notificationEnabled) notificationHelper.sendNotification(inputJson, INVITE);
return entityId;
}

Expand Down Expand Up @@ -298,6 +300,9 @@ public JsonNode readEntity(String userId, String entityType, String label, boole
if (viewTemplate != null) {
ViewTransformer vTransformer = new ViewTransformer();
if (viewTemplateDecryptPrivateFields) {
if (!encryptionEnabled) {
throw new UnreachableException("Encryption should be enabled to decrypt private fields");
}
resultNode = includePrivateFields ? decryptionHelper.getDecryptedJson(resultNode) : resultNode;
}
resultNode = vTransformer.transform(viewTemplate, resultNode);
Expand Down Expand Up @@ -378,7 +383,7 @@ public String updateProperty(JsonNode inputJson, String userId) throws Exception
RecordIdentifier recordId = RecordIdentifier.parse(label);
logger.info("Update Api: shard id: " + recordId.getShardLabel() + " for uuid: " + recordId.getUuid());
registryService.updateEntity(shard, userId, recordId.getUuid(), jsonString, false);
notificationHelper.sendNotification(inputJson, UPDATE);
if(notificationEnabled) notificationHelper.sendNotification(inputJson, UPDATE);
return "SUCCESS";
}

Expand All @@ -389,7 +394,7 @@ public void updateEntityAndState(JsonNode existingNode, JsonNode updatedNode, St
updatedNode = entityStateHelper.applyWorkflowTransitions(existingNode, updatedNode, attestationPolicies);
}
updateEntity(updatedNode, userId);
notificationHelper.sendNotification(updatedNode, UPDATE);
if(notificationEnabled) notificationHelper.sendNotification(updatedNode, UPDATE);
}

public void addEntityProperty(String entityName, String entityId, JsonNode inputJson, HttpServletRequest request) throws Exception {
Expand Down Expand Up @@ -913,7 +918,7 @@ public String authorizeManageEntity(HttpServletRequest request, String entityNam

List<String> managingRoles = getManageRoles(entityName);
if (managingRoles.size() > 0) {
if (managingRoles.contains(ROLE_ANONYMOUS)) {
if (!securityEnabled || managingRoles.contains(ROLE_ANONYMOUS)) {
return ROLE_ANONYMOUS;
}
Set<String> userRoles = getUserRolesFromRequest(request);
Expand Down Expand Up @@ -1036,7 +1041,7 @@ public Vertex deleteEntity(String entityName, String entityId, String userId) th
Vertex vertex = registryService.deleteEntityById(shard, entityName, userId, recordId.getUuid());
VertexReader vertexReader = new VertexReader(shard.getDatabaseProvider(), vertex.graph(), configurator, uuidPropertyName, definitionsManager);
JsonNode deletedNode = JsonNodeFactory.instance.objectNode().set(entityName, vertexReader.constructObject(vertex));
notificationHelper.sendNotification(deletedNode, DELETE);
if(notificationEnabled) notificationHelper.sendNotification(deletedNode, DELETE);
return vertex;
}

Expand Down Expand Up @@ -1156,7 +1161,7 @@ public void deleteAttestationPolicy(String entityName, AttestationPolicy attesta
}

public boolean doesEntityOperationRequireAuthorization(String entity) {
return !getManageRoles(entity).contains("anonymous") && (doesEntityContainOwnershipAttributes(entity) || getManageRoles(entity).size() > 0);
return securityEnabled && !getManageRoles(entity).contains("anonymous") && (doesEntityContainOwnershipAttributes(entity) || getManageRoles(entity).size() > 0);
}

boolean hasAttestationPropertiesChanged(JsonNode updatedNode, JsonNode existingNode, AttestationPolicy attestationPolicy, String entityName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.fasterxml.jackson.databind.JsonNode;
import dev.sunbirdrc.registry.exception.EncryptionException;
import dev.sunbirdrc.registry.util.PrivateField;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConditionalOnProperty(name = "encryption.enabled", havingValue = "true")
public class DecryptionHelper extends PrivateField {

public JsonNode getDecryptedJson(JsonNode rootNode) throws EncryptionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.fasterxml.jackson.databind.JsonNode;
import dev.sunbirdrc.registry.exception.EncryptionException;
import dev.sunbirdrc.registry.util.PrivateField;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConditionalOnProperty(name = "encryption.enabled", havingValue = "true")
public class EncryptionHelper extends PrivateField {
protected Map<String, Object> performOperation(Map<String, Object> plainMap) throws EncryptionException {
return encryptionService.encrypt(plainMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.io.IOException;
Expand All @@ -26,6 +27,7 @@
import static dev.sunbirdrc.registry.middleware.util.Constants.MOBILE;

@Service
@ConditionalOnProperty(name = "notification.service.enabled", havingValue = "true")
public class NotificationHelper {
private static Logger logger = LoggerFactory.getLogger(NotificationHelper.class);
boolean notificationEnabled;
Expand All @@ -46,6 +48,7 @@ public NotificationHelper() {
}

public void sendNotification(JsonNode inputJson, String operationType) throws Exception {
if (!notificationEnabled) return;
String entityType = inputJson.fields().next().getKey();
List<NotificationTemplate> templates = getNotificationTemplate(entityType, operationType);
Map<String, Object> objectNodeMap = (Map<String, Object>) JSONUtil.convertJsonNodeToMap(inputJson).get(entityType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

Expand All @@ -14,16 +15,14 @@
import dev.sunbirdrc.pojos.AuditRecord;
import dev.sunbirdrc.registry.exception.AuditFailedException;
import dev.sunbirdrc.registry.middleware.util.Constants;
import dev.sunbirdrc.registry.model.DBConnectionInfo;
import dev.sunbirdrc.registry.sink.shard.Shard;
import dev.sunbirdrc.registry.sink.shard.ShardManager;
import dev.sunbirdrc.registry.util.Definition;
import dev.sunbirdrc.registry.util.IDefinitionsManager;

/**
* Audit service implementation for audit layer in the application
*/
@Component
@ConditionalOnExpression("${audit.enabled} and 'database'.equalsIgnoreCase('${audit.frame.store}')")
public class AuditDBImpl extends AuditServiceImpl {

private static Logger logger = LoggerFactory.getLogger(AuditDBImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -16,6 +16,7 @@
* Audit service implementation for audit layer in the application
*/
@Component
@ConditionalOnExpression("${audit.enabled} and 'file'.equalsIgnoreCase('${audit.frame.store}')")
public class AuditFileImpl extends AuditServiceImpl {

private static Logger logger = LoggerFactory.getLogger(AuditFileImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
Expand All @@ -28,6 +29,7 @@
import static dev.sunbirdrc.registry.middleware.util.Constants.SUNBIRD_ENCRYPTION_SERVICE_NAME;

@Component
@ConditionalOnProperty(name = "encryption.enabled", havingValue = "true")
public class EncryptionServiceImpl implements EncryptionService {

private static Logger logger = LoggerFactory.getLogger(EncryptionServiceImpl.class);
Expand All @@ -41,7 +43,7 @@ public class EncryptionServiceImpl implements EncryptionService {
private String encryptionBatchUri;
@Value("${decryption.batch.uri}")
private String decryptionBatchUri;
@Value("${encryption.base}")
@Value("${encryption.healthCheckURL}")
private String encryptionServiceHealthCheckUri;
@Autowired
private RetryRestTemplate retryRestTemplate;
Expand Down
Loading

0 comments on commit fa87187

Please sign in to comment.