From 2a61dfa3adbe247a4bdef92ca4e55b213a7bdb15 Mon Sep 17 00:00:00 2001 From: BLasan Date: Tue, 2 Jan 2024 21:05:02 +0530 Subject: [PATCH] Add: Configuration to enable/disable Policy Loading --- .../SynapseAnalyticsDataProvider.java | 1 - .../wso2/carbon/apimgt/impl/APIConstants.java | 3 ++ .../apimgt/impl/APIManagerConfiguration.java | 4 ++ .../impl/dao/SubscriptionValidationDAO.java | 19 +++++-- .../swagger.json | 39 ++++++++++++++ .../src/main/resources/publisher-api.yaml | 53 +++++++++++++++++++ .../repository/conf/api-manager.xml.j2 | 2 + 7 files changed, 115 insertions(+), 6 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java index 53ccbf0dde9e..efb2374af789 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/SynapseAnalyticsDataProvider.java @@ -169,7 +169,6 @@ public API getApi() throws DataNotFoundException { api.setApiCreator(apiObj.getApiProvider()); api.setApiCreatorTenantDomain(MultitenantUtils.getTenantDomain(api.getApiCreator())); List uriTemplates = new ArrayList<>(); - for (URLMapping uriTemplate : apiObj.getUrlMappings()) { org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate uriTemplateObj = new org.wso2.carbon.apimgt.common.analytics.publishers.dto.URITemplate(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 047139c8c59a..18fff7890b8e 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -44,6 +44,8 @@ public final class APIConstants { //governance registry apimgt root location public static final String APIMGT_REGISTRY_LOCATION = "/apimgt"; + public static final String POLICY_ENABLED_FOR_ANALYTICS = "policyEnabled"; + public static final String API_CONTEXT_ID = "api.context.id"; //This is the resource name of API public static final String API_RESOURCE_NAME = "/api"; @@ -973,6 +975,7 @@ private Permissions() { public static final String API_ANALYTICS = "Analytics."; public static final String API_USAGE_ENABLED = API_ANALYTICS + "Enabled"; + public static final String API_ANALYTICS_POLICY_ENABLED = API_ANALYTICS + "PolicyEnabled"; public static final String API_ANALYTICS_REPORTER_CLASS = API_ANALYTICS + "ReporterClass"; public static final String API_ANALYTICS_PROPERTIES = API_ANALYTICS + "Properties"; public static final String API_ANALYTICS_RESPONSE_SCHEMA_NAME = API_ANALYTICS + "ResponseSchemaName"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java index 78344794a64b..ae27c12eb4ac 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java @@ -363,6 +363,10 @@ private void readChildElements(OMElement serverConfig, OMElement analyticsType = element.getFirstChildWithName(new QName("Type")); analyticsProps.put("type", analyticsType.getText()); + + OMElement enablePolicy = element.getFirstChildWithName(new QName("PolicyEnabled")); + analyticsProps.put("policyEnabled", enablePolicy.getText()); + analyticsProperties = analyticsProps; } else if ("PersistenceConfigs".equals(localName)) { OMElement properties = element.getFirstChildWithName(new QName("Properties")); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java index 887ca65b04ee..d21c6be5cbe5 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java @@ -43,6 +43,7 @@ import org.wso2.carbon.apimgt.api.model.subscription.SubscriptionPolicy; import org.wso2.carbon.apimgt.api.model.subscription.URLMapping; import org.wso2.carbon.apimgt.impl.APIConstants; +import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; import org.wso2.carbon.apimgt.impl.ThrottlePolicyConstants; import org.wso2.carbon.apimgt.impl.dao.constants.SQLConstants; import org.wso2.carbon.apimgt.impl.dao.constants.SubscriptionValidationSQLConstants; @@ -63,6 +64,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import static org.wso2.carbon.apimgt.impl.APIConstants.POLICY_ENABLED_FOR_ANALYTICS; + /** * This Class used to handle DAO access for subscription Validation. */ @@ -70,6 +73,7 @@ public class SubscriptionValidationDAO { private static Log log = LogFactory.getLog(SubscriptionValidationDAO.class); private static String OPERATION_POLICY_ENABLE_WITH_ANALYTICS_EVENT = "operationPolicyEnableWithAnalyticsEvent"; + private static Map configs = APIManagerConfiguration.getAnalyticsProperties(); /* * This method can be used to retrieve all the Subscriptions in the database @@ -1174,8 +1178,11 @@ private void attachURlMappingDetailsOfApiProduct(Connection connection, API api, } } - if (Boolean.parseBoolean(System.getProperty(OPERATION_POLICY_ENABLE_WITH_ANALYTICS_EVENT))) { - attachPolicies(connection, revisionId, api); + if(configs.containsKey(POLICY_ENABLED_FOR_ANALYTICS)) { + boolean isPolicyEnabled = Boolean.parseBoolean(configs.get(POLICY_ENABLED_FOR_ANALYTICS)); + if (isPolicyEnabled) { + attachPolicies(connection, revisionId, api); + } } } @@ -1267,9 +1274,11 @@ private void attachURLMappingDetails(Connection connection, String revisionId, A } } - if (Boolean.parseBoolean(System.getProperty(OPERATION_POLICY_ENABLE_WITH_ANALYTICS_EVENT))) { - // Attach the relevant operation policies and api to the resources and to the API. - attachPolicies(connection, revisionId, api); + if(configs.containsKey(POLICY_ENABLED_FOR_ANALYTICS)) { + boolean isPolicyEnabled = Boolean.parseBoolean(configs.get(POLICY_ENABLED_FOR_ANALYTICS)); + if (isPolicyEnabled) { + attachPolicies(connection, revisionId, api); + } } } diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json b/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json index 9a89d4cfe705..7b508d0f1996 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json @@ -1108,6 +1108,12 @@ "example" : false, "description" : "Whether this is the default version of the API." }, + "apiPolicies" : { + "type" : "array", + "items" : { + "$ref" : "#/definitions/OperationPolicy" + } + }, "urlMappings" : { "type" : "array", "items" : { @@ -1703,9 +1709,42 @@ "items" : { "type" : "string" } + }, + "operationPolicies" : { + "type" : "array", + "items" : { + "$ref" : "#/definitions/OperationPolicy" + } } } }, + "OperationPolicy" : { + "properties" : { + "policyName" : { + "type" : "string" + }, + "policyVersion" : { + "type" : "string" + }, + "direction" : { + "type" : "string" + }, + "policyId" : { + "type" : "string" + }, + "order" : { + "type" : "integer" + }, + "parameters" : { + "type" : "object", + "additionalProperties" : { + "type" : "object", + "properties" : { } + } + } + }, + "title" : "Operation Policy" + }, "Pagination" : { "properties" : { "offset" : { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml index 87da4e93bcfe..3cda113b3788 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/publisher-api.yaml @@ -1807,6 +1807,40 @@ paths: source: 'curl -k -X POST -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" "https://127.0.0.1:9443/api/am/publisher/v4/apis/890a4f4d-09eb-4877-a323-57f6ce2ed79b/restore-revision?revisionId=e0824883-3e86-403a-aec1-22bbc454eb7c"' + /apis/{apiId}/cancel-revision-workflow/{revisionId}/{envName}: + #-------------------------------------------------------- + # Cancel a pending revision deployment workflow task given revision id and environment name + #-------------------------------------------------------- + delete: + tags: + - API Revisions + summary: Delete Pending Revision Deployment Workflow Tasks + description: | + This operation can be used to remove pending revision deployment requests that are in pending state + parameters: + - $ref: '#/components/parameters/apiId' + - $ref: '#/components/parameters/revisionId' + - $ref: '#/components/parameters/envName' + responses: + 200: + description: | + OK. + Revision deployment pending task removed successfully. + content: { } + 404: + $ref: '#/components/responses/NotFound' + 412: + $ref: '#/components/responses/PreconditionFailed' + security: + - OAuth2Security: + - apim:api_publish + - apim:api_manage + x-code-samples: + - lang: Curl + source: 'curl -k -X DELETE -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8" + "https://127.0.0.1:9443/api/am/publisher/v4/apis/890a4f4d-09eb-4877-a323-57f6ce2ed79b/cancel-revision-workflow/e0824883-3e86-403a-aec1-22bbc454eb7c/Default"' + operationId: deleteAPIRevisionDeploymentPendingTask + /apis/import-service: post: tags: @@ -9156,6 +9190,14 @@ components: minLength: 1 type: string example: Default + status: + type: string + example: CREATED + default: CREATED + enum: + - CREATED + - APPROVED + - REJECTED vhost: maxLength: 255 minLength: 1 @@ -12287,6 +12329,17 @@ components: schema: type: string + # API Revision Environment + # Specified as part of the path expression + envName: + name: envName + in: path + description: | + Environment name of an Revision + required: true + schema: + type: string + # API Revision Identifier # Specified as part of the query string revisionId-Q: diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/conf_templates/templates/repository/conf/api-manager.xml.j2 b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/conf_templates/templates/repository/conf/api-manager.xml.j2 index 850daa43de5f..213ede226f12 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/conf_templates/templates/repository/conf/api-manager.xml.j2 +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/conf_templates/templates/repository/conf/api-manager.xml.j2 @@ -325,6 +325,8 @@ {{apim.analytics.enable}} + {{apim.analytics.enablePolicy}} + {{apim.analytics.type}} {{apim.analytics.auth_token}}