Skip to content

Commit

Permalink
Add diagnostics logs to action execution
Browse files Browse the repository at this point in the history
  • Loading branch information
osandamaleesha committed Sep 27, 2024
1 parent 17ecd1a commit 6dbfd14
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<artifactId>mockito-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.identity.framework</groupId>
<artifactId>org.wso2.carbon.identity.application.authentication.framework</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
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.utils.DiagnosticLog;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -57,6 +59,8 @@
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants;

/**
* This class is responsible for executing the action based on the action type and the event context.
* It is responsible for building the request payload, calling the API, processing the response and
Expand Down Expand Up @@ -99,11 +103,33 @@ public ActionExecutionStatus execute(ActionType actionType, Map<String, Object>
try {
List<Action> actions = getActionsByActionType(actionType, tenantDomain);
validateActions(actions, actionType);
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.EXECUTE_ACTION);
diagLogBuilder
.resultMessage(actionType + " action execution is initiated ")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.SUCCESS)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
// As of now only one action is allowed.
Action action = actions.get(0);
return execute(action, eventContext);
} catch (ActionExecutionRuntimeException e) {
// todo: add to diagnostics
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.EXECUTE_ACTION);
diagLogBuilder
.resultMessage("Skip executing actions for " + actionType + " type")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
}
Expand All @@ -127,6 +153,17 @@ public ActionExecutionStatus execute(ActionType actionType, String[] actionIdLis
return execute(action, eventContext);
} catch (ActionExecutionRuntimeException e) {
// todo: add to diagnostics
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.EXECUTE_ACTION);
diagLogBuilder
.resultMessage("Skip executing actions for " + actionType + " type")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
LOG.debug("Skip executing actions for action type: " + actionType.name(), e);
return new ActionExecutionStatus(ActionExecutionStatus.Status.FAILED, eventContext);
}
Expand Down Expand Up @@ -271,6 +308,23 @@ private ActionInvocationResponse executeActionAsynchronously(Action action,
private void logActionRequest(Action action, String payload) {

//todo: Add to diagnostics
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.PROCESS_ACTION_REQUEST);
diagLogBuilder
.configParam("action id", action.getId())
.configParam("action type", action.getType().getActionType())
.configParam("action endpoint", action.getEndpoint().getUri())
.configParam("action endpoint authentication type",
action.getEndpoint().getAuthentication().getType().getName())
.resultMessage("Call external service endpoint " + action.getEndpoint().getUri() + " for "
+ action.getType().getActionType()+ " action ")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.SUCCESS)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
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 @@ -321,7 +375,7 @@ private ActionExecutionStatus processSuccessResponse(Action action,
}

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 @@ -359,6 +413,23 @@ private void logSuccessResponse(Action action, ActionInvocationSuccessResponse s

try {
String responseBody = serializeSuccessResponse(successResponse);
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.RECEIVE_ACTION_RESPONSE);
diagLogBuilder
.configParam("action id", action.getId()+successResponse)
.configParam("action type", action.getType().getActionType())
.configParam("action endpoint", action.getEndpoint().getUri())
.configParam("action endpoint authentication type",
action.getEndpoint().getAuthentication().getType().getName())
.resultMessage("Received success response from external endpoint " +
action.getEndpoint().getUri() + " for " + action.getType().getActionType() + " action")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.SUCCESS)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
LOG.debug(String.format(
"Received success response from API: %s for action type: %s action id: %s with authentication: %s. "
+ "Response: %s",
Expand All @@ -379,6 +450,24 @@ private void logErrorResponse(Action action, ActionInvocationErrorResponse error
// todo: add to diagnostic logs
try {
String responseBody = serializeErrorResponse(errorResponse);
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.RECEIVE_ACTION_RESPONSE);
diagLogBuilder
.configParam("action id", action.getId())
.configParam("action type", action.getType().getActionType())
.configParam("action endpoint", action.getEndpoint().getUri())
.configParam("action endpoint authentication type",
action.getEndpoint().getAuthentication().getType().getName())
.resultMessage("Received error response from external endpoint " +
action.getEndpoint().getUri() + " for " + action.getType().getActionType() +
" action")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
LOG.debug(String.format(
"Received error response from API: %s for action type: %s action id: %s with " +
"authentication: %s. Response: %s",
Expand All @@ -400,6 +489,24 @@ private void logFailureResponse(Action action, ActionInvocationFailureResponse f
// todo: add to diagnostic logs
try {
String responseBody = serializeFailureResponse(failureResponse);
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.RECEIVE_ACTION_RESPONSE);
diagLogBuilder
.configParam("action id", action.getId())
.configParam("action type", action.getType().getActionType())
.configParam("action endpoint", action.getEndpoint().getUri())
.configParam("action endpoint authentication type",
action.getEndpoint().getAuthentication().getType().getName())
.resultMessage("Received failure response from external endpoint " +
action.getEndpoint().getUri() + " for " + action.getType().getActionType() +
" action")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
LOG.debug(String.format(
"Received failure response from API: %s for action type: %s action id: %s with " +
"authentication: %s. Response: %s",
Expand All @@ -417,6 +524,24 @@ private void logFailureResponse(Action action, ActionInvocationFailureResponse f

private void logErrorResponse(Action action, ActionInvocationResponse actionInvocationResponse) {
// todo: add to diagnostic logs
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.RECEIVE_ACTION_RESPONSE);
diagLogBuilder
.configParam("action id", action.getId())
.configParam("action type", action.getType().getActionType())
.configParam("action endpoint", action.getEndpoint().getUri())
.configParam("action endpoint authentication type",
action.getEndpoint().getAuthentication().getType().getName())
.resultMessage("Failed to call external endpoint for action. " +
(actionInvocationResponse.getErrorLog() != null ? actionInvocationResponse.getErrorLog() :
"Unknown error occured."))
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
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 @@ -456,7 +581,7 @@ private String serializeFailureResponse(ActionInvocationFailureResponse response
}

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

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

Expand All @@ -466,8 +591,7 @@ private List<PerformableOperation> validatePerformableOperations(ActionExecution
performableOperation)))
.collect(Collectors.toList());

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

Expand All @@ -479,8 +603,30 @@ private List<PerformableOperation> validatePerformableOperations(ActionExecution
notAllowedOps.add(operationDetails);
}
});
LOG.debug("Allowed Operations: " + String.join(", ", allowedOps) +
". Not Allowed Operations: " + String.join(", ", notAllowedOps));
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagLogBuilder = new DiagnosticLog.DiagnosticLogBuilder(
FrameworkConstants.LogConstants.ACTION_EXECUTION,
FrameworkConstants.LogConstants.ActionIDs.VALIDATE_ACTION_OPERATIONS);
diagLogBuilder
.configParam("action id", action.getId())
.configParam("action type", action.getType().getActionType())
.configParam("action endpoint", action.getEndpoint().getUri())
.configParam("action endpoint authentication type",
action.getEndpoint().getAuthentication().getType().getName())
.configParam("allowed operations", allowedOps.isEmpty() ? "empty" : allowedOps)
.configParam("not allowed operations", notAllowedOps.isEmpty() ? "empty" : notAllowedOps)
.resultMessage("Validated performable operations relevant to "+ action.getType().getActionType()
+ " action.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.SUCCESS)
.build();
LoggerUtils.triggerDiagnosticLogEvent(diagLogBuilder);
}
if (LOG.isDebugEnabled()) {
// todo: add to diagnostics
LOG.debug("Allowed Operations: " + String.join(", ", allowedOps) +
". Not Allowed Operations: " + String.join(", ", notAllowedOps));
}
}

return allowedPerformableOperations;
Expand Down
Loading

0 comments on commit 6dbfd14

Please sign in to comment.