Search in sources :

Example 11 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO 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 12 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO 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 13 with CertificateMetadataDTO

use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO 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 14 with CertificateMetadataDTO

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

the class EndpointCertificatesApiServiceImpl method getEndpointCertificates.

public Response getEndpointCertificates(Integer limit, Integer offset, String alias, String endpoint, MessageContext messageContext) {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    List<CertificateMetadataDTO> certificates;
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    String query = CertificateRestApiUtils.buildQueryString("alias", alias, "endpoint", endpoint);
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        if (StringUtils.isNotEmpty(alias) || StringUtils.isNotEmpty(endpoint)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Call the search certificate api to get the filtered certificates for " + "tenant id : %d, alias : %s, and endpoint : %s", tenantId, alias, endpoint));
            }
            certificates = apiProvider.searchCertificates(tenantId, alias, endpoint);
        } else {
            if (log.isDebugEnabled()) {
                log.debug(String.format("There is no query parameters provided. So, retrieve all the certificates" + " belongs to the tenantId : %d", tenantId));
            }
            certificates = apiProvider.getCertificates(userName);
        }
        CertificatesDTO certificatesDTO = CertificateRestApiUtils.getPaginatedCertificates(certificates, limit, offset, query);
        return Response.status(Response.Status.OK).entity(certificatesDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving the certificates.", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CertificatesDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificatesDTO)

Example 15 with CertificateMetadataDTO

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

the class EndpointCertificatesApiServiceImpl method updateEndpointCertificateByAlias.

public Response updateEndpointCertificateByAlias(String alias, InputStream certificateInputStream, Attachment certificateDetail, MessageContext messageContext) {
    try {
        if (StringUtils.isEmpty(alias)) {
            RestApiUtil.handleBadRequest("The alias should not be empty", log);
        }
        ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
        String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
        if (StringUtils.isBlank(fileName)) {
            RestApiUtil.handleBadRequest("Certificate update failed. The Certificate should not be empty", log);
        }
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        int tenantId = APIUtil.getTenantId(userName);
        if (!apiProvider.isCertificatePresent(tenantId, alias)) {
            if (log.isDebugEnabled()) {
                log.debug(String.format("Could not find a certificate in truststore which belongs to tenant : %d " + "and with alias : %s. Hence the operation is terminated.", tenantId, alias));
            }
            RestApiUtil.handleResourceNotFoundError("Could not update the certificate. " + "The alias '" + alias + "' not found.", log);
        }
        String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
        int responseCode = apiProvider.updateCertificate(base64EncodedCert, alias);
        List<CertificateMetadataDTO> updatedCertificate = apiProvider.searchCertificates(tenantId, alias, null);
        if (ResponseCode.SUCCESS.getResponseCode() == responseCode && updatedCertificate.size() > 0) {
            CertificateMetadataDTO certificateMetadata = updatedCertificate.get(0);
            CertMetadataDTO certificateDTO = new CertMetadataDTO();
            certificateDTO.setAlias(certificateMetadata.getAlias());
            certificateDTO.setEndpoint(certificateMetadata.getEndpoint());
            URI updatedCertUri = new URI(RestApiConstants.CERTS_BASE_PATH + "?alias=" + alias);
            return Response.ok(updatedCertUri).entity(certificateDTO).build();
        }
        if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
            RestApiUtil.handleInternalServerError("Error while updating the certificate due to an internal " + "server error", log);
        } else if (ResponseCode.CERTIFICATE_NOT_FOUND.getResponseCode() == responseCode) {
            RestApiUtil.handleResourceNotFoundError("", log);
        } else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
            RestApiUtil.handleBadRequest("Error while updating the certificate. Certificate Expired.", log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while adding the certificate due to an internal server " + "error", log);
    } catch (IOException e) {
        RestApiUtil.handleInternalServerError("Error while encoding certificate", log);
    } catch (URISyntaxException e) {
        RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", log);
    }
    return null;
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CertificateMetadataDTO(org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CertMetadataDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertMetadataDTO) URI(java.net.URI)

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