-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add filter for logging principal of incoming requests #3649
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
base: 7.7.x
Are you sure you want to change the base?
Changes from all commits
31aa667
59d64f8
b4d68a5
abe1193
46d938d
7fa7648
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2025 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.kafka.schemaregistry.rest; | ||
|
||
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 java.io.IOException; | ||
import java.security.Principal; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import io.confluent.kafka.schemaregistry.utils.PrincipalContext; | ||
|
||
/** | ||
* This class is a servlet filter that logs the user principal for each incoming request to | ||
* Schema Registry. It is a necessary step to allow for building resource associations | ||
*/ | ||
public class PrincipalLoggingFilter implements Filter { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PrincipalLoggingFilter.class.getName()); | ||
|
||
@Override | ||
public void init(FilterConfig filterConfig) throws ServletException { | ||
Check failure on line 40 in core/src/main/java/io/confluent/kafka/schemaregistry/rest/PrincipalLoggingFilter.java
|
||
} | ||
|
||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse servletResponse, | ||
FilterChain filterChain) throws IOException, ServletException { | ||
HttpServletRequest req = (HttpServletRequest) request; | ||
Principal principal = req.getUserPrincipal(); | ||
|
||
if (principal != null) { | ||
log.info("User Principal: {}", principal.getName()); | ||
PrincipalContext.setPrincipal(principal.getName()); | ||
} else { | ||
log.info("No User Principal found for the request."); | ||
PrincipalContext.clear(); | ||
} | ||
|
||
try { | ||
filterChain.doFilter(request, servletResponse); | ||
} finally { | ||
PrincipalContext.clear(); // Clear the principal after the request is processed | ||
} | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
Check failure on line 65 in core/src/main/java/io/confluent/kafka/schemaregistry/rest/PrincipalLoggingFilter.java
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2025 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.kafka.schemaregistry.utils; | ||
|
||
public class PrincipalContext { | ||
Check warning on line 18 in core/src/main/java/io/confluent/kafka/schemaregistry/utils/PrincipalContext.java
|
||
private static final ThreadLocal<String> principalHolder = new ThreadLocal<>(); | ||
|
||
public static void setPrincipal(String principal) { | ||
principalHolder.set(principal); | ||
} | ||
|
||
public static String getPrincipal() { | ||
return principalHolder.get(); | ||
} | ||
|
||
public static void clear() { | ||
principalHolder.remove(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this pull out the user_id field from the REQUEST types? I am not super familiar with this code base to know what this does implicitly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all requests that come into SR will pass through this request filter, which will set the principal user (unique identifier for the request sender) in a principalContext. this is needed because the logs are populated at the service level, not at the request level - so we keep it in a principalContext which can be read when the actual CRUD SR calls happen. I have also added a sample log to the PR description - let me know if you want it reformatted another way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, yeah I wasn't sure which id or reference the principalContext would use by default. This and the example log message clarify that, thanks!