@Test public void transferSSLCertificate_Success() throws Exception { String jsonStr = "{ \"username\": \"testusername1\", \"password\": \"testpassword1\"}"; String jsonStr2 = "{\"certificates\":[{\"sortedSubjectName\": \"CN=CertificateName.t-mobile.com, C=US, " + "ST=Washington, " + "L=Bellevue, O=T-Mobile USA, Inc\"," + "\"certificateId\":57258,\"certificateStatus\":\"Active\"," + "\"containerName\":\"cont_12345\",\"NotAfter\":\"2021-06-15T04:35:58-07:00\"}]}"; Response userResponse = getMockResponse(HttpStatus.OK, true, "{\"data\":{\"bound_cidrs\":[],\"max_ttl\":0,\"policies\":[\"default\",\"r_cert_CertificateName.t-mobile.com\"],\"ttl\":0,\"groups\":\"admin\"}}"); SSLCertificateMetadataDetails sslCertificateRequest = getSSLCertificateMetadataDetails(); UserDetails userDetails = new UserDetails(); userDetails.setSelfSupportToken("tokentTest"); userDetails.setUsername("normaluser"); userDetails.setAdmin(true); userDetails.setClientToken(token); userDetails.setSelfSupportToken(token); Map<String, Object> requestMap = new HashMap<>(); requestMap.put("access_token", "12345"); requestMap.put("token_type", "type"); when(ControllerUtil.parseJson(jsonStr)).thenReturn(requestMap); CertManagerLogin certManagerLogin = new CertManagerLogin(); certManagerLogin.setToken_type("token type"); certManagerLogin.setAccess_token("1234"); String metaDataJson = "{\"data\":{\"akmid\":\"102463\",\"applicationName\":\"tvs\",\"applicationOwnerEmailId\":\"SpectrumClearingTools@T-Mobile.com\",\"applicationTag\":\"TVS\",\"authority\":\"T-Mobile Issuing CA 01 - SHA2\",\"certCreatedBy\":\"nnazeer1\",\"certOwnerNtid\": \"testusername1\",\"certOwnerEmailId\":\"ltest@smail.com\",\"certType\":\"internal\",\"certificateId\":59880,\"certificateName\":\"certtest260630.t-mobile.com\",\"certificateStatus\":\"Revoked\",\"containerName\":\"VenafiBin_12345\",\"createDate\":\"2020-06-26T05:10:41-07:00\",\"expiryDate\":\"2021-06-26T05:10:41-07:00\",\"projectLeadEmailId\":\"Daniel.Urrutia@T-Mobile.Com\",\"users\":{\"normaluser\":\"write\",\"certuser\":\"read\",\"safeadmin\":\"deny\",\"testsafeuser\":\"write\",\"testuser1\":\"deny\",\"testuser2\":\"read\"}}}"; Response response = new Response(); response.setHttpstatus(HttpStatus.OK); response.setResponse(metaDataJson); response.setSuccess(true); when(reqProcessor.process(eq("/read"), anyObject(), anyString())).thenReturn(response); CertResponse certResponse = new CertResponse(); certResponse.setHttpstatus(HttpStatus.OK); certResponse.setResponse(jsonStr); certResponse.setSuccess(true); when(reqProcessor.processCert(eq("/auth/certmanager/login"), anyObject(), anyString(), anyString())).thenReturn(certResponse); when(ControllerUtil.updateMetaData(anyString(), anyMap(), anyString())).thenReturn(Boolean.TRUE); when(reqProcessor.process("/auth/userpass/read","{\"username\":\"testuser2\"}",token)).thenReturn(userResponse); DirectoryObjects obj =new DirectoryObjects(); DirectoryObjectsList objList = new DirectoryObjectsList(); DirectoryUser user = new DirectoryUser(); user.setDisplayName("name"); user.setUserName("213"); Object[] values = null; objList.setValues(values); obj.setData(objList); when(directoryService.searchByUPN(anyString())). thenReturn(ResponseEntity.status(HttpStatus.OK).body(obj)); ResponseEntity<?> transferCertResponse = sSLCertificateService.updateCertOwner(token,"internal","certificatename.t-mobile.com","owneremail@test.com",userDetails); assertNotNull(transferCertResponse); }
public ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId, UserDetails userDetails) throws Exception { Map<String, String> metaDataParams = new HashMap<String, String>(); Map<String, String> dataMetaDataParams = new HashMap<String, String>(); SSLCertificateRequest certificateRequest = new SSLCertificateRequest(); token = userDetails.getSelfSupportToken(); boolean isValidEmail = true; String certOwnerNtId =""; Object[] users = null; DirectoryUser dirUser = new DirectoryUser(); if (!isValidInputs(certName, certType) || !validateCertficateEmail(certOwnerEmailId)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "transferCertificate") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } ResponseEntity<DirectoryObjects> userResponse = directoryService.searchByUPN(certOwnerEmailId); if(userResponse.getStatusCode().equals(HttpStatus.OK)) { users = userResponse.getBody().getData().getValues(); if(!ObjectUtils.isEmpty(users)) { dirUser = (DirectoryUser) users[0]; certOwnerNtId = dirUser.getUserName(); } } if(certOwnerNtId==""){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"User unavailable\"]}"); } String endPoint = certName; CertResponse enrollResponse = new CertResponse(); String metaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH + "/" + endPoint :SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH + "/" + endPoint; String permissionMetaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH_VALUE + endPoint :SSLCertificateConstants.SSL_CERT_PATH_VALUE_EXT + endPoint; Response response = new Response(); Response dataResponse = new Response(); if (!userDetails.isAdmin()) { Boolean isPermission = validateCertOwnerPermissionForNonAdmin(userDetails, certName, certType); if (!isPermission) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("{\"errors\":[\"" + "Access denied: No permission to transfer the ownership of this certificate" + "\"]}"); } } try { if (userDetails.isAdmin()) { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", token); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", token); } else { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", userDetails.getSelfSupportToken()); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", userDetails.getSelfSupportToken()); } } catch (Exception e) { log.error( JSONUtil.getJSON( ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), response.getResponse())) .build())); return ResponseEntity.status(response.getHttpstatus()) .body("{\"messages\":[\"" + "Certificate unavailable" + "\"]}"); } if (!HttpStatus.OK.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()) .body("{\"errors\":[\"" + "Certificate unavailable" + "\"]}"); } JsonParser jsonParser = new JsonParser(); ObjectMapper objMapper = new ObjectMapper(); JsonObject object = ((JsonObject) jsonParser.parse(response.getResponse())).getAsJsonObject("data"); JsonObject dataObject = ((JsonObject) jsonParser.parse(dataResponse.getResponse())).getAsJsonObject("data"); metaDataParams = new Gson().fromJson(object.toString(), Map.class); if(certOwnerEmailId.equalsIgnoreCase(metaDataParams.get("certOwnerEmailId"))) { isValidEmail=false; } if(dataObject!=null) { dataMetaDataParams = new Gson().fromJson(dataObject.toString(), Map.class); dataMetaDataParams.put("certOwnerNtid", certOwnerNtId); dataMetaDataParams.put("certOwnerEmailId", certOwnerEmailId); } if((Objects.nonNull(metaDataParams)) && (Objects.nonNull(metaDataParams.get("requestStatus"))) && metaDataParams.get("requestStatus").equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Certificate may not be approved or rejected from NCLM\"]}"); } if(!isValidEmail) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "New owner email id should not be same as owner email id" + "\"]}"); } String certificateUser = metaDataParams.get("certOwnerNtid"); boolean sslMetaDataUpdationStatus; metaDataParams.put("certOwnerEmailId", certOwnerEmailId); metaDataParams.put("certOwnerNtid", certOwnerNtId); certificateRequest.setCertificateName(certName); certificateRequest.setCertType(certType); certificateRequest.setCertOwnerEmailId(certOwnerEmailId); certificateRequest.setCertOwnerNtid(certOwnerNtId); try { if (userDetails.isAdmin()) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, token); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, token); } } else { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, userDetails.getSelfSupportToken()); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, userDetails.getSelfSupportToken()); } } if (sslMetaDataUpdationStatus) { boolean isPoliciesCreated=true; removeSudoPermissionForPreviousOwner( certificateUser.toLowerCase(), certName,userDetails,certType); addSudoPermissionToCertificateOwner(certificateRequest, userDetails, enrollResponse, isPoliciesCreated, true,token,"transfer"); sendTransferEmail(metaDataParams,certificateRequest.getCertOwnerNtid(),certificateUser); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "sendTransferEmail"). put(LogMessage.MESSAGE, String.format("Successfully sent Transfer email notification oldOwner= " + "[%s] newOwner =[%s] for certificate [%s] on date = [%s]", getUserEmail(getUserDetails(certificateUser)), getUserEmail(getUserDetails(certificateRequest.getCertOwnerNtid())) , metaDataParams.get("certificateName"),java.time.LocalDateTime.now())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK) .body("{\"messages\":[\"" + "Certificate Owner Transferred Successfully" + "\"]}"); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Certificate owner Transfer failed for CertificateID") .put(LogMessage.STATUS, HttpStatus.INTERNAL_SERVER_ERROR.toString()) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()) .build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "Certificate owner Transfer failed" + "\"]}"); } } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Inside Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), e.getMessage())) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"errors\":[\"" + e.getMessage() + "\"]}"); } }
SSLCertificateService { public ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId, UserDetails userDetails) throws Exception { Map<String, String> metaDataParams = new HashMap<String, String>(); Map<String, String> dataMetaDataParams = new HashMap<String, String>(); SSLCertificateRequest certificateRequest = new SSLCertificateRequest(); token = userDetails.getSelfSupportToken(); boolean isValidEmail = true; String certOwnerNtId =""; Object[] users = null; DirectoryUser dirUser = new DirectoryUser(); if (!isValidInputs(certName, certType) || !validateCertficateEmail(certOwnerEmailId)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "transferCertificate") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } ResponseEntity<DirectoryObjects> userResponse = directoryService.searchByUPN(certOwnerEmailId); if(userResponse.getStatusCode().equals(HttpStatus.OK)) { users = userResponse.getBody().getData().getValues(); if(!ObjectUtils.isEmpty(users)) { dirUser = (DirectoryUser) users[0]; certOwnerNtId = dirUser.getUserName(); } } if(certOwnerNtId==""){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"User unavailable\"]}"); } String endPoint = certName; CertResponse enrollResponse = new CertResponse(); String metaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH + "/" + endPoint :SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH + "/" + endPoint; String permissionMetaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH_VALUE + endPoint :SSLCertificateConstants.SSL_CERT_PATH_VALUE_EXT + endPoint; Response response = new Response(); Response dataResponse = new Response(); if (!userDetails.isAdmin()) { Boolean isPermission = validateCertOwnerPermissionForNonAdmin(userDetails, certName, certType); if (!isPermission) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("{\"errors\":[\"" + "Access denied: No permission to transfer the ownership of this certificate" + "\"]}"); } } try { if (userDetails.isAdmin()) { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", token); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", token); } else { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", userDetails.getSelfSupportToken()); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", userDetails.getSelfSupportToken()); } } catch (Exception e) { log.error( JSONUtil.getJSON( ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), response.getResponse())) .build())); return ResponseEntity.status(response.getHttpstatus()) .body("{\"messages\":[\"" + "Certificate unavailable" + "\"]}"); } if (!HttpStatus.OK.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()) .body("{\"errors\":[\"" + "Certificate unavailable" + "\"]}"); } JsonParser jsonParser = new JsonParser(); ObjectMapper objMapper = new ObjectMapper(); JsonObject object = ((JsonObject) jsonParser.parse(response.getResponse())).getAsJsonObject("data"); JsonObject dataObject = ((JsonObject) jsonParser.parse(dataResponse.getResponse())).getAsJsonObject("data"); metaDataParams = new Gson().fromJson(object.toString(), Map.class); if(certOwnerEmailId.equalsIgnoreCase(metaDataParams.get("certOwnerEmailId"))) { isValidEmail=false; } if(dataObject!=null) { dataMetaDataParams = new Gson().fromJson(dataObject.toString(), Map.class); dataMetaDataParams.put("certOwnerNtid", certOwnerNtId); dataMetaDataParams.put("certOwnerEmailId", certOwnerEmailId); } if((Objects.nonNull(metaDataParams)) && (Objects.nonNull(metaDataParams.get("requestStatus"))) && metaDataParams.get("requestStatus").equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Certificate may not be approved or rejected from NCLM\"]}"); } if(!isValidEmail) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "New owner email id should not be same as owner email id" + "\"]}"); } String certificateUser = metaDataParams.get("certOwnerNtid"); boolean sslMetaDataUpdationStatus; metaDataParams.put("certOwnerEmailId", certOwnerEmailId); metaDataParams.put("certOwnerNtid", certOwnerNtId); certificateRequest.setCertificateName(certName); certificateRequest.setCertType(certType); certificateRequest.setCertOwnerEmailId(certOwnerEmailId); certificateRequest.setCertOwnerNtid(certOwnerNtId); try { if (userDetails.isAdmin()) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, token); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, token); } } else { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, userDetails.getSelfSupportToken()); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, userDetails.getSelfSupportToken()); } } if (sslMetaDataUpdationStatus) { boolean isPoliciesCreated=true; removeSudoPermissionForPreviousOwner( certificateUser.toLowerCase(), certName,userDetails,certType); addSudoPermissionToCertificateOwner(certificateRequest, userDetails, enrollResponse, isPoliciesCreated, true,token,"transfer"); sendTransferEmail(metaDataParams,certificateRequest.getCertOwnerNtid(),certificateUser); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "sendTransferEmail"). put(LogMessage.MESSAGE, String.format("Successfully sent Transfer email notification oldOwner= " + "[%s] newOwner =[%s] for certificate [%s] on date = [%s]", getUserEmail(getUserDetails(certificateUser)), getUserEmail(getUserDetails(certificateRequest.getCertOwnerNtid())) , metaDataParams.get("certificateName"),java.time.LocalDateTime.now())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK) .body("{\"messages\":[\"" + "Certificate Owner Transferred Successfully" + "\"]}"); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Certificate owner Transfer failed for CertificateID") .put(LogMessage.STATUS, HttpStatus.INTERNAL_SERVER_ERROR.toString()) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()) .build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "Certificate owner Transfer failed" + "\"]}"); } } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Inside Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), e.getMessage())) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"errors\":[\"" + e.getMessage() + "\"]}"); } } }
SSLCertificateService { public ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId, UserDetails userDetails) throws Exception { Map<String, String> metaDataParams = new HashMap<String, String>(); Map<String, String> dataMetaDataParams = new HashMap<String, String>(); SSLCertificateRequest certificateRequest = new SSLCertificateRequest(); token = userDetails.getSelfSupportToken(); boolean isValidEmail = true; String certOwnerNtId =""; Object[] users = null; DirectoryUser dirUser = new DirectoryUser(); if (!isValidInputs(certName, certType) || !validateCertficateEmail(certOwnerEmailId)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "transferCertificate") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } ResponseEntity<DirectoryObjects> userResponse = directoryService.searchByUPN(certOwnerEmailId); if(userResponse.getStatusCode().equals(HttpStatus.OK)) { users = userResponse.getBody().getData().getValues(); if(!ObjectUtils.isEmpty(users)) { dirUser = (DirectoryUser) users[0]; certOwnerNtId = dirUser.getUserName(); } } if(certOwnerNtId==""){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"User unavailable\"]}"); } String endPoint = certName; CertResponse enrollResponse = new CertResponse(); String metaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH + "/" + endPoint :SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH + "/" + endPoint; String permissionMetaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH_VALUE + endPoint :SSLCertificateConstants.SSL_CERT_PATH_VALUE_EXT + endPoint; Response response = new Response(); Response dataResponse = new Response(); if (!userDetails.isAdmin()) { Boolean isPermission = validateCertOwnerPermissionForNonAdmin(userDetails, certName, certType); if (!isPermission) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("{\"errors\":[\"" + "Access denied: No permission to transfer the ownership of this certificate" + "\"]}"); } } try { if (userDetails.isAdmin()) { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", token); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", token); } else { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", userDetails.getSelfSupportToken()); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", userDetails.getSelfSupportToken()); } } catch (Exception e) { log.error( JSONUtil.getJSON( ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), response.getResponse())) .build())); return ResponseEntity.status(response.getHttpstatus()) .body("{\"messages\":[\"" + "Certificate unavailable" + "\"]}"); } if (!HttpStatus.OK.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()) .body("{\"errors\":[\"" + "Certificate unavailable" + "\"]}"); } JsonParser jsonParser = new JsonParser(); ObjectMapper objMapper = new ObjectMapper(); JsonObject object = ((JsonObject) jsonParser.parse(response.getResponse())).getAsJsonObject("data"); JsonObject dataObject = ((JsonObject) jsonParser.parse(dataResponse.getResponse())).getAsJsonObject("data"); metaDataParams = new Gson().fromJson(object.toString(), Map.class); if(certOwnerEmailId.equalsIgnoreCase(metaDataParams.get("certOwnerEmailId"))) { isValidEmail=false; } if(dataObject!=null) { dataMetaDataParams = new Gson().fromJson(dataObject.toString(), Map.class); dataMetaDataParams.put("certOwnerNtid", certOwnerNtId); dataMetaDataParams.put("certOwnerEmailId", certOwnerEmailId); } if((Objects.nonNull(metaDataParams)) && (Objects.nonNull(metaDataParams.get("requestStatus"))) && metaDataParams.get("requestStatus").equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Certificate may not be approved or rejected from NCLM\"]}"); } if(!isValidEmail) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "New owner email id should not be same as owner email id" + "\"]}"); } String certificateUser = metaDataParams.get("certOwnerNtid"); boolean sslMetaDataUpdationStatus; metaDataParams.put("certOwnerEmailId", certOwnerEmailId); metaDataParams.put("certOwnerNtid", certOwnerNtId); certificateRequest.setCertificateName(certName); certificateRequest.setCertType(certType); certificateRequest.setCertOwnerEmailId(certOwnerEmailId); certificateRequest.setCertOwnerNtid(certOwnerNtId); try { if (userDetails.isAdmin()) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, token); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, token); } } else { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, userDetails.getSelfSupportToken()); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, userDetails.getSelfSupportToken()); } } if (sslMetaDataUpdationStatus) { boolean isPoliciesCreated=true; removeSudoPermissionForPreviousOwner( certificateUser.toLowerCase(), certName,userDetails,certType); addSudoPermissionToCertificateOwner(certificateRequest, userDetails, enrollResponse, isPoliciesCreated, true,token,"transfer"); sendTransferEmail(metaDataParams,certificateRequest.getCertOwnerNtid(),certificateUser); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "sendTransferEmail"). put(LogMessage.MESSAGE, String.format("Successfully sent Transfer email notification oldOwner= " + "[%s] newOwner =[%s] for certificate [%s] on date = [%s]", getUserEmail(getUserDetails(certificateUser)), getUserEmail(getUserDetails(certificateRequest.getCertOwnerNtid())) , metaDataParams.get("certificateName"),java.time.LocalDateTime.now())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK) .body("{\"messages\":[\"" + "Certificate Owner Transferred Successfully" + "\"]}"); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Certificate owner Transfer failed for CertificateID") .put(LogMessage.STATUS, HttpStatus.INTERNAL_SERVER_ERROR.toString()) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()) .build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "Certificate owner Transfer failed" + "\"]}"); } } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Inside Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), e.getMessage())) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"errors\":[\"" + e.getMessage() + "\"]}"); } }  }
SSLCertificateService { public ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId, UserDetails userDetails) throws Exception { Map<String, String> metaDataParams = new HashMap<String, String>(); Map<String, String> dataMetaDataParams = new HashMap<String, String>(); SSLCertificateRequest certificateRequest = new SSLCertificateRequest(); token = userDetails.getSelfSupportToken(); boolean isValidEmail = true; String certOwnerNtId =""; Object[] users = null; DirectoryUser dirUser = new DirectoryUser(); if (!isValidInputs(certName, certType) || !validateCertficateEmail(certOwnerEmailId)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "transferCertificate") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } ResponseEntity<DirectoryObjects> userResponse = directoryService.searchByUPN(certOwnerEmailId); if(userResponse.getStatusCode().equals(HttpStatus.OK)) { users = userResponse.getBody().getData().getValues(); if(!ObjectUtils.isEmpty(users)) { dirUser = (DirectoryUser) users[0]; certOwnerNtId = dirUser.getUserName(); } } if(certOwnerNtId==""){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"User unavailable\"]}"); } String endPoint = certName; CertResponse enrollResponse = new CertResponse(); String metaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH + "/" + endPoint :SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH + "/" + endPoint; String permissionMetaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH_VALUE + endPoint :SSLCertificateConstants.SSL_CERT_PATH_VALUE_EXT + endPoint; Response response = new Response(); Response dataResponse = new Response(); if (!userDetails.isAdmin()) { Boolean isPermission = validateCertOwnerPermissionForNonAdmin(userDetails, certName, certType); if (!isPermission) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("{\"errors\":[\"" + "Access denied: No permission to transfer the ownership of this certificate" + "\"]}"); } } try { if (userDetails.isAdmin()) { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", token); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", token); } else { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", userDetails.getSelfSupportToken()); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", userDetails.getSelfSupportToken()); } } catch (Exception e) { log.error( JSONUtil.getJSON( ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), response.getResponse())) .build())); return ResponseEntity.status(response.getHttpstatus()) .body("{\"messages\":[\"" + "Certificate unavailable" + "\"]}"); } if (!HttpStatus.OK.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()) .body("{\"errors\":[\"" + "Certificate unavailable" + "\"]}"); } JsonParser jsonParser = new JsonParser(); ObjectMapper objMapper = new ObjectMapper(); JsonObject object = ((JsonObject) jsonParser.parse(response.getResponse())).getAsJsonObject("data"); JsonObject dataObject = ((JsonObject) jsonParser.parse(dataResponse.getResponse())).getAsJsonObject("data"); metaDataParams = new Gson().fromJson(object.toString(), Map.class); if(certOwnerEmailId.equalsIgnoreCase(metaDataParams.get("certOwnerEmailId"))) { isValidEmail=false; } if(dataObject!=null) { dataMetaDataParams = new Gson().fromJson(dataObject.toString(), Map.class); dataMetaDataParams.put("certOwnerNtid", certOwnerNtId); dataMetaDataParams.put("certOwnerEmailId", certOwnerEmailId); } if((Objects.nonNull(metaDataParams)) && (Objects.nonNull(metaDataParams.get("requestStatus"))) && metaDataParams.get("requestStatus").equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Certificate may not be approved or rejected from NCLM\"]}"); } if(!isValidEmail) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "New owner email id should not be same as owner email id" + "\"]}"); } String certificateUser = metaDataParams.get("certOwnerNtid"); boolean sslMetaDataUpdationStatus; metaDataParams.put("certOwnerEmailId", certOwnerEmailId); metaDataParams.put("certOwnerNtid", certOwnerNtId); certificateRequest.setCertificateName(certName); certificateRequest.setCertType(certType); certificateRequest.setCertOwnerEmailId(certOwnerEmailId); certificateRequest.setCertOwnerNtid(certOwnerNtId); try { if (userDetails.isAdmin()) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, token); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, token); } } else { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, userDetails.getSelfSupportToken()); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, userDetails.getSelfSupportToken()); } } if (sslMetaDataUpdationStatus) { boolean isPoliciesCreated=true; removeSudoPermissionForPreviousOwner( certificateUser.toLowerCase(), certName,userDetails,certType); addSudoPermissionToCertificateOwner(certificateRequest, userDetails, enrollResponse, isPoliciesCreated, true,token,"transfer"); sendTransferEmail(metaDataParams,certificateRequest.getCertOwnerNtid(),certificateUser); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "sendTransferEmail"). put(LogMessage.MESSAGE, String.format("Successfully sent Transfer email notification oldOwner= " + "[%s] newOwner =[%s] for certificate [%s] on date = [%s]", getUserEmail(getUserDetails(certificateUser)), getUserEmail(getUserDetails(certificateRequest.getCertOwnerNtid())) , metaDataParams.get("certificateName"),java.time.LocalDateTime.now())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK) .body("{\"messages\":[\"" + "Certificate Owner Transferred Successfully" + "\"]}"); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Certificate owner Transfer failed for CertificateID") .put(LogMessage.STATUS, HttpStatus.INTERNAL_SERVER_ERROR.toString()) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()) .build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "Certificate owner Transfer failed" + "\"]}"); } } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Inside Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), e.getMessage())) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"errors\":[\"" + e.getMessage() + "\"]}"); } }  ResponseEntity<String> authenticate(CertManagerLoginRequest certManagerLoginRequest); CertManagerLogin login(CertManagerLoginRequest certManagerLoginRequest); ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest,
                                                               UserDetails userDetails ,String token); ResponseEntity<String> getServiceCertificates(String token, UserDetails userDetails, String certName, Integer limit, Integer offset, String certType); String getNclmToken(); ResponseEntity<String> getTargetSystemList(String token, UserDetails userDetails,String certType); ResponseEntity<String> getTargetSystemServiceList(String token, UserDetails userDetails, String targetSystemId); ResponseEntity<String> getRevocationReasons(Integer certificateId, String token); ResponseEntity<String> issueRevocationRequest(String certType, String certificateName, UserDetails userDetails, String token,
			RevocationRequest revocationRequest); String getCurrentLocalDateTimeStamp(); Boolean validateOwnerPermissionForNonAdmin(UserDetails userDetails, String certificateName); ResponseEntity<String> addUserToCertificate(CertificateUser certificateUser, UserDetails userDetails, boolean addSudoPermission); ResponseEntity<String> addGroupToCertificate(UserDetails userDetails, String userToken, CertificateGroup certificateGroup); boolean isAuthorized(UserDetails userDetails, String certificatename, String certType); ResponseEntity<String> addingGroupToCertificate(String token, CertificateGroup certificateGroup, UserDetails userDetails); ResponseEntity<String> associateApproletoCertificate(CertificateApprole certificateApprole, UserDetails userDetails); boolean hasDownloadPermission(String certificateName, UserDetails userDetails, String certType); SSLCertificateMetadataDetails getCertificateMetadata(String token, String certificateName); ResponseEntity<InputStreamResource> downloadCertificateWithPrivateKey(String token, CertificateDownloadRequest certificateDownloadRequest, UserDetails userDetails); ResponseEntity<InputStreamResource> downloadCertificateWithPrivateKey(CertificateDownloadRequest certificateDownloadRequest, SSLCertificateMetadataDetails sslCertificateMetadataDetails); ResponseEntity<InputStreamResource> downloadCertificate(String token, UserDetails userDetails,
                                                                   String certificateName, String certificateType,
                                                                   String sslCertType); ResponseEntity<String> getCertificateDetails(String token, String certificateName, String certificateType); ResponseEntity<String> renewCertificate(String certType, String certificateName, UserDetails userDetails, String token); ResponseEntity<String> removeUserFromCertificate(CertificateUser certificateUser, UserDetails userDetails); ResponseEntity<String> removeGroupFromCertificate(CertificateGroup certificateGroup, UserDetails userDetails); ResponseEntity<String> getListOfCertificates(String token, String certificateType); ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId,  UserDetails userDetails); ResponseEntity<String> validateApprovalStatusAndGetCertificateDetails(String certName, String certType,
			UserDetails userDetails); ResponseEntity<String> deleteCertificate( String token, String certType, String certificateName, UserDetails userDetails); Boolean validateCertOwnerPermissionForNonAdmin(UserDetails userDetails, String certificateName, String certType); ResponseEntity<String> getAllCertificates(String token, String certName, Integer limit, Integer offset); ResponseEntity<String> getAllCertificatesOnCertType(UserDetails userDetails, String certificateType); ResponseEntity<String> checkCertificateStatus(String certName, String certType,	
			UserDetails userDetails); ResponseEntity<String> getAllSelfServiceGroups(UserDetails userDetails); ResponseEntity<String> removeGroupFromCertificateForDelete(CertificateGroup certificateGroup, UserDetails userDetails); }
