Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into ft_notification_t…
Browse files Browse the repository at this point in the history
…emplate_api
  • Loading branch information
RushanNanayakkara committed Oct 22, 2024
2 parents 157dbbb + bc6882c commit 18ea2ee
Show file tree
Hide file tree
Showing 293 changed files with 1,469 additions and 658 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>action-mgt</artifactId>
<version>7.5.70-SNAPSHOT</version>
<version>7.5.78-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -41,6 +41,10 @@
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.action.management</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.central.log.mgt</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -77,9 +81,13 @@
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Private-Package>
org.wso2.carbon.identity.action.execution.internal,
org.wso2.carbon.identity.action.execution.impl,
org.wso2.carbon.identity.action.execution.util,
</Private-Package>
<Export-Package>
!org.wso2.carbon.identity.action.execution.internal,
!org.wso2.carbon.identity.action.execution.impl,
!org.wso2.carbon.identity.action.execution.util,
org.wso2.carbon.identity.action.execution.*;
version="${carbon.identity.package.export.version}"
</Export-Package>
Expand All @@ -100,7 +108,9 @@
org.apache.http.impl.conn; version="${httpcomponents-httpclient.imp.pkg.version.range}",
org.osgi.framework; version="${osgi.framework.imp.pkg.version.range}",
org.osgi.service.component; version="${osgi.service.component.imp.pkg.version.range}",
org.wso2.carbon.identity.central.log.mgt.utils; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core.util; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.identity.core; version="${carbon.identity.package.import.version.range}",
org.wso2.carbon.utils; version="${carbon.kernel.package.import.version.range}",
com.fasterxml.jackson.core.*; version="${com.fasterxml.jackson.annotation.version.range}",
com.fasterxml.jackson.databind.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2024, 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.identity.action.execution;

