Search in sources :

Example 6 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testRemoveFromPublisher.

@Test
public void testRemoveFromPublisher() throws CertificateManagementException {
    PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.SUCCESS);
    Mockito.when(certificateMgtDAO.deleteCertificate(ALIAS, END_POINT, TENANT_ID)).thenReturn(true);
    List<CertificateMetadataDTO> certificateMetadataDTOList = new ArrayList<>();
    CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
    certificateMetadataDTO.setAlias(ALIAS);
    certificateMetadataDTO.setEndpoint(END_POINT);
    certificateMetadataDTOList.add(certificateMetadataDTO);
    Mockito.when(certificateMgtDAO.getCertificates(ALIAS, null, TENANT_ID)).thenReturn(certificateMetadataDTOList);
    ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode(ALIAS, END_POINT, TENANT_ID);
    Assert.assertEquals(ResponseCode.SUCCESS, responseCode);
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.

the class APIControllerUtil method handleEndpointCertificates.

/**
 * This method will be used to generate Endpoint certificates and meta information related to endpoint certs.
 *
 * @param certificates  JsonArray of endpoint-certificates
 * @param pathToArchive String of the archive project
 * @throws IOException            If an error occurs when generating new certs and yaml file or when moving certs
 * @throws APIManagementException If an error while generating new directory
 */
private static void handleEndpointCertificates(JsonArray certificates, String pathToArchive) throws IOException, APIManagementException {
    JsonArray updatedCertsArray = new JsonArray();
    for (JsonElement certificate : certificates) {
        JsonObject certObject = certificate.getAsJsonObject();
        String alias = certObject.get(ImportExportConstants.ALIAS_JSON_KEY).getAsString();
        CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
        certificateMetadataDTO.setAlias(alias);
        certificateMetadataDTO.setEndpoint(certObject.get(ImportExportConstants.CERTIFICATE_HOST_NAME_PROPERTY).getAsString());
        // Add certificate element to cert object
        JsonElement jsonElement = new Gson().toJsonTree(certificateMetadataDTO);
        JsonObject updatedCertObj = jsonElement.getAsJsonObject();
        String certName = certObject.get(ImportExportConstants.CERTIFICATE_PATH_PROPERTY).getAsString();
        updatedCertObj.addProperty(ImportExportConstants.CERTIFICATE_FILE, certName);
        updatedCertsArray.add(updatedCertObj);
        // check and create a directory
        String endpointCertificatesDirectory = pathToArchive + ImportExportConstants.ENDPOINT_CERTIFICATES_DIRECTORY_PATH;
        if (!CommonUtil.checkFileExistence(endpointCertificatesDirectory)) {
            try {
                CommonUtil.createDirectory(endpointCertificatesDirectory);
            } catch (APIImportExportException e) {
                throw new APIManagementException(e);
            }
        }
        // copy certs file from certificates
        String userCertificatesTempDirectory = pathToArchive + ImportExportConstants.DEPLOYMENT_DIRECTORY + ImportExportConstants.CERTIFICATE_DIRECTORY;
        String sourcePath = userCertificatesTempDirectory + File.separator + certName;
        String destinationPath = endpointCertificatesDirectory + File.separator + certName;
        if (Files.notExists(Paths.get(sourcePath))) {
            String errorMessage = "The mentioned certificate file " + certName + " is not in the certificates directory";
            throw new APIManagementException(errorMessage, ExceptionCodes.ERROR_READING_PARAMS_FILE);
        }
        CommonUtil.moveFile(sourcePath, destinationPath);
    }
    // generate meta-data yaml file
    String metadataFilePath = pathToArchive + ImportExportConstants.ENDPOINT_CERTIFICATES_META_DATA_FILE_PATH;
    try {
        if (CommonUtil.checkFileExistence(metadataFilePath + ImportExportConstants.YAML_EXTENSION)) {
            File oldFile = new File(metadataFilePath + ImportExportConstants.YAML_EXTENSION);
            oldFile.delete();
        }
        if (CommonUtil.checkFileExistence(metadataFilePath + ImportExportConstants.JSON_EXTENSION)) {
            File oldFile = new File(metadataFilePath + ImportExportConstants.JSON_EXTENSION);
            oldFile.delete();
        }
        CommonUtil.writeDtoToFile(metadataFilePath, ExportFormat.JSON, ImportExportConstants.TYPE_ENDPOINT_CERTIFICATES, updatedCertsArray);
    } catch (APIImportExportException e) {
        throw new APIManagementException(e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) JsonElement(com.google.gson.JsonElement) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) File(java.io.File)

Example 8 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.

the class CertificateManagerImpl method deleteCertificateFromParentNode.

@Override
public ResponseCode deleteCertificateFromParentNode(String alias, String endpoint, int tenantId) {
    try {
        List<CertificateMetadataDTO> certificateMetadataDTOList = certificateMgtDAO.getCertificates(alias, null, tenantId);
        if (certificateMetadataDTOList != null && certificateMetadataDTOList.size() == 1) {
            CertificateMetadataDTO certificate = certificateMetadataDTOList.get(0);
            boolean removeFromDB = certificateMgtDAO.deleteCertificate(alias, endpoint, tenantId);
            if (removeFromDB) {
                ResponseCode responseCode = certificateMgtUtils.removeCertificateFromTrustStore(alias);
                if (responseCode == ResponseCode.INTERNAL_SERVER_ERROR) {
                    certificateMgtDAO.addCertificate(certificate.getCertificate(), alias, endpoint, tenantId);
                    log.error("Error removing the Certificate from Trust Store. Rolling back...");
                } else if (responseCode.getResponseCode() == ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode()) {
                    log.warn("The Certificate for Alias '" + alias + "' has been previously removed from " + "Trust Store. Hence DB entry is removed.");
                } else {
                    log.info("Certificate is successfully removed from the Publisher Trust Store with Alias '" + alias + "'");
                }
                return responseCode;
            } else {
                log.error("Failed to remove certificate from the data base. No certificate changes will be affected" + ".");
                return ResponseCode.INTERNAL_SERVER_ERROR;
            }
        }
    } catch (CertificateManagementException e) {
        log.error("Error persisting/ deleting certificate metadata. ", e);
        return ResponseCode.INTERNAL_SERVER_ERROR;
    } catch (CertificateAliasExistsException e) {
        return ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE;
    }
    return ResponseCode.CERTIFICATE_NOT_FOUND;
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) CertificateAliasExistsException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)

