Search in sources :

Example 16 with CertificateManagementException

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

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

Example 18 with CertificateManagementException

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

the class CertificateManagerImplTest method testGetCertificate.

@Test
public void testGetCertificate() throws APIManagementException, CertificateManagementException {
    String alias = "testGetCertificate";
    String endpoint = "testGetCertificate";
    int tenant = 1234;
    CertificateMetadataDTO certificateMetadata = generateMetadata();
    List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
    certificateMetadataList.add(certificateMetadata);
    Mockito.when(certificateMgtDAO.getCertificates(alias, endpoint, tenant)).thenReturn(certificateMetadataList);
    List<CertificateMetadataDTO> result = certificateManager.getCertificates(tenant, alias, endpoint);
    Assert.assertNotNull(result);
    Assert.assertEquals(certificateMetadataList, result);
}
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 19 with CertificateManagementException

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

the class CertificateManagerImplTest method testIsCertificatePresent.

@Test
public void testIsCertificatePresent() throws APIManagementException, CertificateManagementException {
    CertificateMetadataDTO certificateMetadataDTO = generateMetadata();
    List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
    certificateMetadataList.add(certificateMetadataDTO);
    Mockito.when(certificateMgtDAO.getCertificates(Mockito.anyString(), Mockito.anyString(), Mockito.anyInt())).thenReturn(certificateMetadataList);
    boolean result = certificateManager.isCertificatePresent(TENANT_ID, ALIAS);
    Assert.assertTrue(result);
}
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 20 with CertificateManagementException

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

the class CertificateMgtDAO method getClientCertificates.

/**
 * Method to retrieve certificate metadata from db for specific tenant which matches alias or api identifier.
 * Both alias and api identifier are optional
 *
 * @param tenantId      : The id of the tenant which the certificate belongs to.
 * @param alias         : Alias for the certificate. (Optional)
 * @param apiIdentifier : The API which the certificate is mapped to. (Optional)
 * @param organization  : Organization
 * @return : A CertificateMetadataDTO object if the certificate is retrieved successfully, null otherwise.
 */
public List<ClientCertificateDTO> getClientCertificates(int tenantId, String alias, APIIdentifier apiIdentifier, String organization) throws CertificateManagementException {
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    List<ClientCertificateDTO> clientCertificateDTOS = new ArrayList<>();
    int apiId = 0;
    int index = 1;
    String selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT;
    if (StringUtils.isNotEmpty(alias) && apiIdentifier != null) {
        selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_ALIAS_APIID;
    } else if (StringUtils.isNotEmpty(alias)) {
        selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_ALIAS;
    } else if (apiIdentifier != null) {
        selectQuery = SQLConstants.ClientCertificateConstants.SELECT_CERTIFICATE_FOR_TENANT_APIID;
    }
    try {
        connection = APIMgtDBUtil.getConnection();
        if (apiIdentifier != null) {
            String apiUuid;
            if (apiIdentifier.getUUID() != null) {
                apiUuid = apiIdentifier.getUUID();
                APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiUuid);
                if (apiRevision != null && apiRevision.getApiUUID() != null) {
                    apiUuid = apiRevision.getApiUUID();
                }
            } else {
                apiUuid = ApiMgtDAO.getInstance().getUUIDFromIdentifier(apiIdentifier, organization);
            }
            apiId = ApiMgtDAO.getInstance().getAPIID(apiUuid, connection);
        }
        preparedStatement = connection.prepareStatement(selectQuery);
        preparedStatement.setBoolean(index, false);
        index++;
        preparedStatement.setInt(index, tenantId);
        index++;
        if (alias != null) {
            preparedStatement.setString(index, alias);
            index++;
        }
        if (apiIdentifier != null) {
            preparedStatement.setInt(index, apiId);
        }
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            alias = resultSet.getString("ALIAS");
            ClientCertificateDTO clientCertificateDTO = new ClientCertificateDTO();
            clientCertificateDTO.setTierName(resultSet.getString("TIER_NAME"));
            clientCertificateDTO.setAlias(alias);
            clientCertificateDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(resultSet.getBinaryStream("CERTIFICATE")));
            if (apiIdentifier == null) {
                apiIdentifier = new APIIdentifier(APIUtil.replaceEmailDomain(resultSet.getString("API_PROVIDER")), resultSet.getString("API_NAME"), resultSet.getString("API_VERSION"));
            }
            clientCertificateDTO.setApiIdentifier(apiIdentifier);
            clientCertificateDTOS.add(clientCertificateDTO);
        }
    } catch (SQLException e) {
        handleException("Error while searching client certificate details for the tenant " + tenantId, e);
    } catch (APIManagementException e) {
        handleException("API Management Exception while searching client certificate details for the tenant " + tenantId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(preparedStatement, connection, resultSet);
    }
    return clientCertificateDTOS;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

Test (org.junit.Test)11 CertificateMetadataDTO (org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO)9 CertificateManagementException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateManagementException)8 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 Connection (java.sql.Connection)5 SQLException (java.sql.SQLException)5 File (java.io.File)4 PreparedStatement (java.sql.PreparedStatement)4 ResponseCode (org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode)4 CertificateAliasExistsException (org.wso2.carbon.apimgt.impl.certificatemgt.exceptions.CertificateAliasExistsException)4 FileInputStream (java.io.FileInputStream)3 KeyStore (java.security.KeyStore)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 X509Certificate (java.security.cert.X509Certificate)3