You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2021/05/13 00:17:57 UTC

[GitHub] [ozone] vivekratnavel commented on a change in pull request #2200: HDDS-5165. OM DB checkpoint servlet not accessible in a secure cluster

vivekratnavel commented on a change in pull request #2200:
URL: https://github.com/apache/ozone/pull/2200#discussion_r631483197



##########
File path: hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMDbCheckpointServlet.java
##########
@@ -83,114 +93,171 @@
    * <p>
    * Ozone is made active by setting OZONE_ENABLED = true
    *
-   * @throws IOException
+   * @throws Exception
    */
   @BeforeClass
   public static void init() throws Exception {
     conf = new OzoneConfiguration();
-    clusterId = UUID.randomUUID().toString();
-    scmId = UUID.randomUUID().toString();
-    omId = UUID.randomUUID().toString();
-    conf.setBoolean(OZONE_ACL_ENABLED, false);
-    conf.set(OZONE_ADMINISTRATORS, OZONE_ADMINISTRATORS_WILDCARD);
-    conf.setInt(OZONE_OPEN_KEY_EXPIRE_THRESHOLD_SECONDS, 2);
-    cluster = MiniOzoneCluster.newBuilder(conf)
-        .setClusterId(clusterId)
-        .setScmId(scmId)
-        .setOmId(omId)
-        .setNumDatanodes(1)
-        .build();
-    cluster.waitForClusterToBeReady();
-    omMetrics = cluster.getOzoneManager().getMetrics();
+
+    tempFile = File.createTempFile("testDoGet_" + System
+        .currentTimeMillis(), ".tar.gz");
+
+    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
+
+    servletOutputStream = new ServletOutputStream() {
+      @Override
+      public boolean isReady() {
+        return true;
+      }
+
+      @Override
+      public void setWriteListener(WriteListener writeListener) {
+      }
+
+      @Override
+      public void write(int b) throws IOException {
+        fileOutputStream.write(b);
+      }
+    };
   }
 
   /**
    * Shutdown MiniDFSCluster.
    */
   @AfterClass
