[java-identity-provider] branch main updated: IDP-2207 - Support logging request headers

Scott Cantor cantor.2 at osu.edu
Tue Nov 28 18:34:58 UTC 2023


This is an automated email from the git hooks/post-receive script.

scantor pushed a commit to branch main
in repository java-identity-provider.

View the commit online:
http://git.shibboleth.net/view/?p=java-identity-provider.git;a=commit;h=dd92cd92f6244651d32db9037d608bd4983ee0f6

The following commit(s) were added to refs/heads/main by this push:
     new dd92cd92f IDP-2207 - Support logging request headers
dd92cd92f is described below

commit dd92cd92f6244651d32db9037d608bd4983ee0f6
Author: Scott Cantor <cantor.2 at osu.edu>
AuthorDate: Tue Nov 28 13:34:55 2023 -0500

    IDP-2207 - Support logging request headers
    
    https://shibboleth.atlassian.net/browse/IDP-2207
    
    Added reserved map bean to specify MDC keys and headers to save to MDC.
---
 .../idp/conf/impl/SLF4JMDCServletFilter.java       | 28 ++++++++++++++++++++++
 .../resources/net/shibboleth/idp/conf/filters.xml  |  3 ++-
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/idp-conf-impl/src/main/java/net/shibboleth/idp/conf/impl/SLF4JMDCServletFilter.java b/idp-conf-impl/src/main/java/net/shibboleth/idp/conf/impl/SLF4JMDCServletFilter.java
index efab627b0..8177a92e8 100644
--- a/idp-conf-impl/src/main/java/net/shibboleth/idp/conf/impl/SLF4JMDCServletFilter.java
+++ b/idp-conf-impl/src/main/java/net/shibboleth/idp/conf/impl/SLF4JMDCServletFilter.java
@@ -15,8 +15,10 @@
 package net.shibboleth.idp.conf.impl;
 
 import java.io.IOException;
+import java.util.Map;
 
 import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
 
 import org.slf4j.MDC;
 
@@ -28,6 +30,7 @@ import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpSession;
 import net.shibboleth.idp.Version;
 import net.shibboleth.shared.annotation.constraint.NotEmpty;
+import net.shibboleth.shared.collection.CollectionSupport;
 import net.shibboleth.shared.servlet.AbstractConditionalFilter;
 import net.shibboleth.shared.servlet.HttpServletSupport;
 import net.shibboleth.shared.spring.servlet.ChainableFilter;
@@ -53,6 +56,9 @@ public class SLF4JMDCServletFilter extends AbstractConditionalFilter implements
 
     /** Whether to create a session if it doesn't already exist. */
     private boolean createSession;
+    
+    /** Map of attribute/header pairs to inject. */
+    @Nullable private Map<String,String> headerMap;
 
     /** Constructor. */
     public SLF4JMDCServletFilter() {
@@ -67,6 +73,21 @@ public class SLF4JMDCServletFilter extends AbstractConditionalFilter implements
     public void setCreateSession(final boolean flag) {
         createSession = flag;
     }
+    
+    /**
+     * Set a map of MDC attribute names to header names to add to context.
+     * 
+     * @param map
+     * 
+     * @since 5.1.0
+     */
+    public void setHeaderMap(@Nullable final Map<String,String> map) {
+        if (map != null) {
+            headerMap = CollectionSupport.copyToMap(map);
+        } else {
+            headerMap = null;
+        }
+    }
 
     /** {@inheritDoc} */
     @Override
@@ -77,6 +98,13 @@ public class SLF4JMDCServletFilter extends AbstractConditionalFilter implements
             MDC.put(CLIENT_ADDRESS_MDC_ATTRIBUTE, HttpServletSupport.getRemoteAddr(request));
             MDC.put(SERVER_ADDRESS_MDC_ATTRIBUTE, request.getServerName());
             MDC.put(SERVER_PORT_MDC_ATTRIBUTE, Integer.toString(request.getServerPort()));
+            
+            if (headerMap != null && request instanceof HttpServletRequest http) {
+                headerMap.entrySet().forEach(entry -> {
+                    MDC.put(entry.getKey(), http.getHeader(entry.getValue()));
+                });
+            }
+            
             if (request instanceof HttpServletRequest) {
                 final HttpSession session = ((HttpServletRequest) request).getSession(createSession);
                 if (session != null) {
diff --git a/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/filters.xml b/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/filters.xml
index 704cff616..f597c9d5d 100644
--- a/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/filters.xml
+++ b/idp-conf-impl/src/main/resources/net/shibboleth/idp/conf/filters.xml
@@ -60,7 +60,8 @@
     <!-- Runs NEUTRAL, can be disabled if desired. -->
     <bean class="net.shibboleth.idp.conf.impl.SLF4JMDCServletFilter"
         p:activationCondition="%{idp.logging.MDC.enabled:true}"
-        p:createSession="%{idp.logging.MDC.createSession:true}" />
+        p:createSession="%{idp.logging.MDC.createSession:true}"
+        p:headerMap="#{getObject('shibboleth.MDCHeaders')}" />
 
     <!--
     This was, Scott thinks, needed for client storage to work, but if it runs, it has to be after the

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list