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;)", "&").replaceAll("<", "<").replaceAll(">", ">") + "</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;
}
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;
}
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);
}
}
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);
}
}
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;
}
Aggregations