Search in sources :

Example 11 with ClientCertificateDTO

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

the class TemplateBuilderUtil method createAPIGatewayDTOtoPublishAPI.

private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environment, APIProduct apiProduct, APITemplateBuilder builder, String tenantDomain, Map<String, APIDTO> associatedAPIsMap, List<ClientCertificateDTO> clientCertificatesDTOList) throws APITemplateException, XMLStreamException, APIManagementException {
    APIProductIdentifier id = apiProduct.getId();
    GatewayAPIDTO productAPIDto = new GatewayAPIDTO();
    productAPIDto.setProvider(id.getProviderName());
    productAPIDto.setApiId(apiProduct.getUuid());
    productAPIDto.setName(id.getName());
    productAPIDto.setVersion(id.getVersion());
    productAPIDto.setTenantDomain(tenantDomain);
    productAPIDto.setKeyManagers(Collections.singletonList(APIConstants.KeyManager.API_LEVEL_ALL_KEY_MANAGERS));
    String definition = apiProduct.getDefinition();
    productAPIDto.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(apiProduct.getUuid(), productAPIDto.getLocalEntriesToBeRemove()));
    GatewayContentDTO productLocalEntry = new GatewayContentDTO();
    productLocalEntry.setName(apiProduct.getUuid());
    productLocalEntry.setContent("<localEntry key=\"" + apiProduct.getUuid() + "\">" + definition.replaceAll("&(?!amp;)", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;") + "</localEntry>");
    productAPIDto.setLocalEntriesToBeAdd(addGatewayContentToList(productLocalEntry, productAPIDto.getLocalEntriesToBeAdd()));
    setClientCertificatesToBeAdded(tenantDomain, productAPIDto, clientCertificatesDTOList);
    for (Map.Entry<String, APIDTO> apidtoEntry : associatedAPIsMap.entrySet()) {
        String apiExtractedPath = apidtoEntry.getKey();
        APIDTO apidto = apidtoEntry.getValue();
        API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
        api.setUuid(apidto.getId());
        GatewayUtils.setCustomSequencesToBeRemoved(apiProduct.getId(), api.getUuid(), productAPIDto);
        APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api, apiProduct);
        addEndpoints(api, apiTemplateBuilder, productAPIDto);
        setCustomSequencesToBeAdded(apiProduct, api, productAPIDto, apiExtractedPath, apidto);
        setAPIFaultSequencesToBeAdded(api, productAPIDto, apiExtractedPath, apidto);
        String prefix = id.getName() + "--v" + id.getVersion();
        setSecureVaultPropertyToBeAdded(prefix, api, productAPIDto);
    }
    productAPIDto.setApiDefinition(builder.getConfigStringForTemplate(environment));
    return productAPIDto;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APITemplateBuilder(org.wso2.carbon.apimgt.impl.template.APITemplateBuilder) API(org.wso2.carbon.apimgt.api.model.API) Map(java.util.Map) HashMap(java.util.HashMap) APITemplateBuilderImpl(org.wso2.carbon.apimgt.rest.api.publisher.v1.common.template.APITemplateBuilderImpl) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 12 with ClientCertificateDTO

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

the class CertificateRestApiUtils method preValidateClientCertificate.

/**
 * To pre validate client certificate given for an alias
 *
 * @param alias Alias of the certificate.
 * @param apiIdentifier Identifier of the API.
 * @param organization Identifier of the organization.
 * @return Client certificate
 * @throws APIManagementException API Management Exception.
 */
public static ClientCertificateDTO preValidateClientCertificate(String alias, APIIdentifier apiIdentifier, String organization) throws APIManagementException {
    int tenantId = APIUtil.getInternalOrganizationId(organization);
    if (StringUtils.isEmpty(alias)) {
        throw new APIManagementException("The alias cannot be empty", ExceptionCodes.ALIAS_CANNOT_BE_EMPTY);
    }
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    ClientCertificateDTO clientCertificate = apiProvider.getClientCertificate(tenantId, alias, apiIdentifier, organization);
    if (clientCertificate == null) {
        if (log.isDebugEnabled()) {
            log.debug(String.format("Could not find a client certificate in truststore which belongs to " + "tenant : %d and with alias : %s. Hence the operation is terminated.", tenantId, alias));
        }
        String message = "Certificate for alias '" + alias + "' is not found.";
        throw new APIMgtResourceNotFoundException(message);
    }
    return clientCertificate;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 13 with ClientCertificateDTO

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

the class ImportUtils method addClientCertificates.

/**
 * Import client certificates for Mutual SSL related configuration.
 *
 * @param pathToArchive Location of the extracted folder of the API
 * @param apiProvider   API Provider
 * @param preserveProvider Decision to keep or replace the provider
 * @param provider     Provider Id
 * @param organization Identifier of the organization
 * @throws APIImportExportException
 */
private static void addClientCertificates(String pathToArchive, APIProvider apiProvider, Boolean preserveProvider, String provider, String organization) throws APIManagementException {
    try {
        List<ClientCertificateDTO> certificateMetadataDTOS = retrieveClientCertificates(pathToArchive);
        for (ClientCertificateDTO certDTO : certificateMetadataDTOS) {
            APIIdentifier apiIdentifier = !preserveProvider ? new APIIdentifier(provider, certDTO.getApiIdentifier().getApiName(), certDTO.getApiIdentifier().getVersion()) : certDTO.getApiIdentifier();
            apiProvider.addClientCertificate(APIUtil.replaceEmailDomainBack(provider), apiIdentifier, certDTO.getCertificate(), certDTO.getAlias(), certDTO.getTierName(), organization);
        }
    } catch (APIManagementException e) {
        throw new APIManagementException("Error while importing client certificate", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 14 with ClientCertificateDTO

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

the class ImportUtils method retrieveClientCertificates.

public static List<ClientCertificateDTO> retrieveClientCertificates(String pathToArchive) throws APIManagementException {
    String jsonContent = null;
    String pathToClientCertificatesDirectory = pathToArchive + File.separator + ImportExportConstants.CLIENT_CERTIFICATES_DIRECTORY;
    String pathToYamlFile = pathToClientCertificatesDirectory + ImportExportConstants.CLIENT_CERTIFICATE_FILE + ImportExportConstants.YAML_EXTENSION;
    String pathToJsonFile = pathToClientCertificatesDirectory + ImportExportConstants.CLIENT_CERTIFICATE_FILE + ImportExportConstants.JSON_EXTENSION;
    try {
        // try loading file as YAML
        if (CommonUtil.checkFileExistence(pathToYamlFile)) {
            log.debug("Found client certificate file " + pathToYamlFile);
            String yamlContent = FileUtils.readFileToString(new File(pathToYamlFile));
            jsonContent = CommonUtil.yamlToJson(yamlContent);
        } else if (CommonUtil.checkFileExistence(pathToJsonFile)) {
            // load as a json fallback
            log.debug("Found client certificate file " + pathToJsonFile);
            jsonContent = FileUtils.readFileToString(new File(pathToJsonFile));
        }
        if (jsonContent == null) {
            log.debug("No client certificate file found to be added, skipping");
            return new ArrayList<>();
        }
        JsonElement configElement = new JsonParser().parse(jsonContent).getAsJsonObject().get(APIConstants.DATA);
        JsonArray modifiedCertificatesData = addFileContentToCertificates(configElement.getAsJsonArray(), pathToClientCertificatesDirectory);
        Gson gson = new Gson();
        return gson.fromJson(modifiedCertificatesData, new TypeToken<ArrayList<ClientCertificateDTO>>() {
        }.getType());
    } catch (IOException e) {
        throw new APIManagementException("Error in reading certificates file", e);
    }
}
Also used : JsonArray(com.google.gson.JsonArray) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) IOException(java.io.IOException) File(java.io.File) JsonParser(com.google.gson.JsonParser)

Example 15 with ClientCertificateDTO

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

the class ApisApiServiceImpl method getAPIClientCertificateByAlias.

@Override
public Response getAPIClientCertificateByAlias(String alias, String apiId, MessageContext messageContext) {
    String organization = null;
    CertificateMgtUtils certificateMgtUtils = CertificateMgtUtils.getInstance();
    try {
        organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        API api = apiProvider.getAPIbyUUID(apiId, organization);
        ClientCertificateDTO clientCertificateDTO = CertificateRestApiUtils.preValidateClientCertificate(alias, api.getId(), organization);
        CertificateInformationDTO certificateInformationDTO = certificateMgtUtils.getCertificateInfo(clientCertificateDTO.getCertificate());
        if (certificateInformationDTO != null) {
            CertificateInfoDTO certificateInfoDTO = CertificateMappingUtil.fromCertificateInformationToDTO(certificateInformationDTO);
            return Response.ok().entity(certificateInfoDTO).build();
        } else {
            RestApiUtil.handleResourceNotFoundError("Certificate is empty for alias " + alias, log);
        }
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving the client certificate with alias " + alias + " for the tenant " + organization, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) ClientCertificateDTO(org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO) CertificateInformationDTO(org.wso2.carbon.apimgt.api.dto.CertificateInformationDTO) CertificateMgtUtils(org.wso2.carbon.apimgt.impl.utils.CertificateMgtUtils) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) CertificateInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CertificateInfoDTO)

Aggregations

ClientCertificateDTO (org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO)22 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)11 ArrayList (java.util.ArrayList)9 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)9 Gson (com.google.gson.Gson)7 HashMap (java.util.HashMap)7 API (org.wso2.carbon.apimgt.api.model.API)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)6 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)5 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)5 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStream (java.io.InputStream)4 LinkedHashMap (java.util.LinkedHashMap)4 GatewayAPIDTO (org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO)4