Search in sources :

Example 6 with CertificateManagementException

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

the class CertificateMgtDAO method getCertificates.

/**
 * Method to retrieve certificate metadata from db for specific tenant which matches alias or endpoint.
 * From alias and endpoint, only one parameter is required.
 *
 * @param tenantId : The id of the tenant which the certificate belongs to.
 * @param alias    : Alias for the certificate. (Optional)
 * @param endpoint : The endpoint/ server url which the certificate is mapped to. (Optional)
 * @return : A CertificateMetadataDTO object if the certificate is retrieved successfully, null otherwise.
 */
public List<CertificateMetadataDTO> getCertificates(String alias, String endpoint, int tenantId) throws CertificateManagementException {
    String getCertQuery;
    CertificateMetadataDTO certificateMetadataDTO;
    List<CertificateMetadataDTO> certificateMetadataList = new ArrayList<>();
    if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
        if (log.isDebugEnabled()) {
            log.debug("The alias and endpoint are not empty. Invoking the search query with parameters " + "alias = " + alias + " endpoint = " + endpoint);
        }
        getCertQuery = SQLConstants.CertificateConstants.GET_CERTIFICATE_TENANT;
    } else {
        if (log.isDebugEnabled()) {
            log.debug("The alias and endpoint are empty. Invoking the get all certificates for tenant " + tenantId);
        }
        getCertQuery = SQLConstants.CertificateConstants.GET_CERTIFICATES;
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(getCertQuery)) {
            preparedStatement.setInt(1, tenantId);
            if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
                preparedStatement.setString(2, alias);
                preparedStatement.setString(3, endpoint);
            }
            try (ResultSet resultSet = preparedStatement.executeQuery()) {
                while (resultSet.next()) {
                    certificateMetadataDTO = new CertificateMetadataDTO();
                    certificateMetadataDTO.setAlias(resultSet.getString("ALIAS"));
                    certificateMetadataDTO.setEndpoint(resultSet.getString("END_POINT"));
                    try (InputStream certificate = resultSet.getBinaryStream("CERTIFICATE")) {
                        certificateMetadataDTO.setCertificate(APIMgtDBUtil.getStringFromInputStream(certificate));
                    }
                    certificateMetadataList.add(certificateMetadataDTO);
                }
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error while retrieving certificate metadata.", e);
    }
    return certificateMetadataList;
}
Also used : CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 7 with CertificateManagementException

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

the class CertificateMgtUtilTest method testGetCertificateInformation.

@Test
public void testGetCertificateInformation() throws CertificateManagementException {
    certificateMgtUtils.addCertificateToTrustStore(BASE64_ENCODED_CERT_STRING, ALIAS);
    CertificateInformationDTO certificateInformationDTO = certificateMgtUtils.getCertificateInformation(ALIAS);
    Assert.assertNotNull(certificateInformationDTO);
}
Also used : CertificateInformationDTO(org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO) Test(org.junit.Test)

Example 8 with CertificateManagementException

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

the class CertificateMgtUtilTest method testUpdateCertificate.

@Test
public void testUpdateCertificate() throws CertificateManagementException {
    ResponseCode responseCode = certificateMgtUtils.updateCertificate(BASE64_ENCODED_CERT_STRING, ALIAS);
    Assert.assertEquals(ResponseCode.SUCCESS, responseCode);
}
Also used : ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) Test(org.junit.Test)

Example 9 with CertificateManagementException

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

the class CertificateMgtUtilTest method testUpdateCertificateWithExpiredCertificate.

@Test
public void testUpdateCertificateWithExpiredCertificate() throws CertificateManagementException {
    ResponseCode responseCode = certificateMgtUtils.updateCertificate(EXPIRED_CERTIFICATE, ALIAS);
    Assert.assertEquals(ResponseCode.CERTIFICATE_EXPIRED, responseCode);
}
Also used : ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) Test(org.junit.Test)

Example 10 with CertificateManagementException

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

the class CertificateMgtUtilTest method testUpdateCertificateWithEmptyCertificate.

@Test
public void testUpdateCertificateWithEmptyCertificate() throws CertificateManagementException {
    ResponseCode responseCode = certificateMgtUtils.updateCertificate("", ALIAS);
    Assert.assertEquals(ResponseCode.INTERNAL_SERVER_ERROR, responseCode);
}
Also used : ResponseCode(org.wso2.carbon.apimgt.impl.certificatemgt.ResponseCode) Test(org.junit.Test)

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