Skip to content

Commit

Permalink
Merge branch 'wso2:master' into add-cutsom-auth-configs-service-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thisara-Welmilla authored Nov 11, 2024
2 parents 86c5ebf + 6ffda9f commit 4a85b28
Show file tree
Hide file tree
Showing 348 changed files with 4,348 additions and 3,092 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.94-SNAPSHOT</version>
<version>7.6.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -173,6 +173,7 @@
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<!-- temp decreasing the coverage -->
<minimum>0.79</minimum>
</limit>
<limit implementation="org.jacoco.report.check.Limit">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class ActionIDs {
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";
public static final String VALIDATE_ACTION_RESPONSE = "validate-action-response";
public static final String PROCESS_ACTION_RESPONSE = "process-action-response";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
import org.wso2.carbon.identity.action.execution.model.ActionInvocationFailureResponse;
import org.wso2.carbon.identity.action.execution.model.ActionInvocationSuccessResponse;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.Error;
import org.wso2.carbon.identity.action.execution.model.ErrorStatus;
import org.wso2.carbon.identity.action.execution.model.Event;
import org.wso2.carbon.identity.action.execution.model.FailedStatus;
import org.wso2.carbon.identity.action.execution.model.Failure;
import org.wso2.carbon.identity.action.execution.model.Success;

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

/**
Expand All @@ -38,21 +42,26 @@ public interface ActionExecutionResponseProcessor {

ActionType getSupportedActionType();

ActionExecutionStatus processSuccessResponse(Map<String, Object> eventContext,
Event actionEvent,
ActionInvocationSuccessResponse successResponse) throws
ActionExecutionStatus<Success> processSuccessResponse(Map<String, Object> eventContext,
Event actionEvent,
ActionInvocationSuccessResponse successResponse) throws
ActionExecutionResponseProcessorException;

ActionExecutionStatus processErrorResponse(Map<String, Object> eventContext,
Event actionEvent,
ActionInvocationErrorResponse errorResponse) throws
ActionExecutionResponseProcessorException;
default ActionExecutionStatus<Error> processErrorResponse(Map<String, Object> eventContext,
Event actionEvent,
ActionInvocationErrorResponse errorResponse) throws
ActionExecutionResponseProcessorException {

return new ErrorStatus(new Error(errorResponse.getErrorMessage(), errorResponse.getErrorDescription()));
}

default ActionExecutionStatus processFailureResponse(Map<String, Object> eventContext,
Event actionEvent,
ActionInvocationFailureResponse failureResponse) throws
default ActionExecutionStatus<Failure> processFailureResponse(Map<String, Object> eventContext,
Event actionEvent,
ActionInvocationFailureResponse failureResponse)
throws
ActionExecutionResponseProcessorException {

return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, new HashMap<>());
return new FailedStatus(new Failure(failureResponse.getFailureReason(),
failureResponse.getFailureDescription()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@
import org.wso2.carbon.identity.action.execution.model.ActionInvocationSuccessResponse;
import org.wso2.carbon.identity.action.execution.model.ActionType;
import org.wso2.carbon.identity.action.execution.model.AllowedOperation;
import org.wso2.carbon.identity.action.execution.model.Error;
import org.wso2.carbon.identity.action.execution.model.Failure;
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.model.Success;
import org.wso2.carbon.identity.action.execution.model.SuccessStatus;
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;
Expand All @@ -58,7 +62,6 @@
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;
Expand Down Expand Up @@ -103,8 +106,8 @@ public boolean isExecutionEnabled(ActionType actionType) {
* @param tenantDomain Tenant domain.
* @return Action execution status.
*/
public ActionExecutionStatus execute(ActionType actionType, Map<String, Object> eventContext, String tenantDomain)
throws ActionExecutionException {
public ActionExecutionStatus<?> execute(ActionType actionType, Map<String, Object> eventContext,
String tenantDomain) throws ActionExecutionException {

try {
List<Action> actions = getActionsByActionType(actionType, tenantDomain);
Expand All @@ -116,7 +119,9 @@ public ActionExecutionStatus execute(ActionType actionType, Map<String, Object>
} catch (ActionExecutionRuntimeException e) {
DIAGNOSTIC_LOGGER.logSkippedActionExecution(actionType);
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
// Skip executing actions when no action available or due to a failure in retrieving actions,
// is considered as action execution being successful.
return new SuccessStatus.Builder().setResponseContext(eventContext).build();
}
}

Expand All @@ -129,8 +134,9 @@ public ActionExecutionStatus execute(ActionType actionType, Map<String, Object>
* @param tenantDomain Tenant domain.
* @return Action execution status.
*/
public ActionExecutionStatus execute(ActionType actionType, String[] actionIdList, Map<String, Object> eventContext,
String tenantDomain) throws ActionExecutionException {
public ActionExecutionStatus<?> execute(ActionType actionType, String[] actionIdList,
Map<String, Object> eventContext, String tenantDomain)
throws ActionExecutionException {

validateActionIdList(actionType, actionIdList);
Action action = getActionByActionId(actionType, actionIdList[0], tenantDomain);
Expand All @@ -140,7 +146,9 @@ public ActionExecutionStatus execute(ActionType actionType, String[] actionIdLis
} catch (ActionExecutionRuntimeException e) {
DIAGNOSTIC_LOGGER.logSkippedActionExecution(actionType);
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
// Skip executing actions when no action available or due to a failure in retrieving actions,
// is considered as action execution being successful.
return new SuccessStatus.Builder().setResponseContext(eventContext).build();
}
}

Expand All @@ -156,18 +164,19 @@ private void validateActionIdList(ActionType actionType, String[] actionIdList)
}
}

private ActionExecutionStatus execute(Action action, Map<String, Object> eventContext)
private ActionExecutionStatus<?> execute(Action action, Map<String, Object> eventContext)
throws ActionExecutionException {

ActionType actionType = ActionType.valueOf(action.getType().getActionType());
ActionExecutionRequest actionRequest = buildActionExecutionRequest(actionType, eventContext);
ActionExecutionResponseProcessor actionExecutionResponseProcessor = getResponseProcessor(actionType);

return Optional.ofNullable(action)
.filter(activeAction -> activeAction.getStatus() == Action.Status.ACTIVE)
.map(activeAction -> executeAction(activeAction, actionRequest, eventContext,
actionExecutionResponseProcessor))
.orElse(new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext));
if (action.getStatus() == Action.Status.ACTIVE) {
return executeAction(action, actionRequest, eventContext, actionExecutionResponseProcessor);
} else {
// If no active actions are detected, it is regarded as the action execution being successful.
return new SuccessStatus.Builder().setResponseContext(eventContext).build();
}
}

private Action getActionByActionId(ActionType actionType, String actionId, String tenantDomain)
Expand Down Expand Up @@ -226,7 +235,7 @@ private ActionExecutionRequest buildActionExecutionRequest(ActionType actionType
request.setAdditionalParams(RequestFilter.getFilteredParams(request.getAdditionalParams(), actionType));
return actionExecutionRequest;
} catch (ActionExecutionRequestBuilderException e) {
throw new ActionExecutionRuntimeException("Error occurred while building the request payload.", e);
throw new ActionExecutionException("Failed to build the request payload for action type: " + actionType, e);
}
}

Expand All @@ -241,11 +250,11 @@ private ActionExecutionResponseProcessor getResponseProcessor(ActionType actionT
return responseProcessor;
}

private ActionExecutionStatus executeAction(Action action,
ActionExecutionRequest actionRequest,
Map<String, Object> eventContext,
ActionExecutionResponseProcessor actionExecutionResponseProcessor)
throws ActionExecutionRuntimeException {
private ActionExecutionStatus<?> executeAction(Action action,
ActionExecutionRequest actionRequest,
Map<String, Object> eventContext,
ActionExecutionResponseProcessor actionExecutionResponseProcessor)
throws ActionExecutionException {

Authentication endpointAuthentication = action.getEndpoint().getAuthentication();
AuthMethods.AuthMethod authenticationMethod;
Expand All @@ -261,21 +270,21 @@ private ActionExecutionStatus executeAction(Action action,
return processActionResponse(action, actionInvocationResponse, eventContext, actionRequest,
actionExecutionResponseProcessor);
} catch (ActionMgtException | JsonProcessingException | ActionExecutionResponseProcessorException e) {
throw new ActionExecutionRuntimeException("Error occurred while executing action: " + action.getId(), e);
throw new ActionExecutionException("Error occurred while executing action: " + action.getId(), e);
}
}

private ActionInvocationResponse executeActionAsynchronously(Action action,
AuthMethods.AuthMethod authenticationMethod,
String payload) {
String payload) throws ActionExecutionException {

String apiEndpoint = action.getEndpoint().getUri();
CompletableFuture<ActionInvocationResponse> actionExecutor = CompletableFuture.supplyAsync(
() -> apiClient.callAPI(apiEndpoint, authenticationMethod, payload), executorService);
try {
return actionExecutor.get();
} catch (InterruptedException | ExecutionException e) {
throw new ActionExecutionRuntimeException("Error occurred while executing action: " + action.getId(),
throw new ActionExecutionException("Error occurred while executing action: " + action.getId(),
e);
}
}
Expand All @@ -294,13 +303,13 @@ private void logActionRequest(Action action, String payload) {
}
}

private ActionExecutionStatus processActionResponse(Action action,
ActionInvocationResponse actionInvocationResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException {
private ActionExecutionStatus<?> processActionResponse(Action action,
ActionInvocationResponse actionInvocationResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException, ActionExecutionException {

if (actionInvocationResponse.isSuccess()) {
return processSuccessResponse(action,
Expand All @@ -312,18 +321,17 @@ private ActionExecutionStatus processActionResponse(Action action,
} else if (actionInvocationResponse.isError() && actionInvocationResponse.getResponse() != null) {
return processErrorResponse(action, (ActionInvocationErrorResponse) actionInvocationResponse.getResponse(),
eventContext, actionRequest, actionExecutionResponseProcessor);
} else {
logErrorResponse(action, actionInvocationResponse);
}

return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
logErrorResponse(action, actionInvocationResponse);
throw new ActionExecutionException("Received an invalid or unexpected response for action type: "
+ action.getType() + " action ID: " + action.getId());
}

private ActionExecutionStatus processSuccessResponse(Action action,
ActionInvocationSuccessResponse successResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
private ActionExecutionStatus<Success> processSuccessResponse(Action action,
ActionInvocationSuccessResponse successResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException {

Expand All @@ -338,11 +346,11 @@ private ActionExecutionStatus processSuccessResponse(Action action,
actionRequest.getEvent(), successResponseBuilder.build());
}

private ActionExecutionStatus processErrorResponse(Action action,
ActionInvocationErrorResponse errorResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
private ActionExecutionStatus<Error> processErrorResponse(Action action,
ActionInvocationErrorResponse errorResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException {

Expand All @@ -351,11 +359,11 @@ private ActionExecutionStatus processErrorResponse(Action action,
errorResponse);
}

private ActionExecutionStatus processFailureResponse(Action action,
ActionInvocationFailureResponse failureResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
private ActionExecutionStatus<Failure> processFailureResponse(Action action,
ActionInvocationFailureResponse failureResponse,
Map<String, Object> eventContext,
ActionExecutionRequest actionRequest,
ActionExecutionResponseProcessor
actionExecutionResponseProcessor)
throws ActionExecutionResponseProcessorException {

Expand Down
Loading

0 comments on commit 4a85b28

Please sign in to comment.