Skip to content

Commit

Permalink
Updated misp license key regenerate logic
Browse files Browse the repository at this point in the history
Signed-off-by: Balaji <[email protected]>
Signed-off-by: kameshsr <[email protected]>
  • Loading branch information
balaji-alluru authored and kameshsr committed Jun 7, 2024
1 parent 45c9449 commit 0584cb2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.Page;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

import io.mosip.pms.common.constant.ConfigKeyConstants;
Expand Down Expand Up @@ -86,12 +87,12 @@ public class InfraProviderServiceImpl implements InfraServiceProviderService {

@Autowired
private PageUtils pageUtils;

@Autowired
private PartnerPolicyRequestRepository partnerPolicyRequestRepository;
private PartnerPolicyRequestRepository partnerPolicyRequestRepository;

@Autowired
private AuthPolicyRepository authPolicyRepository;
private AuthPolicyRepository authPolicyRepository;

public static final String APPROVED_STATUS = "approved";
public static final String REJECTED_STATUS = "rejected";
Expand All @@ -101,7 +102,7 @@ public class InfraProviderServiceImpl implements InfraServiceProviderService {
public static final String NOTACTIVE = "NOT_ACTIVE";

/**
*
*
*/
@Override
public MISPLicenseResponseDto approveInfraProvider(String mispId) {
Expand Down Expand Up @@ -133,14 +134,14 @@ public MISPLicenseResponseDto approveInfraProvider(String mispId) {
throw new MISPServiceException(MISPErrorMessages.MISP_POLICY_NOT_APPROVED.getErrorCode(),
MISPErrorMessages.MISP_POLICY_NOT_APPROVED.getErrorMessage());
}

mispPolicyFromDb = authPolicyRepository.findById(approvedPolicyMappedReq.get(0).getPolicyId());
if(mispPolicyFromDb.isEmpty()) {
throw new MISPServiceException(MISPErrorMessages.MISP_POLICY_NOT_EXISTS.getErrorCode(),
MISPErrorMessages.MISP_POLICY_NOT_EXISTS.getErrorMessage());
}
}

String policyId = mispPolicyFromDb.isPresent()?mispPolicyFromDb.get().getId():null;
MISPLicenseEntity newLicenseKey = generateLicense(mispId, policyId);
MISPLicenseResponseDto response = new MISPLicenseResponseDto();
Expand All @@ -158,9 +159,9 @@ public MISPLicenseResponseDto approveInfraProvider(String mispId) {

return response;
}

/**
*
*
* @param policy
* @return
*/
Expand All @@ -171,13 +172,13 @@ private JSONObject getPolicyObject(String policy) {
return ((JSONObject) parser.parse(policy));
} catch (ParseException e) {
error = e.getMessage();
}
}
throw new MISPServiceException(ErrorCode.POLICY_PARSING_ERROR.getErrorCode(),
ErrorCode.POLICY_PARSING_ERROR.getErrorMessage() + error);
}