SSLCertificateService { public ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId, UserDetails userDetails) throws Exception { Map<String, String> metaDataParams = new HashMap<String, String>(); Map<String, String> dataMetaDataParams = new HashMap<String, String>(); SSLCertificateRequest certificateRequest = new SSLCertificateRequest(); token = userDetails.getSelfSupportToken(); boolean isValidEmail = true; String certOwnerNtId =""; Object[] users = null; DirectoryUser dirUser = new DirectoryUser(); if (!isValidInputs(certName, certType) || !validateCertficateEmail(certOwnerEmailId)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "transferCertificate") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } ResponseEntity<DirectoryObjects> userResponse = directoryService.searchByUPN(certOwnerEmailId); if(userResponse.getStatusCode().equals(HttpStatus.OK)) { users = userResponse.getBody().getData().getValues(); if(!ObjectUtils.isEmpty(users)) { dirUser = (DirectoryUser) users[0]; certOwnerNtId = dirUser.getUserName(); } } if(certOwnerNtId==""){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Invalid user inputs") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"User unavailable\"]}"); } String endPoint = certName; CertResponse enrollResponse = new CertResponse(); String metaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH + "/" + endPoint :SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH + "/" + endPoint; String permissionMetaDataPath = (certType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH_VALUE + endPoint :SSLCertificateConstants.SSL_CERT_PATH_VALUE_EXT + endPoint; Response response = new Response(); Response dataResponse = new Response(); if (!userDetails.isAdmin()) { Boolean isPermission = validateCertOwnerPermissionForNonAdmin(userDetails, certName, certType); if (!isPermission) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("{\"errors\":[\"" + "Access denied: No permission to transfer the ownership of this certificate" + "\"]}"); } } try { if (userDetails.isAdmin()) { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", token); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", token); } else { response = reqProcessor.process("/read", "{\"path\":\"" + metaDataPath + "\"}", userDetails.getSelfSupportToken()); dataResponse = reqProcessor.process("/read", "{\"path\":\"" + permissionMetaDataPath + "\"}", userDetails.getSelfSupportToken()); } } catch (Exception e) { log.error( JSONUtil.getJSON( ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), response.getResponse())) .build())); return ResponseEntity.status(response.getHttpstatus()) .body("{\"messages\":[\"" + "Certificate unavailable" + "\"]}"); } if (!HttpStatus.OK.equals(response.getHttpstatus())) { return ResponseEntity.status(response.getHttpstatus()) .body("{\"errors\":[\"" + "Certificate unavailable" + "\"]}"); } JsonParser jsonParser = new JsonParser(); ObjectMapper objMapper = new ObjectMapper(); JsonObject object = ((JsonObject) jsonParser.parse(response.getResponse())).getAsJsonObject("data"); JsonObject dataObject = ((JsonObject) jsonParser.parse(dataResponse.getResponse())).getAsJsonObject("data"); metaDataParams = new Gson().fromJson(object.toString(), Map.class); if(certOwnerEmailId.equalsIgnoreCase(metaDataParams.get("certOwnerEmailId"))) { isValidEmail=false; } if(dataObject!=null) { dataMetaDataParams = new Gson().fromJson(dataObject.toString(), Map.class); dataMetaDataParams.put("certOwnerNtid", certOwnerNtId); dataMetaDataParams.put("certOwnerEmailId", certOwnerEmailId); } if((Objects.nonNull(metaDataParams)) && (Objects.nonNull(metaDataParams.get("requestStatus"))) && metaDataParams.get("requestStatus").equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL)) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"Certificate may not be approved or rejected from NCLM\"]}"); } if(!isValidEmail) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "New owner email id should not be same as owner email id" + "\"]}"); } String certificateUser = metaDataParams.get("certOwnerNtid"); boolean sslMetaDataUpdationStatus; metaDataParams.put("certOwnerEmailId", certOwnerEmailId); metaDataParams.put("certOwnerNtid", certOwnerNtId); certificateRequest.setCertificateName(certName); certificateRequest.setCertType(certType); certificateRequest.setCertOwnerEmailId(certOwnerEmailId); certificateRequest.setCertOwnerNtid(certOwnerNtId); try { if (userDetails.isAdmin()) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, token); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, token); } } else { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(metaDataPath, metaDataParams, userDetails.getSelfSupportToken()); if(dataObject!=null) { sslMetaDataUpdationStatus = ControllerUtil.updateMetaData(permissionMetaDataPath, dataMetaDataParams, userDetails.getSelfSupportToken()); } } if (sslMetaDataUpdationStatus) { boolean isPoliciesCreated=true; removeSudoPermissionForPreviousOwner( certificateUser.toLowerCase(), certName,userDetails,certType); addSudoPermissionToCertificateOwner(certificateRequest, userDetails, enrollResponse, isPoliciesCreated, true,token,"transfer"); sendTransferEmail(metaDataParams,certificateRequest.getCertOwnerNtid(),certificateUser); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, "sendTransferEmail"). put(LogMessage.MESSAGE, String.format("Successfully sent Transfer email notification oldOwner= " + "[%s] newOwner =[%s] for certificate [%s] on date = [%s]", getUserEmail(getUserDetails(certificateUser)), getUserEmail(getUserDetails(certificateRequest.getCertOwnerNtid())) , metaDataParams.get("certificateName"),java.time.LocalDateTime.now())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(HttpStatus.OK) .body("{\"messages\":[\"" + "Certificate Owner Transferred Successfully" + "\"]}"); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, "updateCertOwner") .put(LogMessage.MESSAGE, "Certificate owner Transfer failed for CertificateID") .put(LogMessage.STATUS, HttpStatus.INTERNAL_SERVER_ERROR.toString()) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()) .build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("{\"errors\":[\"" + "Certificate owner Transfer failed" + "\"]}"); } } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format("Inside Exception = [%s] = Message [%s]", Arrays.toString(e.getStackTrace()), e.getMessage())) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"errors\":[\"" + e.getMessage() + "\"]}"); } }  ResponseEntity<String> authenticate(CertManagerLoginRequest certManagerLoginRequest); CertManagerLogin login(CertManagerLoginRequest certManagerLoginRequest); ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest,
                                                               UserDetails userDetails ,String token); ResponseEntity<String> getServiceCertificates(String token, UserDetails userDetails, String certName, Integer limit, Integer offset, String certType); String getNclmToken(); ResponseEntity<String> getTargetSystemList(String token, UserDetails userDetails,String certType); ResponseEntity<String> getTargetSystemServiceList(String token, UserDetails userDetails, String targetSystemId); ResponseEntity<String> getRevocationReasons(Integer certificateId, String token); ResponseEntity<String> issueRevocationRequest(String certType, String certificateName, UserDetails userDetails, String token,
			RevocationRequest revocationRequest); String getCurrentLocalDateTimeStamp(); Boolean validateOwnerPermissionForNonAdmin(UserDetails userDetails, String certificateName); ResponseEntity<String> addUserToCertificate(CertificateUser certificateUser, UserDetails userDetails, boolean addSudoPermission); ResponseEntity<String> addGroupToCertificate(UserDetails userDetails, String userToken, CertificateGroup certificateGroup); boolean isAuthorized(UserDetails userDetails, String certificatename, String certType); ResponseEntity<String> addingGroupToCertificate(String token, CertificateGroup certificateGroup, UserDetails userDetails); ResponseEntity<String> associateApproletoCertificate(CertificateApprole certificateApprole, UserDetails userDetails); boolean hasDownloadPermission(String certificateName, UserDetails userDetails, String certType); SSLCertificateMetadataDetails getCertificateMetadata(String token, String certificateName); ResponseEntity<InputStreamResource> downloadCertificateWithPrivateKey(String token, CertificateDownloadRequest certificateDownloadRequest, UserDetails userDetails); ResponseEntity<InputStreamResource> downloadCertificateWithPrivateKey(CertificateDownloadRequest certificateDownloadRequest, SSLCertificateMetadataDetails sslCertificateMetadataDetails); ResponseEntity<InputStreamResource> downloadCertificate(String token, UserDetails userDetails,
                                                                   String certificateName, String certificateType,
                                                                   String sslCertType); ResponseEntity<String> getCertificateDetails(String token, String certificateName, String certificateType); ResponseEntity<String> renewCertificate(String certType, String certificateName, UserDetails userDetails, String token); ResponseEntity<String> removeUserFromCertificate(CertificateUser certificateUser, UserDetails userDetails); ResponseEntity<String> removeGroupFromCertificate(CertificateGroup certificateGroup, UserDetails userDetails); ResponseEntity<String> getListOfCertificates(String token, String certificateType); ResponseEntity<String> updateCertOwner(String token, String certType,String certName,String certOwnerEmailId,  UserDetails userDetails); ResponseEntity<String> validateApprovalStatusAndGetCertificateDetails(String certName, String certType,
			UserDetails userDetails); ResponseEntity<String> deleteCertificate( String token, String certType, String certificateName, UserDetails userDetails); Boolean validateCertOwnerPermissionForNonAdmin(UserDetails userDetails, String certificateName, String certType); ResponseEntity<String> getAllCertificates(String token, String certName, Integer limit, Integer offset); ResponseEntity<String> getAllCertificatesOnCertType(UserDetails userDetails, String certificateType); ResponseEntity<String> checkCertificateStatus(String certName, String certType,	
			UserDetails userDetails); ResponseEntity<String> getAllSelfServiceGroups(UserDetails userDetails); ResponseEntity<String> removeGroupFromCertificateForDelete(CertificateGroup certificateGroup, UserDetails userDetails); @Value("${sslcertmanager.targetsystemgroup.public_single_san.ts_gp_id}")
    private int public_single_san_ts_gp_id; @Value("${sslcertmanager.targetsystemgroup.public_multi_san.ts_gp_id}")
    private int public_multi_san_ts_gp_id; }