Skip to content

Commit

Permalink
Fix: APICTL Import scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Sep 7, 2024
1 parent dcf2e6d commit eb6e555
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20902,14 +20902,15 @@ public void updateAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplat
}
}

public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type, String backendUUID) throws APIManagementException {
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND;
public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type,
String backendUUID) throws APIManagementException {
// delete current working copy
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) {
PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) {
connection.setAutoCommit(false);
prepStmt.setString(1, apiUUID);
prepStmt.setString(2, backendUUID);
prepStmt.setString(3, type);
prepStmt.setString(2, type);
prepStmt.executeUpdate();
addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID);
connection.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4405,7 +4405,8 @@ public static class CustomBackendConstants {
public static final String ADD_CUSTOM_BACKEND =
"INSERT INTO AM_API_CUSTOM_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?,?)";
public static final String DELETE_WORKING_COPY_OF_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = '0'";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND TYPE = ? AND REVISION_UUID = '0'";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND TYPE = ? AND ID = ? AND REVISION_UUID = '0'";
public static final String DELETE_CUSTOM_BACKEND_BY_API_AND_TYPE = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND TYPE = ? AND REVISION_UUID = '0'";
public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String DELETE_CUSTOM_BACKEND_BY_API = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT ID, NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9977,6 +9977,20 @@ public static void loadCommonOperationPolicies(String organization) {
}
}

public static InputStream getCustomBackendSequence(String extractedFolderPath, String customBackendFileName,
String fileExtension) throws APIManagementException {
String fileName = extractedFolderPath + File.separator + customBackendFileName + fileExtension;
InputStream inputStream = null;
if (checkFileExistence(fileName)) {
try {
inputStream = new FileInputStream(fileName);
} catch (IOException ex) {
handleException("Error reading Custom Backend " + customBackendFileName);
}
}
return inputStream;
}

