This is an automated email from the ASF dual-hosted git repository.

nihaljain pushed a commit to branch branch-2
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2 by this push:
     new 32d664d09ea HBASE-28367 Backport "HBASE-27811 Enable cache control for 
logs endpoint and set max age as 0" to branch-2 (#5679)
32d664d09ea is described below

commit 32d664d09ead23c3e65db8c175c66086bbebc0ce
Author: Nihal Jain <[email protected]>
AuthorDate: Mon Feb 19 00:53:39 2024 +0530

    HBASE-28367 Backport "HBASE-27811 Enable cache control for logs endpoint 
and set max age as 0" to branch-2 (#5679)
    
    Co-authored-by: Yash Dodeja <[email protected]>
    Signed-off-by: Rajeshbabu Chintaguntla <[email protected]>
---
 .../java/org/apache/hadoop/hbase/http/HttpServer.java  | 18 ++++++++++++++----
 .../org/apache/hadoop/hbase/http/NoCacheFilter.java    | 15 ++++++++++++++-
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git 
a/hbase-http/src/main/java/org/apache/hadoop/hbase/http/HttpServer.java 
b/hbase-http/src/main/java/org/apache/hadoop/hbase/http/HttpServer.java
index 0050cec0615..d5af8df1c7f 100644
--- a/hbase-http/src/main/java/org/apache/hadoop/hbase/http/HttpServer.java
+++ b/hbase-http/src/main/java/org/apache/hadoop/hbase/http/HttpServer.java
@@ -56,6 +56,7 @@ import org.apache.hadoop.hbase.http.conf.ConfServlet;
 import org.apache.hadoop.hbase.http.log.LogLevel;
 import org.apache.hadoop.hbase.util.ReflectionUtils;
 import org.apache.hadoop.hbase.util.Threads;
+import org.apache.hadoop.security.AuthenticationFilterInitializer;
 import org.apache.hadoop.security.SecurityUtil;
 import org.apache.hadoop.security.UserGroupInformation;
 import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
@@ -149,6 +150,7 @@ public class HttpServer implements FilterContainer {
     HTTP_SPNEGO_AUTHENTICATION_PREFIX + "admin.groups";
   public static final String HTTP_PRIVILEGED_CONF_KEY =
     "hbase.security.authentication.ui.config.protected";
+  public static final String HTTP_UI_NO_CACHE_ENABLE_KEY = 
"hbase.http.filter.no-store.enable";
   public static final boolean HTTP_PRIVILEGED_CONF_DEFAULT = false;
 
   // The ServletContext attribute where the daemon Configuration
@@ -690,7 +692,7 @@ public class HttpServer implements FilterContainer {
     
ctx.getServletContext().setAttribute(org.apache.hadoop.http.HttpServer2.CONF_CONTEXT_ATTRIBUTE,
       conf);
     ctx.getServletContext().setAttribute(ADMINS_ACL, adminsAcl);
-    addNoCacheFilter(ctx);
+    addNoCacheFilter(ctx, conf);
     return ctx;
   }
 
@@ -712,9 +714,16 @@ public class HttpServer implements FilterContainer {
     return gzipHandler;
   }
 
-  private static void addNoCacheFilter(WebAppContext ctxt) {
-    defineFilter(ctxt, NO_CACHE_FILTER, NoCacheFilter.class.getName(),
-      Collections.<String, String> emptyMap(), new String[] { "/*" });
+  private static void addNoCacheFilter(ServletContextHandler ctxt, 
Configuration conf) {
+    if (conf.getBoolean(HTTP_UI_NO_CACHE_ENABLE_KEY, false)) {
+      Map<String, String> filterConfig =
+        AuthenticationFilterInitializer.getFilterConfigMap(conf, 
"hbase.http.filter.");
+      defineFilter(ctxt, NO_CACHE_FILTER, NoCacheFilter.class.getName(), 
filterConfig,
+        new String[] { "/*" });
+    } else {
+      defineFilter(ctxt, NO_CACHE_FILTER, NoCacheFilter.class.getName(),
+        Collections.<String, String> emptyMap(), new String[] { "/*" });
+    }
   }
 
   /** Get an array of FilterConfiguration specified in the conf */
@@ -755,6 +764,7 @@ public class HttpServer implements FilterContainer {
         conf.getBoolean(ServerConfigurationKeys.HBASE_JETTY_LOGS_SERVE_ALIASES,
           ServerConfigurationKeys.DEFAULT_HBASE_JETTY_LOGS_SERVE_ALIASES));
       setContextAttributes(logContext, conf);
+      addNoCacheFilter(logContext, conf);
       defaultContexts.put(logContext, true);
     }
     // set up the context for "/static/*"
diff --git 
a/hbase-http/src/main/java/org/apache/hadoop/hbase/http/NoCacheFilter.java 
b/hbase-http/src/main/java/org/apache/hadoop/hbase/http/NoCacheFilter.java
index 0c6aaa05079..54b458d8b9f 100644
--- a/hbase-http/src/main/java/org/apache/hadoop/hbase/http/NoCacheFilter.java
+++ b/hbase-http/src/main/java/org/apache/hadoop/hbase/http/NoCacheFilter.java
@@ -31,15 +31,28 @@ import org.apache.yetus.audience.InterfaceAudience;
 
 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.CONFIG)
 public class NoCacheFilter implements Filter {
+
+  /**
+   * Constant for the configuration property that indicates no-store cache 
control is enabled.
+   */
+  public static final String NO_STORE = "no-store.enable";
+
+  private boolean noStoreEnabled = false;
+
   @Override
   public void init(FilterConfig filterConfig) throws ServletException {
+    this.noStoreEnabled = 
Boolean.valueOf(filterConfig.getInitParameter(NO_STORE));
   }
 
   @Override
   public void doFilter(ServletRequest req, ServletResponse res, FilterChain 
chain)
     throws IOException, ServletException {
     HttpServletResponse httpRes = (HttpServletResponse) res;
-    httpRes.setHeader("Cache-Control", "no-cache");
+    StringBuilder header = new StringBuilder("no-cache");
+    if (noStoreEnabled) {
+      header.append(", no-store, max-age=0");
+    }
+    httpRes.setHeader("Cache-Control", header.toString());
     long now = EnvironmentEdgeManager.currentTime();
     httpRes.addDateHeader("Expires", now);
     httpRes.addDateHeader("Date", now);

Reply via email to