/**
*
*
* @return
*/
private String generateLicenseKey() {
Expand All @@ -189,7 +190,7 @@ private String generateLicenseKey() {
}

/**
*
*
*/
@Override
public MISPLicenseResponseDto updateInfraProvider(String id, String licenseKey, String status) {
Expand Down Expand Up @@ -218,19 +219,19 @@ public MISPLicenseResponseDto updateInfraProvider(String id, String licenseKey,
}

/**
*
*
*/
@Override
public List<MISPLicenseEntity> getInfraProvider() {
return mispLicenseRepository.findAll();
}

/**
*
*
* @param mispId
* @return
*/
private MISPLicenseEntity generateLicense(String mispId, String policyId) {
private MISPLicenseEntity generateLicense(String mispId,@Nullable String policyId) {
MISPLicenseEntity entity = new MISPLicenseEntity();
entity.setMispId(mispId);
entity.setLicenseKey(generateLicenseKey());
Expand All @@ -246,7 +247,7 @@ private MISPLicenseEntity generateLicense(String mispId, String policyId) {
}

/**
*
*
*/
@Override
public MISPLicenseResponseDto regenerateKey(String mispId) {
Expand All @@ -259,74 +260,65 @@ public MISPLicenseResponseDto regenerateKey(String mispId) {
throw new MISPServiceException(MISPErrorMessages.MISP_IS_INACTIVE.getErrorCode(),
MISPErrorMessages.MISP_IS_INACTIVE.getErrorMessage());
}
List<MISPLicenseEntity> mispLicenses = mispLicenseRepository.findByMispId(mispId);
boolean isActiveLicenseExists = false;
List<MISPLicenseEntity> mispValidLicenses = mispLicenseRepository.findByMispIdandExpirydate(mispId);
if(mispLicenseRepository.findByMispId(mispId).isEmpty()) {
throw new MISPServiceException(MISPErrorMessages.MISP_LICENSE_KEY_NOT_ASSOCIATED_MISP_ID.getErrorCode(),
MISPErrorMessages.MISP_LICENSE_KEY_NOT_ASSOCIATED_MISP_ID.getErrorMessage());
}
List<PartnerPolicyRequest> approvedPolicyMappedReq = partnerPolicyRequestRepository.findByPartnerId(mispId);
String policyId = (!approvedPolicyMappedReq.isEmpty() && !approvedPolicyMappedReq.get(0).getPolicyId().isBlank())?approvedPolicyMappedReq.get(0).getPolicyId():null;
MISPLicenseResponseDto response = new MISPLicenseResponseDto();
for (MISPLicenseEntity licenseKey : mispLicenses) {
if (licenseKey.getIsActive() && licenseKey.getValidToDate().isBefore(LocalDateTime.now())) {
isActiveLicenseExists = true;
licenseKey.setIsActive(false);
licenseKey.setUpdatedBy(getLoggedInUserId());
licenseKey.setUpdatedDateTime(LocalDateTime.now());
mispLicenseRepository.save(licenseKey);
MISPLicenseEntity newLicenseKey = generateLicense(mispId, licenseKey.getPolicyId());
response.setLicenseKey(newLicenseKey.getLicenseKey());
response.setLicenseKeyExpiry(newLicenseKey.getValidToDate());
response.setLicenseKeyStatus("Active");
response.setProviderId(mispId);
List<PartnerPolicyRequest> approvedPolicyMappedReq = partnerPolicyRequestRepository.findByPartnerId(mispId);
Optional<AuthPolicy> mispPolicyFromDb = Optional.empty();
if(!approvedPolicyMappedReq.isEmpty()) {
if(!approvedPolicyMappedReq.stream().allMatch(p->p.getStatusCode().equalsIgnoreCase(APPROVED_STATUS))){
throw new MISPServiceException(MISPErrorMessages.MISP_POLICY_NOT_APPROVED.getErrorCode(),
MISPErrorMessages.MISP_POLICY_NOT_APPROVED.getErrorMessage());
}

mispPolicyFromDb = authPolicyRepository.findById(approvedPolicyMappedReq.get(0).getPolicyId());
if(mispPolicyFromDb.isEmpty()) {
throw new MISPServiceException(MISPErrorMessages.MISP_POLICY_NOT_EXISTS.getErrorCode(),
MISPErrorMessages.MISP_POLICY_NOT_EXISTS.getErrorMessage());
}
if (mispValidLicenses.isEmpty()) {
MISPLicenseEntity newLicenseKey = generateLicense(mispId, policyId);
response.setLicenseKey(newLicenseKey.getLicenseKey());
response.setLicenseKeyExpiry(newLicenseKey.getValidToDate());
response.setLicenseKeyStatus("Active");
response.setProviderId(mispId);

Optional<AuthPolicy> mispPolicyFromDb = Optional.empty();
if(!approvedPolicyMappedReq.isEmpty()) {
if(!approvedPolicyMappedReq.stream().allMatch(p->p.getStatusCode().equalsIgnoreCase(APPROVED_STATUS))){
throw new MISPServiceException(MISPErrorMessages.MISP_POLICY_NOT_APPROVED.getErrorCode(),
MISPErrorMessages.MISP_POLICY_NOT_APPROVED.getErrorMessage());
}

String policyId = mispPolicyFromDb.isPresent()?mispPolicyFromDb.get().getId():null;
if(mispPolicyFromDb.isPresent()) {
notify(MapperUtils.mapDataToPublishDto(newLicenseKey), MapperUtils.mapPolicyToPublishDto(mispPolicyFromDb.get(),
getPolicyObject(mispPolicyFromDb.get().getPolicyFileId())), EventType.MISP_LICENSE_UPDATED);
}
else {
notify(MapperUtils.mapDataToPublishDto(newLicenseKey), EventType.MISP_LICENSE_UPDATED);

mispPolicyFromDb = authPolicyRepository.findById(approvedPolicyMappedReq.get(0).getPolicyId());
if(mispPolicyFromDb.isEmpty()) {
throw new MISPServiceException(MISPErrorMessages.MISP_POLICY_NOT_EXISTS.getErrorCode(),
MISPErrorMessages.MISP_POLICY_NOT_EXISTS.getErrorMessage());
}

}

if (licenseKey.getIsActive() && licenseKey.getValidToDate().isAfter(LocalDateTime.now())) {
isActiveLicenseExists = true;
response.setLicenseKey(licenseKey.getLicenseKey());
response.setLicenseKeyExpiry(licenseKey.getValidToDate());
response.setLicenseKeyStatus("Active");
response.setProviderId(mispId);
if(mispPolicyFromDb.isPresent()) {
notify(MapperUtils.mapDataToPublishDto(newLicenseKey), MapperUtils.mapPolicyToPublishDto(mispPolicyFromDb.get(),
getPolicyObject(mispPolicyFromDb.get().getPolicyFileId())), EventType.MISP_LICENSE_UPDATED);
}
else {
notify(MapperUtils.mapDataToPublishDto(newLicenseKey), EventType.MISP_LICENSE_UPDATED);
}
}

if (!isActiveLicenseExists) {
throw new MISPServiceException(MISPErrorMessages.MISP_LICENSE_ARE_NOT_ACTIVE.getErrorCode(),
MISPErrorMessages.MISP_LICENSE_ARE_NOT_ACTIVE.getErrorMessage());
}
else {
response.setLicenseKey(mispValidLicenses.get(0).getLicenseKey());
response.setLicenseKeyExpiry(mispValidLicenses.get(0).getValidToDate());
response.setLicenseKeyStatus("Active");
response.setProviderId(mispId);
}


return response;
}

/**
*
*
* @return
*/
public String getLoggedInUserId() {
return UserDetailUtil.getLoggedInUserId();
}

/**
*
*
* @param dataToPublish
* @param eventType
*/
Expand All @@ -338,13 +330,13 @@ private void notify(MISPDataPublishDto dataToPublish, EventType eventType) {
data.put("mispLicenseData", dataToPublish);
webSubPublisher.notify(eventType, data, type);
}

private void notify(MISPDataPublishDto dataToPublish, PolicyPublishDto policyDataToPublish,EventType eventType) {
Type type = new Type();
type.setName("InfraProviderServiceImpl");
type.setNamespace("io.mosip.pmp.partner.service.impl.InfraProviderServiceImpl");
Map<String, Object> data = new HashMap<>();
data.put("mispLicenseData", dataToPublish);
data.put("mispLicenseData", dataToPublish);
if (dataToPublish != null) {
data.put(PartnerConstants.MISP_DATA, dataToPublish);
}
Expand Down Expand Up @@ -391,7 +383,7 @@ public PageResponseDto<MISPLicenseEntity> search(SearchDto dto) {
}
return pageDto;
}

/**
* validates the loggedInUser authorization
* @param loggedInUserId
Expand All @@ -402,4 +394,4 @@ public void validateLoggedInUserAuthorization(String loggedInUserId) {
ErrorCode.LOGGEDIN_USER_NOT_AUTHORIZED.getErrorMessage());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ public void regenerateKeyTest() {
mispLicense.add(mispLicenseEntity);
Optional<Partner> opt_partner = Optional.of(partner);
Mockito.when(partnerRepository.findById(misp_Id)).thenReturn(opt_partner);
Mockito.when(mispLicenseRepository.findByMispIdandExpirydate(misp_Id)).thenReturn(mispLicense);
Mockito.when(mispLicenseRepository.findByMispId(misp_Id)).thenReturn(mispLicense);
infraProviderServiceImpl.regenerateKey(misp_Id);
}
Expand All @@ -260,6 +261,7 @@ public void regenerateKeyTest_001() {
mispLicense.add(mispLicenseEntity);
Optional<Partner> opt_partner = Optional.of(partner);
Mockito.when(partnerRepository.findById(misp_Id)).thenReturn(opt_partner);
Mockito.when(mispLicenseRepository.findByMispIdandExpirydate(misp_Id)).thenReturn(mispLicense);
Mockito.when(mispLicenseRepository.findByMispId(misp_Id)).thenReturn(mispLicense);
infraProviderServiceImpl.regenerateKey(misp_Id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public interface MispLicenseRepository extends JpaRepository<MISPLicenseEntity,
@Query(value = "select * from misp_license ml where ml.misp_id=?", nativeQuery = true)
List<MISPLicenseEntity> findByMispId(String mispId);


@Query(value = "select * from misp_license ml where ml.misp_id=? and ml.valid_to_date > now() and (ml.is_deleted is null or ml.is_deleted = false) and ml.is_active = true", nativeQuery = true)
List<MISPLicenseEntity> findByMispIdandExpirydate(String mispId);

@Query(value = "select * from misp_license ml where ml.misp_id = ?1 and ml.license_key=?2", nativeQuery = true)
MISPLicenseEntity findByIdAndKey(String id, String licenseKey);

Expand Down

0 comments on commit 0584cb2

Please sign in to comment.