sentry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ha...@apache.org
Subject incubator-sentry git commit: SENTRY-1078: Add servlet for dumping configurations (Li Li, Reviewed by: Hao Hao) )
Date Tue, 08 Mar 2016 04:25:08 GMT
Repository: incubator-sentry
Updated Branches:
  refs/heads/master 36db9ba13 -> b095b5b99


SENTRY-1078: Add servlet for dumping configurations (Li Li, Reviewed by: Hao Hao)
)

Change-Id: I88bad89b99fcdded2a74c61471a1433f86549e18


Project: http://git-wip-us.apache.org/repos/asf/incubator-sentry/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-sentry/commit/b095b5b9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-sentry/tree/b095b5b9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-sentry/diff/b095b5b9

Branch: refs/heads/master
Commit: b095b5b991a164983163390f0c8c6f78557d017e
Parents: 36db9ba
Author: hahao <hao.hao@cloudera.com>
Authored: Mon Mar 7 20:23:48 2016 -0800
Committer: hahao <hao.hao@cloudera.com>
Committed: Mon Mar 7 20:23:48 2016 -0800

----------------------------------------------------------------------
 .../provider/db/service/thrift/ConfServlet.java | 69 ++++++++++++++++++++
 .../db/service/thrift/SentryWebServer.java      |  7 +-
 .../src/main/webapp/SentryService.html          |  2 +
 .../TestSentryWebServerWithoutSecurity.java     | 29 ++++++++
 4 files changed, 106 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/b095b5b9/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/ConfServlet.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/ConfServlet.java
b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/ConfServlet.java
new file mode 100644
index 0000000..9e7fca8
--- /dev/null
+++ b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/ConfServlet.java
@@ -0,0 +1,69 @@
+package org.apache.sentry.provider.db.service.thrift;
+
+/**
+ * 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.
+ */
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.hadoop.conf.Configuration;
+
+/**
+ * Servlet to print out all sentry configuration.
+ */
+public class ConfServlet extends HttpServlet {
+  public static final String CONF_CONTEXT_ATTRIBUTE = "sentry.conf";
+  public static final String FORMAT_JSON = "json";
+  public static final String FORMAT_XML = "xml";
+  public static final String FORMAT_PARAM = "format";
+  private static final long serialVersionUID = 1L;
+
+  @Override
+  public void doGet(HttpServletRequest request, HttpServletResponse response)
+      throws ServletException, IOException {
+    String format = request.getParameter(FORMAT_PARAM);
+    if (format == null) {
+      format = FORMAT_XML;
+    }
+
+    if (FORMAT_XML.equals(format)) {
+      response.setContentType("text/xml; charset=utf-8");
+    } else if (FORMAT_JSON.equals(format)) {
+      response.setContentType("application/json; charset=utf-8");
+    }
+
+    Configuration conf = (Configuration)getServletContext().getAttribute(
+        CONF_CONTEXT_ATTRIBUTE);
+    assert conf != null;
+
+    Writer out = response.getWriter();
+    if (FORMAT_JSON.equals(format)) {
+      Configuration.dumpConfiguration(conf, out);
+    } else if (FORMAT_XML.equals(format)) {
+      conf.writeXml(out);
+    } else {
+      response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Bad format: " + format);
+    }
+    out.close();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/b095b5b9/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/SentryWebServer.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/SentryWebServer.java
b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/SentryWebServer.java
index 9778c96..1bdea2c 100644
--- a/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/SentryWebServer.java
+++ b/sentry-provider/sentry-provider-db/src/main/java/org/apache/sentry/provider/db/service/thrift/SentryWebServer.java
@@ -68,6 +68,11 @@ public class SentryWebServer {
       servletContextHandler.addEventListener(listener);
     }
 
+    ServletHolder confServletHolder = new ServletHolder(ConfServlet.class);
+    servletContextHandler.addServlet(confServletHolder, "/conf");
+    servletContextHandler.getServletContext()
+        .setAttribute(ConfServlet.CONF_CONTEXT_ATTRIBUTE, conf);
+
     ResourceHandler resourceHandler = new ResourceHandler();
     resourceHandler.setDirectoriesListed(true);
     URL url = this.getClass().getResource(RESOURCE_DIR);
@@ -150,4 +155,4 @@ public class SentryWebServer {
           + principal + "] keytab [" + keytabFile + "]");
     }
   }
-}
\ No newline at end of file
+}

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/b095b5b9/sentry-provider/sentry-provider-db/src/main/webapp/SentryService.html
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/main/webapp/SentryService.html b/sentry-provider/sentry-provider-db/src/main/webapp/SentryService.html
index f630973..cd19dd8 100644
--- a/sentry-provider/sentry-provider-db/src/main/webapp/SentryService.html
+++ b/sentry-provider/sentry-provider-db/src/main/webapp/SentryService.html
@@ -39,6 +39,7 @@ limitations under the License.
             <li><a href="/ping">Ping</a></li>
             <li><a href="/threads">Threads</a></li>
             <li><a href="/healthcheck">Healthcheck</a></li>
+            <li><a href="/conf">Configuration</a></li>
           </ul>
         </div>
       </div>
@@ -50,6 +51,7 @@ limitations under the License.
         <li><a href="/ping">Ping</a></li>
         <li><a href="/threads">Threads</a></li>
         <li><a href="/healthcheck">Healthcheck</a></li>
+        <li><a href="/conf">Configuration</a></li>
       </ul>
     </div>
   </body>

http://git-wip-us.apache.org/repos/asf/incubator-sentry/blob/b095b5b9/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/service/thrift/TestSentryWebServerWithoutSecurity.java
----------------------------------------------------------------------
diff --git a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/service/thrift/TestSentryWebServerWithoutSecurity.java
b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/service/thrift/TestSentryWebServerWithoutSecurity.java
index 0d82d99..4a913e5 100644
--- a/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/service/thrift/TestSentryWebServerWithoutSecurity.java
+++ b/sentry-provider/sentry-provider-db/src/test/java/org/apache/sentry/provider/db/service/thrift/TestSentryWebServerWithoutSecurity.java
@@ -55,4 +55,33 @@ public class TestSentryWebServerWithoutSecurity extends SentryServiceIntegration
     String response = IOUtils.toString(conn.getInputStream());
     Assert.assertEquals("pong\n", response);
   }
+
+  @Test
+  public void testConf() throws Exception {
+    // test bad format
+    final URL url = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/conf?"
+        + ConfServlet.FORMAT_PARAM + "=badformat");
+    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+    Assert.assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, conn.getResponseCode());
+
+    // test json format
+    final URL url1 = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/conf?"
+        + ConfServlet.FORMAT_PARAM +"=" +  ConfServlet.FORMAT_JSON);
+    conn = (HttpURLConnection) url1.openConnection();
+    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
+
+    // test xml format
+    final URL url2 = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/conf?"
+        + ConfServlet.FORMAT_PARAM +"=" + ConfServlet.FORMAT_XML);
+    conn = (HttpURLConnection) url2.openConnection();
+    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
+    String xmlResponse = IOUtils.toString(conn.getInputStream());
+
+    // test default is xml format
+    final URL url3 = new URL("http://" + SERVER_HOST + ":" + webServerPort + "/conf");
+    conn = (HttpURLConnection) url3.openConnection();
+    Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
+    String defaultResponse = IOUtils.toString(conn.getInputStream());
+    Assert.assertEquals(xmlResponse, defaultResponse);
+  }
 }


Mime
View raw message