/**
* Diagnostics Logs Constants for Action execution service.
*/
public class ActionExecutionLogConstants {

private ActionExecutionLogConstants() {}

public static final String ACTION_EXECUTION_COMPONENT_ID = "action-execution";

/**
* Action IDs.
*/
public static class ActionIDs {

public static final String EXECUTE_ACTION = "execute-action";
public static final String PROCESS_ACTION_REQUEST = "process-action-request";
public static final String SEND_ACTION_REQUEST = "send-action-request";
public static final String RECEIVE_ACTION_RESPONSE = "receive-action-response";
public static final String VALIDATE_ACTION_OPERATIONS = "validate-action-response";
public static final String EXECUTE_ACTION_OPERATIONS = "process-action-response";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
* under the License.
*/

package org.wso2.carbon.identity.action.execution;
package org.wso2.carbon.identity.action.execution.impl;

import org.wso2.carbon.identity.action.execution.ActionExecutionRequestBuilder;
import org.wso2.carbon.identity.action.execution.model.ActionExecutionRequest;
import org.wso2.carbon.identity.action.execution.model.ActionType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
* under the License.
*/

package org.wso2.carbon.identity.action.execution;
package org.wso2.carbon.identity.action.execution.impl;

import org.wso2.carbon.identity.action.execution.ActionExecutionResponseProcessor;
import org.wso2.carbon.identity.action.execution.model.ActionType;

import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
* under the License.
*/

package org.wso2.carbon.identity.action.execution;
package org.wso2.carbon.identity.action.execution.impl;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.identity.action.execution.ActionExecutionRequestBuilder;
import org.wso2.carbon.identity.action.execution.ActionExecutionResponseProcessor;
import org.wso2.carbon.identity.action.execution.ActionExecutorService;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionException;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionRequestBuilderException;
import org.wso2.carbon.identity.action.execution.exception.ActionExecutionResponseProcessorException;
Expand All @@ -40,6 +43,7 @@
import org.wso2.carbon.identity.action.execution.model.PerformableOperation;
import org.wso2.carbon.identity.action.execution.model.Request;
import org.wso2.carbon.identity.action.execution.util.APIClient;
import org.wso2.carbon.identity.action.execution.util.ActionExecutionDiagnosticLogger;
import org.wso2.carbon.identity.action.execution.util.ActionExecutorConfig;
import org.wso2.carbon.identity.action.execution.util.AuthMethods;
import org.wso2.carbon.identity.action.execution.util.OperationComparator;
Expand All @@ -48,13 +52,16 @@
import org.wso2.carbon.identity.action.management.model.Action;
import org.wso2.carbon.identity.action.management.model.AuthProperty;
import org.wso2.carbon.identity.action.management.model.Authentication;
import org.wso2.carbon.identity.central.log.mgt.utils.LoggerUtils;
import org.wso2.carbon.identity.core.ThreadLocalAwareExecutors;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

/**
Expand All @@ -66,8 +73,11 @@ public class ActionExecutorServiceImpl implements ActionExecutorService {

private static final Log LOG = LogFactory.getLog(ActionExecutorServiceImpl.class);

private static final int THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2;
private static final ActionExecutorServiceImpl INSTANCE = new ActionExecutorServiceImpl();
private static final ActionExecutionDiagnosticLogger DIAGNOSTIC_LOGGER = new ActionExecutionDiagnosticLogger();
private final APIClient apiClient;
private final ExecutorService executorService = ThreadLocalAwareExecutors.newFixedThreadPool(THREAD_POOL_SIZE);

private ActionExecutorServiceImpl() {

Expand Down Expand Up @@ -101,9 +111,10 @@ public ActionExecutionStatus execute(ActionType actionType, Map<String, Object>
validateActions(actions, actionType);
// As of now only one action is allowed.
Action action = actions.get(0);
DIAGNOSTIC_LOGGER.logActionInitiation(action);
return execute(action, eventContext);
} catch (ActionExecutionRuntimeException e) {
// todo: add to diagnostics
DIAGNOSTIC_LOGGER.logSkippedActionExecution(actionType);
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
}
Expand All @@ -123,10 +134,11 @@ public ActionExecutionStatus execute(ActionType actionType, String[] actionIdLis

validateActionIdList(actionType, actionIdList);
Action action = getActionByActionId(actionType, actionIdList[0], tenantDomain);
DIAGNOSTIC_LOGGER.logActionInitiation(action);
try {
return execute(action, eventContext);
} catch (ActionExecutionRuntimeException e) {
// todo: add to diagnostics
DIAGNOSTIC_LOGGER.logSkippedActionExecution(actionType);
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
}
Expand Down Expand Up @@ -259,7 +271,7 @@ private ActionInvocationResponse executeActionAsynchronously(Action action,

String apiEndpoint = action.getEndpoint().getUri();
CompletableFuture<ActionInvocationResponse> actionExecutor = CompletableFuture.supplyAsync(
() -> apiClient.callAPI(apiEndpoint, authenticationMethod, payload));
() -> apiClient.callAPI(apiEndpoint, authenticationMethod, payload), executorService);
try {
return actionExecutor.get();
} catch (InterruptedException | ExecutionException e) {
Expand All @@ -270,7 +282,7 @@ private ActionInvocationResponse executeActionAsynchronously(Action action,

private void logActionRequest(Action action, String payload) {

//todo: Add to diagnostics
DIAGNOSTIC_LOGGER.logActionRequest(action);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(
"Calling API: %s for action type: %s action id: %s with authentication: %s payload: %s",
Expand Down Expand Up @@ -315,13 +327,10 @@ private ActionExecutionStatus processSuccessResponse(Action action,
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException {

if (LOG.isDebugEnabled()) {
// todo: add to diagnostic logs
logSuccessResponse(action, successResponse);
}
logSuccessResponse(action, successResponse);

List<PerformableOperation> allowedPerformableOperations =
validatePerformableOperations(actionRequest, successResponse);
validatePerformableOperations(actionRequest, successResponse, action);
ActionInvocationSuccessResponse.Builder successResponseBuilder =
new ActionInvocationSuccessResponse.Builder().actionStatus(ActionInvocationResponse.Status.SUCCESS)
.operations(allowedPerformableOperations);
Expand Down Expand Up @@ -357,26 +366,29 @@ private ActionExecutionStatus processFailureResponse(Action action,

private void logSuccessResponse(Action action, ActionInvocationSuccessResponse successResponse) {

try {
String responseBody = serializeSuccessResponse(successResponse);
LOG.debug(String.format(
"Received success response from API: %s for action type: %s action id: %s with authentication: %s. "
+ "Response: %s",
action.getEndpoint().getUri(),
action.getType().getActionType(),
action.getId(),
action.getEndpoint().getAuthentication().getType(),
responseBody));
} catch (JsonProcessingException e) {
LOG.error("Error occurred while deserializing the success response for action: " +
action.getId() + " for action type: " + action.getType().getActionType(), e);
DIAGNOSTIC_LOGGER.logSuccessResponse(action);
if (LOG.isDebugEnabled()) {
try {
String responseBody = serializeSuccessResponse(successResponse);
LOG.debug(String.format(
"Received success response from API: %s for action type: %s action id: %s with " +
"authentication: %s. Response: %s",
action.getEndpoint().getUri(),
action.getType().getActionType(),
action.getId(),
action.getEndpoint().getAuthentication().getType(),
responseBody));
} catch (JsonProcessingException e) {
LOG.error("Error occurred while deserializing the success response for action: " +
action.getId() + " for action type: " + action.getType().getActionType(), e);
}
}
}

private void logErrorResponse(Action action, ActionInvocationErrorResponse errorResponse) {

DIAGNOSTIC_LOGGER.logErrorResponse(action);
if (LOG.isDebugEnabled()) {
// todo: add to diagnostic logs
try {
String responseBody = serializeErrorResponse(errorResponse);
LOG.debug(String.format(
Expand All @@ -396,8 +408,8 @@ private void logErrorResponse(Action action, ActionInvocationErrorResponse error

private void logFailureResponse(Action action, ActionInvocationFailureResponse failureResponse) {

DIAGNOSTIC_LOGGER.logFailureResponse(action);
if (LOG.isDebugEnabled()) {
// todo: add to diagnostic logs
try {
String responseBody = serializeFailureResponse(failureResponse);
LOG.debug(String.format(
Expand All @@ -416,7 +428,8 @@ private void logFailureResponse(Action action, ActionInvocationFailureResponse f
}

private void logErrorResponse(Action action, ActionInvocationResponse actionInvocationResponse) {
// todo: add to diagnostic logs

DIAGNOSTIC_LOGGER.logErrorResponse(action, actionInvocationResponse);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format(
"Failed to call API: %s for action type: %s action id: %s with authentication: %s. Error: %s",
Expand Down Expand Up @@ -455,8 +468,8 @@ private String serializeFailureResponse(ActionInvocationFailureResponse response
return objectMapper.writeValueAsString(response);
}

private List<PerformableOperation> validatePerformableOperations(ActionExecutionRequest request,
ActionInvocationSuccessResponse response) {
private List<PerformableOperation> validatePerformableOperations(
ActionExecutionRequest request, ActionInvocationSuccessResponse response, Action action) {

List<AllowedOperation> allowedOperations = request.getAllowedOperations();

Expand All @@ -466,22 +479,24 @@ private List<PerformableOperation> validatePerformableOperations(ActionExecution
performableOperation)))
.collect(Collectors.toList());

if (LOG.isDebugEnabled()) {
// todo: add to diagnostics
List<String> allowedOps = new ArrayList<>();
List<String> notAllowedOps = new ArrayList<>();

response.getOperations().forEach(operation -> {
String operationDetails = "Operation: " + operation.getOp() + " with path: " + operation.getPath();
if (allowedPerformableOperations.contains(operation)) {
allowedOps.add(operationDetails);
} else {
notAllowedOps.add(operationDetails);
if (LOG.isDebugEnabled() || LoggerUtils.isDiagnosticLogsEnabled()) {
List<String> allowedOps = new ArrayList<>();
List<String> notAllowedOps = new ArrayList<>();

response.getOperations().forEach(operation -> {
String operationDetails = "Operation: " + operation.getOp() + " Path: " + operation.getPath();
if (allowedPerformableOperations.contains(operation)) {
allowedOps.add(operationDetails);
} else {
notAllowedOps.add(operationDetails);
}
});
DIAGNOSTIC_LOGGER.logPerformableOperations(action, allowedOps, notAllowedOps);
if (LOG.isDebugEnabled()) {
LOG.debug("Allowed Operations: " + String.join(", ", allowedOps) +
". Not Allowed Operations: " + String.join(", ", notAllowedOps));
}
});
LOG.debug("Allowed Operations: " + String.join(", ", allowedOps) +
". Not Allowed Operations: " + String.join(", ", notAllowedOps));
}
}

return allowedPerformableOperations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.wso2.carbon.identity.action.execution.ActionExecutionRequestBuilder;
import org.wso2.carbon.identity.action.execution.ActionExecutionRequestBuilderFactory;
import org.wso2.carbon.identity.action.execution.ActionExecutionResponseProcessor;
import org.wso2.carbon.identity.action.execution.ActionExecutionResponseProcessorFactory;
import org.wso2.carbon.identity.action.execution.ActionExecutorService;
import org.wso2.carbon.identity.action.execution.ActionExecutorServiceImpl;
import org.wso2.carbon.identity.action.execution.impl.ActionExecutionRequestBuilderFactory;
import org.wso2.carbon.identity.action.execution.impl.ActionExecutionResponseProcessorFactory;
import org.wso2.carbon.identity.action.execution.impl.ActionExecutorServiceImpl;
import org.wso2.carbon.identity.action.management.ActionManagementService;

/**
Expand Down
Loading

0 comments on commit 18ea2ee

Please sign in to comment.