Skip to content

Commit

Permalink
Merge pull request #161 from Sachin-Mamoru/update-diagnostic-logs
Browse files Browse the repository at this point in the history
Improved diagnostic logs for httpGet and httpPost adaptive functions
  • Loading branch information
Sachin-Mamoru authored May 21, 2024
2 parents 39dbe91 + 6ba0c9e commit 0eb37ca
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class Constants {
public static final String OUTCOME_SUCCESS = "onSuccess";
public static final String OUTCOME_FAIL = "onFail";
public static final String OUTCOME_TIMEOUT = "onTimeout";
public static final String GET = "GET";
public static final String POST = "POST";

public static final String RECEIVER_URL = "AdaptiveAuth.EventPublisher.ReceiverURL";
public static final String HTTP_CONNECTION_TIMEOUT = "AdaptiveAuth.HTTPConnectionTimeout";
Expand Down Expand Up @@ -61,8 +63,10 @@ public static class LogConstants {
*/
public static class ActionIDs {

public static final String RECEIVE_TOKEN = "receive-token";
public static final String RECEIVE_API_RESPONSE = "receive-api-response";
public static final String REQUEST_TOKEN_HTTP_GET = "request-token-http-get";
public static final String REQUEST_TOKEN_HTTP_POST = "request-token-http-post";
public static final String INVOKE_API_HTTP_GET = "invoke-api-http-get";
public static final String INVOKE_API_HTTP_POST = "invoke-api-http-post";
public static final String VALIDATE_INPUT_PARAMS = "validate-input-parameters";
public static final String UPDATE_USER_PASSWORD = "update-user-password";
}
Expand All @@ -74,15 +78,15 @@ public static class InputKeys {

public static final String TOKEN_ENDPOINT = "token endpoint";
public static final String API = "external api";
public static final String GRANT_TYPE = "grant type";
}

/**
* Define common and reusable Configuration keys for diagnostic logs.
*/
public static class ConfigKeys {

public static final String SUPPORTED_GRANT_TYPES = "supported grant types";

public static final String MAX_REQUEST_ATTEMPTS = "max request attempts";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import java.util.Map;

import static org.apache.http.HttpHeaders.ACCEPT;
import static org.wso2.carbon.identity.conditional.auth.functions.http.util.HttpUtil.getInvokeApiActionId;

/**
* Abstract class for handling http calls.
Expand Down Expand Up @@ -155,8 +156,9 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.configParam(Constants.LogConstants.ConfigKeys.MAX_REQUEST_ATTEMPTS, maxRetries)
.resultMessage("Retrying the request for external api. Attempt: " + attempts)
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED);
Expand Down Expand Up @@ -201,7 +203,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Successfully called the external api. Status code: " + responseCode)
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -216,7 +218,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("External api invocation returned a redirection. Status code: " +
responseCode)
Expand All @@ -232,7 +234,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("External api invocation returned a client error. Status code: " +
responseCode)
Expand All @@ -248,7 +250,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Received unknown response from external API call. Status code: " +
responseCode)
Expand All @@ -267,7 +269,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Invalid Url for external API call.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -280,7 +282,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Request for the external API timed out.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -297,7 +299,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Failed to parse the response from the external API.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -310,7 +312,7 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Received an error while invoking the external API.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import static org.apache.http.HttpHeaders.ACCEPT;
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
import static org.wso2.carbon.identity.conditional.auth.functions.common.utils.Constants.OUTCOME_FAIL;
import static org.wso2.carbon.identity.conditional.auth.functions.http.util.HttpUtil.getRequestTokenActionId;

/**
* Implementation of the {@link AuthConfig}
Expand Down Expand Up @@ -92,6 +93,7 @@ public class ClientCredentialAuthConfig implements AuthConfig {
private String tokenEndpoint;
private AuthenticationContext authenticationContext;
private AsyncReturn asyncReturn;
private HttpUriRequest request;

public void setAuthenticationContext(AuthenticationContext authenticationContext) {
this.authenticationContext = authenticationContext;
Expand Down Expand Up @@ -129,6 +131,14 @@ public String getConsumerSecret() {
return consumerSecret;
}

public HttpUriRequest getRequest() {
return request;
}

public void setRequest(HttpUriRequest request) {
this.request = request;
}

public String getScopes() {
return scopes;
}
Expand All @@ -146,6 +156,7 @@ public boolean shouldRetry() {
public HttpUriRequest applyAuth(HttpUriRequest request, AuthConfigModel authConfigModel)
throws FrameworkException {

setRequest(request);
maxRequestAttemptsForAPIEndpointTimeout = ConfigProvider.getInstance().
getRequestRetryCount();
this.apiAccessTokenCache = APIAccessTokenCache.getInstance();
Expand All @@ -163,10 +174,11 @@ public HttpUriRequest applyAuth(HttpUriRequest request, AuthConfigModel authConf
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_TOKEN);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.configParam(Constants.LogConstants.ConfigKeys.SUPPORTED_GRANT_TYPES,
GRANT_TYPE_CLIENT_CREDENTIALS)
.inputParam(Constants.LogConstants.InputKeys.GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS)
.configParam(Constants.LogConstants.ConfigKeys.MAX_REQUEST_ATTEMPTS,
maxRequestAttemptsForAPIEndpointTimeout)
.resultMessage("Failed to retrieve access token for the provided token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
.resultStatus(DiagnosticLog.ResultStatus.FAILED);
Expand Down Expand Up @@ -241,7 +253,7 @@ private String getAccessToken() throws FrameworkException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Failed to parse token expiry.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand Down Expand Up @@ -272,10 +284,10 @@ private String attemptAccessTokenRequest(int maxAttempts) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_TOKEN);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.configParam(Constants.LogConstants.ConfigKeys.SUPPORTED_GRANT_TYPES,
GRANT_TYPE_CLIENT_CREDENTIALS)
.inputParam(Constants.LogConstants.InputKeys.GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS)
.configParam(Constants.LogConstants.ConfigKeys.MAX_REQUEST_ATTEMPTS, maxAttempts)
.resultMessage("Retrying token request for the provided token endpoint. Attempt: " +
attemptCount + ".")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand Down Expand Up @@ -340,7 +352,7 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Token endpoint returned a redirection. Status code: " + responseCode)
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -354,7 +366,7 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Token endpoint returned a client error. Status code: " + responseCode)
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -368,7 +380,7 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Received unknown response from token endpoint. Status code: " +
responseCode)
Expand All @@ -386,7 +398,7 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Invalid Url for token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -398,7 +410,7 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Request for the token endpoint timed out.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -413,7 +425,7 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Received an error while invoking the token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand Down Expand Up @@ -444,7 +456,7 @@ private Pair<RetryDecision, String> processSuccessfulResponse(CloseableHttpRespo
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
Constants.LogConstants.ActionIDs.RECEIVE_API_RESPONSE);
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Received access token from the token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand Down
Loading

0 comments on commit 0eb37ca

Please sign in to comment.