Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify implementation to allow an API and a Common policies with same and version #12660

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix incorrectly added policy type
  • Loading branch information
RusJaI committed Jan 3, 2025
commit ae7a057ceb2b8f1ed47edf37678036baf2bbd37f
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,9 @@ EnvironmentPropertiesDTO getEnvironmentSpecificAPIProperties(String apiUuid, Str
*/
String importOperationPolicy(OperationPolicyData operationPolicyData, String organization)
throws APIManagementException;
String importOperationPolicyOfGivenType(OperationPolicyData operationPolicyData, String policyType,
String organization) throws APIManagementException;

RusJaI marked this conversation as resolved.
Show resolved Hide resolved

/**
* Add an API specific operation policy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class OperationPolicy implements Comparable<OperationPolicy> {

private String policyName = "";
private String policyVersion = "v1";
private String policyType = null;
private String direction = null;
private Map<String, Object> parameters = null;
private String policyId = null;
Expand All @@ -50,6 +51,16 @@ public void setPolicyVersion(String policyVersion) {
this.policyVersion = policyVersion;
}

public String getPolicyType() {

return policyType;
}

public void setPolicyType(String policyType) {

this.policyType = policyType;
}

public Map<String, Object> getParameters() {

return parameters;
Expand Down Expand Up @@ -98,7 +109,7 @@ public boolean equals(Object o) {
if (o instanceof OperationPolicy) {
OperationPolicy policyObj = (OperationPolicy) o;
return Objects.equals(policyName, policyObj.policyName) && Objects.equals(policyVersion,
policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && Objects.equals(
policyObj.policyVersion) && Objects.equals(direction, policyObj.direction) && policyType.equals(policyObj.policyType) && Objects.equals(
RusJaI marked this conversation as resolved.
Show resolved Hide resolved
parameters, policyObj.parameters) && Objects.equals(policyId, policyObj.policyId);
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
import org.wso2.carbon.apimgt.impl.importexport.APIImportExportException;
import org.wso2.carbon.apimgt.impl.importexport.ExportFormat;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.lifecycle.CheckListItem;
import org.wso2.carbon.apimgt.impl.lifecycle.LCManagerFactory;
Expand Down Expand Up @@ -5280,6 +5281,53 @@ public String getSecuritySchemeOfAPI(String uuid, String organization) throws AP
}
}

public String getPolicyType(OperationPolicy policy, String apiUUId, String tenantDomain) throws APIManagementException {
String policyType = null;
if (policy.getPolicyId() == null) {
policyType = ImportExportConstants.POLICY_TYPE_MEDIATION;
} else {
OperationPolicyData basicPolicyData =
getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(),
apiUUId, tenantDomain, false);
if (basicPolicyData.getClonedCommonPolicyId() == null) {
policyType = ImportExportConstants.POLICY_TYPE_API;
} else {
policyType = ImportExportConstants.POLICY_TYPE_COMMON;
}
}
return policyType;
}

public API addPolicyTypeFieldToApi(API api, String tenantDomain)
throws APIManagementException {

Set<URITemplate> uriTemplates = api.getUriTemplates();
for (URITemplate uriTemplate : uriTemplates) {
List<OperationPolicy> operationPolicies = uriTemplate.getOperationPolicies();
if (!operationPolicies.isEmpty()) {
for (OperationPolicy operationPolicy : operationPolicies) {
String policyType = getPolicyType(operationPolicy, api.getUuid(),
tenantDomain);
operationPolicy.setPolicyType(policyType);
}
}
}
api.setUriTemplates(uriTemplates);

if (isAPILevelPolicySupportEnabled) {
List<OperationPolicy> apiPolicies = api.getApiPolicies();
for (OperationPolicy policy : apiPolicies) {
// TODO : confirm whether common polices were there in previous versions,
// if not this is always of api type
String policyType = getPolicyType(policy, api.getUuid(),
tenantDomain);
policy.setPolicyType(policyType);
}
api.setApiPolicies(apiPolicies);
}
return api;
}

@Override
public boolean isSubscriptionValidationDisabled(String uuid) throws APIManagementException {
String status = apiMgtDAO.getSubscriptionValidationStatus(uuid);
Expand Down Expand Up @@ -5322,6 +5370,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx
populateApiInfo(api);
populateSubtypeConfiguration(api);
populateDefaultVersion(api);
api = addPolicyTypeFieldToApi(api, organization);
return api;
} else {
String msg = "Failed to get API. API artifact corresponding to artifactId " + uuid + " does not exist";
Expand Down Expand Up @@ -7092,6 +7141,71 @@ public String importOperationPolicy(OperationPolicyData importedPolicyData, Stri

return policyId;
}
@Override
RusJaI marked this conversation as resolved.
Show resolved Hide resolved
public String importOperationPolicyOfGivenType(OperationPolicyData importedPolicyData, String policyType,
String organization)
throws APIManagementException {
RusJaI marked this conversation as resolved.
Show resolved Hide resolved

OperationPolicySpecification importedSpec = importedPolicyData.getSpecification();
OperationPolicyData existingOperationPolicy;

String policyId = null;
if (policyType == null){
importOperationPolicy(importedPolicyData, organization);
} else if (policyType.equalsIgnoreCase(ImportExportConstants.POLICY_TYPE_COMMON)) {
existingOperationPolicy = getCommonOperationPolicyByPolicyName(importedSpec.getName(),
importedSpec.getVersion(),organization, false);

if (existingOperationPolicy != null) {
if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching common policy found for imported policy and Md5 hashes match.");
}
policyId = existingOperationPolicy.getPolicyId();
} else {
importedSpec.setName(importedSpec.getName() + "_imported");
importedSpec.setDisplayName(importedSpec.getDisplayName() + " Imported");
importedPolicyData.setSpecification(importedSpec);
importedPolicyData.setMd5Hash(APIUtil.getMd5OfOperationPolicy(importedPolicyData));
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData,
organization);
if (log.isDebugEnabled()) {
log.debug("Even though existing common policy name match with imported policy, "
+ "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId()
+ ". A new policy created with ID " + policyId);
}
}
} else {
policyId = addAPISpecificOperationPolicy(importedPolicyData.getApiUUID(), importedPolicyData,
organization);
if (log.isDebugEnabled()) {
log.debug(
"There aren't any existing policies for the imported policy. A new policy created with ID "
+ policyId);
}
}
} else { //api level policy by default
existingOperationPolicy =
getAPISpecificOperationPolicyByPolicyName(importedSpec.getName(), importedSpec.getVersion(),
importedPolicyData.getApiUUID(), null, organization, false);

if (existingOperationPolicy.getMd5Hash().equals(importedPolicyData.getMd5Hash())) {
if (log.isDebugEnabled()) {
log.debug("Matching API specific policy found for imported policy and MD5 hashes match.");
}
} else {
if (log.isDebugEnabled()) {
log.debug("Even though existing API specific policy name match with imported policy, "
+ "the MD5 hashes does not match in the policy " + existingOperationPolicy.getPolicyId()
+ ".Therefore updating the existing policy");
}
updateOperationPolicy(existingOperationPolicy.getPolicyId(), importedPolicyData, organization);
}
policyId = existingOperationPolicy.getPolicyId();
}

return policyId;
}

@Override
public String addAPISpecificOperationPolicy(String apiUUID, OperationPolicyData operationPolicyData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO;
import org.wso2.carbon.apimgt.impl.factory.KeyManagerHolder;
import org.wso2.carbon.apimgt.impl.factory.SQLConstantManagerFactory;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.utils.APIMgtDBUtil;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
Expand Down Expand Up @@ -21197,7 +21198,8 @@ public Set<String> getCommonOperationPolicyNames(String organization) throws API
while (rs.next()) {
String policyName = rs.getString("POLICY_NAME");
String policyVersion = rs.getString("POLICY_VERSION");
policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion));
policyNames.add(APIUtil.getOperationPolicyFileName(policyName, policyVersion,
ImportExportConstants.POLICY_TYPE_COMMON));
}
}
} catch (SQLException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,9 @@ public final class ImportExportConstants {
public static final String EXPORT_POLICY_TYPE_JSON = "JSON";

public static final String POLICY_NAME = "name";

public static final String POLICY_TYPE_API = "api";
public static final String POLICY_TYPE_COMMON = "common";
public static final String POLICY_TYPE_MEDIATION = "mediation";

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
import org.wso2.carbon.apimgt.impl.dto.ThrottleProperties;
import org.wso2.carbon.apimgt.impl.dto.WorkflowDTO;
import org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.DataLoadingException;
import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants;
import org.wso2.carbon.apimgt.impl.internal.APIManagerComponent;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.kmclient.ApacheFeignHttpClient;
Expand Down Expand Up @@ -10089,7 +10090,7 @@ public static void loadCommonOperationPolicies(String organization) {
policyData.setSpecification(policySpec);
policyData.setOrganization(organization);
String policyFileName = getOperationPolicyFileName(policySpec.getName(),
policySpec.getVersion());
policySpec.getVersion(), ImportExportConstants.POLICY_TYPE_COMMON);
OperationPolicyDefinition synapsePolicyDefinition =
getOperationPolicyDefinitionFromFile(policyDefinitionLocation,
policyFileName, APIConstants.SYNAPSE_POLICY_DEFINITION_EXTENSION);
Expand Down Expand Up @@ -10413,11 +10414,14 @@ public static OperationPolicyData generateOperationPolicyDataObject(String apiUu
}


public static String getOperationPolicyFileName(String policyName, String policyVersion) {
public static String getOperationPolicyFileName(String policyName, String policyVersion, String policyType) {
if (StringUtils.isEmpty(policyVersion)) {
policyVersion = "v1";
}
return policyName + "_" + policyVersion;
if (policyType == null) {
return policyName + "_" + policyVersion;
}
return policyName + "_" + policyVersion + "_" + policyType;
}

public static String getCustomBackendFileName(String apiUUID, String endpointType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13533,6 +13533,8 @@ components:
policyVersion:
type: string
default: v1
policyType:
type: string
policyId:
type: string
parameters:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class OperationPolicyDTO {

private String policyName = null;
private String policyVersion = "v1";
private String policyType = null;
private String policyId = null;
private Map<String, Object> parameters = new HashMap<String, Object>();

Expand Down Expand Up @@ -63,6 +64,23 @@ public void setPolicyVersion(String policyVersion) {
this.policyVersion = policyVersion;
}

/**
**/
public OperationPolicyDTO policyType(String policyType) {
this.policyType = policyType;
return this;
}


@ApiModelProperty(value = "")
@JsonProperty("policyType")
public String getPolicyType() {
return policyType;
}
public void setPolicyType(String policyType) {
this.policyType = policyType;
}

/**
**/
public OperationPolicyDTO policyId(String policyId) {
Expand Down Expand Up @@ -109,13 +127,14 @@ public boolean equals(java.lang.Object o) {
OperationPolicyDTO operationPolicy = (OperationPolicyDTO) o;
return Objects.equals(policyName, operationPolicy.policyName) &&
Objects.equals(policyVersion, operationPolicy.policyVersion) &&
Objects.equals(policyType, operationPolicy.policyType) &&
Objects.equals(policyId, operationPolicy.policyId) &&
Objects.equals(parameters, operationPolicy.parameters);
}

@Override
public int hashCode() {
return Objects.hash(policyName, policyVersion, policyId, parameters);
return Objects.hash(policyName, policyVersion, policyType, policyId, parameters);
}

@Override
Expand All @@ -125,6 +144,7 @@ public String toString() {

sb.append(" policyName: ").append(toIndentedString(policyName)).append("\n");
sb.append(" policyVersion: ").append(toIndentedString(policyVersion)).append("\n");
sb.append(" policyType: ").append(toIndentedString(policyType)).append("\n");
sb.append(" policyId: ").append(toIndentedString(policyId)).append("\n");
sb.append(" parameters: ").append(toIndentedString(parameters)).append("\n");
sb.append("}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private static List<String> renderPolicyMapping(List<OperationPolicy> policyList
if (flow.equals(policy.getDirection())) {
Map<String, Object> policyParameters = policy.getParameters();
String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(),
policy.getPolicyVersion());
policy.getPolicyVersion(), policy.getPolicyType());
OperationPolicySpecification policySpecification = ImportUtils
.getOperationPolicySpecificationFromFile(policyDirectory, policyFileName);
if (policySpecification.getSupportedGateways()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena
for (OperationPolicy policy : operationPolicies) {
if (!exportedPolicies.contains(policy.getPolicyName() + "_" + policy.getPolicyVersion())) {
String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(),
policy.getPolicyVersion());
policy.getPolicyVersion(), policy.getPolicyType());
if (policy.getPolicyId() != null) {
OperationPolicyData policyData =
apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(),
Expand Down Expand Up @@ -748,7 +748,7 @@ public static void addOperationPoliciesToArchive(String archivePath, String tena
if (api.getApiPolicies() != null && !api.getApiPolicies().isEmpty()) {
for (OperationPolicy policy : api.getApiPolicies()) {
String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(),
policy.getPolicyVersion());
policy.getPolicyVersion(), policy.getPolicyType());
if (!exportedPolicies.contains(policyFileName)) {
OperationPolicyData policyData =
apiProvider.getAPISpecificOperationPolicyByPolicyId(policy.getPolicyId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public static void validateAppliedPolicy(OperationPolicy appliedPolicy,
String policyDirectory = extractedFolderPath + File.separator + ImportExportConstants.POLICIES_DIRECTORY;
appliedPolicy.setPolicyId(null);
String policyFileName = APIUtil.getOperationPolicyFileName(appliedPolicy.getPolicyName(),
appliedPolicy.getPolicyVersion());
appliedPolicy.getPolicyVersion(), appliedPolicy.getPolicyType());
OperationPolicySpecification policySpec = null;

if (visitedPoliciesMap.containsKey(policyFileName)) {
Expand Down Expand Up @@ -791,7 +791,7 @@ public static List<OperationPolicy> findOrImportPolicy(List<OperationPolicy> pol
boolean policyImported = false;
try {
String policyFileName = APIUtil.getOperationPolicyFileName(policy.getPolicyName(),
policy.getPolicyVersion());
policy.getPolicyVersion(), policy.getPolicyType());
String policyID = null;
if (!importedPolicies.containsKey(policyFileName)) {
OperationPolicySpecification policySpec =
Expand Down Expand Up @@ -824,7 +824,8 @@ public static List<OperationPolicy> findOrImportPolicy(List<OperationPolicy> pol
}
operationPolicyData.setMd5Hash(
APIUtil.getHashOfOperationPolicy(operationPolicyData));
policyID = provider.importOperationPolicy(operationPolicyData, tenantDomain);
policyID = provider.importOperationPolicyOfGivenType(operationPolicyData,
policy.getPolicyType(), tenantDomain);
importedPolicies.put(policyFileName, policyID);
policyImported = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static OperationPolicy fromDTOToOperationPolicy(OperationPolicyDTO operat
OperationPolicy operationPolicy = new OperationPolicy();
operationPolicy.setPolicyName(operationPolicyDTO.getPolicyName());
operationPolicy.setPolicyVersion(operationPolicyDTO.getPolicyVersion());
operationPolicy.setPolicyType(operationPolicyDTO.getPolicyType());
operationPolicy.setPolicyId(operationPolicyDTO.getPolicyId());
operationPolicy.setParameters(operationPolicyDTO.getParameters());
return operationPolicy;
Expand All @@ -67,6 +68,7 @@ public static OperationPolicyDTO fromOperationPolicyToDTO(OperationPolicy operat
OperationPolicyDTO dto = new OperationPolicyDTO();
dto.setPolicyName(operationPolicy.getPolicyName());
dto.setPolicyVersion(operationPolicy.getPolicyVersion());
dto.setPolicyType(operationPolicy.getPolicyType());
dto.setPolicyId(operationPolicy.getPolicyId());
dto.setParameters(operationPolicy.getParameters());
return dto;
Expand Down
Loading