Skip to content

Commit

Permalink
feat: add correlation id in pages and rest endpoints to track logs (#410
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Milton-Ch committed Jan 13, 2022
1 parent abcc732 commit 27fab9f
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 10 deletions.
2 changes: 2 additions & 0 deletions model/src/main/java/io/jans/as/model/config/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@ private Constants() {
public static final String AUTHORIZATION_BASIC = "Authorization: Basic ";

public static final String REASON_CLIENT_NOT_AUTHORIZED = "The client is not authorized.";

public static final String CORRELATION_ID_HEADER = "X-Correlation-Id";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.jans.as.model.ciba.BackchannelAuthenticationErrorResponseType;
import io.jans.as.model.clientinfo.ClientInfoErrorResponseType;
import io.jans.as.model.common.ComponentType;
import io.jans.as.model.config.Constants;
import io.jans.as.model.configuration.AppConfiguration;
import io.jans.as.model.configuration.Configuration;
import io.jans.as.model.fido.u2f.U2fErrorResponseType;
Expand All @@ -22,6 +23,8 @@
import io.jans.as.model.uma.UmaErrorResponseType;
import io.jans.as.model.userinfo.UserInfoErrorResponseType;
import io.jans.as.model.util.Util;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.ThreadContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand All @@ -32,6 +35,7 @@
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -190,7 +194,12 @@ public DefaultErrorResponse getErrorResponse(IErrorType type) {

if (list != null) {
final ErrorMessage m = getError(list, type);
response.setErrorDescription(m.getDescription());

String description = Optional.ofNullable(ThreadContext.get(Constants.CORRELATION_ID_HEADER))
.map(id -> m.getDescription().concat(" CorrelationId: " + id))
.orElse(m.getDescription());

response.setErrorDescription(description);
response.setErrorUri(m.getUri());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.jans.as.server.filter;

import io.jans.as.server.uma.authorization.UmaWebException;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
import java.util.stream.Stream;

import static io.jans.as.model.config.Constants.CORRELATION_ID_HEADER;

@WebFilter(filterName = "CorrelationIdFilter", asyncSupported = true, urlPatterns = {"/*"})
public class CorrelationIdFilter implements Filter {

private static final Logger log = LoggerFactory.getLogger(UmaWebException.class);

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;

String correlationId = httpRequest.getHeader(CORRELATION_ID_HEADER);
if (StringUtils.isBlank(correlationId)) {
correlationId = UUID.randomUUID().toString();

Cookie[] cookies = httpRequest.getCookies();
if (cookies == null || Stream.of(cookies).noneMatch(cookie -> cookie.getName().contains(CORRELATION_ID_HEADER))) {
Cookie cookie = new Cookie(CORRELATION_ID_HEADER, correlationId);
cookie.setSecure(true);
cookie.setHttpOnly(true);
httpResponse.addCookie(cookie);
}
}

ThreadContext.put(CORRELATION_ID_HEADER, correlationId);

chain.doFilter(request, response);
}

@Override
public void destroy() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.jans.as.model.common.ComponentType;
import io.jans.as.model.common.ResponseMode;
import io.jans.as.model.common.ResponseType;
import io.jans.as.model.config.Constants;
import io.jans.as.model.configuration.AppConfiguration;
import io.jans.as.model.error.ErrorResponse;
import io.jans.as.model.error.ErrorResponseFactory;
Expand All @@ -19,6 +20,7 @@
import io.jans.as.server.util.QueryStringDecoder;
import io.jans.as.server.util.ServerUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.ThreadContext;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

Expand All @@ -40,6 +42,8 @@
import javax.ws.rs.core.SecurityContext;
import java.net.URI;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* Implementation based on https://datatracker.ietf.org/doc/html/draft-ietf-oauth-par-08
Expand Down Expand Up @@ -199,8 +203,16 @@ private ErrorResponse createErrorResponseFromRedirectErrorUri(@NotNull URI locat
locationRedirect.parseQueryString(location.getQuery());

final ErrorResponse response = new ErrorResponse();
response.setErrorCode(locationRedirect.getResponseParameter("error"));
response.setErrorDescription(locationRedirect.getResponseParameter("error_description"));

String errorDescription = locationRedirect.getResponseParameter("error_description");
errorDescription = Optional.ofNullable(errorDescription)
.map(description -> Optional.ofNullable(ThreadContext.get(Constants.CORRELATION_ID_HEADER))
.map(id -> description.concat(" CorrelationId: " + id))
.orElse(description))
.orElse(null);

response.setErrorCode(errorDescription);
response.setErrorDescription(errorDescription);
return response;
}

Expand Down
12 changes: 6 additions & 6 deletions server/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<RollingFile name="FILE" fileName="${sys:log.base}/logs/jans-auth.log" filePattern="${sys:log.base}/logs/jans-auth-%d{yyyy-MM-dd}-%i.log">

<PatternLayout pattern="%d %-5p [%t] [%C{6}] (%F:%L) - %m%n" />
<PatternLayout pattern="%d %-5p [%t] %X{X-Correlation-Id} [%C{6}] (%F:%L) - %m%n" />

<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Expand All @@ -30,7 +30,7 @@

<RollingFile name="JANS_AUTH_PERSISTENCE_FILE" fileName="${sys:log.base}/logs/jans-auth_persistence.log" filePattern="${sys:log.base}/logs/jans-auth_persistence-%d{yyyy-MM-dd}-%i.log">

<PatternLayout pattern="%d %-5p [%t] [%C{6}] (%F:%L) - %m%n" />
<PatternLayout pattern="%d %-5p [%t] %X{X-Correlation-Id} [%C{6}] (%F:%L) - %m%n" />

<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Expand All @@ -41,7 +41,7 @@

<RollingFile name="JANS_AUTH_PERSISTENCE_DURATION_FILE" fileName="${sys:log.base}/logs/jans-auth_persistence_duration.log" filePattern="${sys:log.base}/logs/jans-auth_persistence_duration-%d{yyyy-MM-dd}-%i.log">

<PatternLayout pattern="%d %-5p [%t] [%C{6}] (%F:%L) - %m%n" />
<PatternLayout pattern="%d %-5p [%t] %X{X-Correlation-Id} [%C{6}] (%F:%L) - %m%n" />

<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Expand All @@ -53,7 +53,7 @@
<RollingFile name="JANS_AUTH_PERSISTENCE_LDAP_STATISTICS_FILE" fileName="${sys:log.base}/logs/jans-auth_persistence_ldap_statistics.log"
filePattern="${sys:log.base}/logs/jans-auth_persistence_ldap_statistics-%d{yyyy-MM-dd}-%i.log">

<PatternLayout pattern="%d %-5p [%t] [%C{6}] (%F:%L) - %m%n" />
<PatternLayout pattern="%d %-5p [%t] %X{X-Correlation-Id} [%C{6}] (%F:%L) - %m%n" />

<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Expand All @@ -64,7 +64,7 @@

<RollingFile name="JANS_AUTH_SCRIPT_LOG_FILE" fileName="${sys:log.base}/logs/jans-auth_script.log" filePattern="${sys:log.base}/logs/jans-auth_script-%d{yyyy-MM-dd}-%i.log">

<PatternLayout pattern="%d %-5p [%t] [%C{6}] (%F:%L) - %m%n" />
<PatternLayout pattern="%d %-5p [%t] %X{X-Correlation-Id} [%C{6}] (%F:%L) - %m%n" />

<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Expand All @@ -75,7 +75,7 @@

<RollingFile name="JANS_AUTH_AUDIT_LOG_FILE" fileName="${sys:log.base}/logs/jans-auth_audit.log" filePattern="${sys:log.base}/logs/jans-auth_audit-%d{yyyy-MM-dd}-%i.log">

<PatternLayout pattern="%d %-5p [%macAddr] [%t] [%C{6}] (%F:%L) - %m%n" />
<PatternLayout pattern="%d %-5p [%macAddr] [%t] %X{X-Correlation-Id} [%C{6}] (%F:%L) - %m%n" />

<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/webapp/auth/otp_sms/otp_sms.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ input.btn.btn-done {
<a target="_blank" href='#{client.getPolicyUri()}'>#{msgs['otp_sms.termsPrivacy']}</a>
</p>
</h:panelGroup>
<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</h:panelGroup>
</h:form>
<div class="footer-copyright text-center py-3"
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/webapp/auth/register/register.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
</div>

</h:panelGroup>
<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</h:form>
</div>
<h:panelGroup layout="block"
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/webapp/auth/thumbsignin/tsLogin.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@
</div>
</div>
</h:panelGroup>
<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</h:form>
</div>
</div>
Expand Down
8 changes: 7 additions & 1 deletion server/src/main/webapp/casa/login.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@
<h:commandButton class="f7-cust bw0 br1 ph4 pv2 bg-bsgreen-success white hover-bsgreen-success hover-white btn cust-primary-button"
value="#{msgs['login.login']}" action="#{authenticator.authenticate}" />
</div>


<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
<h:inputHidden id="platform" />
</h:form>
</section>
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/webapp/casa/otp.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
<h:commandButton class="f7-cust bw0 br1 ph4 pv2 bg-bsgreen-success white hover-bsgreen-success hover-white btn cust-primary-button"
value="#{msgs['login.login']}" action="#{authenticator.authenticate}" />
</div>
<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</h:form>
</section>
<ui:include src="/casa/casa.xhtml" />
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/webapp/ciba/home.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
</div>
</div>

<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>

</div>
</main>
</div>
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/webapp/device_authorization.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@
<f:ajax execute="@form" render="@form messages" listener="#{deviceAuthorizationAction.initializeSession()}" />
</h:commandButton>
</h:panelGroup>

<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</div>
</h:form>
<script type="text/javascript">
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/webapp/error.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"
globalOnly="true" />
</div>
<h:panelGroup layout="block" rendered="#{cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</div>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/webapp/error_service.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"
globalOnly="true" />
</div>

<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</div>
</ui:define>
</ui:composition>
7 changes: 7 additions & 0 deletions server/src/main/webapp/error_session.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
globalOnly="true" />
</div>
</div>

<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions server/src/main/webapp/login.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
</div>
</div>
</div>
<h:panelGroup layout="block" rendered="#{not empty facesContext.messageList and cookie['X-Correlation-Id'] != null}">
<br/>
<p style="font-size: 0.7em">
<strong>Correlation Id: </strong> <h:outputText value="#{cookie['X-Correlation-Id'].value}" />
</p>
</h:panelGroup>
<h:inputHidden id="platform" />
</h:form>
<div class="row">
Expand Down

0 comments on commit 27fab9f

Please sign in to comment.