Search in sources :

Example 1 with CertificateAliasExistsException

use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException in project carbon-apimgt by wso2.

the class CertificateMgtDAO method addCertificate.

/**
 * Method to add a new certificate to the database.
 *
 * @param alias    : Alias for the new certificate.
 * @param endpoint : The endpoint/ server url which the certificate will be mapped to.
 * @param tenantId : The Id of the tenant who uploaded the certificate.
 * @return : True if the information is added successfully, false otherwise.
 * @throws CertificateManagementException if existing entry is found for the given endpoint or alias.
 */
public boolean addCertificate(String certificate, String alias, String endpoint, int tenantId) throws CertificateManagementException, CertificateAliasExistsException {
    boolean result = false;
    String addCertQuery = SQLConstants.CertificateConstants.INSERT_CERTIFICATE;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        boolean certificateExist = isCertificateExist(connection, alias, tenantId);
        if (certificateExist) {
            if (log.isDebugEnabled()) {
                log.debug("A certificate for the endpoint " + endpoint + " has already added with alias " + alias);
            }
            String message = "Alias or Endpoint exists in the database!";
            throw new CertificateAliasExistsException(message);
        }
        connection.setAutoCommit(false);
        try (PreparedStatement preparedStatement = connection.prepareStatement(addCertQuery)) {
            preparedStatement.setInt(1, tenantId);
            preparedStatement.setString(2, endpoint);
            preparedStatement.setString(3, alias);
            preparedStatement.setBinaryStream(4, getInputStream(certificate));
            result = preparedStatement.executeUpdate() == 1;
            connection.commit();
        } catch (SQLException e) {
            handleConnectionRollBack(connection);
            if (log.isDebugEnabled()) {
                log.debug("Error occurred while adding certificate metadata to database.", e);
            }
            handleException("Error while persisting certificate metadata.", e);
        }
    } catch (SQLException e) {
        handleException("Error while retrieving connection", e);
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) CertificateAliasExistsException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with CertificateAliasExistsException

use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException 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 3 with CertificateAliasExistsException

use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException 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)

Example 4 with CertificateAliasExistsException

use of org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException in project carbon-apimgt by wso2.

the class CertificateManagerImplTest method testRemoveFromPublisherCertificateManagementException.

@Test
public void testRemoveFromPublisherCertificateManagementException() {
    PowerMockito.stub(PowerMockito.method(CertificateMgtUtils.class, "removeCertificateFromTrustStore", String.class)).toReturn(ResponseCode.INTERNAL_SERVER_ERROR);
    try {
        Mockito.when(certificateMgtDAO.deleteCertificate("testRemoveFromPublisherCertificateManagementException", "testRemoveFromPublisherCertificateManagementException", TENANT_ID)).thenReturn(true);
        Mockito.when(certificateMgtDAO.addCertificate(BASE64_ENCODED_CERT, "testRemoveFromPublisherCertificateManagementException", "testRemoveFromPublisherCertificateManagementException", TENANT_ID)).thenThrow(CertificateManagementException.class);
        CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
        certificateMetadataDTO.setEndpoint("testRemoveFromPublisherCertificateManagementException");
        certificateMetadataDTO.setCertificate(BASE64_ENCODED_CERT);
        certificateMetadataDTO.setAlias("testRemoveFromPublisherCertificateManagementException");
        List<CertificateMetadataDTO> certificateMetadataDTOList = new ArrayList<>();
        certificateMetadataDTOList.add(certificateMetadataDTO);
        Mockito.when(certificateMgtDAO.getCertificates("testRemoveFromPublisherCertificateManagementException", null, TENANT_ID)).thenReturn(certificateMetadataDTOList);
    } catch (CertificateManagementException | CertificateAliasExistsException e) {
        e.printStackTrace();
    }
    ResponseCode responseCode = certificateManager.deleteCertificateFromParentNode("testRemoveFromPublisherCertificateManagementException", "testRemoveFromPublisherCertificateManagementException", 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) ArrayList(java.util.ArrayList) CertificateManagementException(org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CertificateAliasExistsException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException)4 CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)3 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)3 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1