Skip to content

Commit

Permalink
Start workflow config migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Avishka-Shamendra committed Nov 1, 2023
1 parent 2180de9 commit 59aaeea
Show file tree
Hide file tree
Showing 24 changed files with 654 additions and 595 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,6 @@ public final class APIConstants {
public static final String GA_CONF_KEY = "ga-config-key";
public static final String GA_CONF_MEDIA_TYPE = "ga-config";

public static final String WORKFLOW_EXECUTOR_LOCATION = API_APPLICATION_DATA_LOCATION + "/workflow-extensions.xml";

public static final String WORKFLOW_MEDIA_TYPE = "workflow-config";

// Constants used in API Security Audit feature
// For configs
public static final String API_SECURITY_AUDIT = "APISecurityAudit.";
Expand Down Expand Up @@ -1468,8 +1464,6 @@ private OAuthConstants() {
API_STORE + "RESTApiGroupingExtractor";
public static final String API_CUSTOM_SEQUENCES_FOLDER_LOCATION =
"repository" + File.separator + "resources" + File.separator + "customsequences";
public static final String WORKFLOW_EXTENSION_LOCATION =
"repository" + File.separator + "resources" + File.separator + "default-workflow-extensions.xml";
public static final String API_CUSTOM_SEQUENCE_TYPE_IN = "in";
public static final String API_CUSTOM_SEQUENCE_TYPE_OUT = "out";
public static final String API_CUSTOM_SEQUENCE_TYPE_FAULT = "fault";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public void updateExternalStoreConfig(String organization, String externalStoreC
public void updateTenantConfig(String organization, String tenantConfig) throws APIManagementException;

/**
* Retrieve workflow configuration relevant to to organization.
* Retrieve workflow configuration relevant to organization.
*
* @param organization organization of the user.
* @return workflow configuration.
* @throws APIManagementException throw if workflow-config couldn't retrieve.
*/
public String getWorkFlowConfig(String organization) throws APIManagementException;
public Object getWorkFlowConfig(String organization) throws APIManagementException;

/**
* Update workflow configuration relevant to to organization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
import org.wso2.carbon.apimgt.impl.caching.CacheProvider;
import org.wso2.carbon.apimgt.impl.dao.SystemConfigurationsDAO;
import org.wso2.carbon.apimgt.impl.dto.UserRegistrationConfigDTO;
import org.wso2.carbon.apimgt.impl.dto.WorkflowConfigDTO;
import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.apimgt.impl.workflow.WorkflowConstants;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.registry.core.ActionConstants;
Expand All @@ -42,11 +44,15 @@
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.registry.core.utils.RegistryUtils;
import org.wso2.carbon.user.api.UserStoreException;

import javax.cache.Cache;
import javax.cache.Caching;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
* Config Service Implementation for retrieve configurations.
Expand Down Expand Up @@ -188,101 +194,57 @@ public void updateTenantConfig(String organization, String tenantConfig) throws
Cache tenantConfigCache = CacheProvider.getTenantConfigCache();
String cacheName = organization + "_" + APIConstants.TENANT_CONFIG_CACHE_NAME;
tenantConfigCache.remove(cacheName);
String workflowCacheName = organization + "_" + APIConstants.WORKFLOW_CACHE_NAME;
Cache workflowCache = Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER)
.getCache(APIConstants.WORKFLOW_CACHE_NAME);
workflowCache.remove(workflowCacheName);

Check warning on line 200 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/config/APIMConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/config/APIMConfigServiceImpl.java#L197-L200

Added lines #L197 - L200 were not covered by tests
systemConfigurationsDAO.updateSystemConfig(organization, ConfigType.TENANT.toString(), tenantConfig);
}

@Override
public String getWorkFlowConfig(String organization) throws APIManagementException {
public WorkflowConfigDTO getWorkFlowConfig(String organization) throws APIManagementException {

if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService()
.getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(APIConstants.WORKFLOW_EXECUTOR_LOCATION)) {
Resource resource = registry.get(APIConstants.WORKFLOW_EXECUTOR_LOCATION);
return new String((byte[]) resource.getContent(), Charset.defaultCharset());
} else {
return null;
}

} catch (RegistryException e) {
String msg = "Error while retrieving External Stores Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
JsonObject tenantConfig = JsonParser.parseString(getTenantConfig(organization)).getAsJsonObject();
return getWorkFlowConfigDTO((JsonObject)
tenantConfig.get(WorkflowConstants.WF_TENANT_CONF_NAME));
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}

@Override
public void updateWorkflowConfig(String organization, String workflowConfig) throws APIManagementException {

if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService()
.getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(APIConstants.WORKFLOW_EXECUTOR_LOCATION)) {
Resource resource = registry.get(APIConstants.WORKFLOW_EXECUTOR_LOCATION);
byte[] data = IOUtils.toByteArray(new StringReader(workflowConfig));
resource.setContent(data);
resource.setMediaType(APIConstants.WORKFLOW_MEDIA_TYPE);
registry.put(APIConstants.WORKFLOW_EXECUTOR_LOCATION, resource);
private WorkflowConfigDTO getWorkFlowConfigDTO(JsonObject workflowConfig) {
WorkflowConfigDTO config = new WorkflowConfigDTO();
if (workflowConfig != null) {
Set<Map.Entry<String, JsonElement>> configEntries = workflowConfig.entrySet();
for (Map.Entry<String, JsonElement> entry : configEntries) {
String workflowName = entry.getKey();
JsonObject workflowConfigEntry = (JsonObject) entry.getValue();

boolean isEnabled = workflowConfigEntry.get(WorkflowConstants.WF_TENANT_CONF_ENABLED) != null
&& workflowConfigEntry.get(WorkflowConstants.WF_TENANT_CONF_ENABLED).getAsBoolean();
String className = workflowConfigEntry.get(WorkflowConstants.WF_TENANT_CONF_CLASS) != null ?
workflowConfigEntry.get(WorkflowConstants.WF_TENANT_CONF_CLASS).getAsString() : "";
JsonObject properties = workflowConfigEntry.get(WorkflowConstants.WF_TENANT_CONF_PROPERTIES) != null ?
workflowConfigEntry.get(WorkflowConstants.WF_TENANT_CONF_PROPERTIES).getAsJsonObject() : null;

config.addWorkflowConfig(workflowName, isEnabled, className, properties);
}
} catch (RegistryException | IOException e) {
String msg = "Error while retrieving External Stores Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return config;
}

@Override
public void addWorkflowConfig(String organization, String workflowConfig) throws APIManagementException {

if (organization == null) {
organization = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(organization, true);
int tenantId = APIUtil.getTenantIdFromTenantDomain(organization);
if (!MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(organization)) {
APIUtil.loadTenantRegistry(tenantId);
}
UserRegistry registry = ServiceReferenceHolder.getInstance().getRegistryService()
.getGovernanceSystemRegistry(tenantId);
if (!registry.resourceExists(APIConstants.WORKFLOW_EXECUTOR_LOCATION)) {
Resource resource = registry.newResource();
byte[] data = IOUtils.toByteArray(new StringReader(workflowConfig));
resource.setContent(data);
resource.setMediaType(APIConstants.WORKFLOW_MEDIA_TYPE);
registry.put(APIConstants.WORKFLOW_EXECUTOR_LOCATION, resource);
}
public void updateWorkflowConfig(String organization, String workflowConfig) throws APIManagementException {
}

Check warning on line 244 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/config/APIMConfigServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/config/APIMConfigServiceImpl.java#L244

Added line #L244 was not covered by tests

} catch (RegistryException | IOException e) {
String msg = "Error while retrieving External Stores Configuration from registry";
log.error(msg, e);
throw new APIManagementException(msg, e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
@Override
public void addWorkflowConfig(String organization, String workflowConfig) throws APIManagementException {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.apimgt.impl.dto;

import com.google.gson.JsonObject;

import java.util.HashMap;
import java.util.Map;

/**
* Workflow configurations DTO
*/
public class WorkflowConfigDTO {

private final Map<String, WorkflowConfig> workflowConfigMap;

public WorkflowConfigDTO() {
workflowConfigMap = new HashMap<>();
}

public void addWorkflowConfig(String workflowName, boolean enabled, String className, JsonObject properties) {
WorkflowConfig workflowConfig = new WorkflowConfig(enabled, className, properties);
workflowConfigMap.put(workflowName, workflowConfig);
}

public Map<String, WorkflowConfig> getWorkflowConfigMap() {
return workflowConfigMap;
}


public static class WorkflowConfig {
private boolean enabled;
private String className;
private JsonObject properties;

private WorkflowConfig(boolean enabled, String className, JsonObject properties) {

this.enabled = enabled;
this.className = className;
this.properties = properties;
}

public boolean isEnabled() {

return enabled;
}

public void setEnabled(boolean enabled) {

this.enabled = enabled;
}

Check warning on line 67 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/WorkflowConfigDTO.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/WorkflowConfigDTO.java#L66-L67

Added lines #L66 - L67 were not covered by tests

public String getClassName() {

return className;
}

public void setClassName(String className) {

this.className = className;
}

Check warning on line 77 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/WorkflowConfigDTO.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/WorkflowConfigDTO.java#L76-L77

Added lines #L76 - L77 were not covered by tests

public JsonObject getProperties() {

return properties;
}

public void setProperties(JsonObject properties) {

this.properties = properties;
}

Check warning on line 87 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/WorkflowConfigDTO.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dto/WorkflowConfigDTO.java#L86-L87

Added lines #L86 - L87 were not covered by tests
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ protected void activate(ComponentContext componentContext) throws Exception {
APIUtil.loadAndSyncTenantConf(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
APIUtil.loadTenantExternalStoreConfig(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
APIUtil.loadTenantGAConfig(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
APIUtil.loadTenantWorkFlowExtensions(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
APIUtil.loadCommonOperationPolicies(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
APIManagerAnalyticsConfiguration analyticsConfiguration = APIManagerAnalyticsConfiguration.getInstance();
analyticsConfiguration.setAPIManagerConfiguration(configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,6 @@ public void run() {
log.error("Failed to load ga-config.xml to tenant " + tenantDomain + "'s registry", e);
}

try {
//load workflow-extension configuration to the registry
APIUtil.loadTenantWorkFlowExtensions(tenantDomain);
} catch (Exception e) {
log.error("Failed to load workflow-extension.xml to tenant " + tenantDomain + "'s registry", e);
}

try {
APIUtil.loadAndSyncTenantConf(tenantDomain);
} catch (APIManagementException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2923,31 +2923,6 @@ public static void loadTenantGAConfig(String organization) throws APIManagementE
}
}

public static void loadTenantWorkFlowExtensions(String organization)
throws APIManagementException {

try {
String workflowExtensionLocation =
CarbonUtils.getCarbonHome() + File.separator + APIConstants.WORKFLOW_EXTENSION_LOCATION;

File wfExtension = new File(workflowExtensionLocation);
if (log.isDebugEnabled()) {
log.debug("Adding External Stores configuration to the tenant's registry");
}

InputStream inputStream;
if (wfExtension.exists()) {
inputStream = new FileInputStream(workflowExtensionLocation);
} else {
inputStream = APIManagerComponent.class
.getResourceAsStream("/workflowextensions/default-workflow-extensions.xml");
}
ServiceReferenceHolder.getInstance().getApimConfigService().addWorkflowConfig(organization, IOUtils.toString(inputStream));
} catch (IOException e) {
throw new APIManagementException("Error while reading Workflow configuration file content", e);
}
}

/**
* Load tenant self sign up configurations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowExceptio
}
} else {
String msg =
"State change list is not provided. Please check <stateList> element in workflow-extensions" +
".xml";
"State change list is not provided. Please check \"StateList\" property in the workflow configuration.";
log.error(msg);
throw new WorkflowException(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public WorkflowResponse execute(WorkflowDTO workflowDTO) throws WorkflowExceptio
super.complete(workflowDTO);
}
} else {
String msg = "State change list is not provided. Please check <stateList> element in workflow-extensions.xml";
String msg = "State change list is not provided. Please check \"StateList\" property in the workflow configuration.";

Check warning on line 87 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/workflow/APIStateChangeApprovalWorkflowExecutor.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/workflow/APIStateChangeApprovalWorkflowExecutor.java#L87

Added line #L87 was not covered by tests
log.error(msg);
throw new WorkflowException(msg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public void cleanUpPendingTask(String workflowExtRef) throws WorkflowException {
*/
private String getBasicAuthHeader() {

// if credentials are not defined in the workflow-extension.xml file, then get the global credentials from the
// if credentials are not defined in the workflow config, then get the global credentials from the
// api-manager.xml configuration
if (username == null || password == null) {
WorkflowProperties workflowProperties = ServiceReferenceHolder.getInstance()
Expand Down Expand Up @@ -472,7 +472,7 @@ private String buildPayloadForBPMNProcess(APIStateWorkflowDTO apiStateWorkFlowDT
* set information that are needed to invoke callback service
*/
private void setOAuthApplicationInfo(APIStateWorkflowDTO apiStateWorkFlowDTO) throws WorkflowException {
// if credentials are not defined in the workflow-extension.xml file call dcr endpoint and generate a
// if credentials are not defined in the workflow config call dcr endpoint and generate a
// oauth application and pass the client id and secret
WorkflowProperties workflowProperties = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService()
.getAPIManagerConfiguration().getWorkflowProperties();
Expand Down Expand Up @@ -546,7 +546,7 @@ private void setOAuthApplicationInfo(APIStateWorkflowDTO apiStateWorkFlowDTO) th
}

/**
* Read the user provided lifecycle states for the approval task. These are provided in the workflow-extension.xml
* Read the user provided lifecycle states for the approval task. These are provided in the workflow config
*/
private Map<String, List<String>> getSelectedStatesToApprove() {
Map<String, List<String>> stateAction = new HashMap<String, List<String>>();
Expand Down
Loading

0 comments on commit 59aaeea

Please sign in to comment.