roryqi commented on code in PR #11191: URL: https://github.com/apache/gravitino/pull/11191#discussion_r3287182762
########## server/src/main/java/org/apache/gravitino/server/web/filter/RequestContextFilter.java: ########## @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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.apache.gravitino.server.web.filter; + +import java.io.IOException; +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 org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.utils.RequestContext; + +/** + * A servlet filter that captures the client remote address from each HTTP request and stores it in + * {@link RequestContext} so that audit event constructors can read it on the same thread. + * + * <p>When a reverse proxy is in use, the real client IP is taken from the first entry of the {@code + * X-Forwarded-For} header (a de-facto standard header set by reverse proxies; note that it is + * trusted unconditionally — deployments where the server is reachable directly, without a trusted + * reverse proxy, should be aware that clients can spoof this header). If the header is absent, + * {@link HttpServletRequest#getRemoteAddr()} is used instead. + * + * <p>The stored value is always cleared in a {@code finally} block to prevent thread-pool leaks. + */ +public class RequestContextFilter implements Filter { + + private static final String X_FORWARDED_FOR = "X-Forwarded-For"; Review Comment: Could we extract this constant to the module `server-common` to reuse for the other auxiliary services modules? ########## core/src/main/java/org/apache/gravitino/listener/api/event/Event.java: ########## @@ -21,11 +21,33 @@ import org.apache.gravitino.NameIdentifier; import org.apache.gravitino.annotation.DeveloperApi; +import org.apache.gravitino.utils.RequestContext; -/** Represents a post event. */ +/** + * Represents a post event for Gravitino server operations. + * + * <p>The client remote address is captured from {@link RequestContext} at construction time on the + * servlet thread, so async listener threads can safely call {@link #remoteAddress()} without + * accessing thread-local storage. + */ @DeveloperApi public abstract class Event extends BaseEvent { + + private final String remoteAddress; + protected Event(String user, NameIdentifier identifier) { super(user, identifier); + String addr = RequestContext.getRemoteAddress(); + this.remoteAddress = addr != null ? addr : "unknown"; Review Comment: StringUtils.isBlanke? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
