From b21195d36d6f4d5f227c0239d248b1811b6b6c0c Mon Sep 17 00:00:00 2001 From: Shilpa M Date: Mon, 28 Feb 2022 17:46:37 +0530 Subject: [PATCH 01/14] Added query in the location service --- .../kernel/masterdata/repository/LocationRepository.java | 4 ++++ .../kernel/masterdata/service/impl/LocationServiceImpl.java | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java index aa99cbd731b..1e3339cc1de 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/repository/LocationRepository.java @@ -145,6 +145,10 @@ List findLocationByHierarchyLevelStartsWith(Short hierarchyLevel, Stri @Query("FROM Location l WHERE l.name=?1 AND l.hierarchyLevel=?2 AND l.langCode=?3 AND NOT code=?4") List findByNameAndLevelLangCodeNotCode(String name,Short hierarchyLevel, String langCode, String code); + @Query("FROM Location l WHERE l.name=?1 AND l.parentLocCode=?5 AND l.hierarchyLevel=?2 AND l.langCode=?3 AND NOT code=?4") + List findByNameParentlocCodeAndLevelLangCodeNotCode(String name,Short hierarchyLevel, String langCode, String code,String parentLocCode); + + /** * give list of the immediate Locations for the given parent location code * diff --git a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java index 64416859674..3341314f5da 100644 --- a/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java +++ b/admin/kernel-masterdata-service/src/main/java/io/mosip/kernel/masterdata/service/impl/LocationServiceImpl.java @@ -313,8 +313,10 @@ public LocationPutResponseDto updateLocationDetails(LocationDto locationDto) { throw new RequestException(LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorCode(), LocationErrorCode.INVALID_HIERARCY_LEVEL.getErrorMessage()); } - List list = locationRepository.findByNameAndLevelLangCodeNotCode(locationDto.getName(), - locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode()); + List list = (null==locationDto.getParentLocCode() || locationDto.getParentLocCode().isEmpty())? locationRepository.findByNameAndLevelLangCodeNotCode(locationDto.getName(), + locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode()):locationRepository.findByNameParentlocCodeAndLevelLangCodeNotCode(locationDto.getName(), + locationDto.getHierarchyLevel(), locationDto.getLangCode(), locationDto.getCode(),locationDto.getParentLocCode()); + if (list != null && !list.isEmpty()) { auditUtil.auditRequest( String.format(MasterDataConstant.FAILURE_CREATE, LocationDto.class.getSimpleName()), From 37579583f7e824702a00bd2b7585743230868d92 Mon Sep 17 00:00:00 2001 From: Shilpa M Date: Mon, 28 Feb 2022 17:53:53 +0530 Subject: [PATCH 02/14] test case changes --- .../test/integration/LocationControllerIntegrationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java index ec0a59c4581..654c01dc9ed 100644 --- a/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java +++ b/admin/kernel-masterdata-service/src/test/java/io/mosip/kernel/masterdata/test/integration/LocationControllerIntegrationTest.java @@ -245,6 +245,7 @@ public void updateLocationAlreadyExistsUnderHeirarchyExceptionTest() throws Exce String requestJson = mapper.writeValueAsString(request); when(repo.findLocationHierarchyByCodeAndLanguageCode(Mockito.any(), Mockito.any())) .thenReturn(Arrays.asList(location1)); + when(repo.findByNameParentlocCodeAndLevelLangCodeNotCode(Mockito.any(),Mockito.any(),Mockito.any(), Mockito.any(),Mockito.any())).thenReturn(Arrays.asList(location1)); when(repo.findByNameAndLevelLangCodeNotCode(Mockito.any(),Mockito.any(),Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(location1)); mockMvc.perform(put("/locations").contentType(MediaType.APPLICATION_JSON).content(requestJson)) .andExpect(status().isOk()); From abd6f12a59fcda3effcb0ee9704c881448a4d71d Mon Sep 17 00:00:00 2001 From: Keshav Mishra Date: Wed, 20 Apr 2022 13:25:13 +0530 Subject: [PATCH 03/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3041cf387ca..c82f93d176a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ [![Build Status](https://travis-ci.org/mosip/admin-services.svg?branch=1.0.9)](https://travis-ci.org/mosip/admin-services) # mosip-platform -This repository contains the source code of the Modular Open Source Identity Platform. To know more about MOSIP, its architecture, external integrations, releases, etc., please check the [Platform Documentation](https://github.com/mosip/mosip-docs/wiki) +This repository contains the source code of the Modular Open Source Identity Platform. To know more about MOSIP, its architecture, external integrations, releases, etc., please check the [Platform Documentation](https://github.com/mosip/mosip-docs/wiki). ### Introduction MOSIP consists of the following modules - From af6bab87cd2f611ea2b64247aea8ea6f6bfc4a4e Mon Sep 17 00:00:00 2001 From: ase-101 <> Date: Tue, 29 Mar 2022 07:54:22 +0530 Subject: [PATCH 04/14] ClientType check during encrypt and signature validation changes added --- .../dto/RegistrationCenterMachineDto.java | 10 + .../repository/MachineRepository.java | 3 +- .../service/helper/ClientSettingsHelper.java | 267 +++ .../impl/SyncAuthTokenServiceImpl.java | 73 +- .../impl/SyncMasterDataServiceImpl.java | 132 +- .../utils/SyncMasterDataServiceHelper.java | 2044 +++++++---------- .../SyncClientSettingsIntegrationTest.java | 1083 +++------ .../SyncUserDetailsAndRolesServiceTest.java | 66 +- .../test/service/syncAuthServiceTest.java | 60 +- 9 files changed, 1699 insertions(+), 2039 deletions(-) create mode 100644 admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/helper/ClientSettingsHelper.java diff --git a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/dto/RegistrationCenterMachineDto.java b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/dto/RegistrationCenterMachineDto.java index 713650a948a..ac9c8ea0038 100644 --- a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/dto/RegistrationCenterMachineDto.java +++ b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/dto/RegistrationCenterMachineDto.java @@ -3,6 +3,7 @@ import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; +import io.mosip.kernel.clientcrypto.constant.ClientType; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; @@ -30,4 +31,13 @@ public class RegistrationCenterMachineDto extends BaseDto { @NotNull private String publicKey; + @NotNull + private String machineSpecId; + + @NotNull + private String machineTypeId; + + @NotNull + private ClientType clientType; + } diff --git a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/repository/MachineRepository.java b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/repository/MachineRepository.java index e39731d97e7..009ba21c52a 100644 --- a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/repository/MachineRepository.java +++ b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/repository/MachineRepository.java @@ -100,8 +100,7 @@ List findAllLatestCreatedUpdatedDeleted(String regCenterId, LocalDateTi @Query("From Machine m WHERE lower(m.signKeyIndex) = lower(?1) and (m.isDeleted is null or m.isDeleted =false)") List findBySignKeyIndex(String signKeyIndex); - @Query(value = "select distinct mm.regcntr_id , mm.id, mm.public_key from master.machine_master mm where lower(mm.key_index) = lower(?1)", nativeQuery = true) - List getRegistrationCenterMachineWithKeyIndexWithoutStatusCheck(String keyIndex); + Machine findOneByKeyIndexIgnoreCase(String keyIndex); @Query("From Machine m WHERE lower(m.name) = lower(?1) and (m.isDeleted is null or m.isDeleted =false)") List findByMachineName(String machineName); diff --git a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/helper/ClientSettingsHelper.java b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/helper/ClientSettingsHelper.java new file mode 100644 index 00000000000..a5c39b74f69 --- /dev/null +++ b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/helper/ClientSettingsHelper.java @@ -0,0 +1,267 @@ +package io.mosip.kernel.syncdata.service.helper; + +import io.mosip.kernel.clientcrypto.dto.TpmCryptoRequestDto; +import io.mosip.kernel.clientcrypto.dto.TpmCryptoResponseDto; +import io.mosip.kernel.clientcrypto.service.spi.ClientCryptoManagerService; +import io.mosip.kernel.core.util.CryptoUtil; +import io.mosip.kernel.syncdata.dto.*; +import io.mosip.kernel.syncdata.dto.response.SyncDataBaseDto; +import io.mosip.kernel.syncdata.entity.*; +import io.mosip.kernel.syncdata.repository.ModuleDetailRepository; +import io.mosip.kernel.syncdata.service.helper.beans.RegistrationCenterMachine; +import io.mosip.kernel.syncdata.service.helper.beans.RegistrationCenterUser; +import io.mosip.kernel.syncdata.utils.MapperUtils; +import io.mosip.kernel.syncdata.utils.SyncMasterDataServiceHelper; +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.core.env.Environment; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.util.*; +import java.util.concurrent.CompletableFuture; + +@Component +public class ClientSettingsHelper { + + private final static Logger LOGGER = LoggerFactory.getLogger(ClientSettingsHelper.class); + + @Autowired + private SyncMasterDataServiceHelper serviceHelper; + + @Autowired + private ModuleDetailRepository moduleDetailRepository; + + @Value("${mosip.syncdata.regclient.module.id:10002}") + private String regClientModuleId; + + @Value("#{'${mosip.registration.sync.scripts:applicanttype.mvel}'.split(',')}") + private Set scriptNames; + + @Autowired + private Environment environment; + + @Autowired + private MapperUtils mapper; + + @Autowired + private ClientCryptoManagerService clientCryptoManagerService; + + private boolean hasURLDetails(Class clazz, boolean isV2API, boolean deltaSync) { + if (!isV2API) + return false; + + String entityName = clazz.getSimpleName().toUpperCase(); + if (!environment.containsProperty(String.format("mosip.sync.entity.url.%s", entityName))) + return false; + + Boolean onlyOnFullSync = environment + .getProperty(String.format("mosip.sync.entity.only-on-fullsync.%s", entityName), Boolean.class, true); + + return onlyOnFullSync ? !deltaSync : true; + } + + public Map getInitiateDataFetch(String machineId, String regCenterId, + LocalDateTime lastUpdated, LocalDateTime currentTimestamp, boolean isV2API, boolean deltaSync) { + + Map futuresMap = new HashMap<>(); + futuresMap.put(AppAuthenticationMethod.class, + hasURLDetails(AppAuthenticationMethod.class, isV2API, deltaSync) + ? getURLDetails(AppAuthenticationMethod.class) + : serviceHelper.getAppAuthenticationMethodDetails(lastUpdated, currentTimestamp)); + futuresMap.put(AppRolePriority.class, + hasURLDetails(AppRolePriority.class, isV2API, deltaSync) ? getURLDetails(AppRolePriority.class) + : serviceHelper.getAppRolePriorityDetails(lastUpdated, currentTimestamp)); + + futuresMap.put(Machine.class, serviceHelper.getMachines(regCenterId, lastUpdated, currentTimestamp, machineId)); + futuresMap.put(RegistrationCenter.class, + serviceHelper.getRegistrationCenter(regCenterId, lastUpdated, currentTimestamp)); + + futuresMap.put(Template.class, hasURLDetails(Template.class, isV2API, deltaSync) ? getURLDetails(Template.class) + : serviceHelper.getTemplates(regClientModuleId, lastUpdated, currentTimestamp)); + + futuresMap.put(DocumentType.class, + hasURLDetails(DocumentType.class, isV2API, deltaSync) ? getURLDetails(DocumentType.class) + : serviceHelper.getDocumentTypes(lastUpdated, currentTimestamp)); + futuresMap.put(ApplicantValidDocument.class, + hasURLDetails(ApplicantValidDocument.class, isV2API, deltaSync) + ? getURLDetails(ApplicantValidDocument.class) + : serviceHelper.getApplicantValidDocument(lastUpdated, currentTimestamp)); + + futuresMap.put(Location.class, hasURLDetails(Location.class, isV2API, deltaSync) ? getURLDetails(Location.class) + : serviceHelper.getLocationHierarchy(lastUpdated, currentTimestamp)); + + futuresMap.put(ReasonCategory.class, + hasURLDetails(ReasonCategory.class, isV2API, deltaSync) ? getURLDetails(ReasonCategory.class) + : serviceHelper.getReasonCategory(lastUpdated, currentTimestamp)); + futuresMap.put(ReasonList.class, + hasURLDetails(ReasonList.class, isV2API, deltaSync) ? getURLDetails(ReasonList.class) + : serviceHelper.getReasonList(lastUpdated, currentTimestamp)); + futuresMap.put(Holiday.class, serviceHelper.getHolidays(lastUpdated, machineId, currentTimestamp)); + futuresMap.put(BlocklistedWords.class, + hasURLDetails(BlocklistedWords.class, isV2API, deltaSync) ? getURLDetails(BlocklistedWords.class) + : serviceHelper.getBlackListedWords(lastUpdated, currentTimestamp)); + futuresMap.put(ScreenAuthorization.class, + hasURLDetails(ScreenAuthorization.class, isV2API, deltaSync) ? getURLDetails(ScreenAuthorization.class) + : serviceHelper.getScreenAuthorizationDetails(lastUpdated, currentTimestamp)); + futuresMap.put(ScreenDetail.class, + hasURLDetails(ScreenDetail.class, isV2API, deltaSync) ? getURLDetails(ScreenDetail.class) + : serviceHelper.getScreenDetails(lastUpdated, currentTimestamp)); + futuresMap.put(ProcessList.class, + hasURLDetails(ProcessList.class, isV2API, deltaSync) ? getURLDetails(ProcessList.class) + : serviceHelper.getProcessList(lastUpdated, currentTimestamp)); + futuresMap.put(SyncJobDef.class, + hasURLDetails(SyncJobDef.class, isV2API, deltaSync) ? getURLDetails(SyncJobDef.class) + : serviceHelper.getSyncJobDefDetails(lastUpdated, currentTimestamp)); + futuresMap.put(PermittedLocalConfig.class, + hasURLDetails(PermittedLocalConfig.class, isV2API, deltaSync) + ? getURLDetails(PermittedLocalConfig.class) + : serviceHelper.getPermittedConfig(lastUpdated, currentTimestamp)); + + // to handle backward compatibility + if (!isV2API) { + // template_file_format & template_type + futuresMap.put(TemplateFileFormat.class, + hasURLDetails(TemplateFileFormat.class, isV2API, deltaSync) + ? getURLDetails(TemplateFileFormat.class) + : serviceHelper.getTemplateFileFormats(lastUpdated, currentTimestamp)); + futuresMap.put(TemplateType.class, + hasURLDetails(TemplateType.class, isV2API, deltaSync) ? getURLDetails(TemplateType.class) + : serviceHelper.getTemplateTypes(lastUpdated, currentTimestamp)); + + futuresMap.put(RegistrationCenterMachine.class, + serviceHelper.getRegistrationCenterMachines(regCenterId, lastUpdated, currentTimestamp, machineId)); + futuresMap.put(RegistrationCenterUser.class, + serviceHelper.getRegistrationCenterUsers(regCenterId, lastUpdated, currentTimestamp)); + + // valid_document + futuresMap.put(ValidDocument.class, + hasURLDetails(ValidDocument.class, isV2API, deltaSync) ? getURLDetails(ValidDocument.class) + : serviceHelper.getValidDocuments(lastUpdated, currentTimestamp)); + } + + // invokes master-data-service + futuresMap.put(LocationHierarchy.class, + hasURLDetails(LocationHierarchy.class, isV2API, deltaSync) ? getURLDetails(LocationHierarchy.class) + : serviceHelper.getLocationHierarchyList(lastUpdated)); + futuresMap.put(DynamicFieldDto.class, + hasURLDetails(DynamicFieldDto.class, isV2API, deltaSync) ? getURLDetails(DynamicFieldDto.class) + : serviceHelper.getAllDynamicFields(lastUpdated)); + + return futuresMap; + } + + public List retrieveData(Map futures, RegistrationCenterMachineDto regCenterMachineDto, boolean isV2API) + throws RuntimeException { + final List list = new ArrayList<>(); + futures.entrySet().parallelStream().forEach(entry -> { + try { + Object result = entry.getValue().get(); + if (result != null) { + String entityType = (result instanceof Map) + ? (entry.getKey() == DynamicFieldDto.class ? "dynamic-url" : "structured-url") + : (entry.getKey() == DynamicFieldDto.class ? "dynamic" : "structured"); + + switch (entityType) { + case "structured-url": + case "dynamic-url": + list.add(getEncryptedSyncDataBaseDto(entry.getKey(), regCenterMachineDto, entityType, result)); + break; + case "dynamic": + handleDynamicData((List) result, list, regCenterMachineDto, isV2API); + break; + case "structured": + if (isV2API) + serviceHelper.getSyncDataBaseDtoV2(entry.getKey().getSimpleName(), entityType, + (List) result, regCenterMachineDto, list); + else + serviceHelper.getSyncDataBaseDto(entry.getKey().getSimpleName(), entityType, (List) result, + regCenterMachineDto, list); + break; + } + } + } catch (InterruptedException ie) { + LOGGER.error("InterruptedException: ", ie); + Thread.currentThread().interrupt(); + } catch (Throwable e) { + LOGGER.error("Failed to construct client settings response", e); + throw new RuntimeException(e); + } + }); + return list; + } + + private void handleDynamicData(List entities, List list, RegistrationCenterMachineDto registrationCenterMachineDto, boolean isV2) { + Map> data = new HashMap>(); + entities.forEach(dto -> { + if (!data.containsKey(((DynamicFieldDto) dto).getName())) { + List langBasedData = new ArrayList(); + langBasedData.add(((DynamicFieldDto) dto)); + data.put(((DynamicFieldDto) dto).getName(), langBasedData); + } else + data.get(((DynamicFieldDto) dto).getName()).add(((DynamicFieldDto) dto)); + }); + + for (String key : data.keySet()) { + if (isV2) + serviceHelper.getSyncDataBaseDtoV2(key, "dynamic", data.get(key), registrationCenterMachineDto, list); + else + serviceHelper.getSyncDataBaseDto(key, "dynamic", data.get(key), registrationCenterMachineDto, list); + } + } + + private SyncDataBaseDto getEncryptedSyncDataBaseDto(Class clazz, RegistrationCenterMachineDto registrationCenterMachineDto, String entityType, + Object urlDetails) { + try { + TpmCryptoRequestDto tpmCryptoRequestDto = new TpmCryptoRequestDto(); + tpmCryptoRequestDto + .setValue(CryptoUtil.encodeToURLSafeBase64(mapper.getObjectAsJsonString(urlDetails).getBytes())); + tpmCryptoRequestDto.setPublicKey(registrationCenterMachineDto.getPublicKey()); + tpmCryptoRequestDto.setClientType(registrationCenterMachineDto.getClientType()); + TpmCryptoResponseDto tpmCryptoResponseDto = clientCryptoManagerService.csEncrypt(tpmCryptoRequestDto); + return new SyncDataBaseDto(clazz.getSimpleName(), entityType, tpmCryptoResponseDto.getValue()); + } catch (Exception e) { + LOGGER.error("Failed to encrypt urlDetails for {} data to json", clazz.getSimpleName(), e); + } + return null; + } + + public List getConfiguredScriptUrlDetail(String publicKey) { + List list = new ArrayList<>(); + scriptNames.forEach(fileName -> { + Map urlDetail = buildUrlDetailMap(fileName); + try { + TpmCryptoRequestDto tpmCryptoRequestDto = new TpmCryptoRequestDto(); + tpmCryptoRequestDto + .setValue(CryptoUtil.encodeToURLSafeBase64(mapper.getObjectAsJsonString(urlDetail).getBytes())); + tpmCryptoRequestDto.setPublicKey(publicKey); + TpmCryptoResponseDto tpmCryptoResponseDto = clientCryptoManagerService.csEncrypt(tpmCryptoRequestDto); + list.add(new SyncDataBaseDto(fileName, "script", tpmCryptoResponseDto.getValue())); + } catch (Exception e) { + LOGGER.error("Failed to create script url detail {} data to json", fileName, e); + } + }); + return list; + } + + private CompletableFuture> getURLDetails(Class clazz) { + Map urlDetails = buildUrlDetailMap(clazz.getSimpleName()); + return CompletableFuture.completedFuture(urlDetails); + } + + private Map buildUrlDetailMap(String name) { + Map urlDetail = new HashMap<>(); + urlDetail.put("url", environment.getProperty(String.format("mosip.sync.entity.url.%s", name.toUpperCase()))); + urlDetail.put("auth-required", + environment.getProperty(String.format("mosip.sync.entity.auth-required.%s", name.toUpperCase()))); + urlDetail.put("auth-token", + environment.getProperty(String.format("mosip.sync.entity.auth-token.%s", name.toUpperCase()))); + urlDetail.put("encrypted", + environment.getProperty(String.format("mosip.sync.entity.encrypted.%s", name.toUpperCase()))); + urlDetail.put("headers", + environment.getProperty(String.format("mosip.sync.entity.headers.%s", name.toUpperCase()))); + return urlDetail; + } +} diff --git a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncAuthTokenServiceImpl.java b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncAuthTokenServiceImpl.java index 723c9397d94..ef47c2e0b1a 100644 --- a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncAuthTokenServiceImpl.java +++ b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncAuthTokenServiceImpl.java @@ -7,7 +7,6 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import io.mosip.kernel.clientcrypto.exception.ClientCryptoException; import io.mosip.kernel.clientcrypto.service.impl.ClientCryptoFacade; @@ -15,18 +14,17 @@ import io.mosip.kernel.core.http.RequestWrapper; import io.mosip.kernel.core.http.ResponseWrapper; import io.mosip.kernel.core.util.CryptoUtil; -import io.mosip.kernel.core.util.DateUtils; +import io.mosip.kernel.cryptomanager.util.CryptomanagerUtils; import io.mosip.kernel.syncdata.constant.MasterDataErrorCode; import io.mosip.kernel.syncdata.constant.SyncAuthErrorCode; -import io.mosip.kernel.syncdata.dto.AuthLoginUser; -import io.mosip.kernel.syncdata.dto.IdSchemaDto; import io.mosip.kernel.syncdata.dto.MachineAuthDto; import io.mosip.kernel.syncdata.dto.MachineOtpDto; import io.mosip.kernel.syncdata.dto.response.TokenResponseDto; +import io.mosip.kernel.syncdata.dto.response.UserDetailResponseDto; import io.mosip.kernel.syncdata.entity.Machine; import io.mosip.kernel.syncdata.exception.RequestException; import io.mosip.kernel.syncdata.repository.MachineRepository; -import net.minidev.json.JSONObject; +import io.mosip.kernel.syncdata.service.SyncUserDetailsService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -39,21 +37,17 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; +import javax.annotation.PostConstruct; import java.io.IOException; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; -import java.time.temporal.TemporalUnit; -import java.util.Base64; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.concurrent.TimeUnit; +import java.util.*; /** - * @since 1.1.5.2 + * @since 1.2.0-SNAPSHOT */ @RefreshScope @Service @@ -100,13 +94,20 @@ public class SyncAuthTokenServiceImpl { @Autowired private MachineRepository machineRepository; + @Autowired + private SyncUserDetailsService syncUserDetailsService; + @Autowired private RestTemplate restTemplate; - private static ObjectMapper objectMapper = new ObjectMapper(); + @Autowired + private CryptomanagerUtils cryptomanagerUtils; + + @Autowired + private ObjectMapper objectMapper; - static { - objectMapper.registerModule(new JavaTimeModule()); + @PostConstruct + public void init() { objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } @@ -114,9 +115,9 @@ public class SyncAuthTokenServiceImpl { public String getAuthToken(String requestData) { String[] parts = requestData.split("\\."); if(parts.length == 3) { - byte[] header = Base64.getUrlDecoder().decode(parts[0]); - byte[] payload = Base64.getUrlDecoder().decode(parts[1]); - byte[] signature = Base64.getUrlDecoder().decode(parts[2]); + byte[] header = CryptoUtil.decodeURLSafeBase64(parts[0]); + byte[] payload = CryptoUtil.decodeURLSafeBase64(parts[1]); + byte[] signature = CryptoUtil.decodeURLSafeBase64(parts[2]); Machine machine = validateRequestData(header, payload, signature); try { @@ -124,7 +125,10 @@ public String getAuthToken(String requestData) { validateRequestTimestamp(machineAuthDto.getTimestamp()); ResponseWrapper responseWrapper = getTokenResponseDTO(machineAuthDto); String token = objectMapper.writeValueAsString(responseWrapper.getResponse()); - byte[] cipher = clientCryptoFacade.encrypt(CryptoUtil.decodeBase64(machine.getPublicKey()), + + byte[] cipher = clientCryptoFacade.encrypt( + SyncMasterDataServiceHelper.getClientType(machine), + cryptomanagerUtils.decodeBase64Data(machine.getPublicKey()), token.getBytes()); return CryptoUtil.encodeBase64(cipher); @@ -136,12 +140,12 @@ public String getAuthToken(String requestData) { SyncAuthErrorCode.INVALID_REQUEST.getErrorMessage()); } - public AuthNResponse sendOTP(String requestData) { + public ResponseWrapper sendOTP(String requestData) { String[] parts = requestData.split("\\."); if(parts.length == 3) { - byte[] header = Base64.getUrlDecoder().decode(parts[0]); - byte[] payload = Base64.getUrlDecoder().decode(parts[1]); - byte[] signature = Base64.getUrlDecoder().decode(parts[2]); + byte[] header = CryptoUtil.decodeURLSafeBase64(parts[0]); + byte[] payload = CryptoUtil.decodeURLSafeBase64(parts[1]); + byte[] signature = CryptoUtil.decodeURLSafeBase64(parts[2]); validateRequestData(header, payload, signature); try { @@ -156,7 +160,8 @@ public AuthNResponse sendOTP(String requestData) { ResponseEntity responseEntity = restTemplate.postForEntity(builder.build().toUri(), new HttpEntity<>(requestWrapper), String.class); - return objectMapper.readValue(responseEntity.getBody(), new TypeReference() {}); + return objectMapper.readValue(responseEntity.getBody(), + new TypeReference>() {}); } catch (Exception ex) { logger.error("Failed to send otp", ex); } @@ -167,7 +172,17 @@ public AuthNResponse sendOTP(String requestData) { private OtpUser getOtpUser(MachineOtpDto machineOtpDto) { OtpUser otpUser = new OtpUser(); - otpUser.setUserId(machineOtpDto.getUserId()); + + UserDetailResponseDto userDetailResponseDto = syncUserDetailsService.getUserDetailsFromAuthServer(Arrays.asList(machineOtpDto.getUserId())); + if(userDetailResponseDto != null && userDetailResponseDto.getMosipUserDtoList() != null && + !userDetailResponseDto.getMosipUserDtoList().isEmpty()) { + otpUser.setUserId(machineOtpDto.getOtpChannel().contains("email") ? + userDetailResponseDto.getMosipUserDtoList().get(0).getMail() : + userDetailResponseDto.getMosipUserDtoList().get(0).getMobile()); + } + else + otpUser.setUserId(machineOtpDto.getUserId()); + otpUser.setOtpChannel(machineOtpDto.getOtpChannel()); otpUser.setAppId(machineOtpDto.getAppId()); otpUser.setUseridtype(machineOtpDto.getUseridtype()); @@ -190,7 +205,9 @@ private Machine validateRequestData(byte[] header, byte[] payload, byte[] signat try { logger.info("validateRequestData for machine : {} with status : {}", machines.get(0).getId(), machines.get(0).getIsActive()); - boolean verified = clientCryptoFacade.validateSignature(CryptoUtil.decodeBase64(machines.get(0).getSignPublicKey()), + boolean verified = clientCryptoFacade.validateSignature( + SyncMasterDataServiceHelper.getClientType(machines.get(0)), + cryptomanagerUtils.decodeBase64Data(machines.get(0).getSignPublicKey()), signature, payload); logger.info("validateRequestData verified : {}", verified); if(verified) { return machines.get(0); } @@ -253,7 +270,11 @@ private ResponseWrapper getTokenResponseDTO(MachineAuthDto mac responseEntity = restTemplate.postForEntity(refreshRequestBuilder.build().toUri(), httpEntity, String.class); break; } + if (null == responseEntity) { + throw new RequestException(SyncAuthErrorCode.ERROR_GETTING_TOKEN.getErrorCode(), + SyncAuthErrorCode.ERROR_GETTING_TOKEN.getErrorMessage()); + } ResponseWrapper responseWrapper = objectMapper.readValue(responseEntity.getBody(), new TypeReference>() {}); diff --git a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncMasterDataServiceImpl.java b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncMasterDataServiceImpl.java index 65d7667f479..987abb90c7f 100644 --- a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncMasterDataServiceImpl.java +++ b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/service/impl/SyncMasterDataServiceImpl.java @@ -1,53 +1,49 @@ package io.mosip.kernel.syncdata.service.impl; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; import java.time.LocalDateTime; import java.time.ZoneOffset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; -import java.util.concurrent.ExecutionException; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import io.mosip.kernel.clientcrypto.dto.TpmCryptoRequestDto; +import io.mosip.kernel.clientcrypto.dto.TpmCryptoResponseDto; +import io.mosip.kernel.clientcrypto.service.spi.ClientCryptoManagerService; import io.mosip.kernel.core.exception.ExceptionUtils; +import io.mosip.kernel.core.exception.FileNotFoundException; import io.mosip.kernel.core.http.ResponseWrapper; +import io.mosip.kernel.core.util.FileUtils; +import io.mosip.kernel.core.util.HMACUtils2; +import io.mosip.kernel.cryptomanager.util.CryptomanagerUtils; import io.mosip.kernel.keymanagerservice.entity.CACertificateStore; import io.mosip.kernel.keymanagerservice.repository.CACertificateStoreRepository; +import io.mosip.kernel.syncdata.constant.SyncConfigDetailsErrorCode; import io.mosip.kernel.syncdata.dto.*; import io.mosip.kernel.syncdata.dto.response.*; -import io.mosip.kernel.syncdata.entity.AppDetail; import io.mosip.kernel.syncdata.exception.*; -import io.mosip.kernel.syncdata.repository.AppDetailRepository; -import io.mosip.kernel.syncdata.service.helper.KeymanagerHelper; +import io.mosip.kernel.syncdata.repository.ModuleDetailRepository; +import io.mosip.kernel.syncdata.service.helper.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.dao.DataAccessException; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import io.mosip.kernel.core.dataaccess.exception.DataAccessLayerException; import io.mosip.kernel.core.util.CryptoUtil; import io.mosip.kernel.syncdata.constant.MasterDataErrorCode; import io.mosip.kernel.syncdata.entity.Machine; import io.mosip.kernel.syncdata.repository.MachineRepository; import io.mosip.kernel.syncdata.service.SyncMasterDataService; -import io.mosip.kernel.syncdata.service.helper.ApplicationDataHelper; -import io.mosip.kernel.syncdata.service.helper.DeviceDataHelper; -import io.mosip.kernel.syncdata.service.helper.DocumentDataHelper; -import io.mosip.kernel.syncdata.service.helper.HistoryDataHelper; -import io.mosip.kernel.syncdata.service.helper.IdentitySchemaHelper; -import io.mosip.kernel.syncdata.service.helper.IndividualDataHelper; -import io.mosip.kernel.syncdata.service.helper.MachineDataHelper; -import io.mosip.kernel.syncdata.service.helper.MiscellaneousDataHelper; -import io.mosip.kernel.syncdata.service.helper.RegistrationCenterDataHelper; -import io.mosip.kernel.syncdata.service.helper.TemplateDataHelper; import io.mosip.kernel.syncdata.utils.MapperUtils; import io.mosip.kernel.syncdata.utils.SyncMasterDataServiceHelper; import org.springframework.web.client.RestTemplate; @@ -65,7 +61,7 @@ @Service public class SyncMasterDataServiceImpl implements SyncMasterDataService { - private Logger logger = LoggerFactory.getLogger(SyncMasterDataServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(SyncMasterDataServiceImpl.class); @Autowired private SyncMasterDataServiceHelper serviceHelper; @@ -95,89 +91,57 @@ public class SyncMasterDataServiceImpl implements SyncMasterDataService { private CACertificateStoreRepository caCertificateStoreRepository; @Autowired - private AppDetailRepository appDetailRepository; + private ModuleDetailRepository moduleDetailRepository; - @Value("${mosip.syncdata.regclient.module.id:10002}") - private String regClientModuleId; + @Autowired + private ClientSettingsHelper clientSettingsHelper; + + @Value("${mosip.syncdata.clientsettings.data.dir:./_SNAPSHOTS}") + private String clientSettingsDir; + + @Autowired + private Environment environment; + + @Autowired + private ClientCryptoManagerService clientCryptoManagerService; + + @Autowired + private CryptomanagerUtils cryptomanagerUtils; @Override public SyncDataResponseDto syncClientSettings(String regCenterId, String keyIndex, - LocalDateTime lastUpdated, LocalDateTime currentTimestamp) - throws InterruptedException, ExecutionException { - + LocalDateTime lastUpdated, LocalDateTime currentTimestamp) { logger.info("syncClientSettings invoked for timespan from {} to {}", lastUpdated, currentTimestamp); - + SyncDataResponseDto response = new SyncDataResponseDto(); RegistrationCenterMachineDto regCenterMachineDto = serviceHelper.getRegistrationCenterMachine(regCenterId, keyIndex); - String machineId = regCenterMachineDto.getMachineId(); String registrationCenterId = regCenterMachineDto.getRegCenterId(); - SyncDataResponseDto response = new SyncDataResponseDto(); - - List futures = new ArrayList(); - - ApplicationDataHelper applicationDataHelper = new ApplicationDataHelper(lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - applicationDataHelper.retrieveData(serviceHelper, futures); - - MachineDataHelper machineDataHelper = new MachineDataHelper(registrationCenterId, machineId, - lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - machineDataHelper.retrieveData(serviceHelper, futures); - - DeviceDataHelper deviceDataHelper = new DeviceDataHelper(registrationCenterId, lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - deviceDataHelper.retrieveData(serviceHelper, futures); + Map futureMap = clientSettingsHelper.getInitiateDataFetch(machineId, registrationCenterId, + lastUpdated, currentTimestamp, false, lastUpdated!=null); - IndividualDataHelper individualDataHelper = new IndividualDataHelper(lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - individualDataHelper.retrieveData(serviceHelper, futures); - - RegistrationCenterDataHelper RegistrationCenterDataHelper = new RegistrationCenterDataHelper(registrationCenterId, machineId, - lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - RegistrationCenterDataHelper.retrieveData(serviceHelper, futures); - - TemplateDataHelper templateDataHelper = new TemplateDataHelper(lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey(), - regClientModuleId); - templateDataHelper.retrieveData(serviceHelper, futures); - - DocumentDataHelper documentDataHelper = new DocumentDataHelper(lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - documentDataHelper.retrieveData(serviceHelper, futures); - - MiscellaneousDataHelper miscellaneousDataHelper = new MiscellaneousDataHelper(machineId, lastUpdated, currentTimestamp, regCenterMachineDto.getPublicKey()); - miscellaneousDataHelper.retrieveData(serviceHelper, futures); - - CompletableFuture array [] = new CompletableFuture[futures.size()]; - CompletableFuture future = CompletableFuture.allOf(futures.toArray(array)); + CompletableFuture[] array = new CompletableFuture[futureMap.size()]; + CompletableFuture future = CompletableFuture.allOf(futureMap.values().toArray(array)); try { future.join(); } catch (CompletionException e) { + logger.error("Failed to fetch data", e); if (e.getCause() instanceof SyncDataServiceException) { throw (SyncDataServiceException) e.getCause(); } else { throw (RuntimeException) e.getCause(); } } - - List list = new ArrayList(); - applicationDataHelper.fillRetrievedData(serviceHelper, list); - machineDataHelper.fillRetrievedData(serviceHelper, list); - deviceDataHelper.fillRetrievedData(serviceHelper, list); - individualDataHelper.fillRetrievedData(serviceHelper, list); - RegistrationCenterDataHelper.fillRetrievedData(serviceHelper, list); - templateDataHelper.fillRetrievedData(serviceHelper, list); - documentDataHelper.fillRetrievedData(serviceHelper, list); - //historyDataHelper.fillRetrievedData(serviceHelper, list); - miscellaneousDataHelper.fillRetrievedData(serviceHelper, list); - - //Fills dynamic field data - identitySchemaHelper.fillRetrievedData(list, regCenterMachineDto.getPublicKey(), lastUpdated); - - response.setDataToSync(list); + + response.setDataToSync(clientSettingsHelper.retrieveData(futureMap, regCenterMachineDto, false)); return response; } @Override public UploadPublicKeyResponseDto validateKeyMachineMapping(UploadPublicKeyRequestDto dto) { - List machines = machineRepo.findByMachineNameAndIsActive(dto.getMachineName()); + List machines = machineRepo.findByMachineName(dto.getMachineName()); if(machines == null || machines.isEmpty()) throw new RequestException(MasterDataErrorCode.MACHINE_NOT_FOUND.getErrorCode(), @@ -187,8 +151,8 @@ public UploadPublicKeyResponseDto validateKeyMachineMapping(UploadPublicKeyReque throw new RequestException(MasterDataErrorCode.MACHINE_PUBLIC_KEY_NOT_WHITELISTED.getErrorCode(), MasterDataErrorCode.MACHINE_PUBLIC_KEY_NOT_WHITELISTED.getErrorMessage()); - if(Arrays.equals(CryptoUtil.decodeBase64(dto.getPublicKey()), - CryptoUtil.decodeBase64(machines.get(0).getPublicKey()))) { + if(Arrays.equals(cryptomanagerUtils.decodeBase64Data(dto.getPublicKey()), + cryptomanagerUtils.decodeBase64Data(machines.get(0).getPublicKey()))) { return new UploadPublicKeyResponseDto(machines.get(0).getKeyIndex()); } @@ -197,8 +161,9 @@ public UploadPublicKeyResponseDto validateKeyMachineMapping(UploadPublicKeyReque } @Override - public IdSchemaDto getLatestPublishedIdSchema(LocalDateTime lastUpdated, double schemaVersion) { - return identitySchemaHelper.getLatestIdentitySchema(lastUpdated, schemaVersion); + public JsonNode getLatestPublishedIdSchema(LocalDateTime lastUpdated, double schemaVersion, String domain, + String type) { + return identitySchemaHelper.getLatestIdentitySchema(lastUpdated, schemaVersion, domain, type); } @Override @@ -260,7 +225,6 @@ private MachineResponseDto getMachineById(String machineId) { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(String.format(machineUrl, machineId)); ResponseEntity responseEntity = restTemplate.getForEntity(builder.build().toUri(), String.class); - objectMapper.registerModule(new JavaTimeModule()); ResponseWrapper resp = objectMapper.readValue(responseEntity.getBody(), new TypeReference>() {}); diff --git a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/utils/SyncMasterDataServiceHelper.java b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/utils/SyncMasterDataServiceHelper.java index 637730342bd..1a9d5c58f2c 100644 --- a/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/utils/SyncMasterDataServiceHelper.java +++ b/admin/kernel-syncdata-service/src/main/java/io/mosip/kernel/syncdata/utils/SyncMasterDataServiceHelper.java @@ -1,5 +1,6 @@ package io.mosip.kernel.syncdata.utils; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.ArrayList; @@ -7,32 +8,135 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.CompletableFuture; +import java.util.stream.Collectors; -import io.mosip.kernel.clientcrypto.dto.TpmCryptoRequestDto; -import io.mosip.kernel.clientcrypto.dto.TpmCryptoResponseDto; -import io.mosip.kernel.clientcrypto.service.spi.ClientCryptoManagerService; - -import io.mosip.kernel.core.dataaccess.exception.DataAccessLayerException; -import io.mosip.kernel.syncdata.constant.AdminServiceErrorCode; -import io.mosip.kernel.syncdata.dto.*; -import io.mosip.kernel.syncdata.entity.*; -import io.mosip.kernel.syncdata.exception.AdminServiceException; -import io.mosip.kernel.syncdata.exception.RequestException; -import io.mosip.kernel.syncdata.repository.*; +import io.mosip.kernel.clientcrypto.constant.ClientType; +import io.mosip.kernel.syncdata.entity.id.HolidayID; 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.dao.DataAccessException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; + +import io.mosip.kernel.clientcrypto.dto.TpmCryptoRequestDto; +import io.mosip.kernel.clientcrypto.dto.TpmCryptoResponseDto; +import io.mosip.kernel.clientcrypto.service.spi.ClientCryptoManagerService; +import io.mosip.kernel.core.dataaccess.exception.DataAccessLayerException; +import io.mosip.kernel.core.exception.ExceptionUtils; +import io.mosip.kernel.core.exception.ServiceError; +import io.mosip.kernel.core.http.ResponseWrapper; import io.mosip.kernel.core.util.CryptoUtil; import io.mosip.kernel.core.util.DateUtils; +import io.mosip.kernel.syncdata.constant.AdminServiceErrorCode; import io.mosip.kernel.syncdata.constant.MasterDataErrorCode; +import io.mosip.kernel.syncdata.dto.AppAuthenticationMethodDto; +import io.mosip.kernel.syncdata.dto.AppRolePriorityDto; +import io.mosip.kernel.syncdata.dto.ApplicantValidDocumentDto; +import io.mosip.kernel.syncdata.dto.BlacklistedWordsDto; +import io.mosip.kernel.syncdata.dto.DocumentTypeDto; +import io.mosip.kernel.syncdata.dto.DynamicFieldDto; +import io.mosip.kernel.syncdata.dto.EntityDtimes; +import io.mosip.kernel.syncdata.dto.HolidayDto; +import io.mosip.kernel.syncdata.dto.LocationDto; +import io.mosip.kernel.syncdata.dto.LocationHierarchyDto; +import io.mosip.kernel.syncdata.dto.LocationHierarchyLevelResponseDto; +import io.mosip.kernel.syncdata.dto.MachineDto; +import io.mosip.kernel.syncdata.dto.PageDto; +import io.mosip.kernel.syncdata.dto.PermittedConfigDto; +import io.mosip.kernel.syncdata.dto.PostReasonCategoryDto; +import io.mosip.kernel.syncdata.dto.ProcessListDto; +import io.mosip.kernel.syncdata.dto.ReasonListDto; +import io.mosip.kernel.syncdata.dto.RegistrationCenterDto; +import io.mosip.kernel.syncdata.dto.RegistrationCenterMachineDto; +import io.mosip.kernel.syncdata.dto.RegistrationCenterUserDto; +import io.mosip.kernel.syncdata.dto.ScreenAuthorizationDto; +import io.mosip.kernel.syncdata.dto.ScreenDetailDto; +import io.mosip.kernel.syncdata.dto.SyncJobDefDto; +import io.mosip.kernel.syncdata.dto.TemplateDto; +import io.mosip.kernel.syncdata.dto.TemplateFileFormatDto; +import io.mosip.kernel.syncdata.dto.TemplateTypeDto; +import io.mosip.kernel.syncdata.dto.ValidDocumentDto; import io.mosip.kernel.syncdata.dto.response.SyncDataBaseDto; +import io.mosip.kernel.syncdata.entity.AppAuthenticationMethod; +import io.mosip.kernel.syncdata.entity.AppRolePriority; +import io.mosip.kernel.syncdata.entity.ApplicantValidDocument; +import io.mosip.kernel.syncdata.entity.BlocklistedWords; +import io.mosip.kernel.syncdata.entity.DocumentType; +import io.mosip.kernel.syncdata.entity.Holiday; +import io.mosip.kernel.syncdata.entity.Location; +import io.mosip.kernel.syncdata.entity.Machine; +import io.mosip.kernel.syncdata.entity.PermittedLocalConfig; +import io.mosip.kernel.syncdata.entity.ProcessList; +import io.mosip.kernel.syncdata.entity.ReasonCategory; +import io.mosip.kernel.syncdata.entity.ReasonList; +import io.mosip.kernel.syncdata.entity.RegistrationCenter; +import io.mosip.kernel.syncdata.entity.ScreenAuthorization; +import io.mosip.kernel.syncdata.entity.ScreenDetail; +import io.mosip.kernel.syncdata.entity.SyncJobDef; +import io.mosip.kernel.syncdata.entity.Template; +import io.mosip.kernel.syncdata.entity.TemplateFileFormat; +import io.mosip.kernel.syncdata.entity.TemplateType; +import io.mosip.kernel.syncdata.entity.UserDetails; +import io.mosip.kernel.syncdata.entity.ValidDocument; +import io.mosip.kernel.syncdata.exception.AdminServiceException; +import io.mosip.kernel.syncdata.exception.RequestException; import io.mosip.kernel.syncdata.exception.SyncDataServiceException; -import io.mosip.kernel.syncdata.service.SyncJobDefService; +import io.mosip.kernel.syncdata.exception.SyncInvalidArgumentException; +import io.mosip.kernel.syncdata.exception.SyncServiceException; +import io.mosip.kernel.syncdata.repository.AppAuthenticationMethodRepository; +import io.mosip.kernel.syncdata.repository.AppDetailRepository; +import io.mosip.kernel.syncdata.repository.AppRolePriorityRepository; +import io.mosip.kernel.syncdata.repository.ApplicantValidDocumentRespository; +import io.mosip.kernel.syncdata.repository.ApplicationRepository; +import io.mosip.kernel.syncdata.repository.BiometricAttributeRepository; +import io.mosip.kernel.syncdata.repository.BiometricTypeRepository; +import io.mosip.kernel.syncdata.repository.BlocklistedWordsRepository; +import io.mosip.kernel.syncdata.repository.DeviceHistoryRepository; +import io.mosip.kernel.syncdata.repository.DeviceProviderRepository; +import io.mosip.kernel.syncdata.repository.DeviceRepository; +import io.mosip.kernel.syncdata.repository.DeviceServiceRepository; +import io.mosip.kernel.syncdata.repository.DeviceSpecificationRepository; +import io.mosip.kernel.syncdata.repository.DeviceSubTypeDPMRepository; +import io.mosip.kernel.syncdata.repository.DeviceTypeDPMRepository; +import io.mosip.kernel.syncdata.repository.DeviceTypeRepository; +import io.mosip.kernel.syncdata.repository.DocumentCategoryRepository; +import io.mosip.kernel.syncdata.repository.DocumentTypeRepository; +import io.mosip.kernel.syncdata.repository.FoundationalTrustProviderRepository; +import io.mosip.kernel.syncdata.repository.HolidayRepository; +import io.mosip.kernel.syncdata.repository.IdTypeRepository; +import io.mosip.kernel.syncdata.repository.LanguageRepository; +import io.mosip.kernel.syncdata.repository.LocationRepository; +import io.mosip.kernel.syncdata.repository.MachineHistoryRepository; +import io.mosip.kernel.syncdata.repository.MachineRepository; +import io.mosip.kernel.syncdata.repository.MachineSpecificationRepository; +import io.mosip.kernel.syncdata.repository.MachineTypeRepository; +import io.mosip.kernel.syncdata.repository.PermittedLocalConfigRepository; +import io.mosip.kernel.syncdata.repository.ProcessListRepository; +import io.mosip.kernel.syncdata.repository.ReasonCategoryRepository; +import io.mosip.kernel.syncdata.repository.ReasonListRepository; +import io.mosip.kernel.syncdata.repository.RegisteredDeviceRepository; +import io.mosip.kernel.syncdata.repository.RegistrationCenterRepository; +import io.mosip.kernel.syncdata.repository.RegistrationCenterTypeRepository; +import io.mosip.kernel.syncdata.repository.ScreenAuthorizationRepository; +import io.mosip.kernel.syncdata.repository.ScreenDetailRepository; +import io.mosip.kernel.syncdata.repository.SyncJobDefRepository; +import io.mosip.kernel.syncdata.repository.TemplateFileFormatRepository; +import io.mosip.kernel.syncdata.repository.TemplateRepository; +import io.mosip.kernel.syncdata.repository.TemplateTypeRepository; +import io.mosip.kernel.syncdata.repository.TitleRepository; +import io.mosip.kernel.syncdata.repository.UserDetailsHistoryRepository; +import io.mosip.kernel.syncdata.repository.UserDetailsRepository; +import io.mosip.kernel.syncdata.repository.ValidDocumentRepository; /** * Sync handler masterData service helper @@ -43,8 +147,8 @@ */ @Component public class SyncMasterDataServiceHelper { - - private Logger logger = LoggerFactory.getLogger(SyncMasterDataServiceHelper.class); + + private final static Logger logger = LoggerFactory.getLogger(SyncMasterDataServiceHelper.class); @Autowired private MapperUtils mapper; @@ -77,8 +181,6 @@ public class SyncMasterDataServiceHelper { @Autowired private LanguageRepository languageRepository; @Autowired - private GenderRepository genderTypeRepository; - @Autowired private DeviceRepository deviceRepository; @Autowired private DocumentCategoryRepository documentCategoryRepository; @@ -105,8 +207,6 @@ public class SyncMasterDataServiceHelper { @Autowired private ApplicantValidDocumentRespository applicantValidDocumentRepository; @Autowired - private IndividualTypeRepository individualTypeRepository; - @Autowired private AppAuthenticationMethodRepository appAuthenticationMethodRepository; @Autowired private AppDetailRepository appDetailRepository; @@ -141,9 +241,20 @@ public class SyncMasterDataServiceHelper { @Autowired private ClientCryptoManagerService clientCryptoManagerService; + @Autowired + private RestTemplate restTemplate; + @Autowired + private ObjectMapper objectMapper; + + @Value("${mosip.kernel.masterdata.locationhierarchylevels.uri}") + private String locationHirerarchyUrl; + + @Value("${mosip.kernel.syncdata-service-dynamicfield-url}") + private String dynamicfieldUrl; + + private static final String ANDROID_MACHINE_TYPE_CODE = "ANDROID"; + - @Value("${mosip.syncdata.tpm.required:false}") - private boolean isTPMRequired; /** * Method to fetch machine details by regCenter id @@ -160,6 +271,7 @@ public CompletableFuture> getMachines(String regCenterId, Local List machineDetailList = null; List machineDetailDtoList = new ArrayList<>(); try { + if(!isChangesFound("Machine", lastUpdated)) { return CompletableFuture.completedFuture(machineDetailDtoList); } @@ -171,6 +283,7 @@ public CompletableFuture> getMachines(String regCenterId, Local currentTimeStamp, machineId); } catch (DataAccessException e) { + logger.error(e.getMessage(), e); throw new SyncDataServiceException(MasterDataErrorCode.MACHINE_DETAIL_FETCH_EXCEPTION.getErrorCode(), e.getMessage(), e); } @@ -192,6 +305,7 @@ public CompletableFuture> getMachines(String regCenterId, Local responseDto.setName(machine.getName()); responseDto.setSerialNum(machine.getSerialNum()); responseDto.setValidityDateTime(machine.getValidityDateTime()); + responseDto.setRegCenterId(machine.getRegCenterId()); machineDetailDtoList.add(responseDto); }); @@ -199,74 +313,78 @@ public CompletableFuture> getMachines(String regCenterId, Local return CompletableFuture.completedFuture(machineDetailDtoList); } - + /** - * Method to fetch machine type + * Method to fetch location hierarchy details * - * @param regCenterId registration center id - * @param lastUpdated lastupdated timestamp - * @param currentTimeStamp - current time stamp - * @return list of {@link MachineType} + * @param lastUpdated lastUpdated time-stamp + * + * @return list of {@link LocationHierarchyDto} list of + * locationHierarchyList dto */ @Async - public CompletableFuture> getMachineType(String regCenterId, LocalDateTime lastUpdated, - LocalDateTime currentTimeStamp) { - List machineTypeList = null; - List machineTypes = null; - try { - if (lastUpdated == null) { - lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); - } - machineTypes = machineTypeRepository.findLatestByRegCenterId(regCenterId, lastUpdated, currentTimeStamp); - - } catch ( - - DataAccessException e) { - throw new SyncDataServiceException(MasterDataErrorCode.MACHINE_TYPE_FETCH_EXCEPTION.getErrorCode(), - e.getMessage(), e); - } - if (machineTypes != null && !machineTypes.isEmpty()) + public CompletableFuture> getLocationHierarchyList(LocalDateTime lastUpdated) { + List locationHierarchyLevelDtos = new ArrayList(); - machineTypeList = MapperUtils.mapAll(machineTypes, MachineTypeDto.class); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(locationHirerarchyUrl); + if(lastUpdated != null) { builder.queryParam("lastUpdated", DateUtils.formatToISOString(lastUpdated)); } + ResponseEntity response = restTemplate.getForEntity(builder.build().toUri(), String.class); - return CompletableFuture.completedFuture(machineTypeList); + if (response.getStatusCode().equals(HttpStatus.OK)) { + String responseBody = response.getBody(); + List validationErrorsList = ExceptionUtils.getServiceErrorList(responseBody); + if (!validationErrorsList.isEmpty()) { + throw new SyncServiceException(validationErrorsList); + } + try { + ResponseWrapper responseObject = objectMapper.readValue(response.getBody(), ResponseWrapper.class); + LocationHierarchyLevelResponseDto locationHierarchyResponseDto = objectMapper.readValue( + objectMapper.writeValueAsString(responseObject.getResponse()), + LocationHierarchyLevelResponseDto.class); + locationHierarchyLevelDtos = locationHierarchyResponseDto.getLocationHierarchyLevels(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new SyncDataServiceException( + MasterDataErrorCode.LOCATION_HIERARCHY_DESERIALIZATION_FAILED.getErrorCode(), + MasterDataErrorCode.LOCATION_HIERARCHY_DESERIALIZATION_FAILED.getErrorMessage()); + } + } + return CompletableFuture.completedFuture(locationHierarchyLevelDtos); } - /** - * Method to fetch machine specification - * - * @param regCenterId registration center id - * @param lastUpdated lastupdated timestamp - * @param currentTimeStamp - current time stamp - * @return list of {@link MachineSpecificationDto} - */ @Async - public CompletableFuture> getMachineSpecification(String regCenterId, - LocalDateTime lastUpdated, LocalDateTime currentTimeStamp) { - List machineSpecification = null; - List machineSpecificationDto = null; + public CompletableFuture> getLocationHierarchyList(LocalDateTime lastUpdated, RestTemplate restClient) { + List locationHierarchyLevelDtos = new ArrayList(); - try { - if (regCenterId != null) { - if (lastUpdated == null) { - lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); - } - machineSpecification = machineSpecificationRepository.findLatestByRegCenterId(regCenterId, lastUpdated, - currentTimeStamp); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(locationHirerarchyUrl); + if(lastUpdated != null) { builder.queryParam("lastUpdated", DateUtils.formatToISOString(lastUpdated)); } + ResponseEntity response = restClient.getForEntity(builder.build().toUri(), String.class); + if (response.getStatusCode().equals(HttpStatus.OK)) { + String responseBody = response.getBody(); + List validationErrorsList = ExceptionUtils.getServiceErrorList(responseBody); + if (!validationErrorsList.isEmpty()) { + throw new SyncServiceException(validationErrorsList); } - } catch (DataAccessException e) { - throw new SyncDataServiceException(MasterDataErrorCode.APPLICATION_FETCH_EXCEPTION.getErrorCode(), - e.getMessage(), e); - } - if (machineSpecification != null && !machineSpecification.isEmpty()) + try { + ResponseWrapper responseObject = objectMapper.readValue(response.getBody(), ResponseWrapper.class); + LocationHierarchyLevelResponseDto locationHierarchyResponseDto = objectMapper.readValue( + objectMapper.writeValueAsString(responseObject.getResponse()), + LocationHierarchyLevelResponseDto.class); + locationHierarchyLevelDtos = locationHierarchyResponseDto.getLocationHierarchyLevels(); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new SyncDataServiceException( + MasterDataErrorCode.LOCATION_HIERARCHY_DESERIALIZATION_FAILED.getErrorCode(), + MasterDataErrorCode.LOCATION_HIERARCHY_DESERIALIZATION_FAILED.getErrorMessage()); + } + } + return CompletableFuture.completedFuture(locationHierarchyLevelDtos); + } - machineSpecificationDto = MapperUtils.mapAll(machineSpecification, MachineSpecificationDto.class); - return CompletableFuture.completedFuture(machineSpecificationDto); - } /** * Method to fetch registration center detail. @@ -277,90 +395,39 @@ public CompletableFuture> getMachineSpecification( * @return list of {@link RegistrationCenterDto} */ @Async - public CompletableFuture> getRegistrationCenter(String machineId, + public CompletableFuture> getRegistrationCenter(String centerId, LocalDateTime lastUpdated, LocalDateTime currentTimeStamp) { - List registrationCenterList = null; List list = null; try { if(!isChangesFound("RegistrationCenter", lastUpdated)) { - return CompletableFuture.completedFuture(registrationCenterList); + return CompletableFuture.completedFuture(null); } if (lastUpdated == null) { lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); } - list = registrationCenterRepository.findLatestRegistrationCenterByMachineId(machineId, lastUpdated, + list = registrationCenterRepository.findRegistrationCentersById(centerId, lastUpdated, currentTimeStamp); } catch (DataAccessException e) { + logger.error(e.getMessage(), e); throw new SyncDataServiceException(MasterDataErrorCode.APPLICATION_FETCH_EXCEPTION.getErrorCode(), e.getMessage(), e); } - if (list != null && !list.isEmpty()) { - registrationCenterList = MapperUtils.mapAll(list, RegistrationCenterDto.class); - } - - return CompletableFuture.completedFuture(registrationCenterList); - } - - /** - * Method to fetch registration center type - * - * @param machineId machine id - * @param lastUpdated lastUpdated timestamp - * @param currentTimeStamp - current time stamp - * @return list of {@link RegistrationCenterTypeDto} - */ - @Async - public CompletableFuture> getRegistrationCenterType(String machineId, - LocalDateTime lastUpdated, LocalDateTime currentTimeStamp) { - List registrationCenterTypes = null; - List registrationCenterType = null; - try { - if (lastUpdated == null) { - lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); - } - registrationCenterType = registrationCenterTypeRepository - .findLatestRegistrationCenterTypeByMachineId(machineId, lastUpdated, currentTimeStamp); - - } catch (DataAccessException e) { - throw new SyncDataServiceException(MasterDataErrorCode.REG_CENTER_TYPE_FETCH_EXCEPTION.getErrorCode(), - e.getMessage(), e); - } - - if (registrationCenterType != null && !registrationCenterType.isEmpty()) - registrationCenterTypes = MapperUtils.mapAll(registrationCenterType, RegistrationCenterTypeDto.class); - - return CompletableFuture.completedFuture(registrationCenterTypes); + return CompletableFuture.completedFuture(convertRegistrationCenterToDto(list)); } - /** - * Method to fetch applications - * - * @param lastUpdated lastUpdated timestamp - * @param currentTimeStamp - current time stamp - * @return list of {@link ApplicationDto} - */ - @Async - public CompletableFuture> getApplications(LocalDateTime lastUpdated, - LocalDateTime currentTimeStamp) { - List applications = null; - List applicationList = null; - try { - if (lastUpdated == null) { - lastUpdated = LocalDateTime.ofEpochSecond(0, 0, ZoneOffset.UTC); - } - applicationList = applicationRepository.findAllLatestCreatedUpdateDeleted(lastUpdated, currentTimeStamp); - - } catch (DataAccessException e) { - throw new SyncDataServiceException(MasterDataErrorCode.APPLICATION_FETCH_EXCEPTION.getErrorCode(), - e.getMessage(), e); + private List convertRegistrationCenterToDto(List list) { + if(list != null && !list.isEmpty()) { + List registrationCenterDtos = new ArrayList<>(); + list.stream().forEach( entity -> { + RegistrationCenterDto regiDto = copyRegistrationCenterProperties(entity); + registrationCenterDtos.add(regiDto); + }); + return registrationCenterDtos; + } + return null; } - if (!(applicationList.isEmpty())) { - applications = MapperUtils.mapAll(applicationList, ApplicationDto.class); - } - return CompletableFuture.completedFuture(applications); - } /** * Method to fetch templates @@ -372,25 +439,44 @@ public CompletableFuture> getApplications(LocalDateTime las @Async public CompletableFuture> getTemplates(String moduleId, LocalDateTime lastUpdated, LocalDateTime currentTimeStamp) { - List templates = null; List