-  public static void shutdown() {
+  public static void shutdown() throws InterruptedException {
     if (cluster != null) {
       cluster.shutdown();
     }
+    FileUtils.deleteQuietly(tempFile);
+  }
+
+  private static void setCluster(MiniOzoneCluster cluster) {
+    TestOMDbCheckpointServlet.cluster = cluster;
+  }
+
+  private void setupCluster() throws Exception {
+    setCluster(MiniOzoneCluster.newBuilder(conf)
+        .setNumDatanodes(1)
+        .build());
+    cluster.waitForClusterToBeReady();
+    omMetrics = cluster.getOzoneManager().getMetrics();
+
+    omDbCheckpointServletMock =
+        mock(OMDBCheckpointServlet.class);
+
+    doCallRealMethod().when(omDbCheckpointServletMock).init();
+
+    requestMock = mock(HttpServletRequest.class);
+    // Return current user short name when asked
+    when(requestMock.getRemoteUser())
+        .thenReturn(UserGroupInformation.getCurrentUser().getShortUserName());
+    responseMock = mock(HttpServletResponse.class);
+
+    ServletContext servletContextMock = mock(ServletContext.class);
+    when(omDbCheckpointServletMock.getServletContext())
+        .thenReturn(servletContextMock);
+
+    when(servletContextMock.getAttribute(OzoneConsts.OM_CONTEXT_ATTRIBUTE))
+        .thenReturn(cluster.getOzoneManager());
+    when(requestMock.getParameter(OZONE_DB_CHECKPOINT_REQUEST_FLUSH))
+        .thenReturn("true");
+
+    doCallRealMethod().when(omDbCheckpointServletMock).doGet(requestMock,
+        responseMock);
   }
 
   @Test
-  public void testDoGet() throws ServletException, IOException {
-    File tempFile = null;
-    try {
-      OMDBCheckpointServlet omDbCheckpointServletMock =
-          mock(OMDBCheckpointServlet.class);
-
-      final OzoneManager om = cluster.getOzoneManager();
-
-      doCallRealMethod().when(omDbCheckpointServletMock).init();
-      doCallRealMethod().when(omDbCheckpointServletMock).initialize(
-          om.getMetadataManager().getStore(),
-          om.getMetrics().getDBCheckpointMetrics(),
-          om.getAclsEnabled(),
-          om.getOzoneAdmins(om.getConfiguration()));
-
-      HttpServletRequest requestMock = mock(HttpServletRequest.class);
-      // Return current user short name when asked
-      when(requestMock.getRemoteUser())
-          .thenReturn(UserGroupInformation.getCurrentUser().getShortUserName());
-      HttpServletResponse responseMock = mock(HttpServletResponse.class);
-
-      ServletContext servletContextMock = mock(ServletContext.class);
-      when(omDbCheckpointServletMock.getServletContext())
-          .thenReturn(servletContextMock);
-
-      when(servletContextMock.getAttribute(OzoneConsts.OM_CONTEXT_ATTRIBUTE))
-          .thenReturn(cluster.getOzoneManager());
-      when(requestMock.getParameter(OZONE_DB_CHECKPOINT_REQUEST_FLUSH))
-          .thenReturn("true");
-      doNothing().when(responseMock).setContentType("application/x-tgz");
-      doNothing().when(responseMock).setHeader(Matchers.anyString(),
-          Matchers.anyString());
-
-      tempFile = File.createTempFile("testDoGet_" + System
-          .currentTimeMillis(), ".tar.gz");
-
-      FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
-      when(responseMock.getOutputStream()).thenReturn(
-          new ServletOutputStream() {
-            @Override
-            public boolean isReady() {
-              return true;
-            }
-
-            @Override
-            public void setWriteListener(WriteListener writeListener) {
-            }
-
-            @Override
-            public void write(int b) throws IOException {
-              fileOutputStream.write(b);
-            }
-          });
-
-      doCallRealMethod().when(omDbCheckpointServletMock).doGet(requestMock,
-          responseMock);
-
-      omDbCheckpointServletMock.init();
-      long initialCheckpointCount =
-          omMetrics.getDBCheckpointMetrics().getNumCheckpoints();
-
-      omDbCheckpointServletMock.doGet(requestMock, responseMock);
-
-      Assert.assertTrue(tempFile.length() > 0);
-      Assert.assertTrue(
-          omMetrics.getDBCheckpointMetrics().
-              getLastCheckpointCreationTimeTaken() > 0);
-      Assert.assertTrue(
-          omMetrics.getDBCheckpointMetrics().
-              getLastCheckpointStreamingTimeTaken() > 0);
-      Assert.assertTrue(omMetrics.getDBCheckpointMetrics().
-          getNumCheckpoints() > initialCheckpointCount);
-    } finally {
-      FileUtils.deleteQuietly(tempFile);
-    }
+  public void testDoGet() throws Exception {
+    conf.setBoolean(OZONE_ACL_ENABLED, false);
+    conf.set(OZONE_ADMINISTRATORS, OZONE_ADMINISTRATORS_WILDCARD);
+    conf.setInt(OZONE_OPEN_KEY_EXPIRE_THRESHOLD_SECONDS, 2);
+
+    setupCluster();
+
+    final OzoneManager om = cluster.getOzoneManager();
+
+    doCallRealMethod().when(omDbCheckpointServletMock).initialize(
+        om.getMetadataManager().getStore(),
+        om.getMetrics().getDBCheckpointMetrics(),
+        om.getAclsEnabled(),
+        om.getOzoneAdmins(om.getConfiguration()),
+        om.isSpnegoEnabled());
+
+    doNothing().when(responseMock).setContentType("application/x-tgz");
+    doNothing().when(responseMock).setHeader(Matchers.anyString(),
+        Matchers.anyString());
+
+    when(responseMock.getOutputStream()).thenReturn(servletOutputStream);
+
+    omDbCheckpointServletMock.init();
+    long initialCheckpointCount =
+        omMetrics.getDBCheckpointMetrics().getNumCheckpoints();
+
+    omDbCheckpointServletMock.doGet(requestMock, responseMock);
+
+    Assert.assertTrue(tempFile.length() > 0);
+    Assert.assertTrue(
+        omMetrics.getDBCheckpointMetrics().
+            getLastCheckpointCreationTimeTaken() > 0);
+    Assert.assertTrue(
+        omMetrics.getDBCheckpointMetrics().
+            getLastCheckpointStreamingTimeTaken() > 0);
+    Assert.assertTrue(omMetrics.getDBCheckpointMetrics().
+        getNumCheckpoints() > initialCheckpointCount);
+  }
+
+  @Test
+  public void testSpnegoEnabled() throws Exception {
+    cluster.shutdown();

Review comment:
       Yes, it is handled in the `@AfterClass` method - https://github.com/apache/ozone/pull/2200/files#diff-816030b8fda0e307628b659606cf1d37b2117996c83caab10513b1956f72bc13R128

##########
File path: hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/om/TestOMDbCheckpointServlet.java
##########
@@ -83,114 +93,171 @@
    * <p>
    * Ozone is made active by setting OZONE_ENABLED = true
    *
-   * @throws IOException
+   * @throws Exception
    */
   @BeforeClass
   public static void init() throws Exception {
     conf = new OzoneConfiguration();
-    clusterId = UUID.randomUUID().toString();
-    scmId = UUID.randomUUID().toString();
-    omId = UUID.randomUUID().toString();
-    conf.setBoolean(OZONE_ACL_ENABLED, false);
-    conf.set(OZONE_ADMINISTRATORS, OZONE_ADMINISTRATORS_WILDCARD);
-    conf.setInt(OZONE_OPEN_KEY_EXPIRE_THRESHOLD_SECONDS, 2);
-    cluster = MiniOzoneCluster.newBuilder(conf)
-        .setClusterId(clusterId)
-        .setScmId(scmId)
-        .setOmId(omId)
-        .setNumDatanodes(1)
-        .build();
-    cluster.waitForClusterToBeReady();
-    omMetrics = cluster.getOzoneManager().getMetrics();
+
+    tempFile = File.createTempFile("testDoGet_" + System
+        .currentTimeMillis(), ".tar.gz");
+
+    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
+
+    servletOutputStream = new ServletOutputStream() {
+      @Override
+      public boolean isReady() {
+        return true;
+      }
+
+      @Override
+      public void setWriteListener(WriteListener writeListener) {
+      }
+
+      @Override
+      public void write(int b) throws IOException {
+        fileOutputStream.write(b);
+      }
+    };
   }
 
   /**
    * Shutdown MiniDFSCluster.
    */
   @AfterClass
-  public static void shutdown() {
+  public static void shutdown() throws InterruptedException {
     if (cluster != null) {
       cluster.shutdown();
     }
+    FileUtils.deleteQuietly(tempFile);
+  }
+
+  private static void setCluster(MiniOzoneCluster cluster) {
+    TestOMDbCheckpointServlet.cluster = cluster;
+  }
+
+  private void setupCluster() throws Exception {
+    setCluster(MiniOzoneCluster.newBuilder(conf)
+        .setNumDatanodes(1)
+        .build());
+    cluster.waitForClusterToBeReady();
+    omMetrics = cluster.getOzoneManager().getMetrics();
+
+    omDbCheckpointServletMock =
+        mock(OMDBCheckpointServlet.class);
+
+    doCallRealMethod().when(omDbCheckpointServletMock).init();
+
+    requestMock = mock(HttpServletRequest.class);
+    // Return current user short name when asked
+    when(requestMock.getRemoteUser())
+        .thenReturn(UserGroupInformation.getCurrentUser().getShortUserName());
+    responseMock = mock(HttpServletResponse.class);
+
+    ServletContext servletContextMock = mock(ServletContext.class);
+    when(omDbCheckpointServletMock.getServletContext())
+        .thenReturn(servletContextMock);
+
+    when(servletContextMock.getAttribute(OzoneConsts.OM_CONTEXT_ATTRIBUTE))
+        .thenReturn(cluster.getOzoneManager());
+    when(requestMock.getParameter(OZONE_DB_CHECKPOINT_REQUEST_FLUSH))
+        .thenReturn("true");
+
+    doCallRealMethod().when(omDbCheckpointServletMock).doGet(requestMock,
+        responseMock);
   }
 
   @Test
-  public void testDoGet() throws ServletException, IOException {
-    File tempFile = null;
-    try {
-      OMDBCheckpointServlet omDbCheckpointServletMock =
-          mock(OMDBCheckpointServlet.class);
-
-      final OzoneManager om = cluster.getOzoneManager();
-
-      doCallRealMethod().when(omDbCheckpointServletMock).init();
-      doCallRealMethod().when(omDbCheckpointServletMock).initialize(
-          om.getMetadataManager().getStore(),
-          om.getMetrics().getDBCheckpointMetrics(),
-          om.getAclsEnabled(),
-          om.getOzoneAdmins(om.getConfiguration()));
-
-      HttpServletRequest requestMock = mock(HttpServletRequest.class);
-      // Return current user short name when asked
-      when(requestMock.getRemoteUser())
-          .thenReturn(UserGroupInformation.getCurrentUser().getShortUserName());
-      HttpServletResponse responseMock = mock(HttpServletResponse.class);
-
-      ServletContext servletContextMock = mock(ServletContext.class);
-      when(omDbCheckpointServletMock.getServletContext())
-          .thenReturn(servletContextMock);
-
-      when(servletContextMock.getAttribute(OzoneConsts.OM_CONTEXT_ATTRIBUTE))
-          .thenReturn(cluster.getOzoneManager());
-      when(requestMock.getParameter(OZONE_DB_CHECKPOINT_REQUEST_FLUSH))
-          .thenReturn("true");
-      doNothing().when(responseMock).setContentType("application/x-tgz");
-      doNothing().when(responseMock).setHeader(Matchers.anyString(),
-          Matchers.anyString());
-
-      tempFile = File.createTempFile("testDoGet_" + System
-          .currentTimeMillis(), ".tar.gz");
-
-      FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
-      when(responseMock.getOutputStream()).thenReturn(
-          new ServletOutputStream() {
-            @Override
-            public boolean isReady() {
-              return true;
-            }
-
-            @Override
-            public void setWriteListener(WriteListener writeListener) {
-            }
-
-            @Override
-            public void write(int b) throws IOException {
-              fileOutputStream.write(b);
-            }
-          });
-
-      doCallRealMethod().when(omDbCheckpointServletMock).doGet(requestMock,
-          responseMock);
-
-      omDbCheckpointServletMock.init();
-      long initialCheckpointCount =
-          omMetrics.getDBCheckpointMetrics().getNumCheckpoints();
-
-      omDbCheckpointServletMock.doGet(requestMock, responseMock);
-
-      Assert.assertTrue(tempFile.length() > 0);
-      Assert.assertTrue(
-          omMetrics.getDBCheckpointMetrics().
-              getLastCheckpointCreationTimeTaken() > 0);
-      Assert.assertTrue(
-          omMetrics.getDBCheckpointMetrics().
-              getLastCheckpointStreamingTimeTaken() > 0);
-      Assert.assertTrue(omMetrics.getDBCheckpointMetrics().
-          getNumCheckpoints() > initialCheckpointCount);
-    } finally {
-      FileUtils.deleteQuietly(tempFile);
-    }
+  public void testDoGet() throws Exception {
+    conf.setBoolean(OZONE_ACL_ENABLED, false);
+    conf.set(OZONE_ADMINISTRATORS, OZONE_ADMINISTRATORS_WILDCARD);
+    conf.setInt(OZONE_OPEN_KEY_EXPIRE_THRESHOLD_SECONDS, 2);
+
+    setupCluster();
+
+    final OzoneManager om = cluster.getOzoneManager();
+
+    doCallRealMethod().when(omDbCheckpointServletMock).initialize(
+        om.getMetadataManager().getStore(),
+        om.getMetrics().getDBCheckpointMetrics(),
+        om.getAclsEnabled(),
+        om.getOzoneAdmins(om.getConfiguration()),
+        om.isSpnegoEnabled());
+
+    doNothing().when(responseMock).setContentType("application/x-tgz");
+    doNothing().when(responseMock).setHeader(Matchers.anyString(),
+        Matchers.anyString());
+
+    when(responseMock.getOutputStream()).thenReturn(servletOutputStream);
+
+    omDbCheckpointServletMock.init();
+    long initialCheckpointCount =
+        omMetrics.getDBCheckpointMetrics().getNumCheckpoints();
+
+    omDbCheckpointServletMock.doGet(requestMock, responseMock);
+
+    Assert.assertTrue(tempFile.length() > 0);
+    Assert.assertTrue(
+        omMetrics.getDBCheckpointMetrics().
+            getLastCheckpointCreationTimeTaken() > 0);
+    Assert.assertTrue(
+        omMetrics.getDBCheckpointMetrics().
+            getLastCheckpointStreamingTimeTaken() > 0);
+    Assert.assertTrue(omMetrics.getDBCheckpointMetrics().
+        getNumCheckpoints() > initialCheckpointCount);
+  }
+
+  @Test
+  public void testSpnegoEnabled() throws Exception {
+    cluster.shutdown();
+    conf.setBoolean(OZONE_ACL_ENABLED, true);
+    conf.set(OZONE_ADMINISTRATORS, "");
+    conf.set(OZONE_OM_HTTP_AUTH_TYPE, "kerberos");
+    conf.set(OZONE_RECON_KERBEROS_PRINCIPAL_KEY, "recon/host1@REALM");
+
+    setupCluster();
+
+    final OzoneManager om = cluster.getOzoneManager();
+    Collection<String> allowedUsers = om.getOzoneAdmins(om.getConfiguration());
+    allowedUsers.add("recon");
+
+    doCallRealMethod().when(omDbCheckpointServletMock).initialize(
+        om.getMetadataManager().getStore(),
+        om.getMetrics().getDBCheckpointMetrics(),
+        om.getAclsEnabled(),
+        allowedUsers,
+        om.isSpnegoEnabled());
+
+    omDbCheckpointServletMock.init();
+    omDbCheckpointServletMock.doGet(requestMock, responseMock);
+
+    // Response status should be set to 403 Forbidden since there was no user
+    // principal set in the request
+    verify(responseMock, times(1)).setStatus(HttpServletResponse.SC_FORBIDDEN);
+
+    // Set the principal to DN in request
+    // This should also get denied since only OM and recon
+    // users should be granted access to the servlet
+    Principal userPrincipalMock = mock(Principal.class);
+    when(userPrincipalMock.getName()).thenReturn("dn/localhost@REALM");
+    when(requestMock.getUserPrincipal()).thenReturn(userPrincipalMock);
+
+    omDbCheckpointServletMock.doGet(requestMock, responseMock);
+
+    // Verify that the Response status is set to 403 again for DN user.
+    verify(responseMock, times(2)).setStatus(HttpServletResponse.SC_FORBIDDEN);
+
+    // Now, set the principal to recon in request
+    when(userPrincipalMock.getName()).thenReturn("recon/localhost@REALM");
+
+    when(requestMock.getUserPrincipal()).thenReturn(userPrincipalMock);
+    when(responseMock.getOutputStream()).thenReturn(servletOutputStream);
+
+    omDbCheckpointServletMock.doGet(requestMock, responseMock);
 
+    // Recon user should be able to access the servlet and download the
+    // snapshot
+    Assert.assertTrue(tempFile.length() > 0);

Review comment:
       Yes, it is handled in the `@AfterClass` method - https://github.com/apache/ozone/pull/2200/files#diff-816030b8fda0e307628b659606cf1d37b2117996c83caab10513b1956f72bc13R128




-- 
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org