CertificateUtils { public SSLCertificateMetadataDetails getCertificateMetaData(String token, String certificateName, String certificateType){ String metaDataPath = (certificateType.equalsIgnoreCase("internal"))? SSLCertificateConstants.SSL_CERT_PATH :SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH; String certificatePath = metaDataPath + '/' + certificateName; log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "Get Certificate Info"). put(LogMessage.MESSAGE, String.format ("Trying to get Info for [%s]", certificatePath)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); Response response = ControllerUtil.getReqProcessor().process("/certmanager","{\"path\":\""+certificatePath+"\"}",token); SSLCertificateMetadataDetails certificateMetadataDetails = null; if(HttpStatus.OK.equals(response.getHttpstatus())){ try { ObjectMapper objMapper = new ObjectMapper(); JsonNode dataNode = objMapper.readTree(response.getResponse()).get("data"); certificateMetadataDetails = getCertificateInfo(dataNode); } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getCertificateMetaData"). put(LogMessage.MESSAGE, "Error while trying to get details about the certificate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } }else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getCertificateMetaData"). put(LogMessage.MESSAGE, "Error while trying to get certificate metadata"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "Get Certificate metadata Info"). put(LogMessage.MESSAGE, "Getting metaDataInfo completed"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return certificateMetadataDetails; }  boolean hasAddOrRemovePermission(UserDetails userDetails, SSLCertificateMetadataDetails certificateMetaData); SSLCertificateMetadataDetails getCertificateMetaData(String token, String certificateName, String certificateType);  }
@Test public void testGetCertificateMetaDataSuccessfully() { UserDetails userDetails = getMockUser(true); userDetails.setUsername(ADMIN_USER); String certificatePath = SSLCertificateConstants.SSL_CERT_PATH + '/' + CERT_NAME; Response responseObj = getMockResponse(HttpStatus.OK, true, "{ \"data\": {\"akmid\":\"103001\",\"applicationName\":\"tvt\", " + " \"applicationOwnerEmailId\":\"appowneremail@test.com\", \"applicationTag\":\"T-Vault\", " + " \"authority\":\"T-Mobile Issuing CA 01 - SHA2\", \"certCreatedBy\": \"testuser1\", " + " \"certOwnerEmailId\":\"owneremail@test.com\", \"certOwnerNtid\": \"testuser1\", \"certType\": \"internal\", " + " \"certificateId\":\"62765\",\"certificateName\":\"certificatename.t-mobile.com\", " + " \"certificateStatus\":\"Active\", \"containerName\":\"VenafiBin_12345\", " + " \"createDate\":\"2020-06-24\", \"expiryDate\":\"2021-06-24\", " + " \"projectLeadEmailId\":\"project@email.com\"}}"); when(ControllerUtil.getReqProcessor().process(GET_CERT_DETAIL, GET_CERT_DETAIL_VAL +certificatePath+"\"}",userDetails.getClientToken())).thenReturn(responseObj); SSLCertificateMetadataDetails certificateMetadata = getSSLCertificateMetadataDetails(); String certType = "internal"; SSLCertificateMetadataDetails certificateMetadataObj = certificateUtils.getCertificateMetaData(userDetails.getClientToken(),CERT_NAME, certType); assertEquals(certificateMetadataObj.getCertificateName(), certificateMetadata.getCertificateName()); assertEquals(certificateMetadataObj.getAkmid(), certificateMetadata.getAkmid()); assertEquals(certificateMetadataObj.getApplicationName(), certificateMetadata.getApplicationName()); assertEquals(certificateMetadataObj.getCreateDate(), certificateMetadata.getCreateDate()); assertEquals(certificateMetadataObj.getApplicationTag(), certificateMetadata.getApplicationTag()); assertEquals(certificateMetadataObj.getCertOwnerNtid(), certificateMetadata.getCertOwnerNtid()); }
@Test public void testGetExternalCertificateMetaDataSuccessfully() { UserDetails userDetails = getMockUser(true); userDetails.setUsername(ADMIN_USER); String certificatePath = SSLCertificateConstants.SSL_EXTERNAL_CERT_PATH + '/' + CERT_NAME; Response responseObj = getMockResponse(HttpStatus.OK, true, "{ \"data\": {\"akmid\":\"103001\",\"applicationName\":\"tvt\", " + " \"applicationOwnerEmailId\":\"appowneremail@test.com\", \"applicationTag\":\"T-Vault\", " + " \"authority\":\"T-Mobile Issuing CA 01 - SHA2\", \"certCreatedBy\": \"testuser1\", " + " \"certOwnerEmailId\":\"owneremail@test.com\", \"certOwnerNtid\": \"testuser1\", \"certType\": \"external\", " + " \"certificateId\":\"62765\",\"certificateName\":\"certificatename.t-mobile.com\", " + " \"certificateStatus\":\"Active\", \"containerName\":\"VenafiBin_12345\", \"containerId\": \"99\", " + " \"createDate\":\"2020-06-24\", \"expiryDate\":\"2021-06-24\", \"requestStatus\": \"Approved\", " + " \"dnsNames\":\"test.t-mobile.com, test1.t-mobile.com, certtestest.t-mobile.com\"," + " \"projectLeadEmailId\":\"project@email.com\"}}"); when(ControllerUtil.getReqProcessor().process(GET_CERT_DETAIL, GET_CERT_DETAIL_VAL + certificatePath + "\"}", userDetails.getClientToken())).thenReturn(responseObj); SSLCertificateMetadataDetails certificateMetadata = getSSLCertificateMetadataDetails(); certificateMetadata.setRequestStatus("Approved"); certificateMetadata.setCertType("external"); certificateMetadata.setContainerId(99); String certType = "external"; SSLCertificateMetadataDetails certificateMetadataObj = certificateUtils .getCertificateMetaData(userDetails.getClientToken(), CERT_NAME, certType); assertEquals(certificateMetadataObj.getCertificateName(), certificateMetadata.getCertificateName()); assertEquals(certificateMetadataObj.getAkmid(), certificateMetadata.getAkmid()); assertEquals(certificateMetadataObj.getApplicationName(), certificateMetadata.getApplicationName()); assertEquals(certificateMetadataObj.getCreateDate(), certificateMetadata.getCreateDate()); assertEquals(certificateMetadataObj.getApplicationTag(), certificateMetadata.getApplicationTag()); assertEquals(certificateMetadataObj.getCertOwnerNtid(), certificateMetadata.getCertOwnerNtid()); assertEquals(certificateMetadataObj.getRequestStatus(), certificateMetadata.getRequestStatus()); assertEquals(certificateMetadataObj.getContainerId(), certificateMetadata.getContainerId()); }
@Test(expected = Exception.class) public void testGetCertificateMetaDataFailed() { UserDetails userDetails = getMockUser(true); userDetails.setUsername(ADMIN_USER); String certificatePath = SSLCertificateConstants.SSL_CERT_PATH + '/' + CERT_NAME; Response responseObj = getMockResponse(HttpStatus.OK, true, "{ \"data\": [\"akmid\":\"103001\",\"applicationName\":\"tvt\", " + " \"applicationOwnerEmailId\":\"appowneremail@test.com\", \"applicationTag\":\"T-Vault\", " + " \"authority\":\"T-Mobile Issuing CA 01 - SHA2\", \"certCreatedBy\": \"testuser1\", " + " \"certOwnerEmailId\":\"owneremail@test.com\", \"certOwnerNtid\": \"testuser1\", \"certType\": \"internal\", " + " \"certificateId\":\"62765\",\"certificateName\":\"certificatename.t-mobile.com\", " + " \"certificateStatus\":\"Active\", \"containerName\":\"VenafiBin_12345\", " + " \"createDate\":\"2020-06-24\", \"expiryDate\":\"2021-06-24\", " + " \"projectLeadEmailId\":\"project@email.com\"]}"); when(ControllerUtil.getReqProcessor().process(GET_CERT_DETAIL, GET_CERT_DETAIL_VAL +certificatePath+"\"}",userDetails.getClientToken())).thenReturn(responseObj); String certType = "internal"; SSLCertificateMetadataDetails certificateMetadata = getSSLCertificateMetadataDetails(); SSLCertificateMetadataDetails certificateMetadataObj = certificateUtils.getCertificateMetaData(userDetails.getClientToken(),CERT_NAME,certType); assertEquals(certificateMetadataObj.getCertificateName(), certificateMetadata.getCertificateName()); }
@Test public void testGetCertificateMetaDataInvalidResponse() { UserDetails userDetails = getMockUser(true); userDetails.setUsername(ADMIN_USER); String certType = "internal"; String certificatePath = SSLCertificateConstants.SSL_CERT_PATH + '/' + CERT_NAME; Response responseObj = getMockResponse(HttpStatus.INTERNAL_SERVER_ERROR, true, "{}"); when(ControllerUtil.getReqProcessor().process(GET_CERT_DETAIL, GET_CERT_DETAIL_VAL +certificatePath+"\"}",userDetails.getClientToken())).thenReturn(responseObj); SSLCertificateMetadataDetails certificateMetadataObj = certificateUtils.getCertificateMetaData(userDetails.getClientToken(),CERT_NAME,certType); assertEquals(certificateMetadataObj, null); }