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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
resolved comments
Sachin-Mamoru committed May 20, 2024
commit 81b4b372496a3e2c4ba0778f71588e5a3416546a
Original file line number Diff line number Diff line change
@@ -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.
@@ -62,7 +63,6 @@ public abstract class AbstractHTTPFunction {
protected static final String TYPE_TEXT_PLAIN = "text/plain";
private static final char DOMAIN_SEPARATOR = '.';
private static final String RESPONSE = "response";
private String INVOKE_API = "invoke-api";
private final int requestRetryCount;
private final List<String> allowedDomains;

@@ -99,12 +99,6 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
String outcome;
String endpointURL = null;

if (clientRequest.getMethod().equals(Constants.GET)) {
INVOKE_API = Constants.LogConstants.ActionIDs.INVOKE_API_HTTP_GET;
} else if (clientRequest.getMethod().equals(Constants.POST)) {
INVOKE_API = Constants.LogConstants.ActionIDs.INVOKE_API_HTTP_POST;
}

HttpUriRequest request;
try {
if (authConfigModel != null) {
@@ -162,7 +156,7 @@ protected void executeHttpMethod(HttpUriRequest clientRequest, Map<String, Objec
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
INVOKE_API);
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)
@@ -208,7 +202,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
}
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Successfully called the external api. Status code: " + responseCode)
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -222,7 +217,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
} else if (responseCode >= 300 && responseCode < 400) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("External api invocation returned a redirection. Status code: " +
responseCode)
@@ -237,7 +233,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
} else if (responseCode >= 400 && responseCode < 500) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("External api invocation returned a client error. Status code: " +
responseCode)
@@ -252,7 +249,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
} else {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Received unknown response from external API call. Status code: " +
responseCode)
@@ -270,7 +268,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
if (e instanceof IllegalArgumentException) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Invalid Url for external API call.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -282,7 +281,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
} else if (e instanceof ConnectTimeoutException || e instanceof SocketTimeoutException) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Request for the external API timed out.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -298,7 +298,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
} else if (e instanceof ParseException) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Failed to parse the response from the external API.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -310,7 +311,8 @@ private Pair<RetryDecision, Pair<String, JSONObject>> executeRequest(HttpUriRequ
} else {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(request));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.API, endpointURL)
.resultMessage("Received an error while invoking the external API.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
Original file line number Diff line number Diff line change
@@ -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}
@@ -84,8 +86,6 @@ public class ClientCredentialAuthConfig implements AuthConfig {
private static final String JWT_EXP_CLAIM = "exp";
private static final String BEARER = "Bearer ";
private static final String BASIC = "Basic ";
private String INVOKE_API = "invoke-api";
private String REQUEST_TOKEN = "request-token";
private int maxRequestAttemptsForAPIEndpointTimeout;
private APIAccessTokenCache apiAccessTokenCache;
private String consumerKey;
@@ -94,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;
@@ -131,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;
}
@@ -148,13 +157,7 @@ public boolean shouldRetry() {
public HttpUriRequest applyAuth(HttpUriRequest request, AuthConfigModel authConfigModel)
throws FrameworkException {

if (request.getMethod().equals(Constants.GET)) {
INVOKE_API = Constants.LogConstants.ActionIDs.INVOKE_API_HTTP_GET;
REQUEST_TOKEN = Constants.LogConstants.ActionIDs.REQUEST_TOKEN_HTTP_GET;
} else if (request.getMethod().equals(Constants.POST)) {
INVOKE_API = Constants.LogConstants.ActionIDs.INVOKE_API_HTTP_POST;
REQUEST_TOKEN = Constants.LogConstants.ActionIDs.REQUEST_TOKEN_HTTP_POST;
}
setRequest(request);
maxRequestAttemptsForAPIEndpointTimeout = ConfigProvider.getInstance().
getRequestRetryCount();
this.apiAccessTokenCache = APIAccessTokenCache.getInstance();
@@ -171,7 +174,8 @@ public HttpUriRequest applyAuth(HttpUriRequest request, AuthConfigModel authConf
asyncReturn.accept(authenticationContext, Collections.emptyMap(), OUTCOME_FAIL);
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, REQUEST_TOKEN);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getRequestTokenActionId(getRequest()));
Sachin-Mamoru marked this conversation as resolved.
Show resolved Hide resolved
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.inputParam(Constants.LogConstants.InputKeys.GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS)
.configParam(Constants.LogConstants.ConfigKeys.MAX_REQUEST_ATTEMPTS,
@@ -249,7 +253,8 @@ private String getAccessToken() throws FrameworkException {
LOG.error("Error parsing token expiry.", e);
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Failed to parse token expiry.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -279,7 +284,8 @@ private String attemptAccessTokenRequest(int maxAttempts) {
try {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, REQUEST_TOKEN);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getRequestTokenActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.inputParam(Constants.LogConstants.InputKeys.GRANT_TYPE, GRANT_TYPE_CLIENT_CREDENTIALS)
.configParam(Constants.LogConstants.ConfigKeys.MAX_REQUEST_ATTEMPTS, maxAttempts)
@@ -346,7 +352,8 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
} else if (responseCode >= 300 && responseCode < 400) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
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)
@@ -359,7 +366,8 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
} else if (responseCode >= 400 && responseCode < 500) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
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)
@@ -372,7 +380,8 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
} else {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
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)
@@ -389,7 +398,8 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
if (e instanceof IllegalArgumentException) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Invalid Url for token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -400,7 +410,8 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
} else if (e instanceof SocketTimeoutException || e instanceof ConnectTimeoutException) {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Request for the token endpoint timed out.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -414,7 +425,8 @@ private Pair<RetryDecision, String> requestAccessToken() throws IOException {
} else {
if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
getInvokeApiActionId(getRequest()));
diagnosticLogBuilder.inputParam(Constants.LogConstants.InputKeys.TOKEN_ENDPOINT, getTokenEndpoint())
.resultMessage("Received an error while invoking the token endpoint.")
.logDetailLevel(DiagnosticLog.LogDetailLevel.APPLICATION)
@@ -444,7 +456,8 @@ private Pair<RetryDecision, String> processSuccessfulResponse(CloseableHttpRespo

if (LoggerUtils.isDiagnosticLogsEnabled()) {
DiagnosticLog.DiagnosticLogBuilder diagnosticLogBuilder = new
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE, INVOKE_API);
DiagnosticLog.DiagnosticLogBuilder(Constants.LogConstants.ADAPTIVE_AUTH_SERVICE,
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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.conditional.auth.functions.http.util;

import org.apache.http.client.methods.HttpUriRequest;
import org.wso2.carbon.identity.conditional.auth.functions.common.utils.Constants;

/**
* Utility class for HTTP related operations.
*/
public class HttpUtil {

/**
* Get the invoke API Action ID based on the HTTP method.
*
* @param request HttpUriRequest
* @return String
*/
public static String getInvokeApiActionId(HttpUriRequest request) {

String invokeApi;

if (request.getMethod().equals(Constants.GET)) {
invokeApi = Constants.LogConstants.ActionIDs.INVOKE_API_HTTP_GET;
} else if (request.getMethod().equals(Constants.POST)) {
invokeApi = Constants.LogConstants.ActionIDs.INVOKE_API_HTTP_POST;
} else {
invokeApi = "invoke-api";
}

return invokeApi;
}

/**
* Get the request token Action ID based on the HTTP method.
*
* @param request HttpUriRequest
* @return String
*/
public static String getRequestTokenActionId(HttpUriRequest request) {
String requestToken;
if (request.getMethod().equals(Constants.GET)) {
requestToken = Constants.LogConstants.ActionIDs.REQUEST_TOKEN_HTTP_GET;
} else if (request.getMethod().equals(Constants.POST)) {
requestToken = Constants.LogConstants.ActionIDs.REQUEST_TOKEN_HTTP_POST;
} else {
requestToken = "request-token";
}
return requestToken;
}
}