Skip to content

Commit

Permalink
Adding identity_id from keycloak to the logs through MDC Context
Browse files Browse the repository at this point in the history
Signed-off-by: Sun Tan <sutan@redhat.com>
  • Loading branch information
sunix committed Dec 4, 2017
1 parent 845d86a commit 3a3dac2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
9 changes: 9 additions & 0 deletions core/commons/che-core-commons-logback/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
<packaging>jar</packaging>
<name>Che Core :: Commons :: Commons Logback</name>
<dependencies>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-api-core</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2012-2017 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.commons.logback.filter;

import java.io.IOException;
import javax.inject.Singleton;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.eclipse.che.commons.subject.Subject;
import org.slf4j.MDC;

/**
* A servlet filter that retrieves the identity_id from the servlet context and put it in the MDC
* context. Logback can be configured to display this value in each log message when available. MDC
* property name is `identity_id`.
*/
@Singleton
public class IdentityIdLoggerFilter implements Filter {

private static final String IDENTITY_ID_MDC_KEY = "identity_id";

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

@Override
public final void doFilter(
ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
final HttpServletRequest httpRequest = (HttpServletRequest) request;
final HttpSession session = httpRequest.getSession();
Subject subject = (Subject) session.getAttribute("che_subject");

if (subject != null && subject.getUserId() != null) {
MDC.put(IDENTITY_ID_MDC_KEY, subject.getUserId());
}

filterChain.doFilter(request, response);
}

@Override
public void destroy() {}
}
4 changes: 4 additions & 0 deletions multiuser/keycloak/che-multiuser-keycloak-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-logback</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.multiuser</groupId>
<artifactId>che-multiuser-api-authorization</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
@Singleton
public class KeycloakEnvironmentInitalizationFilter extends AbstractKeycloakFilter {

private static final String IDENTITY_ID_MDC_KEY = "identity_id";

private final UserManager userManager;
private final RequestTokenExtractor tokenExtractor;
private final PermissionChecker permissionChecker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import com.google.inject.servlet.ServletModule;
import javax.inject.Singleton;
import org.eclipse.che.commons.logback.filter.IdentityIdLoggerFilter;
import org.eclipse.che.multiuser.keycloak.server.KeycloakAuthenticationFilter;
import org.eclipse.che.multiuser.keycloak.server.KeycloakEnvironmentInitalizationFilter;

Expand All @@ -26,5 +27,7 @@ protected void configureServlets() {
.through(KeycloakAuthenticationFilter.class);
filterRegex("^(?!.*(/docs/))(?!.*(/keycloak/settings/?|/api/oauth/callback/?)$).*")
.through(KeycloakEnvironmentInitalizationFilter.class);
filterRegex("^(?!.*(/docs/))(?!.*(/keycloak/settings/?|/api/oauth/callback/?)$).*")
.through(IdentityIdLoggerFilter.class);
}
}

0 comments on commit 3a3dac2

Please sign in to comment.