use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.
the class APIControllerUtil method handleEndpointCertificates.
/**
* This method will be used to generate Endpoint certificates and meta information related to endpoint certs.
*
* @param certificates JsonArray of endpoint-certificates
* @param pathToArchive String of the archive project
* @throws IOException If an error occurs when generating new certs and yaml file or when moving certs
* @throws APIManagementException If an error while generating new directory
*/
private static void handleEndpointCertificates(JsonArray certificates, String pathToArchive) throws IOException, APIManagementException {
JsonArray updatedCertsArray = new JsonArray();
for (JsonElement certificate : certificates) {
JsonObject certObject = certificate.getAsJsonObject();
String alias = certObject.get(ImportExportConstants.ALIAS_JSON_KEY).getAsString();
CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
certificateMetadataDTO.setAlias(alias);
certificateMetadataDTO.setEndpoint(certObject.get(ImportExportConstants.CERTIFICATE_HOST_NAME_PROPERTY).getAsString());
// Add certificate element to cert object
JsonElement jsonElement = new Gson().toJsonTree(certificateMetadataDTO);
JsonObject updatedCertObj = jsonElement.getAsJsonObject();
String certName = certObject.get(ImportExportConstants.CERTIFICATE_PATH_PROPERTY).getAsString();
updatedCertObj.addProperty(ImportExportConstants.CERTIFICATE_FILE, certName);
updatedCertsArray.add(updatedCertObj);
// check and create a directory
String endpointCertificatesDirectory = pathToArchive + ImportExportConstants.ENDPOINT_CERTIFICATES_DIRECTORY_PATH;
if (!CommonUtil.checkFileExistence(endpointCertificatesDirectory)) {
try {
CommonUtil.createDirectory(endpointCertificatesDirectory);
} catch (APIImportExportException e) {
throw new APIManagementException(e);
}
}
// copy certs file from certificates
String userCertificatesTempDirectory = pathToArchive + ImportExportConstants.DEPLOYMENT_DIRECTORY + ImportExportConstants.CERTIFICATE_DIRECTORY;
String sourcePath = userCertificatesTempDirectory + File.separator + certName;
String destinationPath = endpointCertificatesDirectory + File.separator + certName;
if (Files.notExists(Paths.get(sourcePath))) {
String errorMessage = "The mentioned certificate file " + certName + " is not in the certificates directory";
throw new APIManagementException(errorMessage, ExceptionCodes.ERROR_READING_PARAMS_FILE);
}
CommonUtil.moveFile(sourcePath, destinationPath);
}
// generate meta-data yaml file
String metadataFilePath = pathToArchive + ImportExportConstants.ENDPOINT_CERTIFICATES_META_DATA_FILE_PATH;
try {
if (CommonUtil.checkFileExistence(metadataFilePath + ImportExportConstants.YAML_EXTENSION)) {
File oldFile = new File(metadataFilePath + ImportExportConstants.YAML_EXTENSION);
oldFile.delete();
}
if (CommonUtil.checkFileExistence(metadataFilePath + ImportExportConstants.JSON_EXTENSION)) {
File oldFile = new File(metadataFilePath + ImportExportConstants.JSON_EXTENSION);
oldFile.delete();
}
CommonUtil.writeDtoToFile(metadataFilePath, ExportFormat.JSON, ImportExportConstants.TYPE_ENDPOINT_CERTIFICATES, updatedCertsArray);
} catch (APIImportExportException e) {
throw new APIManagementException(e);
}
}
use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method generateMetadata.
private CertificateMetadataDTO generateMetadata() {
CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
certificateMetadataDTO.setAlias(ALIAS);
certificateMetadataDTO.setEndpoint(END_POINT);
return certificateMetadataDTO;
}
use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.
the class CertificateManagerImplTest method generateCertificates.
private List<CertificateMetadataDTO> generateCertificates() {
List<CertificateMetadataDTO> certificateMetadataDTOList = new ArrayList<CertificateMetadataDTO>();
for (int i = 0; i < 10; i++) {
CertificateMetadataDTO certificateMetadataDTO = new CertificateMetadataDTO();
certificateMetadataDTO.setAlias(ALIAS + "_" + i);
certificateMetadataDTO.setEndpoint(END_POINT + "_" + i);
certificateMetadataDTOList.add(certificateMetadataDTO);
}
return certificateMetadataDTOList;
}
use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO 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);
}
use of org.wso2.carbon.apimgt.api.dto.CertificateMetadataDTO in project carbon-apimgt by wso2.
the class EndpointCertificateDeployer method retrieveCertificatesAndDeploy.
private void retrieveCertificatesAndDeploy(CloseableHttpResponse closeableHttpResponse) throws IOException {
boolean tenantFlowStarted = false;
if (closeableHttpResponse.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(closeableHttpResponse.getEntity());
List<CertificateMetadataDTO> certificateMetadataDTOList;
Type listType = new TypeToken<List<CertificateMetadataDTO>>() {
}.getType();
certificateMetadataDTOList = new Gson().fromJson(content, listType);
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true);
tenantFlowStarted = true;
for (CertificateMetadataDTO certificateMetadataDTO : certificateMetadataDTOList) {
CertificateManagerImpl.getInstance().addCertificateToGateway(certificateMetadataDTO.getCertificate(), certificateMetadataDTO.getAlias());
}
} finally {
if (tenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
}
Aggregations