Search in sources :

Example 11 with CertificateManagementException

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

the class CertificateMgtUtilTest method testUpdateCertificateWithCertificateNotFound.

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

Example 12 with CertificateManagementException

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

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

the class TemplateBuilderUtil method retrieveGatewayAPIDto.

public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environment, String tenantDomain, APIDTO apidto, String extractedFolderPath, APIDefinitionValidationResponse apiDefinitionValidationResponse) throws APIManagementException, XMLStreamException, APITemplateException, CertificateManagementException {
    if (apiDefinitionValidationResponse.isValid()) {
        APIDefinition parser = apiDefinitionValidationResponse.getParser();
        String definition = apiDefinitionValidationResponse.getJsonContent();
        if (parser != null) {
            Set<URITemplate> uriTemplates = parser.getURITemplates(definition);
            for (URITemplate uriTemplate : uriTemplates) {
                for (URITemplate template : api.getUriTemplates()) {
                    if (template.getHTTPVerb().equalsIgnoreCase(uriTemplate.getHTTPVerb()) && template.getUriTemplate().equals(uriTemplate.getUriTemplate())) {
                        template.setMediationScript(uriTemplate.getMediationScript());
                        template.setMediationScripts(uriTemplate.getHTTPVerb(), uriTemplate.getMediationScript());
                        template.setAmznResourceName(uriTemplate.getAmznResourceName());
                        template.setAmznResourceTimeout(uriTemplate.getAmznResourceTimeout());
                        break;
                    }
                }
            }
        }
    }
    return retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath);
}
Also used : APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate)

Example 14 with CertificateManagementException

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

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

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