@Test public void generateSSLCertificate_With_PolicyFailure() throws Exception { String jsonStr = "{ \"username\": \"testusername1\", \"password\": \"testpassword1\"}"; CertManagerLoginRequest certManagerLoginRequest = getCertManagerLoginRequest(); certManagerLoginRequest.setUsername("username"); certManagerLoginRequest.setPassword("password"); SSLCertificateRequest sslCertificateRequest = getSSLCertificateRequest(); sslCertificateRequest.setCertificateName("certificatename"); String[] dnsNames = { }; sslCertificateRequest.setDnsList(dnsNames); 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"); CertResponse response = new CertResponse(); response.setHttpstatus(HttpStatus.OK); response.setResponse(jsonStr); response.setSuccess(true); when(reqProcessor.processCert(eq("/auth/certmanager/login"), anyObject(), anyString(), anyString())).thenReturn(response); when(reqProcessor.processCert(eq("/certmanager/findCertificate"), anyObject(), anyString(), anyString())).thenReturn(response); CertResponse response1 = new CertResponse(); response1.setHttpstatus(HttpStatus.OK); response1.setResponse(jsonStr); response1.setSuccess(true); when(reqProcessor.processCert(eq("/certmanager/findTargetSystem"), anyObject(), anyString(), anyString())).thenReturn(response1); String createTargetSystemResponse = "{ \"name\": \"TARGET SYSTEM1\", \"password\": \"testpassword1\"}"; response1.setResponse(createTargetSystemResponse); Map<String, Object> createTargetSystemMap = new HashMap<>(); createTargetSystemMap.put("targetSystemID", 29); createTargetSystemMap.put("name", "TARGETSYSTEM1"); createTargetSystemMap.put("description", "TARGETSYSTEM1"); createTargetSystemMap.put("address", "address"); when(ControllerUtil.parseJson(createTargetSystemResponse)).thenReturn(createTargetSystemMap); when(reqProcessor.processCert(eq("/certmanager/targetsystem/create"), anyObject(), anyString(), anyString())).thenReturn(response1); CertResponse response2 = new CertResponse(); String jsonStr1 = "{ \"name\": \"targetService\", \"address\": \"targetServiceaddress\"}"; response2.setHttpstatus(HttpStatus.OK); response2.setResponse(jsonStr1); response2.setSuccess(true); when(reqProcessor.processCert(eq("/certmanager/findTargetSystemService"), anyObject(), anyString(), anyString())).thenReturn(response2); String createTargetSystemServiceResponse = "{ \"name\": \"TARGETSYSTEMService\", \"password\": , \"testpassword1\"}"; response2.setResponse(createTargetSystemServiceResponse); Map<String, Object> createTargetSystemServiceMap = new HashMap<>(); createTargetSystemServiceMap.put("targetSystemServiceId", 40); createTargetSystemServiceMap.put("hostname", "TARGETSYSTEMSERVICEHOST"); createTargetSystemServiceMap.put("name", "TARGETSYSTEMSERVICE"); createTargetSystemServiceMap.put("port", 443); createTargetSystemServiceMap.put("targetSystemGroupId", 11); createTargetSystemServiceMap.put("targetSystemId", 12); UserDetails userDetails1 = new UserDetails(); token = "5PDrOhsy4ig8L3EpsJZSLAMg"; userDetails1.setUsername("admin"); userDetails1.setAdmin(true); userDetails1.setClientToken(token); userDetails1.setSelfSupportToken(token); Response responseNoContent = getMockResponse(HttpStatus.INTERNAL_SERVER_ERROR, true, ""); when(ControllerUtil.createMetadata(Mockito.any(), any())).thenReturn(true); when(reqProcessor.process(eq("/access/update"),any(),eq(token))).thenReturn(responseNoContent); when(ControllerUtil.parseJson(createTargetSystemServiceResponse)).thenReturn(createTargetSystemServiceMap); when(reqProcessor.processCert(eq("/certmanager/targetsystemservice/create"), anyObject(), anyString(), anyString())).thenReturn(response2); when(reqProcessor.processCert(eq("/certmanager/getEnrollCA"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCAResponse()); when(reqProcessor.processCert(eq("/certmanager/putEnrollCA"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCAResponse()); when(reqProcessor.processCert(eq("/certmanager/getEnrollTemplates"), anyObject(), anyString(), anyString())).thenReturn(getEnrollTemplateResponse()); when(reqProcessor.processCert(eq("/certmanager/putEnrollTemplates"), anyObject(), anyString(), anyString())).thenReturn(getEnrollTemplateResponse()); when(reqProcessor.processCert(eq("/certmanager/getEnrollkeys"), anyObject(), anyString(), anyString())).thenReturn(getEnrollKeysResponse()); when(reqProcessor.processCert(eq("/certmanager/putEnrollKeys"), anyObject(), anyString(), anyString())).thenReturn(getEnrollKeysResponse()); when(reqProcessor.processCert(eq("/certmanager/getEnrollCSR"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCSRResponse()); when(reqProcessor.processCert(eq("/certmanager/putEnrollCSR"), anyObject(), anyString(), anyString())).thenReturn(getEnrollCSRResponse()); when(reqProcessor.processCert(eq("/certmanager/enroll"), anyObject(), anyString(), anyString())).thenReturn(getEnrollResonse()); ResponseEntity<?> enrollResponse = sSLCertificateService.generateSSLCertificate(sslCertificateRequest,userDetails1,token); assertNotNull(enrollResponse); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, enrollResponse.getStatusCode()); }
public ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest, UserDetails userDetails ,String token) { CertResponse enrollResponse = new CertResponse(); boolean isValidData = validateInputData(sslCertificateRequest, userDetails); if (!isValidData) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } else { String applicationName = getValidApplicationName(sslCertificateRequest); if (!StringUtils.isEmpty(applicationName)) { populateSSLCertificateRequest(sslCertificateRequest, applicationName); } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } } try { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("CERTIFICATE REQUEST [%s]", sslCertificateRequest.toString())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String username = (Objects.nonNull(ControllerUtil.getNclmUsername())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmUsername()))) : (new String(Base64.getDecoder().decode(certManagerUsername))); String password = (Objects.nonNull(ControllerUtil.getNclmPassword())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmPassword()))) : (new String(Base64.getDecoder().decode(certManagerPassword))); CertManagerLoginRequest certManagerLoginRequest = new CertManagerLoginRequest(username, password); CertManagerLogin certManagerLogin = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? login(certManagerLoginRequest):nclmMockUtil.getMockLoginDetails(); if(ObjectUtils.isEmpty(certManagerLogin)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } CertificateData certificateDetails = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? getCertificate(sslCertificateRequest, certManagerLogin):null; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Details for Certificate name =[%s] = Certificate Details " + "[%s]", sslCertificateRequest.getCertificateName(), certificateDetails)). build())); token = (userDetails.isAdmin())?token : userDetails.getSelfSupportToken(); if (Objects.isNull(certificateDetails)) { if (sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { SSLCertificateMetadataDetails certMetaData =certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); if ((Objects.nonNull(certMetaData)) && (Objects.nonNull(certMetaData.getRequestStatus())) && (certMetaData.getRequestStatus().equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL))) { String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " requested by "+ certMetaData.getCertOwnerEmailId() + " and it's " + "waiting for approval" ; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Given Certificate is waiting for NCLM approval " + "[%s] = Certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } if(!isMockingEnabled(sslCertificateRequest.getCertType())) { int targetSystemId = getTargetSystem(sslCertificateRequest, certManagerLogin); if (targetSystemId == 0) { targetSystemId = createTargetSystem(sslCertificateRequest.getTargetSystem(), certManagerLogin, getContainerId(sslCertificateRequest)); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Completed Successfully [%s] for " + "certificate name = [%s]", targetSystemId, sslCertificateRequest.getCertificateName())). build())); if (targetSystemId == 0) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } int targetSystemServiceId = getTargetSystemServiceId(sslCertificateRequest, targetSystemId, certManagerLogin); if (targetSystemServiceId == 0) { TargetSystemServiceRequest targetSystemServiceRequest = prepareTargetSystemServiceRequest(sslCertificateRequest); TargetSystemService targetSystemService = createTargetSystemService(targetSystemServiceRequest, targetSystemId, certManagerLogin); if (Objects.nonNull(targetSystemService)) { targetSystemServiceId = targetSystemService.getTargetSystemServiceId(); } else { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Service Completed Successfully " + "[%s] = certificate name = [%s] ", targetSystemService.toString(), sslCertificateRequest.getCertificateName())). put(LogMessage.MESSAGE, String.format("Target System Service ID [%s]", targetSystemService.getTargetSystemServiceId())). build())); } CertResponse response = getEnrollCA(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { response = prepareEnrollCARequest(response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = " + "For External-MultiSAN-certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); } int updatedSelectedId = putEnrollCA(certManagerLogin, targetSystemServiceId, response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll CA Successfully Completed[%s] = certificate " + "name = [%s]", updatedSelectedId, sslCertificateRequest.getCertificateName())). build())); CertResponse templateResponse = getEnrollTemplates(certManagerLogin, targetSystemServiceId, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Get Enrollment template Completed Successfully [%s] = certificate name = [%s]", templateResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { templateResponse = prepareTemplateReqForExternalCert(templateResponse); } int enrollTemplateId = putEnrollTemplates(certManagerLogin, targetSystemServiceId, templateResponse, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll template Successfully Completed = " + "enrollTemplateId = [%s] = certificate name = [%s]", enrollTemplateId, sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { CertResponse getTemplateResponse = getTemplateParametersResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("GetTemplateParameters Successfully Completed = " + "getTemplateParamterRequest = [%s] = certificate name = [%s] ", getTemplateResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); if (sslCertificateRequest.getDnsList().length > 0) { getTemplateResponse = updateRequestWithRequestedDetails(getTemplateResponse); } CertResponse putTemplateParameterResponse = putTemplateParameterResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId, getTemplateResponse); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PUt TemplateParameters Successfully Completed = " + "putTemplateParamterResponse = [%s] = certificate name = [%s] ", putTemplateParameterResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); } CertResponse getEnrollKeyResponse = getEnrollKeys(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollKeys Completed Successfully [%s] = certificate name = [%s]", getEnrollKeyResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); int enrollKeyId = putEnrollKeys(certManagerLogin, targetSystemServiceId, getEnrollKeyResponse, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("putEnrollKeys Successfully Completed[%s] = certificate name = [%s]", enrollKeyId, sslCertificateRequest.getCertificateName())). build())); String updatedRequest = getEnrollCSR(certManagerLogin, targetSystemServiceId, enrollTemplateId, sslCertificateRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollCSRResponse Completed Successfully [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); if (sslCertificateRequest.getDnsList() != null && sslCertificateRequest.getDnsList().length > 0) { updatedRequest = buildSubjectAlternativeNameRequest(updatedRequest, sslCertificateRequest, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Build Object with DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } else { updatedRequest = removeSubjectAlternativeNameRequest(updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Remove DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } CertResponse putEnrollCSRResponse = putEnrollCSR(certManagerLogin, targetSystemServiceId, updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PutEnroll CSR Successfully Completed = [%s] = certificate name = [%s]", putEnrollCSRResponse, sslCertificateRequest.getCertificateName())). build())); String responseDetails = validateCSRResponse(putEnrollCSRResponse.getResponse()); if (!StringUtils.isEmpty(responseDetails)) { enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseDetails); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Exception While creating certificate " + "[%s] = certificate name = [%s]", responseDetails, sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } enrollResponse = enrollCertificate(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Enroll Certificate response Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); } else { enrollResponse = nclmMockUtil.getEnrollMockResponse(); } int actionId = 0; if (enrollResponse.getHttpstatus().equals(HttpStatus.ACCEPTED) && Objects.nonNull(enrollResponse.getResponse())) { Map<String, Object> responseMap = ControllerUtil.parseJson(enrollResponse.getResponse()); if (!MapUtils.isEmpty(responseMap) && responseMap.get("actionId") != null) { actionId = (Integer) responseMap.get("actionId"); if (actionId != 0) { enrollResponse = approvalRequest(certManagerLogin, actionId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("approvalRequest Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(),sslCertificateRequest.getCertificateName())). build())); } } } if ( (HttpStatus.OK.equals(enrollResponse.getHttpstatus()) || (HttpStatus.NO_CONTENT.equals(enrollResponse.getHttpstatus())))) { boolean isPoliciesCreated; if (userDetails.isAdmin()) { isPoliciesCreated = createPolicies(sslCertificateRequest, token); } else { isPoliciesCreated = createPolicies(sslCertificateRequest, userDetails.getSelfSupportToken()); } if(isPoliciesCreated) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Policies are created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } String metadataJson = populateSSLCertificateMetadata(sslCertificateRequest, userDetails, certManagerLogin,actionId); boolean sslMetaDataCreationStatus; if (userDetails.isAdmin()) { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); } else { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, userDetails.getSelfSupportToken()); } if (sslMetaDataCreationStatus) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Metadata created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } if ((!isPoliciesCreated) || (!sslMetaDataCreationStatus)) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); log.error(JSONUtil.getJSON(ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format( "Metadata or Policies failed for SSL certificate name [%s] - metaDataStatus[%s] - policyStatus[%s]", sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus, isPoliciesCreated)) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\""+enrollResponse.getResponse()+"\"]}"); } else { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "addSudoPermissionToCertificateOwner- STARTED "). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return addSudoPermissionToCertificateOwner(sslCertificateRequest, userDetails, enrollResponse , isPoliciesCreated, sslMetaDataCreationStatus,token,"create"); } } } else { SSLCertificateMetadataDetails certMetaDataDetails = certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " available in system and owned by "+ certMetaDataDetails.getCertOwnerEmailId() +" " + ". Please try with different certificate name"; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Certificate Already Available in NCLM with Active Status " + "[%s] = certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\""+enrollResponse.getResponse()+ "\"]}"); } } catch (TVaultValidationException tex) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Inside TVaultValidationException " + "Exception = [%s] = Message [%s] = certificate name = [%s]", Arrays.toString(tex.getStackTrace()), tex.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + tex.getMessage() + "\"]}"); } 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 " + "Exception = [%s] = Message [%s] = = certificate name = [%s]", Arrays.toString(e.getStackTrace()), e.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body ("{\"errors\":[\"" + SSLCertificateConstants.SSL_CREATE_EXCEPTION + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, String.format ("certificate [%s] before sending an email ", sslCertificateRequest.getCertificateName())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\""+SSLCertificateConstants.SSL_CERT_SUCCESS+"\"]}"); }
SSLCertificateService { public ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest, UserDetails userDetails ,String token) { CertResponse enrollResponse = new CertResponse(); boolean isValidData = validateInputData(sslCertificateRequest, userDetails); if (!isValidData) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } else { String applicationName = getValidApplicationName(sslCertificateRequest); if (!StringUtils.isEmpty(applicationName)) { populateSSLCertificateRequest(sslCertificateRequest, applicationName); } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } } try { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("CERTIFICATE REQUEST [%s]", sslCertificateRequest.toString())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String username = (Objects.nonNull(ControllerUtil.getNclmUsername())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmUsername()))) : (new String(Base64.getDecoder().decode(certManagerUsername))); String password = (Objects.nonNull(ControllerUtil.getNclmPassword())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmPassword()))) : (new String(Base64.getDecoder().decode(certManagerPassword))); CertManagerLoginRequest certManagerLoginRequest = new CertManagerLoginRequest(username, password); CertManagerLogin certManagerLogin = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? login(certManagerLoginRequest):nclmMockUtil.getMockLoginDetails(); if(ObjectUtils.isEmpty(certManagerLogin)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } CertificateData certificateDetails = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? getCertificate(sslCertificateRequest, certManagerLogin):null; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Details for Certificate name =[%s] = Certificate Details " + "[%s]", sslCertificateRequest.getCertificateName(), certificateDetails)). build())); token = (userDetails.isAdmin())?token : userDetails.getSelfSupportToken(); if (Objects.isNull(certificateDetails)) { if (sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { SSLCertificateMetadataDetails certMetaData =certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); if ((Objects.nonNull(certMetaData)) && (Objects.nonNull(certMetaData.getRequestStatus())) && (certMetaData.getRequestStatus().equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL))) { String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " requested by "+ certMetaData.getCertOwnerEmailId() + " and it's " + "waiting for approval" ; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Given Certificate is waiting for NCLM approval " + "[%s] = Certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } if(!isMockingEnabled(sslCertificateRequest.getCertType())) { int targetSystemId = getTargetSystem(sslCertificateRequest, certManagerLogin); if (targetSystemId == 0) { targetSystemId = createTargetSystem(sslCertificateRequest.getTargetSystem(), certManagerLogin, getContainerId(sslCertificateRequest)); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Completed Successfully [%s] for " + "certificate name = [%s]", targetSystemId, sslCertificateRequest.getCertificateName())). build())); if (targetSystemId == 0) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } int targetSystemServiceId = getTargetSystemServiceId(sslCertificateRequest, targetSystemId, certManagerLogin); if (targetSystemServiceId == 0) { TargetSystemServiceRequest targetSystemServiceRequest = prepareTargetSystemServiceRequest(sslCertificateRequest); TargetSystemService targetSystemService = createTargetSystemService(targetSystemServiceRequest, targetSystemId, certManagerLogin); if (Objects.nonNull(targetSystemService)) { targetSystemServiceId = targetSystemService.getTargetSystemServiceId(); } else { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Service Completed Successfully " + "[%s] = certificate name = [%s] ", targetSystemService.toString(), sslCertificateRequest.getCertificateName())). put(LogMessage.MESSAGE, String.format("Target System Service ID [%s]", targetSystemService.getTargetSystemServiceId())). build())); } CertResponse response = getEnrollCA(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { response = prepareEnrollCARequest(response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = " + "For External-MultiSAN-certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); } int updatedSelectedId = putEnrollCA(certManagerLogin, targetSystemServiceId, response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll CA Successfully Completed[%s] = certificate " + "name = [%s]", updatedSelectedId, sslCertificateRequest.getCertificateName())). build())); CertResponse templateResponse = getEnrollTemplates(certManagerLogin, targetSystemServiceId, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Get Enrollment template Completed Successfully [%s] = certificate name = [%s]", templateResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { templateResponse = prepareTemplateReqForExternalCert(templateResponse); } int enrollTemplateId = putEnrollTemplates(certManagerLogin, targetSystemServiceId, templateResponse, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll template Successfully Completed = " + "enrollTemplateId = [%s] = certificate name = [%s]", enrollTemplateId, sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { CertResponse getTemplateResponse = getTemplateParametersResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("GetTemplateParameters Successfully Completed = " + "getTemplateParamterRequest = [%s] = certificate name = [%s] ", getTemplateResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); if (sslCertificateRequest.getDnsList().length > 0) { getTemplateResponse = updateRequestWithRequestedDetails(getTemplateResponse); } CertResponse putTemplateParameterResponse = putTemplateParameterResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId, getTemplateResponse); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PUt TemplateParameters Successfully Completed = " + "putTemplateParamterResponse = [%s] = certificate name = [%s] ", putTemplateParameterResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); } CertResponse getEnrollKeyResponse = getEnrollKeys(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollKeys Completed Successfully [%s] = certificate name = [%s]", getEnrollKeyResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); int enrollKeyId = putEnrollKeys(certManagerLogin, targetSystemServiceId, getEnrollKeyResponse, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("putEnrollKeys Successfully Completed[%s] = certificate name = [%s]", enrollKeyId, sslCertificateRequest.getCertificateName())). build())); String updatedRequest = getEnrollCSR(certManagerLogin, targetSystemServiceId, enrollTemplateId, sslCertificateRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollCSRResponse Completed Successfully [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); if (sslCertificateRequest.getDnsList() != null && sslCertificateRequest.getDnsList().length > 0) { updatedRequest = buildSubjectAlternativeNameRequest(updatedRequest, sslCertificateRequest, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Build Object with DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } else { updatedRequest = removeSubjectAlternativeNameRequest(updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Remove DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } CertResponse putEnrollCSRResponse = putEnrollCSR(certManagerLogin, targetSystemServiceId, updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PutEnroll CSR Successfully Completed = [%s] = certificate name = [%s]", putEnrollCSRResponse, sslCertificateRequest.getCertificateName())). build())); String responseDetails = validateCSRResponse(putEnrollCSRResponse.getResponse()); if (!StringUtils.isEmpty(responseDetails)) { enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseDetails); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Exception While creating certificate " + "[%s] = certificate name = [%s]", responseDetails, sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } enrollResponse = enrollCertificate(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Enroll Certificate response Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); } else { enrollResponse = nclmMockUtil.getEnrollMockResponse(); } int actionId = 0; if (enrollResponse.getHttpstatus().equals(HttpStatus.ACCEPTED) && Objects.nonNull(enrollResponse.getResponse())) { Map<String, Object> responseMap = ControllerUtil.parseJson(enrollResponse.getResponse()); if (!MapUtils.isEmpty(responseMap) && responseMap.get("actionId") != null) { actionId = (Integer) responseMap.get("actionId"); if (actionId != 0) { enrollResponse = approvalRequest(certManagerLogin, actionId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("approvalRequest Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(),sslCertificateRequest.getCertificateName())). build())); } } } if ( (HttpStatus.OK.equals(enrollResponse.getHttpstatus()) || (HttpStatus.NO_CONTENT.equals(enrollResponse.getHttpstatus())))) { boolean isPoliciesCreated; if (userDetails.isAdmin()) { isPoliciesCreated = createPolicies(sslCertificateRequest, token); } else { isPoliciesCreated = createPolicies(sslCertificateRequest, userDetails.getSelfSupportToken()); } if(isPoliciesCreated) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Policies are created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } String metadataJson = populateSSLCertificateMetadata(sslCertificateRequest, userDetails, certManagerLogin,actionId); boolean sslMetaDataCreationStatus; if (userDetails.isAdmin()) { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); } else { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, userDetails.getSelfSupportToken()); } if (sslMetaDataCreationStatus) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Metadata created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } if ((!isPoliciesCreated) || (!sslMetaDataCreationStatus)) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); log.error(JSONUtil.getJSON(ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format( "Metadata or Policies failed for SSL certificate name [%s] - metaDataStatus[%s] - policyStatus[%s]", sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus, isPoliciesCreated)) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\""+enrollResponse.getResponse()+"\"]}"); } else { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "addSudoPermissionToCertificateOwner- STARTED "). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return addSudoPermissionToCertificateOwner(sslCertificateRequest, userDetails, enrollResponse , isPoliciesCreated, sslMetaDataCreationStatus,token,"create"); } } } else { SSLCertificateMetadataDetails certMetaDataDetails = certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " available in system and owned by "+ certMetaDataDetails.getCertOwnerEmailId() +" " + ". Please try with different certificate name"; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Certificate Already Available in NCLM with Active Status " + "[%s] = certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\""+enrollResponse.getResponse()+ "\"]}"); } } catch (TVaultValidationException tex) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Inside TVaultValidationException " + "Exception = [%s] = Message [%s] = certificate name = [%s]", Arrays.toString(tex.getStackTrace()), tex.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + tex.getMessage() + "\"]}"); } 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 " + "Exception = [%s] = Message [%s] = = certificate name = [%s]", Arrays.toString(e.getStackTrace()), e.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body ("{\"errors\":[\"" + SSLCertificateConstants.SSL_CREATE_EXCEPTION + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, String.format ("certificate [%s] before sending an email ", sslCertificateRequest.getCertificateName())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\""+SSLCertificateConstants.SSL_CERT_SUCCESS+"\"]}"); } }
SSLCertificateService { public ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest, UserDetails userDetails ,String token) { CertResponse enrollResponse = new CertResponse(); boolean isValidData = validateInputData(sslCertificateRequest, userDetails); if (!isValidData) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } else { String applicationName = getValidApplicationName(sslCertificateRequest); if (!StringUtils.isEmpty(applicationName)) { populateSSLCertificateRequest(sslCertificateRequest, applicationName); } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } } try { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("CERTIFICATE REQUEST [%s]", sslCertificateRequest.toString())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String username = (Objects.nonNull(ControllerUtil.getNclmUsername())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmUsername()))) : (new String(Base64.getDecoder().decode(certManagerUsername))); String password = (Objects.nonNull(ControllerUtil.getNclmPassword())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmPassword()))) : (new String(Base64.getDecoder().decode(certManagerPassword))); CertManagerLoginRequest certManagerLoginRequest = new CertManagerLoginRequest(username, password); CertManagerLogin certManagerLogin = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? login(certManagerLoginRequest):nclmMockUtil.getMockLoginDetails(); if(ObjectUtils.isEmpty(certManagerLogin)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } CertificateData certificateDetails = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? getCertificate(sslCertificateRequest, certManagerLogin):null; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Details for Certificate name =[%s] = Certificate Details " + "[%s]", sslCertificateRequest.getCertificateName(), certificateDetails)). build())); token = (userDetails.isAdmin())?token : userDetails.getSelfSupportToken(); if (Objects.isNull(certificateDetails)) { if (sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { SSLCertificateMetadataDetails certMetaData =certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); if ((Objects.nonNull(certMetaData)) && (Objects.nonNull(certMetaData.getRequestStatus())) && (certMetaData.getRequestStatus().equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL))) { String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " requested by "+ certMetaData.getCertOwnerEmailId() + " and it's " + "waiting for approval" ; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Given Certificate is waiting for NCLM approval " + "[%s] = Certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } if(!isMockingEnabled(sslCertificateRequest.getCertType())) { int targetSystemId = getTargetSystem(sslCertificateRequest, certManagerLogin); if (targetSystemId == 0) { targetSystemId = createTargetSystem(sslCertificateRequest.getTargetSystem(), certManagerLogin, getContainerId(sslCertificateRequest)); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Completed Successfully [%s] for " + "certificate name = [%s]", targetSystemId, sslCertificateRequest.getCertificateName())). build())); if (targetSystemId == 0) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } int targetSystemServiceId = getTargetSystemServiceId(sslCertificateRequest, targetSystemId, certManagerLogin); if (targetSystemServiceId == 0) { TargetSystemServiceRequest targetSystemServiceRequest = prepareTargetSystemServiceRequest(sslCertificateRequest); TargetSystemService targetSystemService = createTargetSystemService(targetSystemServiceRequest, targetSystemId, certManagerLogin); if (Objects.nonNull(targetSystemService)) { targetSystemServiceId = targetSystemService.getTargetSystemServiceId(); } else { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Service Completed Successfully " + "[%s] = certificate name = [%s] ", targetSystemService.toString(), sslCertificateRequest.getCertificateName())). put(LogMessage.MESSAGE, String.format("Target System Service ID [%s]", targetSystemService.getTargetSystemServiceId())). build())); } CertResponse response = getEnrollCA(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { response = prepareEnrollCARequest(response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = " + "For External-MultiSAN-certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); } int updatedSelectedId = putEnrollCA(certManagerLogin, targetSystemServiceId, response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll CA Successfully Completed[%s] = certificate " + "name = [%s]", updatedSelectedId, sslCertificateRequest.getCertificateName())). build())); CertResponse templateResponse = getEnrollTemplates(certManagerLogin, targetSystemServiceId, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Get Enrollment template Completed Successfully [%s] = certificate name = [%s]", templateResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { templateResponse = prepareTemplateReqForExternalCert(templateResponse); } int enrollTemplateId = putEnrollTemplates(certManagerLogin, targetSystemServiceId, templateResponse, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll template Successfully Completed = " + "enrollTemplateId = [%s] = certificate name = [%s]", enrollTemplateId, sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { CertResponse getTemplateResponse = getTemplateParametersResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("GetTemplateParameters Successfully Completed = " + "getTemplateParamterRequest = [%s] = certificate name = [%s] ", getTemplateResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); if (sslCertificateRequest.getDnsList().length > 0) { getTemplateResponse = updateRequestWithRequestedDetails(getTemplateResponse); } CertResponse putTemplateParameterResponse = putTemplateParameterResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId, getTemplateResponse); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PUt TemplateParameters Successfully Completed = " + "putTemplateParamterResponse = [%s] = certificate name = [%s] ", putTemplateParameterResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); } CertResponse getEnrollKeyResponse = getEnrollKeys(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollKeys Completed Successfully [%s] = certificate name = [%s]", getEnrollKeyResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); int enrollKeyId = putEnrollKeys(certManagerLogin, targetSystemServiceId, getEnrollKeyResponse, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("putEnrollKeys Successfully Completed[%s] = certificate name = [%s]", enrollKeyId, sslCertificateRequest.getCertificateName())). build())); String updatedRequest = getEnrollCSR(certManagerLogin, targetSystemServiceId, enrollTemplateId, sslCertificateRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollCSRResponse Completed Successfully [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); if (sslCertificateRequest.getDnsList() != null && sslCertificateRequest.getDnsList().length > 0) { updatedRequest = buildSubjectAlternativeNameRequest(updatedRequest, sslCertificateRequest, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Build Object with DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } else { updatedRequest = removeSubjectAlternativeNameRequest(updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Remove DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } CertResponse putEnrollCSRResponse = putEnrollCSR(certManagerLogin, targetSystemServiceId, updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PutEnroll CSR Successfully Completed = [%s] = certificate name = [%s]", putEnrollCSRResponse, sslCertificateRequest.getCertificateName())). build())); String responseDetails = validateCSRResponse(putEnrollCSRResponse.getResponse()); if (!StringUtils.isEmpty(responseDetails)) { enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseDetails); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Exception While creating certificate " + "[%s] = certificate name = [%s]", responseDetails, sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } enrollResponse = enrollCertificate(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Enroll Certificate response Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); } else { enrollResponse = nclmMockUtil.getEnrollMockResponse(); } int actionId = 0; if (enrollResponse.getHttpstatus().equals(HttpStatus.ACCEPTED) && Objects.nonNull(enrollResponse.getResponse())) { Map<String, Object> responseMap = ControllerUtil.parseJson(enrollResponse.getResponse()); if (!MapUtils.isEmpty(responseMap) && responseMap.get("actionId") != null) { actionId = (Integer) responseMap.get("actionId"); if (actionId != 0) { enrollResponse = approvalRequest(certManagerLogin, actionId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("approvalRequest Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(),sslCertificateRequest.getCertificateName())). build())); } } } if ( (HttpStatus.OK.equals(enrollResponse.getHttpstatus()) || (HttpStatus.NO_CONTENT.equals(enrollResponse.getHttpstatus())))) { boolean isPoliciesCreated; if (userDetails.isAdmin()) { isPoliciesCreated = createPolicies(sslCertificateRequest, token); } else { isPoliciesCreated = createPolicies(sslCertificateRequest, userDetails.getSelfSupportToken()); } if(isPoliciesCreated) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Policies are created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } String metadataJson = populateSSLCertificateMetadata(sslCertificateRequest, userDetails, certManagerLogin,actionId); boolean sslMetaDataCreationStatus; if (userDetails.isAdmin()) { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); } else { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, userDetails.getSelfSupportToken()); } if (sslMetaDataCreationStatus) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Metadata created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } if ((!isPoliciesCreated) || (!sslMetaDataCreationStatus)) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); log.error(JSONUtil.getJSON(ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format( "Metadata or Policies failed for SSL certificate name [%s] - metaDataStatus[%s] - policyStatus[%s]", sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus, isPoliciesCreated)) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\""+enrollResponse.getResponse()+"\"]}"); } else { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "addSudoPermissionToCertificateOwner- STARTED "). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return addSudoPermissionToCertificateOwner(sslCertificateRequest, userDetails, enrollResponse , isPoliciesCreated, sslMetaDataCreationStatus,token,"create"); } } } else { SSLCertificateMetadataDetails certMetaDataDetails = certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " available in system and owned by "+ certMetaDataDetails.getCertOwnerEmailId() +" " + ". Please try with different certificate name"; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Certificate Already Available in NCLM with Active Status " + "[%s] = certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\""+enrollResponse.getResponse()+ "\"]}"); } } catch (TVaultValidationException tex) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Inside TVaultValidationException " + "Exception = [%s] = Message [%s] = certificate name = [%s]", Arrays.toString(tex.getStackTrace()), tex.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + tex.getMessage() + "\"]}"); } 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 " + "Exception = [%s] = Message [%s] = = certificate name = [%s]", Arrays.toString(e.getStackTrace()), e.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body ("{\"errors\":[\"" + SSLCertificateConstants.SSL_CREATE_EXCEPTION + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, String.format ("certificate [%s] before sending an email ", sslCertificateRequest.getCertificateName())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\""+SSLCertificateConstants.SSL_CERT_SUCCESS+"\"]}"); }  }
SSLCertificateService { public ResponseEntity<String> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest, UserDetails userDetails ,String token) { CertResponse enrollResponse = new CertResponse(); boolean isValidData = validateInputData(sslCertificateRequest, userDetails); if (!isValidData) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } else { String applicationName = getValidApplicationName(sslCertificateRequest); if (!StringUtils.isEmpty(applicationName)) { populateSSLCertificateRequest(sslCertificateRequest, applicationName); } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } } try { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("CERTIFICATE REQUEST [%s]", sslCertificateRequest.toString())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String username = (Objects.nonNull(ControllerUtil.getNclmUsername())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmUsername()))) : (new String(Base64.getDecoder().decode(certManagerUsername))); String password = (Objects.nonNull(ControllerUtil.getNclmPassword())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmPassword()))) : (new String(Base64.getDecoder().decode(certManagerPassword))); CertManagerLoginRequest certManagerLoginRequest = new CertManagerLoginRequest(username, password); CertManagerLogin certManagerLogin = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? login(certManagerLoginRequest):nclmMockUtil.getMockLoginDetails(); if(ObjectUtils.isEmpty(certManagerLogin)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } CertificateData certificateDetails = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? getCertificate(sslCertificateRequest, certManagerLogin):null; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Details for Certificate name =[%s] = Certificate Details " + "[%s]", sslCertificateRequest.getCertificateName(), certificateDetails)). build())); token = (userDetails.isAdmin())?token : userDetails.getSelfSupportToken(); if (Objects.isNull(certificateDetails)) { if (sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { SSLCertificateMetadataDetails certMetaData =certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); if ((Objects.nonNull(certMetaData)) && (Objects.nonNull(certMetaData.getRequestStatus())) && (certMetaData.getRequestStatus().equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL))) { String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " requested by "+ certMetaData.getCertOwnerEmailId() + " and it's " + "waiting for approval" ; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Given Certificate is waiting for NCLM approval " + "[%s] = Certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } if(!isMockingEnabled(sslCertificateRequest.getCertType())) { int targetSystemId = getTargetSystem(sslCertificateRequest, certManagerLogin); if (targetSystemId == 0) { targetSystemId = createTargetSystem(sslCertificateRequest.getTargetSystem(), certManagerLogin, getContainerId(sslCertificateRequest)); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Completed Successfully [%s] for " + "certificate name = [%s]", targetSystemId, sslCertificateRequest.getCertificateName())). build())); if (targetSystemId == 0) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } int targetSystemServiceId = getTargetSystemServiceId(sslCertificateRequest, targetSystemId, certManagerLogin); if (targetSystemServiceId == 0) { TargetSystemServiceRequest targetSystemServiceRequest = prepareTargetSystemServiceRequest(sslCertificateRequest); TargetSystemService targetSystemService = createTargetSystemService(targetSystemServiceRequest, targetSystemId, certManagerLogin); if (Objects.nonNull(targetSystemService)) { targetSystemServiceId = targetSystemService.getTargetSystemServiceId(); } else { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Service Completed Successfully " + "[%s] = certificate name = [%s] ", targetSystemService.toString(), sslCertificateRequest.getCertificateName())). put(LogMessage.MESSAGE, String.format("Target System Service ID [%s]", targetSystemService.getTargetSystemServiceId())). build())); } CertResponse response = getEnrollCA(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { response = prepareEnrollCARequest(response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = " + "For External-MultiSAN-certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); } int updatedSelectedId = putEnrollCA(certManagerLogin, targetSystemServiceId, response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll CA Successfully Completed[%s] = certificate " + "name = [%s]", updatedSelectedId, sslCertificateRequest.getCertificateName())). build())); CertResponse templateResponse = getEnrollTemplates(certManagerLogin, targetSystemServiceId, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Get Enrollment template Completed Successfully [%s] = certificate name = [%s]", templateResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { templateResponse = prepareTemplateReqForExternalCert(templateResponse); } int enrollTemplateId = putEnrollTemplates(certManagerLogin, targetSystemServiceId, templateResponse, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll template Successfully Completed = " + "enrollTemplateId = [%s] = certificate name = [%s]", enrollTemplateId, sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { CertResponse getTemplateResponse = getTemplateParametersResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("GetTemplateParameters Successfully Completed = " + "getTemplateParamterRequest = [%s] = certificate name = [%s] ", getTemplateResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); if (sslCertificateRequest.getDnsList().length > 0) { getTemplateResponse = updateRequestWithRequestedDetails(getTemplateResponse); } CertResponse putTemplateParameterResponse = putTemplateParameterResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId, getTemplateResponse); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PUt TemplateParameters Successfully Completed = " + "putTemplateParamterResponse = [%s] = certificate name = [%s] ", putTemplateParameterResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); } CertResponse getEnrollKeyResponse = getEnrollKeys(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollKeys Completed Successfully [%s] = certificate name = [%s]", getEnrollKeyResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); int enrollKeyId = putEnrollKeys(certManagerLogin, targetSystemServiceId, getEnrollKeyResponse, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("putEnrollKeys Successfully Completed[%s] = certificate name = [%s]", enrollKeyId, sslCertificateRequest.getCertificateName())). build())); String updatedRequest = getEnrollCSR(certManagerLogin, targetSystemServiceId, enrollTemplateId, sslCertificateRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollCSRResponse Completed Successfully [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); if (sslCertificateRequest.getDnsList() != null && sslCertificateRequest.getDnsList().length > 0) { updatedRequest = buildSubjectAlternativeNameRequest(updatedRequest, sslCertificateRequest, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Build Object with DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } else { updatedRequest = removeSubjectAlternativeNameRequest(updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Remove DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } CertResponse putEnrollCSRResponse = putEnrollCSR(certManagerLogin, targetSystemServiceId, updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PutEnroll CSR Successfully Completed = [%s] = certificate name = [%s]", putEnrollCSRResponse, sslCertificateRequest.getCertificateName())). build())); String responseDetails = validateCSRResponse(putEnrollCSRResponse.getResponse()); if (!StringUtils.isEmpty(responseDetails)) { enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseDetails); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Exception While creating certificate " + "[%s] = certificate name = [%s]", responseDetails, sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } enrollResponse = enrollCertificate(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Enroll Certificate response Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); } else { enrollResponse = nclmMockUtil.getEnrollMockResponse(); } int actionId = 0; if (enrollResponse.getHttpstatus().equals(HttpStatus.ACCEPTED) && Objects.nonNull(enrollResponse.getResponse())) { Map<String, Object> responseMap = ControllerUtil.parseJson(enrollResponse.getResponse()); if (!MapUtils.isEmpty(responseMap) && responseMap.get("actionId") != null) { actionId = (Integer) responseMap.get("actionId"); if (actionId != 0) { enrollResponse = approvalRequest(certManagerLogin, actionId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("approvalRequest Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(),sslCertificateRequest.getCertificateName())). build())); } } } if ( (HttpStatus.OK.equals(enrollResponse.getHttpstatus()) || (HttpStatus.NO_CONTENT.equals(enrollResponse.getHttpstatus())))) { boolean isPoliciesCreated; if (userDetails.isAdmin()) { isPoliciesCreated = createPolicies(sslCertificateRequest, token); } else { isPoliciesCreated = createPolicies(sslCertificateRequest, userDetails.getSelfSupportToken()); } if(isPoliciesCreated) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Policies are created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } String metadataJson = populateSSLCertificateMetadata(sslCertificateRequest, userDetails, certManagerLogin,actionId); boolean sslMetaDataCreationStatus; if (userDetails.isAdmin()) { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); } else { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, userDetails.getSelfSupportToken()); } if (sslMetaDataCreationStatus) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Metadata created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } if ((!isPoliciesCreated) || (!sslMetaDataCreationStatus)) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); log.error(JSONUtil.getJSON(ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format( "Metadata or Policies failed for SSL certificate name [%s] - metaDataStatus[%s] - policyStatus[%s]", sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus, isPoliciesCreated)) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\""+enrollResponse.getResponse()+"\"]}"); } else { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "addSudoPermissionToCertificateOwner- STARTED "). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return addSudoPermissionToCertificateOwner(sslCertificateRequest, userDetails, enrollResponse , isPoliciesCreated, sslMetaDataCreationStatus,token,"create"); } } } else { SSLCertificateMetadataDetails certMetaDataDetails = certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " available in system and owned by "+ certMetaDataDetails.getCertOwnerEmailId() +" " + ". Please try with different certificate name"; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Certificate Already Available in NCLM with Active Status " + "[%s] = certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\""+enrollResponse.getResponse()+ "\"]}"); } } catch (TVaultValidationException tex) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Inside TVaultValidationException " + "Exception = [%s] = Message [%s] = certificate name = [%s]", Arrays.toString(tex.getStackTrace()), tex.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + tex.getMessage() + "\"]}"); } 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 " + "Exception = [%s] = Message [%s] = = certificate name = [%s]", Arrays.toString(e.getStackTrace()), e.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body ("{\"errors\":[\"" + SSLCertificateConstants.SSL_CREATE_EXCEPTION + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, String.format ("certificate [%s] before sending an email ", sslCertificateRequest.getCertificateName())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\""+SSLCertificateConstants.SSL_CERT_SUCCESS+"\"]}"); }  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> generateSSLCertificate(SSLCertificateRequest sslCertificateRequest, UserDetails userDetails ,String token) { CertResponse enrollResponse = new CertResponse(); boolean isValidData = validateInputData(sslCertificateRequest, userDetails); if (!isValidData) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } else { String applicationName = getValidApplicationName(sslCertificateRequest); if (!StringUtils.isEmpty(applicationName)) { populateSSLCertificateRequest(sslCertificateRequest, applicationName); } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"Invalid input values\"]}"); } } try { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("CERTIFICATE REQUEST [%s]", sslCertificateRequest.toString())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String username = (Objects.nonNull(ControllerUtil.getNclmUsername())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmUsername()))) : (new String(Base64.getDecoder().decode(certManagerUsername))); String password = (Objects.nonNull(ControllerUtil.getNclmPassword())) ? (new String(Base64.getDecoder().decode(ControllerUtil.getNclmPassword()))) : (new String(Base64.getDecoder().decode(certManagerPassword))); CertManagerLoginRequest certManagerLoginRequest = new CertManagerLoginRequest(username, password); CertManagerLogin certManagerLogin = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? login(certManagerLoginRequest):nclmMockUtil.getMockLoginDetails(); if(ObjectUtils.isEmpty(certManagerLogin)) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "NCLM services are down. Please try after some time"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + nclmErrorMessage + "\"]}"); } CertificateData certificateDetails = (!isMockingEnabled(sslCertificateRequest.getCertType())) ? getCertificate(sslCertificateRequest, certManagerLogin):null; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Details for Certificate name =[%s] = Certificate Details " + "[%s]", sslCertificateRequest.getCertificateName(), certificateDetails)). build())); token = (userDetails.isAdmin())?token : userDetails.getSelfSupportToken(); if (Objects.isNull(certificateDetails)) { if (sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { SSLCertificateMetadataDetails certMetaData =certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); if ((Objects.nonNull(certMetaData)) && (Objects.nonNull(certMetaData.getRequestStatus())) && (certMetaData.getRequestStatus().equalsIgnoreCase(SSLCertificateConstants.REQUEST_PENDING_APPROVAL))) { String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " requested by "+ certMetaData.getCertOwnerEmailId() + " and it's " + "waiting for approval" ; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Given Certificate is waiting for NCLM approval " + "[%s] = Certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } if(!isMockingEnabled(sslCertificateRequest.getCertType())) { int targetSystemId = getTargetSystem(sslCertificateRequest, certManagerLogin); if (targetSystemId == 0) { targetSystemId = createTargetSystem(sslCertificateRequest.getTargetSystem(), certManagerLogin, getContainerId(sslCertificateRequest)); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Completed Successfully [%s] for " + "certificate name = [%s]", targetSystemId, sslCertificateRequest.getCertificateName())). build())); if (targetSystemId == 0) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } } int targetSystemServiceId = getTargetSystemServiceId(sslCertificateRequest, targetSystemId, certManagerLogin); if (targetSystemServiceId == 0) { TargetSystemServiceRequest targetSystemServiceRequest = prepareTargetSystemServiceRequest(sslCertificateRequest); TargetSystemService targetSystemService = createTargetSystemService(targetSystemServiceRequest, targetSystemId, certManagerLogin); if (Objects.nonNull(targetSystemService)) { targetSystemServiceId = targetSystemService.getTargetSystemServiceId(); } else { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("createTargetSystem Service Completed Successfully " + "[%s] = certificate name = [%s] ", targetSystemService.toString(), sslCertificateRequest.getCertificateName())). put(LogMessage.MESSAGE, String.format("Target System Service ID [%s]", targetSystemService.getTargetSystemServiceId())). build())); } CertResponse response = getEnrollCA(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { response = prepareEnrollCARequest(response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("getEnrollCA Completed Successfully [%s] = " + "For External-MultiSAN-certificate name = [%s]", response.getResponse(), sslCertificateRequest.getCertificateName())). build())); } int updatedSelectedId = putEnrollCA(certManagerLogin, targetSystemServiceId, response); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll CA Successfully Completed[%s] = certificate " + "name = [%s]", updatedSelectedId, sslCertificateRequest.getCertificateName())). build())); CertResponse templateResponse = getEnrollTemplates(certManagerLogin, targetSystemServiceId, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Get Enrollment template Completed Successfully [%s] = certificate name = [%s]", templateResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL) && sslCertificateRequest.getDnsList().length > 0) { templateResponse = prepareTemplateReqForExternalCert(templateResponse); } int enrollTemplateId = putEnrollTemplates(certManagerLogin, targetSystemServiceId, templateResponse, updatedSelectedId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("PutEnroll template Successfully Completed = " + "enrollTemplateId = [%s] = certificate name = [%s]", enrollTemplateId, sslCertificateRequest.getCertificateName())). build())); if ((!StringUtils.isEmpty(sslCertificateRequest.getCertType())) && sslCertificateRequest.getCertType().equalsIgnoreCase(SSLCertificateConstants.EXTERNAL)) { CertResponse getTemplateResponse = getTemplateParametersResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("GetTemplateParameters Successfully Completed = " + "getTemplateParamterRequest = [%s] = certificate name = [%s] ", getTemplateResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); if (sslCertificateRequest.getDnsList().length > 0) { getTemplateResponse = updateRequestWithRequestedDetails(getTemplateResponse); } CertResponse putTemplateParameterResponse = putTemplateParameterResponse(certManagerLogin, targetSystemServiceId, enrollTemplateId, getTemplateResponse); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PUt TemplateParameters Successfully Completed = " + "putTemplateParamterResponse = [%s] = certificate name = [%s] ", putTemplateParameterResponse.getResponse(), sslCertificateRequest.getCertificateName())).build())); } CertResponse getEnrollKeyResponse = getEnrollKeys(certManagerLogin, targetSystemServiceId, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollKeys Completed Successfully [%s] = certificate name = [%s]", getEnrollKeyResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); int enrollKeyId = putEnrollKeys(certManagerLogin, targetSystemServiceId, getEnrollKeyResponse, enrollTemplateId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("putEnrollKeys Successfully Completed[%s] = certificate name = [%s]", enrollKeyId, sslCertificateRequest.getCertificateName())). build())); String updatedRequest = getEnrollCSR(certManagerLogin, targetSystemServiceId, enrollTemplateId, sslCertificateRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("getEnrollCSRResponse Completed Successfully [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); if (sslCertificateRequest.getDnsList() != null && sslCertificateRequest.getDnsList().length > 0) { updatedRequest = buildSubjectAlternativeNameRequest(updatedRequest, sslCertificateRequest, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Build Object with DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } else { updatedRequest = removeSubjectAlternativeNameRequest(updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Remove DNS names [%s] = certificate name = [%s]", updatedRequest, sslCertificateRequest.getCertificateName())). build())); } CertResponse putEnrollCSRResponse = putEnrollCSR(certManagerLogin, targetSystemServiceId, updatedRequest); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("PutEnroll CSR Successfully Completed = [%s] = certificate name = [%s]", putEnrollCSRResponse, sslCertificateRequest.getCertificateName())). build())); String responseDetails = validateCSRResponse(putEnrollCSRResponse.getResponse()); if (!StringUtils.isEmpty(responseDetails)) { enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseDetails); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Exception While creating certificate " + "[%s] = certificate name = [%s]", responseDetails, sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\"" + enrollResponse.getResponse() + "\"]}"); } enrollResponse = enrollCertificate(certManagerLogin, targetSystemServiceId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("Enroll Certificate response Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(), sslCertificateRequest.getCertificateName())). build())); } else { enrollResponse = nclmMockUtil.getEnrollMockResponse(); } int actionId = 0; if (enrollResponse.getHttpstatus().equals(HttpStatus.ACCEPTED) && Objects.nonNull(enrollResponse.getResponse())) { Map<String, Object> responseMap = ControllerUtil.parseJson(enrollResponse.getResponse()); if (!MapUtils.isEmpty(responseMap) && responseMap.get("actionId") != null) { actionId = (Integer) responseMap.get("actionId"); if (actionId != 0) { enrollResponse = approvalRequest(certManagerLogin, actionId); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, String.format("approvalRequest Completed Successfully [%s]" + " = certificate name = [%s]", enrollResponse.getResponse(),sslCertificateRequest.getCertificateName())). build())); } } } if ( (HttpStatus.OK.equals(enrollResponse.getHttpstatus()) || (HttpStatus.NO_CONTENT.equals(enrollResponse.getHttpstatus())))) { boolean isPoliciesCreated; if (userDetails.isAdmin()) { isPoliciesCreated = createPolicies(sslCertificateRequest, token); } else { isPoliciesCreated = createPolicies(sslCertificateRequest, userDetails.getSelfSupportToken()); } if(isPoliciesCreated) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Policies are created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } String metadataJson = populateSSLCertificateMetadata(sslCertificateRequest, userDetails, certManagerLogin,actionId); boolean sslMetaDataCreationStatus; if (userDetails.isAdmin()) { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, token); } else { sslMetaDataCreationStatus = ControllerUtil.createMetadata(metadataJson, userDetails.getSelfSupportToken()); } if (sslMetaDataCreationStatus) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Metadata created for SSL certificate name [%s]", sslCertificateRequest.getCertificateName())). build())); } if ((!isPoliciesCreated) || (!sslMetaDataCreationStatus)) { enrollResponse.setResponse(SSLCertificateConstants.SSL_CREATE_EXCEPTION); enrollResponse.setSuccess(Boolean.FALSE); log.error(JSONUtil.getJSON(ImmutableMap.<String, String> builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()) .put(LogMessage.ACTION, String.format( "Metadata or Policies failed for SSL certificate name [%s] - metaDataStatus[%s] - policyStatus[%s]", sslCertificateRequest.getCertificateName(), sslMetaDataCreationStatus, isPoliciesCreated)) .build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\""+enrollResponse.getResponse()+"\"]}"); } else { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, "addSudoPermissionToCertificateOwner- STARTED "). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return addSudoPermissionToCertificateOwner(sslCertificateRequest, userDetails, enrollResponse , isPoliciesCreated, sslMetaDataCreationStatus,token,"create"); } } } else { SSLCertificateMetadataDetails certMetaDataDetails = certificateUtils.getCertificateMetaData(token, sslCertificateRequest.getCertificateName(), sslCertificateRequest.getCertType()); String responseMessage = StringUtils.capitalize(sslCertificateRequest.getCertificateName())+" Already" + " available in system and owned by "+ certMetaDataDetails.getCertOwnerEmailId() +" " + ". Please try with different certificate name"; enrollResponse.setSuccess(Boolean.FALSE); enrollResponse.setHttpstatus(HttpStatus.BAD_REQUEST); enrollResponse.setResponse(responseMessage); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Certificate Already Available in NCLM with Active Status " + "[%s] = certificate name = [%s]", enrollResponse.toString(), sslCertificateRequest.getCertificateName())). build())); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\":[\""+enrollResponse.getResponse()+ "\"]}"); } } catch (TVaultValidationException tex) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER).toString()). put(LogMessage.ACTION, String.format("Inside TVaultValidationException " + "Exception = [%s] = Message [%s] = certificate name = [%s]", Arrays.toString(tex.getStackTrace()), tex.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\":[\"" + tex.getMessage() + "\"]}"); } 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 " + "Exception = [%s] = Message [%s] = = certificate name = [%s]", Arrays.toString(e.getStackTrace()), e.getMessage(),sslCertificateRequest.getCertificateName())).build())); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body ("{\"errors\":[\"" + SSLCertificateConstants.SSL_CREATE_EXCEPTION + "\"]}"); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "generateSSLCertificate"). put(LogMessage.MESSAGE, String.format ("certificate [%s] before sending an email ", sslCertificateRequest.getCertificateName())). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\""+SSLCertificateConstants.SSL_CERT_SUCCESS+"\"]}"); }  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; }