Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved diagnostic logs for httpGet and httpPost adaptive functions #161

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand All @@ -72,15 +76,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,8 @@
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.getInvokeApiActionId;
import static org.wso2.carbon.identity.conditional.auth.functions.http.util.HttpUtil.getRequestTokenActionId;

/**
* Implementation of the {@link AuthConfig}
Expand Down Expand Up @@ -92,6 +94,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 +132,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 +157,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 +175,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()));
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved
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 +254,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);
getInvokeApiActionId(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 +285,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 +353,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);
getInvokeApiActionId(getRequest()));
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved
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 +367,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);
getInvokeApiActionId(getRequest()));
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved
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 +381,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);
getInvokeApiActionId(getRequest()));
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Received unknown response from token endpoint. Status code: " +
responseCode)
Expand All @@ -386,7 +399,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);
getInvokeApiActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Invalid Url for token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand All @@ -398,7 +411,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);
getInvokeApiActionId(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 +426,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);
getInvokeApiActionId(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 +457,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);
getInvokeApiActionId(getRequest()));
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Received access token from the token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Expand Down
Loading
Loading