/**
* Read the operation policy definition from the provided path and return the definition object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -125,6 +126,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.validation.constraints.NotNull;

/**
Expand Down Expand Up @@ -268,18 +270,25 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import
APIUtil.validateAPIEndpointConfig(importedApiDTO.getEndpointConfig(), importedApiDTO.getType().toString(),
importedApiDTO.getName());

Map endpointConfig = (Map) importedApiDTO.getEndpointConfig();

// if a valid one then update the sequence file path
if (endpointConfig != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) {
if (endpointConfig.get("sequence_name") != null) {
String sequenceFileName = endpointConfig.get("sequence_name").toString();
String path = extractedFolderPath + File.separator + sequenceFileName;
endpointConfig.put("sequence_path", path);
}
importedApiDTO.setEndpointConfig(endpointConfig);
}
// Map endpointConfig = (Map) importedApiDTO.getEndpointConfig();
//
// // if a valid one then update the sequence file path
// if (endpointConfig != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
// endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) {
// if (endpointConfig.get("sandbox") != null) {
// Map sandboxConfig = (Map) endpointConfig.get("sandbox");
// String seqName = sandboxConfig.get("sequence_name").toString();
// String path = extractedFolderPath + File.separator + seqName;
// String id = sandboxConfig.get("sequence_id").toString();
//
// }
// if (endpointConfig.get("sequence_name") != null) {
// String sequenceFileName = endpointConfig.get("sequence_name").toString();
// String path = extractedFolderPath + File.separator + sequenceFileName;
// endpointConfig.put("sequence_path", path);
// }
// importedApiDTO.setEndpointConfig(endpointConfig);
// }

API targetApi = retrieveApiToOverwrite(importedApiDTO.getName(), importedApiDTO.getVersion(),
currentTenantDomain, apiProvider, Boolean.TRUE, organization);
Expand Down Expand Up @@ -386,6 +395,13 @@ public static ImportedAPIDTO importApi(String extractedFolderPath, APIDTO import

populateAPIWithPolicies(importedApi, apiProvider, extractedFolderPath, extractedPoliciesMap,
extractedAPIPolicies, currentTenantDomain);
// Update Custom Backend Data if endpoint type is selected to "custom_backend"
Map endpointConf = (Map) importedApiDTO.getEndpointConfig();
if (endpointConf != null && APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConf.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) {
updateAPIWithCustomBackend(importedApi, extractedFolderPath, apiProvider);
}

API oldAPI = apiProvider.getAPIbyUUID(importedApi.getUuid(), importedApi.getOrganization());
apiProvider.updateAPI(importedApi, oldAPI);

Expand Down Expand Up @@ -674,6 +690,46 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy,
}
}

public static void updateAPIWithCustomBackend(API api, String extractedFolderPath, APIProvider apiProvider)
throws APIManagementException {
String customBackendDir = extractedFolderPath + File.separator + ImportExportConstants.CUSTOM_BACKEND_DIRECTORY;
JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();
ObjectMapper objectMapper = new ObjectMapper();
try {
if (endpointConfig != null) {
if (endpointConfig.get("sandbox") != null) {
JsonObject sandboxConf = endpointConfig.get("sandbox").getAsJsonObject();
String seqFile = sandboxConf.get("sequence_file").getAsString();
String type = sandboxConf.get("type").getAsString();
String seqName = APIUtil.getCustomBackendName(api.getUuid(), type);
String seqId = UUID.randomUUID().toString();
sandboxConf.addProperty("sequence_id", seqId);
sandboxConf.addProperty("sequence_name", seqName);
endpointConfig.add("sandbox", sandboxConf);
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
apiProvider.updateCustomBackend(api, type, seq, seqName, seqId);
}
if (endpointConfig.get("production") != null) {
JsonObject prodConf = endpointConfig.get("production").getAsJsonObject();
String seqFile = endpointConfig.get("sequence_file").getAsString();
String type = endpointConfig.get("type").getAsString();
String seqName = APIUtil.getCustomBackendName(api.getUuid(), type);
String seqId = UUID.randomUUID().toString();
prodConf.addProperty("sequence_id", seqId);
prodConf.addProperty("sequence_name", seqName);
endpointConfig.add("production", prodConf);
InputStream seq = APIUtil.getCustomBackendSequence(customBackendDir, seqFile, ".xml");
apiProvider.updateCustomBackend(api, type, seq, seqName, seqId);
}
Map<String, Object> endpointConfMap = new ObjectMapper().readValue(endpointConfig.toString(),
Map.class);
api.setEndpointConfig(objectMapper.writeValueAsString(endpointConfMap));
}
} catch (IOException ex) {
throw new APIManagementException("Error when updating Endpoint Configuration of API: " + api.getUuid());
}
}

/**
* This method is used to populate uri template of the API with API Level Policies and Operation Level Policies.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,26 +318,6 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
encryptEndpointSecurityOAuthCredentials(endpointConfig, cryptoUtil, oldProductionApiSecret, oldSandboxApiSecret,
apiDtoToUpdate);

// update endpointConfig with the provided custom sequence
if (endpointConfig != null) {
if (APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(
endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE))) {
try {
if (endpointConfig.get("sequence_path") != null) {
String pathToSequence = endpointConfig.get("sequence_path").toString();
String sequence = FileUtils.readFileToString(new File(pathToSequence),
Charset.defaultCharset());
endpointConfig.put("sequence", sequence);
apiDtoToUpdate.setEndpointConfig(endpointConfig);
}
} catch (IOException ex) {
throw new APIManagementException(
"Error while reading Custom Sequence of API: " + apiDtoToUpdate.getId(), ex,
ExceptionCodes.ERROR_READING_CUSTOM_SEQUENCE);
}
}
}

// AWS Lambda: secret key encryption while updating the API
if (apiDtoToUpdate.getEndpointConfig() != null) {
if (endpointConfig.containsKey(APIConstants.AMZN_SECRET_KEY)) {
Expand Down

0 comments on commit eb6e555

Please sign in to comment.