From 06e45e0832ef69ae162a93dde38413300b844412 Mon Sep 17 00:00:00 2001 From: BLasan Date: Mon, 9 Oct 2023 12:10:19 +0530 Subject: [PATCH] fixes: https://github.com/wso2/api-manager/issues/2194 --- .../v1/common/mappings/ImportUtils.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java index 758925f4537b..8a0c67dbc5aa 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/ImportUtils.java @@ -358,7 +358,8 @@ public static API importApi(String extractedFolderPath, APIDTO importedApiDTO, B if (log.isDebugEnabled()) { log.debug("Mutual SSL enabled. Importing client certificates."); } - addClientCertificates(extractedFolderPath, apiProvider, new ApiTypeWrapper(importedApi), organization); + addClientCertificates(extractedFolderPath, apiProvider, new ApiTypeWrapper(importedApi), organization, + overwrite, tenantId); } // Change API lifecycle if state transition is required @@ -2164,16 +2165,20 @@ private static void updateAPIWithCertificate(JsonElement certificate, APIProvide * @throws APIImportExportException */ private static void addClientCertificates(String pathToArchive, APIProvider apiProvider, - ApiTypeWrapper apiTypeWrapper, String organization) + ApiTypeWrapper apiTypeWrapper, String organization, boolean isOverwrite, int tenantId) throws APIManagementException { try { - Identifier apiIdentifier = apiTypeWrapper.getId(); + Identifier apiIdentifier = apiTypeWrapper.getId(); List certificateMetadataDTOS = retrieveClientCertificates(pathToArchive); for (ClientCertificateDTO certDTO : certificateMetadataDTOS) { - apiProvider.addClientCertificate(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()), - apiTypeWrapper, certDTO.getCertificate(), certDTO.getAlias(), certDTO.getTierName(), - organization); + if (ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE.getResponseCode() == (apiProvider.addClientCertificate( + APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()), apiTypeWrapper, + certDTO.getCertificate(), certDTO.getAlias(), certDTO.getTierName(), organization)) + && isOverwrite) { + apiProvider.updateClientCertificate(certDTO.getCertificate(), certDTO.getAlias(), apiTypeWrapper, + certDTO.getTierName(), tenantId, organization); + } } } catch (APIManagementException e) { throw new APIManagementException("Error while importing client certificate", e); @@ -2461,7 +2466,9 @@ public static APIProduct importApiProduct(String extractedFolderPath, Boolean pr if (log.isDebugEnabled()) { log.debug("Mutual SSL enabled. Importing client certificates."); } - addClientCertificates(extractedFolderPath, apiProvider, apiTypeWrapperWithUpdatedApiProduct, organization); + int tenantId = APIUtil.getTenantId(RestApiCommonUtil.getLoggedInUsername()); + addClientCertificates(extractedFolderPath, apiProvider, apiTypeWrapperWithUpdatedApiProduct, organization, + overwriteAPIProduct, tenantId); // Change API Product lifecycle if state transition is required if (!lifecycleActions.isEmpty()) {