Example 9 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testRemoveFromPublisherInternalServerError.

@Test
public void testRemoveFromPublisherInternalServerError() throws CertificateManagementException {
    PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.SUCCESS);
    Mockito.when(certificateMgtDAO.deleteCertificate(ALIAS, END_POINT, TENANT_ID)).thenReturn(false);
    List<CertificateMetadataDTO> certificateMetadataDTOList = new ArrayList<>();
    CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
    certificateMetadataDTO.setAlias(ALIAS);
    certificateMetadataDTO.setEndpoint(END_POINT);
    certificateMetadataDTOList.add(certificateMetadataDTO);
    Mockito.when(certificateMgtDAO.getCertificates(ALIAS, null, TENANT_ID)).thenReturn(certificateMetadataDTOList);
    ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode(ALIAS, END_POINT, TENANT_ID);
    Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) ArrayList(java.util.ArrayList) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testRemoveFromPublisherWithInternalServerErrorWhenDeleting.

@Test
public void testRemoveFromPublisherWithInternalServerErrorWhenDeleting() {
    PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.INTERNAL_SERVER_ERROR);
    try {
        Mockito.when(certificateMgtDAO.deleteCertificate("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID)).thenReturn(true);
        Mockito.when(certificateMgtDAO.addCertificate(BASE64_ENCODED_CERT, "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID)).thenReturn(true);
        CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
        certificateMetadataDTO.setEndpoint("testRemoveFromPublisherWithInternalServerErrorWhenDeleting");
        certificateMetadataDTO.setCertificate(BASE64_ENCODED_CERT);
        certificateMetadataDTO.setAlias("testRemoveFromPublisherWithInternalServerErrorWhenDeleting");
        Mockito.when(certificateMgtDAO.getCertificates("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", null, TENANT_ID)).thenReturn(Arrays.asList(certificateMetadataDTO));
    } catch (CertificateManagementException | CertificateAliasExistsException e) {
        e.printStackTrace();
    }
    ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode("testRemoveFromPublisherWithInternalServerErrorWhenDeleting", "testRemoveFromPublisherWithInternalServerErrorWhenDeleting", TENANT_ID);
    Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) CertificateAliasExistsException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)17 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 IOException (java.io.IOException)4 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)4 Gson (com.google.gson.Gson)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 CertificateAliasExistsException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 InputStream (java.io.InputStream)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2 APIImportExportException (org.wso2.carbon.apimgt.impl.importexport.APIImportExportException)2 CertMetadataDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO)2